Represents an access to an sql-projected SQL column. The access may be to an outer select in which case the SelectAlias property is the alias given of the outer select.
Inheritance: SqlBaseExpression
 public SqlIndexedColumnExpression(SqlColumnExpression column, SortOrder sortOrder, bool lowercaseIndex)
     : base(typeof(void))
 {
     this.Column         = column;
     this.SortOrder      = sortOrder;
     this.LowercaseIndex = lowercaseIndex;
 }
		public SqlIndexedColumnExpression(SqlColumnExpression column, SortOrder sortOrder, bool lowercaseIndex)
			: base(typeof(void))
		{
			this.Column = column;
			this.SortOrder = sortOrder;
			this.LowercaseIndex = lowercaseIndex;
		}
 protected override Expression VisitColumn(SqlColumnExpression expression)
 {
     this.hashCode ^= expression.Name?.GetHashCode() ?? 0;
     this.hashCode ^= expression.Special ? 237046631 : 0;
     this.hashCode ^= expression.SelectAlias?.GetHashCode() ?? 0;
     this.hashCode ^= expression.AliasedName?.GetHashCode() ?? 0;
     return base.VisitColumn(expression);
 }
Exemplo n.º 4
0
 protected override Expression VisitColumn(SqlColumnExpression expression)
 {
     this.hashCode ^= expression.Name?.GetHashCode() ?? 0;
     this.hashCode ^= expression.Special ? 237046631 : 0;
     this.hashCode ^= expression.SelectAlias?.GetHashCode() ?? 0;
     this.hashCode ^= expression.AliasedName?.GetHashCode() ?? 0;
     return(base.VisitColumn(expression));
 }
 public SqlIndexedColumnExpression(SqlColumnExpression column, SortOrder sortOrder = default(SortOrder), bool lowercaseIndex = default(bool), bool includeOnly = false)
     : base(typeof(void))
 {
     this.Column         = column;
     this.SortOrder      = sortOrder;
     this.LowercaseIndex = lowercaseIndex;
     this.IncludeOnly    = includeOnly;
 }
Exemplo n.º 6
0
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            if (columnExpression.Type.GetUnwrappedNullableType() == typeof(bool))
            {
                return new BitBooleanExpression(columnExpression);
            }

            return base.VisitColumn(columnExpression);
        }
Exemplo n.º 7
0
		protected override Expression VisitColumn(SqlColumnExpression columnExpression)
		{
			if (this.oldAliasMatch(columnExpression.SelectAlias))
			{
				return columnExpression.ChangeAlias(this.replacement);
			}

			return base.VisitColumn(columnExpression);
		}
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        protected override Expression VisitColumn(SqlColumnExpression column)
        {
            SqlColumnExpression existing;

            if (this.visitedColumns.TryGetValue(column, out existing))
            {
                return existing;
            }

            return column;
        }
Exemplo n.º 10
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;
        }
Exemplo n.º 11
0
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            SqlColumnExpression current;

            if (!TryGetCurrent(columnExpression, out current))
            {
                return(columnExpression);
            }

            result = result && (current.Name == columnExpression.Name &&
                                current.SelectAlias == columnExpression.SelectAlias);

            return(columnExpression);
        }
Exemplo n.º 12
0
        protected override Expression VisitColumn(SqlColumnExpression column)
        {
            Dictionary<string, Expression> columnsByName;

            if (this.columnsBySelectAliasByColumnName.TryGetValue(column.SelectAlias, out columnsByName))
            {
                Expression expr;

                if (columnsByName.TryGetValue(column.Name, out expr))
                {
                    return this.Visit(expr);
                }

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

            return column;
        }
Exemplo n.º 13
0
		private Expression ProcessExpression(Expression expression)
		{
			if (expression.NodeType == (ExpressionType)SqlExpressionType.Column)
			{
				SqlColumnExpression mappedColumnExpression;

				var column = (SqlColumnExpression)expression;

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

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

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

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

					return mappedColumnExpression;
				}

				// Must be referring to outer scope

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

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

				return new SqlColumnExpression(expression.Type, this.newAlias, columnName);
			}
		}
Exemplo n.º 14
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]);
                }
            }
        }
		protected override Expression VisitColumn(SqlColumnExpression columnExpression)
		{
			if (!aliases.Contains(columnExpression.SelectAlias))
			{
				var nullableType = columnExpression.Type.MakeNullable();

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

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

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

			return base.VisitColumn(columnExpression);
		}
Exemplo n.º 16
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]);
		}
Exemplo n.º 17
0
        protected override Expression VisitColumn(SqlColumnExpression columnExpression)
        {
            this.output.AppendFormat("COLUMN({0}.{1})", columnExpression.SelectAlias, columnExpression.Name);

            return columnExpression;
        }
Exemplo n.º 18
0
		protected virtual Expression VisitColumn(SqlColumnExpression columnExpression)
		{
			return columnExpression;
		}
Exemplo n.º 19
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;
        }
Exemplo n.º 20
0
        protected virtual Expression ConvertColumnToIsNull(SqlColumnExpression column)
        {
            var sqlDataType = this.sqlDatabaseContext.SqlDataTypeProvider.GetSqlDataType(column.Type);

            if (!this.columnIndexes.ContainsKey(column.Name))
            {
                return sqlDataType.IsNullExpression(this.dataReader, 0);
            }
            else
            {
                return sqlDataType.IsNullExpression(this.dataReader, this.columnIndexes[column.Name]);
            }
        }
Exemplo n.º 21
0
 protected override Expression VisitColumn(SqlColumnExpression columnExpression)
 {
     return(base.VisitColumn(columnExpression));
 }
        protected override Expression VisitColumn(SqlColumnExpression expression)
        {
            SqlColumnExpression current;
            if (!TryGetCurrent(expression, out current))
            {
                return expression;
            }

            if (!(this.result &= object.Equals(current.Name, expression.Name)))
            {
                return expression;
            }

            if (!(this.result &= current.Special == expression.Special))
            {
                return expression;
            }

            if (!(this.result &= object.Equals(current.SelectAlias, expression.SelectAlias)))
            {
                return expression;
            }

            if (!(this.result &= object.Equals(current.AliasedName, expression.AliasedName)))
            {
                return expression;
            }

            if (!(this.result &= current.NodeType == expression.NodeType))
            {
                return expression;
            }

            if (!(this.result &= current.Type == expression.Type))
            {
                return expression;
            }

            this.currentObject = current;
            return expression;
        }
Exemplo n.º 23
0
 protected override Expression VisitColumn(SqlColumnExpression column)
 {
     return this.ConvertColumnToDataReaderRead(column, column.Type);
 }
Exemplo n.º 24
0
		protected override Expression VisitColumn(SqlColumnExpression column)
		{
			if (treatColumnsAsNullable && column.Type.IsValueType && !column.Type.IsNullableType())
			{
				return this.ConvertColumnToDataReaderRead(column, column.Type.MakeNullable());
			}
			else
			{
				return this.ConvertColumnToDataReaderRead(column, column.Type);
			}
		}
Exemplo n.º 25
0
		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);
		}
Exemplo n.º 26
0
        protected override Expression VisitColumn(SqlColumnExpression column)
        {
            this.MarkColumnAsUsed(column.SelectAlias, column.Name);

            return column;
        }
Exemplo n.º 27
0
        protected override Expression VisitColumn(SqlColumnExpression column)
        {
            this.aliases.Add(column.SelectAlias);

            return column;
        }
Exemplo n.º 28
0
 protected virtual Expression VisitColumn(SqlColumnExpression columnExpression)
 {
     return(columnExpression);
 }