protected void VisitNewInstanceExpression(SelectStatement select,
                                                  DbNewInstanceExpression expression)
        {
            Debug.Assert(expression.ResultType.EdmType is RowType);

            RowType row = expression.ResultType.EdmType as RowType;

            for (int i = 0; i < expression.Arguments.Count; i++)
            {
                ColumnFragment col;

                SqlFragment fragment = expression.Arguments[i].Accept(this);
                if (fragment is ColumnFragment)
                {
                    col = fragment as ColumnFragment;
                }
                else
                {
                    col         = new ColumnFragment(null, null);
                    col.Literal = fragment;
                }

                col.ColumnAlias = row.Properties[i].Name;
                select.Columns.Add(col);
            }
        }
        public override bool Equals(object obj)
        {
            if (!(obj is ColumnFragment))
            {
                return(false);
            }
            ColumnFragment column = obj as ColumnFragment;

            if (column.PropertyFragment != null && PropertyFragment != null)
            {
                return(column.PropertyFragment.Equals(PropertyFragment));
            }
            if (column.TableName != TableName)
            {
                return(false);
            }
            if (column.ColumnName != ColumnName)
            {
                return(false);
            }
            if (column.ColumnAlias != ColumnAlias)
            {
                return(false);
            }
            return(true);
        }
        public ColumnFragment Clone()
        {
            ColumnFragment cf = new ColumnFragment(TableName, ColumnName);

            cf.ColumnAlias = ColumnAlias;
            cf.Literal     = Literal;
            return(cf);
        }
        public override void WriteSql(StringBuilder sql)
        {
            ColumnFragment columnFragment = Column as ColumnFragment;

            Debug.Assert(columnFragment != null);
            columnFragment.WriteSql(sql);
            sql.AppendFormat(" {0}", Ascending ? "ASC" : "DESC");
        }
 public bool HasDifferentNameForColumn(ColumnFragment column)
 {
     Debug.Assert(Left is SelectStatement);
     Debug.Assert(Right is SelectStatement);
     if ((Left as SelectStatement).HasDifferentNameForColumn(column))
     {
         return(true);
     }
     return((Right as SelectStatement).HasDifferentNameForColumn(column));
 }
        public ColumnFragment GetColumnFromProperties(PropertyFragment properties)
        {
            ColumnFragment col = Left.GetColumnFromProperties(properties);

            if (col == null)
            {
                col = Right.GetColumnFromProperties(properties);
            }
            return(col);
        }
 public bool HasDifferentNameForColumn(ColumnFragment column)
 {
     if (!hasRenamedColumns) return false;
     foreach (ColumnFragment c in Columns)
     {
         if (!c.Equals(column)) continue;
         if (String.IsNullOrEmpty(c.ColumnAlias)) return false;
         column.ColumnName = c.ColumnAlias;
         return true;
     }
     return false;
 }
Beispiel #8
0
        private List <ColumnFragment> GetDefaultColumnsForTable(TableFragment table)
        {
            List <ColumnFragment> columns = new List <ColumnFragment>();

            foreach (EdmProperty property in Metadata.GetProperties(table.Type.EdmType))
            {
                ColumnFragment col = new ColumnFragment(table.Name, property.Name);
                col.PushInput(property.Name);
                col.PushInput(table.Name);
                columns.Add(col);
            }
            return(columns);
        }
        public override SqlFragment Visit(DbGroupByExpression expression)
        {
            // first process the input
            DbGroupExpressionBinding e           = expression.Input;
            SelectStatement          innerSelect = VisitInputExpressionEnsureSelect(e.Expression, e.VariableName, e.VariableType);

            scope.Add(e.GroupVariableName, innerSelect);

            SelectStatement select = WrapIfNotCompatible(innerSelect, expression.ExpressionKind);

            CollectionType ct = (CollectionType)expression.ResultType.EdmType;
            RowType        rt = (RowType)ct.TypeUsage.EdmType;

            int propIndex = 0;

            foreach (DbExpression key in expression.Keys)
            {
                var fragment = key.Accept(this);
                select.AddGroupBy(fragment);
                propIndex++;

                var colFragment = fragment as ColumnFragment;

                if (colFragment != null)
                {
                    colFragment             = colFragment.Clone();
                    colFragment.ColumnAlias = String.Format("K{0}", propIndex);
                    select.Columns.Add(colFragment);
                }
            }

            for (int agg = 0; agg < expression.Aggregates.Count; agg++)
            {
                DbAggregate         a  = expression.Aggregates[agg];
                DbFunctionAggregate fa = a as DbFunctionAggregate;
                if (fa == null)
                {
                    throw new NotSupportedException();
                }

                string         alias       = rt.Properties[propIndex++].Name;
                ColumnFragment functionCol = new ColumnFragment(null, null);
                functionCol.Literal     = HandleFunction(fa, a.Arguments[0].Accept(this));
                functionCol.ColumnAlias = alias;
                select.Columns.Add(functionCol);
            }

            return(select);
        }
        public override SqlFragment Visit(DbPropertyExpression expression)
        {
            propertyLevel++;
            PropertyFragment fragment = expression.Instance.Accept(this) as PropertyFragment;

            fragment.Properties.Add(expression.Property.Name);
            propertyLevel--;

            // if we are not at the top level property then just return
            if (propertyLevel > 0)
            {
                return(fragment);
            }

            ColumnFragment column = new ColumnFragment(null, fragment.LastProperty);

            column.PropertyFragment = fragment;
            InputFragment input = scope.FindInputFromProperties(fragment);

            if (input != null)
            {
                column.TableName = input.Name;
            }

            // now we need to check if our column name was possibly renamed
            if (input is TableFragment)
            {
                return(column);
            }

            SelectStatement select = input as SelectStatement;
            UnionFragment   union  = input as UnionFragment;

            if (select != null)
            {
                select.HasDifferentNameForColumn(column);
            }
            else if (union != null)
            {
                union.HasDifferentNameForColumn(column);
            }

            // input is a table, selectstatement, or unionstatement
            return(column);
        }
        public override SqlFragment Visit(DbNewInstanceExpression expression)
        {
            Debug.Assert(expression.ResultType.EdmType is CollectionType);

            SelectStatement s = new SelectStatement();

            ColumnFragment c = new ColumnFragment(null, null);

            if (expression.Arguments.Count != 0)
            {
                c.Literal = (LiteralFragment)expression.Arguments[0].Accept(this);
            }
            else
            {
                c.Literal = new LiteralFragment("NULL");
            }
            c.ColumnAlias = "X";
            s.Columns.Add(c);
            return(s);
        }
Beispiel #12
0
        private List <ColumnFragment> GetDefaultColumnsForFragment(InputFragment input)
        {
            List <ColumnFragment> columns = new List <ColumnFragment>();

            if (input is TableFragment)
            {
                return(GetDefaultColumnsForTable(input as TableFragment));
            }
            else if (input is JoinFragment || input is UnionFragment)
            {
                Debug.Assert(input.Left != null);
                columns = GetDefaultColumnsForFragment(input.Left);
                if (input is JoinFragment && input.Right != null)
                {
                    List <ColumnFragment> right = GetDefaultColumnsForFragment(input.Right);
                    columns.AddRange(right);
                }
            }
            else if (input is SelectStatement)
            {
                SelectStatement select = input as SelectStatement;
                foreach (ColumnFragment cf in select.Columns)
                {
                    ColumnFragment newColumn = new ColumnFragment(cf.TableName, cf.ActualColumnName);
                    newColumn.PushInput(cf.ActualColumnName);
                    columns.Add(newColumn);
                }
            }
            else
            {
                throw new NotImplementedException();
            }
            if (!String.IsNullOrEmpty(input.Name) && input.Name != From.Name)
            {
                foreach (ColumnFragment c in columns)
                {
                    c.PushInput(input.Name);
                }
            }
            return(columns);
        }
Beispiel #13
0
 public bool HasDifferentNameForColumn(ColumnFragment column)
 {
     if (!hasRenamedColumns)
     {
         return(false);
     }
     foreach (ColumnFragment c in Columns)
     {
         if (!c.Equals(column))
         {
             continue;
         }
         if (String.IsNullOrEmpty(c.ColumnAlias))
         {
             return(false);
         }
         column.ColumnName = c.ColumnAlias;
         return(true);
     }
     return(false);
 }
        private List<ColumnFragment> GetDefaultColumnsForFragment(InputFragment input)
        {
            List<ColumnFragment> columns = new List<ColumnFragment>();

            if (input is TableFragment)
            {
                return GetDefaultColumnsForTable(input as TableFragment);
            }
            else if (input is JoinFragment || input is UnionFragment)
            {
                Debug.Assert(input.Left != null);
                columns = GetDefaultColumnsForFragment(input.Left);
                if (input is JoinFragment && input.Right != null)
                {
                    List<ColumnFragment> right = GetDefaultColumnsForFragment(input.Right);
                    columns.AddRange(right);
                }
            }
            else if (input is SelectStatement)
            {
                SelectStatement select = input as SelectStatement;
                foreach (ColumnFragment cf in select.Columns)
                {
                    ColumnFragment newColumn = new ColumnFragment(cf.TableName, cf.ActualColumnName);
                    newColumn.PushInput(cf.ActualColumnName);
                    columns.Add(newColumn);
                }
            }
            else
                throw new NotImplementedException();
            if (!String.IsNullOrEmpty(input.Name) && input.Name != From.Name)
                foreach (ColumnFragment c in columns)
                    c.PushInput(input.Name);
            return columns;
        }
 public bool HasDifferentNameForColumn(ColumnFragment column)
 {
     Debug.Assert(Left is SelectStatement);
     Debug.Assert(Right is SelectStatement);
     if ((Left as SelectStatement).HasDifferentNameForColumn(column))
         return true;
     return (Right as SelectStatement).HasDifferentNameForColumn(column);
 }
 public ColumnFragment Clone()
 {
     ColumnFragment cf = new ColumnFragment(TableName, ColumnName);
     cf.ColumnAlias = ColumnAlias;
     cf.Literal = Literal;
     return cf;
 }
        public override SqlFragment Visit(DbNewInstanceExpression expression)
        {
            Debug.Assert(expression.ResultType.EdmType is CollectionType);

            SelectStatement s = new SelectStatement();

            ColumnFragment c = new ColumnFragment(null, null);
            if (expression.Arguments.Count != 0)
                c.Literal = (LiteralFragment)expression.Arguments[0].Accept(this);
            else
                c.Literal = new LiteralFragment("NULL");
            c.ColumnAlias = "X";
            s.Columns.Add(c);
            return s;
        }
        public override SqlFragment Visit(DbGroupByExpression expression)
        {
            // first process the input
            DbGroupExpressionBinding e = expression.Input;
            SelectStatement innerSelect = VisitInputExpressionEnsureSelect(e.Expression, e.VariableName, e.VariableType);
            scope.Add(e.GroupVariableName, innerSelect);

            SelectStatement select = WrapIfNotCompatible(innerSelect, expression.ExpressionKind);

            CollectionType ct = (CollectionType)expression.ResultType.EdmType;
            RowType rt = (RowType)ct.TypeUsage.EdmType;

            int propIndex = 0;

            foreach (DbExpression key in expression.Keys)
            {
                var fragment = key.Accept(this);
                select.AddGroupBy(fragment);
                propIndex++;

                var colFragment = fragment as ColumnFragment;

                if (colFragment != null)
                {
                    colFragment = colFragment.Clone();
                    colFragment.ColumnAlias = String.Format("K{0}", propIndex);
                    select.Columns.Add(colFragment);
                }
            }

            for (int agg = 0; agg < expression.Aggregates.Count; agg++)
            {
                DbAggregate a = expression.Aggregates[agg];
                DbFunctionAggregate fa = a as DbFunctionAggregate;
                if (fa == null) throw new NotSupportedException();

                string alias = rt.Properties[propIndex++].Name;
                ColumnFragment functionCol = new ColumnFragment(null, null);
                functionCol.Literal = HandleFunction(fa, a.Arguments[0].Accept(this));
                functionCol.ColumnAlias = alias;
                select.Columns.Add(functionCol);
            }

            return select;
        }
        private List<ColumnFragment> GetDefaultColumnsForTable(TableFragment table)
        {
            List<ColumnFragment> columns = new List<ColumnFragment>();

            foreach (EdmProperty property in Metadata.GetProperties(table.Type.EdmType))
            {
                ColumnFragment col = new ColumnFragment(table.Name, property.Name);
                col.PushInput(property.Name);
                col.PushInput(table.Name);
                columns.Add(col);
            }
            return columns;
        }