Пример #1
0
        public Expression ResolveMemberExpression(SqlColumnExpression sqlColumnExpression, MemberInfo memberInfo)
        {
            ArgumentUtility.CheckNotNull("sqlColumnExpression", sqlColumnExpression);
            ArgumentUtility.CheckNotNull("memberInfo", memberInfo);

            throw new UnmappedItemException(
                      string.Format("The member '{0}.{1}' does not identify a mapped property.", memberInfo.ReflectedType.Name, memberInfo.Name));
        }
Пример #2
0
        public void ColumnTest()
        {
            var column = new SqlColumnExpression(SqlType.Boolean, null, "Column");
            var table  = new SqlTableExpression("Table", new [] { column });
            var value  = _generator.GenerateSql(column);

            Assert.Equal("[Table].[Column]", value);
        }
Пример #3
0
 public void SetUp()
 {
     _columnExpression1       = new SqlColumnDefinitionExpression(typeof(int), "t", "ID", true);
     _columnExpression2       = new SqlColumnDefinitionExpression(typeof(int), "t", "Name", false);
     _columnExpression3       = new SqlColumnDefinitionExpression(typeof(int), "t", "City", false);
     _orginalColumns          = new[] { _columnExpression1, _columnExpression2, _columnExpression3 };
     _entityExpression        = new SqlEntityDefinitionExpression(typeof(Cook), "t", null, e => e.GetColumn(typeof(int), "ID", true), _orginalColumns);
     _originalColumnsReadonly = _entityExpression.Columns;
 }
Пример #4
0
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            if (columnExpression.Type.GetUnwrappedNullableType() == typeof(bool))
            {
                return(new BitBooleanExpression(columnExpression));
            }

            return(base.VisitColumn(columnExpression));
        }
Пример #5
0
        protected override Expression VisitColumn(SqlColumnExpression column)
        {
            if (this.visitedColumns.TryGetValue(column, out var existing))
            {
                return(existing);
            }

            return(column);
        }
Пример #6
0
        public Expression VisitSqlColumn(SqlColumnExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

            // Scenario: column.Member (probably originally entity.ColumnMember.Member)
            // Handled by the _mappingResolver

            return(_mappingResolver.ResolveMemberExpression(expression, _memberInfo));
        }
Пример #7
0
        public void OrderedTest()
        {
            var column  = new SqlColumnExpression(SqlType.Boolean, null, "Column");
            var table   = new SqlTableExpression("Table", new[] { column });
            var ordered = new SqlOrderedExpression(column, OrderType.Ascending);
            var value   = _generator.GenerateSql(ordered);

            Assert.Equal("[Table].[Column] ASC", value);
        }
        protected override SqlExpression Visit(SqlColumnExpression expression)
        {
            if (expression.Type == typeof(bool))
            {
                return(new SqlBinaryExpression(SqlNodeType.Equal, expression, new SqlConstantExpression(typeof(bool), true)));
            }

            return(expression);
        }
Пример #9
0
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            if (this.oldAliasMatch(columnExpression.SelectAlias))
            {
                return(columnExpression.ChangeAlias(this.replacement));
            }

            return(base.VisitColumn(columnExpression));
        }
Пример #10
0
        public Expression ResolveMemberExpression(SqlColumnExpression sqlColumnExpression, MemberInfo memberInfo)
        {
            var message = string.Format(
                "Cannot resolve members appplied to expressions representing columns. (Member: {0}, Column: {1})",
                memberInfo.Name,
                sqlColumnExpression);

            throw new UnmappedItemException(message);
        }
Пример #11
0
 protected override Expression VisitColumn(SqlColumnExpression column)
 {
     if (this.treatColumnsAsNullable && column.Type.IsValueType && !column.Type.IsNullableType())
     {
         return(ConvertColumnToDataReaderRead(column, column.Type.MakeNullable()));
     }
     else
     {
         return(ConvertColumnToDataReaderRead(column, column.Type));
     }
 }
Пример #12
0
        public virtual Expression VisitSqlColumnReferenceExpression(SqlColumnReferenceExpression expression)
        {
            ArgumentUtility.CheckNotNull("expression", expression);

            SqlColumnExpression firstColumn = expression.ReferencedEntity.Columns.FirstOrDefault();
            string referencedEntityName     = firstColumn != null && firstColumn.ColumnName == "*" ? null : expression.ReferencedEntity.Name;

            AppendColumn(expression.ColumnName, expression.OwningTableAlias, referencedEntityName);

            return(expression);
        }
Пример #13
0
        /// <summary>
        /// Rebind order expressions to reference a new alias and add to column declarations if necessary
        /// </summary>
        protected virtual BindResult RebindOrderings(IEnumerable <SqlOrderByExpression> orderings, string alias, HashSet <string> existingAliases, IEnumerable <SqlColumnDeclaration> existingColumns)
        {
            List <SqlColumnDeclaration> newColumns = null;

            var newOrderings = new List <SqlOrderByExpression>();

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

                if (column == null || (existingAliases != null && existingAliases.Contains(column.SelectAlias)))
                {
                    // Check to see if a declared column already contains a similar expression
                    var ordinal = 0;

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

                        if ((column != null && decl.Expression == ordering.Expression) ||
                            (column != null && declColumn != null && column.SelectAlias == declColumn.SelectAlias && column.Name == declColumn.Name))
                        {
                            // Found it, so make a reference to this column
                            expr = new SqlColumnExpression(column.Type, alias, decl.Name);

                            break;
                        }

                        ordinal++;
                    }

                    // If not already projected, add a new column declaration for it
                    if (expr == ordering.Expression)
                    {
                        if (newColumns == null)
                        {
                            newColumns = new List <SqlColumnDeclaration>(existingColumns);

                            existingColumns = newColumns;
                        }

                        var colName = column != null ? column.Name : "COL" + ordinal;

                        newColumns.Add(new SqlColumnDeclaration(colName, ordering.Expression));
                        expr = new SqlColumnExpression(expr.Type, alias, colName);
                    }

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

            return(new BindResult(existingColumns, newOrderings));
        }
        public Expression VisitSqlColumn(SqlColumnExpression expression)
        {
            // We always need to convert boolean columns to int columns because in the database, the column is represented as a bit (integer) value
            if (BooleanUtility.IsBooleanType(expression.Type))
            {
                var        intType             = BooleanUtility.GetMatchingIntType(expression.Type);
                Expression convertedExpression = expression.Update(intType, expression.OwningTableAlias, expression.ColumnName, expression.IsPrimaryKey);
                return(new SqlConvertedBooleanExpression(convertedExpression));
            }

            return(expression); // rely on Visit to apply correct semantics
        }
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            if (selectNesting == 1 && (this.ConvertEnumsToText && columnExpression.Type.GetUnwrappedNullableType().IsEnum))
            {
                base.VisitColumn(columnExpression);
                this.Write("::TEXT");

                return(columnExpression);
            }

            return(base.VisitColumn(columnExpression));
        }
Пример #16
0
        protected override void AppendColumnForEntity(SqlEntityExpression entity, SqlColumnExpression column)
        {
            Visit(column);

            string alias = GetAliasForColumnOfEntity(column, entity);

            if (alias != null)
            {
                CommandBuilder.Append(" AS ");
                CommandBuilder.AppendIdentifier(alias);
            }
        }
Пример #17
0
        private static string GetColumnType(SqlColumnExpression c)
        {
            var sqlTypeName = SqlConvertExpression.GetSqlTypeName(c.Type);

            // (MAX) types are not valid in primary key columns.
            if (c.IsPrimaryKey)
            {
                return(sqlTypeName.Replace("(MAX)", "(100)"));
            }

            return(sqlTypeName);
        }
Пример #18
0
        protected override Expression VisitSelect(SqlSelectExpression select)
        {
            var columnRemoved = false;

            select = (SqlSelectExpression)base.VisitSelect(select);

            var columnsOrderedByName = select.Columns.OrderBy(c => c.Name).ToList();

            var removedColumns = new BitArray(select.Columns.Count);

            for (int i = 0, n = columnsOrderedByName.Count; i < n - 1; i++)
            {
                var icolumn = columnsOrderedByName[i];
                var iNewColumnExpression = new SqlColumnExpression(icolumn.Expression.Type, select.Alias, icolumn.Name);

                for (var j = i + 1; j < n; j++)
                {
                    if (!removedColumns.Get(j))
                    {
                        var jcolumn = columnsOrderedByName[j];

                        if (IsSameExpression(icolumn.Expression, jcolumn.Expression))
                        {
                            // 'j' references should now be a reference to 'i'

                            var jNewColumnExpression = new SqlColumnExpression(jcolumn.Expression.Type, select.Alias, jcolumn.Name);
                            this.visitedColumns.Add(jNewColumnExpression, iNewColumnExpression);

                            removedColumns.Set(j, true);
                            columnRemoved = true;
                        }
                    }
                }
            }

            if (columnRemoved)
            {
                var newColumnDeclarations = new List <SqlColumnDeclaration>();

                for (int i = 0, n = columnsOrderedByName.Count; i < n; i++)
                {
                    if (!removedColumns.Get(i))
                    {
                        newColumnDeclarations.Add(columnsOrderedByName[i]);
                    }
                }

                select = select.ChangeColumns(newColumnDeclarations);
            }

            return(select);
        }
Пример #19
0
        protected virtual BindResult RebindOrderings(IEnumerable <SqlOrderByExpression> orderings, string alias, HashSet <string> existingAliases, IReadOnlyList <SqlColumnDeclaration> existingColumns)
        {
            List <SqlColumnDeclaration> newColumns = null;

            var newOrderings = new List <SqlOrderByExpression>();

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

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

                    // If a declared column already contains a similar expression then make a reference to that column

                    foreach (var decl in existingColumns)
                    {
                        if ((column != null && decl.Expression == ordering.Expression) ||
                            (column != null && decl.Expression is SqlColumnExpression declColumn && column.SelectAlias == declColumn.SelectAlias && column.Name == declColumn.Name))
                        {
                            expr = new SqlColumnExpression(column.Type, alias, decl.Name);

                            break;
                        }

                        ordinal++;
                    }

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

                            existingColumns = newColumns;
                        }

                        var colName = column != null ? column.Name : "COL" + ordinal;
                        colName = GetAvailableColumnName(newColumns, colName);

                        newColumns.Add(new SqlColumnDeclaration(colName, ordering.Expression));
                        expr = new SqlColumnExpression(expr.Type, alias, colName);
                    }

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

            return(new BindResult(existingColumns, newOrderings));
        }
Пример #20
0
        protected virtual Expression ConvertColumnToIsNull(SqlColumnExpression column)
        {
            var sqlDataType = this.sqlDatabaseContext.SqlDataTypeProvider.GetSqlDataType(column.Type);

            if (!this.scope.ColumnIndexes.ContainsKey(column.Name))
            {
                return(sqlDataType.IsNullExpression(this.dataReader, 0));
            }
            else
            {
                return(sqlDataType.IsNullExpression(this.dataReader, this.scope.ColumnIndexes[column.Name]));
            }
        }
Пример #21
0
        protected override Expression VisitColumn(SqlColumnExpression column)
        {
            if (this.columnsBySelectAliasByColumnName.TryGetValue(column.SelectAlias, out var columnsByName))
            {
                if (columnsByName.TryGetValue(column.Name, out var expr))
                {
                    return(Visit(expr));
                }

                throw new InvalidOperationException("Reference to undefined column: " + column.AliasedName);
            }

            return(column);
        }
Пример #22
0
        protected virtual Expression ConvertColumnToDataReaderRead(SqlColumnExpression column, Type type)
        {
            if (column.Type.IsDataAccessObjectType())
            {
                return(Expression.Convert(Expression.Constant(null), column.Type));
            }

            var sqlDataType = this.sqlDatabaseContext.SqlDataTypeProvider.GetSqlDataType(type);

            if (!this.scope.ColumnIndexes.ContainsKey(column.Name))
            {
                throw new InvalidOperationException($"Unable to find matching column reference named {column.Name}");
            }

            return(sqlDataType.GetReadExpression(this.dataReader, this.scope.ColumnIndexes[column.Name]));
        }
Пример #23
0
 protected string GetAliasForColumnOfEntity(SqlColumnExpression column, SqlEntityExpression entity)
 {
     if (column.ColumnName != "*")
     {
         if (entity.Name != null)
         {
             return(entity.Name + "_" + column.ColumnName);
         }
         else if ((entity is SqlEntityReferenceExpression) && ((SqlEntityReferenceExpression)entity).ReferencedEntity.Name != null)
         {
             // entity references without a name that point to an entity with a name must assign aliases to their columns;
             // otherwise, their columns would include the referenced entity's name
             return(column.ColumnName);
         }
     }
     return(null);
 }
Пример #24
0
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            if (!String.IsNullOrEmpty(columnExpression.SelectAlias))
            {
                if (this.ignoreAlias == columnExpression.SelectAlias)
                {
                    this.WriteQuotedIdentifier(this.replaceAlias);
                }
                else
                {
                    this.WriteQuotedIdentifier(columnExpression.SelectAlias);
                }

                this.Write(".");
            }

            this.WriteQuotedIdentifier(columnExpression.Name);

            return(columnExpression);
        }
Пример #25
0
        private Expression ProcessExpression(Expression expression)
        {
            if (expression.NodeType == (ExpressionType)SqlExpressionType.Column)
            {
                SqlColumnExpression mappedColumnExpression;

                var column = (SqlColumnExpression)expression;

                if (mappedColumnExpressions.TryGetValue(column, out mappedColumnExpression))
                {
                    return(mappedColumnExpression);
                }

                if (existingAliases.Contains(column.SelectAlias))
                {
                    var columnName = GetUniqueColumnName(column.Name);

                    columns.Add(new SqlColumnDeclaration(columnName, column));
                    mappedColumnExpression = new SqlColumnExpression(column.Type, newAlias, columnName);

                    mappedColumnExpressions[column] = mappedColumnExpression;
                    columnNames.Add(columnName);

                    return(mappedColumnExpression);
                }

                // Must be referring to outer scope

                return(column);
            }
            else
            {
                var columnName = GetNextColumnName();

                columnNames.Add(columnName);
                columns.Add(new SqlColumnDeclaration(columnName, expression));

                return(new SqlColumnExpression(expression.Type, newAlias, columnName));
            }
        }
Пример #26
0
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            if (!this.aliases.Contains(columnExpression.SelectAlias))
            {
                var nullableType = columnExpression.Type.MakeNullable();

                if (nullableType == columnExpression.Type)
                {
                    this.replacedExpressions.Add(columnExpression);

                    return(new SqlConstantPlaceholderExpression(this.placeholderCount++, Expression.Constant(null, columnExpression.Type.MakeNullable())));
                }
                else
                {
                    this.replacedExpressions.Add(columnExpression.ChangeToNullable());

                    return(Expression.Convert(new SqlConstantPlaceholderExpression(this.placeholderCount++, Expression.Constant(null, columnExpression.Type.MakeNullable())), columnExpression.Type));
                }
            }

            return(base.VisitColumn(columnExpression));
        }
Пример #27
0
        protected virtual Expression ConvertColumnToDataReaderRead(SqlColumnExpression column, Type type)
        {
            // Replace all column accesses with the appropriate IDataReader call

            if (column.Type.IsDataAccessObjectType())
            {
                return(Expression.Convert(Expression.Constant(null), column.Type));
            }
            else
            {
                var sqlDataType = this.sqlDatabaseContext.SqlDataTypeProvider.GetSqlDataType(type);

                if (!this.columnIndexes.ContainsKey(column.Name))
                {
                    throw new InvalidOperationException();
                }
                else
                {
                    return(sqlDataType.GetReadExpression(this.dataReader, this.columnIndexes[column.Name]));
                }
            }
        }
Пример #28
0
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            this.output.AppendFormat("COLUMN({0}.{1})", columnExpression.SelectAlias, columnExpression.Name);

            return(columnExpression);
        }
 Expression IResolvedSqlExpressionVisitor.VisitSqlColumnExpression(SqlColumnExpression expression)
 {
     throw new InvalidOperationException("SqlColumnExpression is not valid at this point. (Must be wrapped within a NamedExpression.)");
 }
Пример #30
0
 protected override SqlExpression Visit(SqlColumnExpression expression)
 {
     sql.Append(expression.ColumnName);
     return(expression);
 }
Пример #31
0
 public SqlProjectionExpression(SqlColumnExpression from, string to)
 {
     To = to;
     From = @from;
 }
Пример #32
0
 public SqlOrderingExpression(Directions direction, SqlColumnExpression column)
 {
     Direction = direction;
     Column = column;
 }