Пример #1
0
        private void GetColumns(MappingEntity entity, Dictionary <string, TableAlias> aliases, List <DbColumnDeclaration> columns)
        {
            foreach (var mi in this.mapping.GetMappedMembers(entity))
            {
                if (this.mapping.IsAssociationRelationship(entity, mi) == false)
                {
                    if (this.mapping.IsNestedEntity(entity, mi))
                    {
                        this.GetColumns(this.mapping.GetRelatedEntity(entity, mi), aliases, columns);
                    }
                    else if (this.mapping.IsColumn(entity, mi))
                    {
                        var name      = this.mapping.GetColumnName(entity, mi);
                        var aliasName = this.mapping.GetAlias(entity, mi);

                        aliases.TryGetValue(aliasName, out TableAlias alias);

                        var colType = this.GetColumnType(entity, mi);
                        var ce      = new DbColumnExpression(TypeHelper.GetMemberType(mi), colType, alias, name);
                        var cd      = new DbColumnDeclaration(name, ce, colType);

                        columns.Add(cd);
                    }
                }
            }
        }
Пример #2
0
        protected override Expression VisitColumn(DbColumnExpression column)
        {
            if (this.scope != null && this.scope.TryGetValue(column, out ParameterExpression fieldReader, out int iOrdinal))
            {
                var method = FieldReader.GetReaderMethod(column.Type);

                return(Expression.Call(fieldReader, method, Expression.Constant(iOrdinal)));
            }
Пример #3
0
        protected override Expression VisitColumn(DbColumnExpression column)
        {
            if (this.map.TryGetValue(column.Alias, out TableAlias newAlias))
            {
                return(new DbColumnExpression(column.Type, column.QueryType, newAlias, column.Name));
            }

            return(column);
        }
Пример #4
0
        protected DbColumnAssignment UpdateColumnAssignment(DbColumnAssignment ca, DbColumnExpression c, Expression e)
        {
            if (c != ca.Column || e != ca.Expression)
            {
                return(new DbColumnAssignment(c, e));
            }

            return(ca);
        }
Пример #5
0
        protected override Expression VisitColumn(DbColumnExpression column)
        {
            if (this.map.TryGetValue(column, out DbColumnExpression mapped))
            {
                return(mapped);
            }

            return(column);
        }
Пример #6
0
        protected override Expression VisitColumn(DbColumnExpression column)
        {
            if (this.oldAliases.Contains(column.Alias))
            {
                return(new DbColumnExpression(column.Type, column.QueryType, this.newAlias, column.Name));
            }

            return(column);
        }
Пример #7
0
        private DbCommandExpression GetDependentGeneratedVariableDeclaration(MappingEntity entity, MappingTable table, List <MemberInfo> members, Expression instance, Dictionary <MemberInfo, Expression> map)
        {
            var genIdCommand = null as DbDeclarationCommand;
            var generatedIds = this.mapping.GetMappedMembers(entity).Where(m => this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)).ToList();

            if (generatedIds.Count > 0)
            {
                genIdCommand = this.GetGeneratedIdCommand(entity, members, map);

                if (members.Count == generatedIds.Count)
                {
                    return(genIdCommand);
                }
            }

            members = members.Except(generatedIds).ToList();

            var tableAlias = new TableAlias();
            var tex        = new DbTableExpression(tableAlias, entity, this.mapping.GetTableName(table));

            var where = null as Expression;

            if (generatedIds.Count > 0)
            {
                where = generatedIds.Select((m, i) => this.GetMemberExpression(tex, entity, m).Equal(map[m])).Aggregate((x, y) => x.And(y));
            }
            else
            {
                where = this.GetIdentityCheck(tex, entity, instance);
            }

            var selectAlias = new TableAlias();
            var columns     = new List <DbColumnDeclaration>();
            var variables   = new List <DbVariableDeclaration>();

            foreach (var mi in members)
            {
                var col = (DbColumnExpression)this.GetMemberExpression(tex, entity, mi);

                columns.Add(new DbColumnDeclaration(this.mapping.GetColumnName(entity, mi), col, col.QueryType));

                var vcol = new DbColumnExpression(col.Type, col.QueryType, selectAlias, col.Name);

                variables.Add(new DbVariableDeclaration(mi.Name, col.QueryType, vcol));
                map.Add(mi, new DbVariableExpression(mi.Name, col.Type, col.QueryType));
            }

            var genMembersCommand = new DbDeclarationCommand(variables, new DbSelectExpression(selectAlias, columns, tex, where));

            if (genIdCommand != null)
            {
                return(new DbBlockCommand(genIdCommand, genMembersCommand));
            }

            return(genMembersCommand);
        }
Пример #8
0
        protected override Expression VisitColumn(DbColumnExpression column)
        {
            if (column.Alias != null && !this.HideColumnAliases)
            {
                this.WriteAliasName(GetAliasName(column.Alias));
                this.Write(".");
            }

            this.WriteColumnName(column.Name);

            return(column);
        }
Пример #9
0
        protected virtual BindResult RebindOrderings(IEnumerable <DbOrderExpression> orderings, TableAlias alias, HashSet <TableAlias> existingAliases, IEnumerable <DbColumnDeclaration> existingColumns)
        {
            var newColumns   = null as List <DbColumnDeclaration>;
            var newOrderings = new List <DbOrderExpression>();

            foreach (var ordering in orderings)
            {
                var expr   = ordering.Expression;
                var column = expr as DbColumnExpression;

                if (column == null || (existingAliases != null && existingAliases.Contains(column.Alias)))
                {
                    var iOrdinal = 0;

                    foreach (var decl in existingColumns)
                    {
                        var declColumn = decl.Expression as DbColumnExpression;

                        if (decl.Expression == ordering.Expression || (column != null && declColumn != null && column.Alias == declColumn.Alias && column.Name == declColumn.Name))
                        {
                            expr = new DbColumnExpression(column.Type, column.QueryType, alias, decl.Name);

                            break;
                        }

                        iOrdinal++;
                    }

                    if (expr == ordering.Expression)
                    {
                        if (newColumns == null)
                        {
                            newColumns      = new List <DbColumnDeclaration>(existingColumns);
                            existingColumns = newColumns;
                        }

                        var colName = column != null ? column.Name : "c" + iOrdinal;

                        colName = newColumns.GetAvailableColumnName(colName);

                        var colType = this.language.TypeSystem.GetColumnType(expr.Type);

                        newColumns.Add(new DbColumnDeclaration(colName, ordering.Expression, colType));

                        expr = new DbColumnExpression(expr.Type, colType, alias, colName);
                    }

                    newOrderings.Add(new DbOrderExpression(ordering.OrderType, expr));
                }
            }

            return(new BindResult(existingColumns, newOrderings));
        }
Пример #10
0
        protected virtual Expression VisitColumn(DbColumnExpression column)
        {
            var aliasName = this.aliasMap.TryGetValue(column.Alias, out int iAlias) ? "A" + iAlias : "A" + (column.Alias != null ? column.Alias.GetHashCode().ToString() : "") + "?";

            this.Write(aliasName);
            this.Write(".");
            this.Write("Column(\"");
            this.Write(column.Name);
            this.Write("\")");

            return(column);
        }
Пример #11
0
        protected override Expression Visit(Expression expression)
        {
            if (this.candidates.Contains(expression))
            {
                if (expression.NodeType == (ExpressionType)DbExpressionType.Column)
                {
                    var column = expression as DbColumnExpression;

                    if (this.map.TryGetValue(column, out DbColumnExpression mapped))
                    {
                        return(mapped);
                    }

                    foreach (DbColumnDeclaration existingColumn in this.columns)
                    {
                        if (existingColumn.Expression is DbColumnExpression cex && cex.Alias == column.Alias && cex.Name == column.Name)
                        {
                            return(new DbColumnExpression(column.Type, column.QueryType, this.newAlias, existingColumn.Name));
                        }
                    }

                    if (this.existingAliases.Contains(column.Alias))
                    {
                        var ordinal    = this.columns.Count;
                        var columnName = this.GetUniqueColumnName(column.Name);

                        this.columns.Add(new DbColumnDeclaration(columnName, column, column.QueryType));

                        mapped = new DbColumnExpression(column.Type, column.QueryType, this.newAlias, columnName);

                        this.map.Add(column, mapped);
                        this.columnNames.Add(columnName);

                        return(mapped);
                    }

                    return(column);
                }
                else
                {
                    var columnName = this.GetNextColumnName();
                    var colType    = this.language.TypeSystem.GetColumnType(expression.Type);
                    this.columns.Add(new DbColumnDeclaration(columnName, expression, colType));
                    return(new DbColumnExpression(expression.Type, colType, this.newAlias, columnName));
                }
            }
            else
            {
                return(base.Visit(expression));
            }
        }
        protected override Expression VisitSelect(DbSelectExpression select)
        {
            select = base.VisitSelect(select) as DbSelectExpression;

            var cols       = select.Columns.OrderBy(c => c.Name).ToList();
            var removed    = new BitArray(select.Columns.Count);
            var anyRemoved = false;

            for (int i = 0, n = cols.Count; i < n - 1; i++)
            {
                var ci  = cols[i];
                var cix = ci.Expression as DbColumnExpression;
                var qt  = cix != null ? cix.QueryType : ci.QueryType;
                var cxi = new DbColumnExpression(ci.Expression.Type, qt, select.Alias, ci.Name);

                for (int j = i + 1; j < n; j++)
                {
                    if (!removed.Get(j))
                    {
                        var cj = cols[j];

                        if (SameExpression(ci.Expression, cj.Expression))
                        {
                            var cxj = new DbColumnExpression(cj.Expression.Type, qt, select.Alias, cj.Name);

                            this.map.Add(cxj, cxi);
                            removed.Set(j, true);
                            anyRemoved = true;
                        }
                    }
                }
            }

            if (anyRemoved)
            {
                var newDecls = new List <DbColumnDeclaration>();

                for (int i = 0, n = cols.Count; i < n; i++)
                {
                    if (!removed.Get(i))
                    {
                        newDecls.Add(cols[i]);
                    }
                }

                select = select.SetColumns(newDecls);
            }

            return(select);
        }
Пример #13
0
        protected override Expression VisitColumn(DbColumnExpression column)
        {
            if (this.map.TryGetValue(column.Alias, out Dictionary <string, Expression> nameMap))
            {
                if (nameMap.TryGetValue(column.Name, out Expression expr))
                {
                    return(this.Visit(expr));
                }

                throw new Exception("Reference to undefined column");
            }

            return(column);
        }
        protected override Expression VisitSelect(DbSelectExpression select)
        {
            select = base.VisitSelect(select) as DbSelectExpression;

            if (select.Skip != null)
            {
                var newSelect    = select.SetSkip(null).SetTake(null);
                var canAddColumn = !select.IsDistinct && (select.GroupBy == null || select.GroupBy.Count == 0);

                if (canAddColumn == false)
                {
                    newSelect = newSelect.AddRedundantSelect(this.language, new TableAlias());
                }

                var colType = this.language.TypeSystem.GetColumnType(typeof(int));

                newSelect = newSelect.AddColumn(new DbColumnDeclaration("_rownum", new DbRowNumberExpression(select.OrderBy), colType));

                newSelect = newSelect.AddRedundantSelect(this.language, new TableAlias());
                newSelect = newSelect.RemoveColumn(newSelect.Columns.Single(c => c.Name == "_rownum"));

                var newAlias = (newSelect.From as DbSelectExpression).Alias;
                var rnCol    = new DbColumnExpression(typeof(int), colType, newAlias, "_rownum");
                var where = null as Expression;

                if (select.Take != null)
                {
                    where = new DbBetweenExpression(rnCol, Expression.Add(select.Skip, Expression.Constant(1)), Expression.Add(select.Skip, select.Take));
                }
                else
                {
                    where = rnCol.GreaterThan(select.Skip);
                }

                if (newSelect.Where != null)
                {
                    where = newSelect.Where.And(where);
                }

                newSelect = newSelect.SetWhere(where);

                select = newSelect;
            }

            return(select);
        }
Пример #15
0
        internal virtual DbProjectionExpression AddOuterJoinTest(DbProjectionExpression proj)
        {
            var test    = this.GetOuterJoinTest(proj.Select);
            var select  = proj.Select;
            var testCol = null as DbColumnExpression;

            foreach (var col in select.Columns)
            {
                if (test.Equals(col.Expression))
                {
                    testCol = new DbColumnExpression(test.Type, TypeSystem.GetColumnType(test.Type), select.Alias, col.Name);

                    break;
                }
            }

            if (testCol == null)
            {
                testCol = test as DbColumnExpression;

                var colName = testCol != null ? testCol.Name : "Test";

                if (colName != null)
                {
                    colName = proj.Select.Columns.GetAvailableColumnName(colName);
                }

                var colType = this.TypeSystem.GetColumnType(test.Type);

                if (colType != null)
                {
                    select = select.AddColumn(new DbColumnDeclaration(colName, test, colType));

                    testCol = new DbColumnExpression(test.Type, colType, select.Alias, colName);
                }
            }

            return(new DbProjectionExpression
                   (
                       select,
                       new DbOuterJoinedExpression(testCol, proj.Projector),
                       proj.Aggregator
                   ));
        }
Пример #16
0
        private Expression GetIdentityCheck(DbTableExpression root, MappingEntity entity, Expression instance, MappingTable table)
        {
            if (this.mapping.IsExtensionTable(table))
            {
                var keyColNames    = this.mapping.GetExtensionKeyColumnNames(table).ToArray();
                var relatedMembers = this.mapping.GetExtensionRelatedMembers(table).ToArray();
                var where = null as Expression;

                for (int i = 0, n = keyColNames.Length; i < n; i++)
                {
                    var relatedMember = relatedMembers[i];
                    var cex           = new DbColumnExpression(TypeHelper.GetMemberType(relatedMember), this.GetColumnType(entity, relatedMember), root.Alias, keyColNames[n]);
                    var nex           = this.GetMemberExpression(instance, entity, relatedMember);
                    var eq            = cex.Equal(nex);

                    where = (where != null) ? where.And(eq) : where;
                }

                return(where);
            }

            return(base.GetIdentityCheck(root, entity, instance));
        }
Пример #17
0
        private Expression MakeSubquery(Expression expression)
        {
            var newAlias = new TableAlias();
            var aliases  = DbDeclaredAliasGatherer.Gather(expression);
            var decls    = new List <DbColumnDeclaration>();

            foreach (var ta in aliases)
            {
                foreach (var col in this.columns[ta])
                {
                    var name = decls.GetAvailableColumnName(col.Name);

                    var decl = new DbColumnDeclaration(name, col, col.QueryType);

                    decls.Add(decl);

                    var newCol = new DbColumnExpression(col.Type, col.QueryType, newAlias, col.Name);

                    this.map.Add(col, newCol);
                }
            }

            return(new DbSelectExpression(newAlias, decls, expression, null));
        }
Пример #18
0
 public DbColumnAssignment(DbColumnExpression column, Expression expression)
 {
     this.Column     = column;
     this.Expression = expression;
 }
 protected override Expression VisitColumn(DbColumnExpression column)
 {
     MarkColumnAsUsed(column.Alias, column.Name); return(column);
 }
Пример #20
0
        internal override DbProjectionExpression GetQueryExpression(MappingEntity entity)
        {
            var tables = this.mapping.GetTables(entity);

            if (tables.Count <= 1)
            {
                return(base.GetQueryExpression(entity));
            }

            var aliases   = new Dictionary <string, TableAlias>();
            var rootTable = tables.Single(ta => !this.mapping.IsExtensionTable(ta));
            var tex       = new DbTableExpression(new TableAlias(), entity, this.mapping.GetTableName(rootTable));

            aliases.Add(this.mapping.GetAlias(rootTable), tex.Alias);

            var source = tex as Expression;

            foreach (var table in tables.Where(t => this.mapping.IsExtensionTable(t)))
            {
                var joinedTableAlias = new TableAlias();
                var extensionAlias   = this.mapping.GetAlias(table);

                aliases.Add(extensionAlias, joinedTableAlias);

                var keyColumns     = this.mapping.GetExtensionKeyColumnNames(table).ToList();
                var relatedMembers = this.mapping.GetExtensionRelatedMembers(table).ToList();
                var relatedAlias   = this.mapping.GetExtensionRelatedAlias(table);

                aliases.TryGetValue(relatedAlias, out TableAlias relatedTableAlias);

                var joinedTex = new DbTableExpression(joinedTableAlias, entity, this.mapping.GetTableName(table));
                var cond      = null as Expression;

                for (int i = 0, n = keyColumns.Count; i < n; i++)
                {
                    var memberType    = TypeHelper.GetMemberType(relatedMembers[i]);
                    var colType       = this.GetColumnType(entity, relatedMembers[i]);
                    var relatedColumn = new DbColumnExpression(memberType, colType, relatedTableAlias, this.mapping.GetColumnName(entity, relatedMembers[i]));
                    var joinedColumn  = new DbColumnExpression(memberType, colType, joinedTableAlias, keyColumns[i]);
                    var eq            = joinedColumn.Equal(relatedColumn);

                    cond = (cond != null) ? cond.And(eq) : eq;
                }

                source = new DbJoinExpression(JoinType.SingletonLeftOuter, source, joinedTex, cond);
            }

            var columns = new List <DbColumnDeclaration>();

            this.GetColumns(entity, aliases, columns);

            var root            = new DbSelectExpression(new TableAlias(), columns, source, null);
            var existingAliases = aliases.Values.ToArray();

            var projector   = this.GetEntityExpression(root, entity) as Expression;
            var selectAlias = new TableAlias();
            var pc          = DbColumnProjector.ProjectColumns(this.Translator.Linguist.Language, projector, null, selectAlias, root.Alias);
            var proj        = new DbProjectionExpression
                              (
                new DbSelectExpression(selectAlias, pc.Columns, root, null),
                pc.Projector
                              );

            return(this.Translator.Police.ApplyPolicy(proj, entity.ElementType.GetTypeInfo()) as DbProjectionExpression);
        }
Пример #21
0
 protected virtual bool CompareColumn(DbColumnExpression a, DbColumnExpression b)
 {
     return(this.CompareAlias(a.Alias, b.Alias) && a.Name == b.Name);
 }
 protected override Expression VisitColumn(DbColumnExpression column)
 {
     this.columns.Add(column); return(column);
 }
Пример #23
0
 protected virtual Expression VisitColumn(DbColumnExpression column)
 {
     return(column);
 }
        protected override Expression VisitColumn(DbColumnExpression column)
        {
            this.aliases.Add(column.Alias);

            return(column);
        }