Inheritance: SqlFragment
Ejemplo n.º 1
0
        internal void AddColumn(ColumnFragment column, Scope scope)
        {
            InputFragment input = scope.FindInputFromProperties(column.PropertyFragment);

            column.TableName = input.Name;

            // then we rename the column if necessary
            if (columnHash.ContainsKey(column.ColumnName.ToUpper()))
            {
                column.ColumnAlias = MakeColumnNameUnique(column.ColumnName);
                columnHash.Add(column.ColumnAlias, column);
            }
            else
            {
                if (!string.IsNullOrEmpty(column.ColumnAlias))
                {
                    columnHash.Add(column.ColumnAlias.ToUpper(), column);
                }
                else
                {
                    columnHash.Add(column.ColumnName.ToUpper(), column);
                }
            }
            Columns.Add(column);
        }
Ejemplo n.º 2
0
 public void Visit(ColumnFragment f)
 {
     if ((f != null) && (f.TableName == _oldTableName))
     {
         f.TableName = _newTableName;
     }
 }
        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);
        }
Ejemplo n.º 4
0
        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);
            }

            // we are at the top level property so now we can do our work
            ColumnFragment column = GetColumnFromPropertyTree(fragment);

            for (int i = fragment.Properties.Count - 1; i >= 0; --i)
            {
                InputFragment inputFragment = scope.GetFragment(fragment.Properties[i]);
                if (inputFragment != null)
                {
                    column.TableAlias = inputFragment.Name;
                    break;
                }
            }
            return(column);
        }
Ejemplo n.º 5
0
        ColumnFragment GetColumnFromPropertyTree(PropertyFragment fragment)
        {
            int         lastIndex       = fragment.Properties.Count - 1;
            SqlFragment currentFragment = scope.GetFragment(fragment.Properties[0]);

            if (currentFragment != null)
            {
                for (int i = 1; i < fragment.Properties.Count; i++)
                {
                    SqlFragment f = (currentFragment as InputFragment).GetProperty(fragment.Properties[i]);
                    if (f == null)
                    {
                        break;
                    }
                    currentFragment = f;
                }
                if (currentFragment is ColumnFragment)
                {
                    return(currentFragment as ColumnFragment);
                }
            }
            ColumnFragment col = new ColumnFragment(null, fragment.Properties[lastIndex]);

            return(col);
        }
Ejemplo n.º 6
0
        void AddDefaultColumnsForTable(TableFragment table)
        {
            if (columnHash == null)
            {
                columnHash = new Dictionary <string, ColumnFragment>();
            }

            foreach (EdmProperty property in Metadata.GetProperties(table.Type.EdmType))
            {
                ColumnFragment col = new ColumnFragment(table.Name, property.Name);
                if (table.Columns == null)
                {
                    table.Columns = new List <ColumnFragment>();
                }
                table.Columns.Add(col);
                if (columnHash.ContainsKey(col.ColumnName))
                {
                    col.ColumnAlias = MakeColumnNameUnique(col.ColumnName);
                    columnHash.Add(col.ColumnAlias, col);
                }
                else
                {
                    columnHash.Add(col.ColumnName, col);
                }
                Columns.Add(col);
            }
        }
Ejemplo n.º 7
0
        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);
            }
        }
Ejemplo n.º 8
0
        public override void WriteSql(StringBuilder sql)
        {
            Debug.Assert(Column is ColumnFragment);
            ColumnFragment f = Column as ColumnFragment;

            sql.AppendFormat("{0} {1}", QuoteIdentifier(f.ColumnName), Ascending ? "ASC" : "DESC");
        }
        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));
 }
Ejemplo n.º 12
0
        public ColumnFragment GetColumnFromProperties(PropertyFragment properties)
        {
            ColumnFragment col = Left.GetColumnFromProperties(properties);

            if (col == null)
            {
                col = Right.GetColumnFromProperties(properties);
            }
            return(col);
        }
Ejemplo n.º 13
0
        public override SqlFragment Visit(DbApplyExpression expression)
        {
            DbExpressionBinding inputBinding = expression.Input;
            InputFragment       input        = VisitInputExpression(inputBinding.Expression, inputBinding.VariableName, inputBinding.VariableType);
            DbExpressionBinding applyBinding = expression.Apply;
            InputFragment       apply        = VisitInputExpression(applyBinding.Expression, applyBinding.VariableName, applyBinding.VariableType);
            SelectStatement     select       = new SelectStatement();
            bool isInputSelect = false;

            if (!(input is TableFragment))
            {
                input.Wrap(scope);
                isInputSelect = true;
            }
            apply.Wrap(scope);
            select.From = input;
            select.Wrap(scope);
            if (apply is SelectStatement)
            {
                SelectStatement applySel = apply as SelectStatement;
                foreach (ColumnFragment f in applySel.Columns)
                {
                    SelectStatement newColSelect = new SelectStatement();
                    newColSelect.From  = applySel.From;
                    newColSelect.Where = applySel.Where;
                    if (isInputSelect)
                    {
                        VisitAndReplaceTableName(newColSelect.Where, (input as SelectStatement).From.Name, input.Name);
                    }
                    newColSelect.Limit = applySel.Limit;
                    newColSelect.Columns.Add(f);

                    newColSelect.Wrap(scope);
                    scope.Add(applySel.From.Name, applySel.From);

                    ColumnFragment newCol = new ColumnFragment(apply.Name, f.ColumnName);
                    newCol.Literal = newColSelect;
                    newCol.PushInput(newCol.ColumnName);
                    newCol.PushInput(apply.Name);
                    select.AddColumn(newCol, scope);
                    if (string.IsNullOrEmpty(newCol.ColumnAlias))
                    {
                        newColSelect.Name  = newCol.ColumnName;
                        newCol.ColumnAlias = newCol.ColumnName;
                    }
                    scope.Remove(newColSelect);
                }
                scope.Remove(applySel.From);
                scope.Remove(apply);
            }
            return(select);
        }
Ejemplo n.º 14
0
        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,
                                                                  string.IsNullOrEmpty(cf.ColumnAlias) ? cf.ActualColumnName : cf.ColumnAlias
                                                                  );
                    newColumn.PushInput(cf.ActualColumnName);
                    if (cf.TableName != null && cf.ColumnAlias == null)
                    {
                        newColumn.PushInput(cf.TableName);
                    }
                    if (select.Name != null)
                    {
                        newColumn.PushInput(select.Name); // add the scope
                    }
                    columns.Add(newColumn);
                }
                return(columns);
            }
            else
            {
                throw new NotImplementedException();
            }
            if (!String.IsNullOrEmpty(input.Name) && input.Name != From.Name)
            {
                foreach (ColumnFragment c in columns)
                {
                    c.PushInput(input.Name);
                }
            }
            return(columns);
        }
Ejemplo n.º 15
0
        public override SqlFragment Visit(DbInExpression expression)
        {
            ColumnFragment cf  = Visit(expression.Item as DbPropertyExpression) as ColumnFragment;
            InFragment     inf = new InFragment();

            inf.Argument = cf;
            for (int i = 0; i < expression.List.Count; i++)
            {
                LiteralFragment lf = Visit(expression.List[i] as DbConstantExpression) as LiteralFragment;
                inf.InList.Add(lf);
            }
            return(inf);
        }
Ejemplo n.º 16
0
        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 != null) ? table.Name : table.Table);
                columns.Add(col);
            }
            return(columns);
        }
        public void Visit(ColumnFragment f)
        {
            ColumnFragment cf;

            if ((f != null) && (f.TableName == _oldTableName))
            {
                f.TableName = _newTableName;
            }
            if (_dicColumns != null && f.ColumnName != null && _dicColumns.TryGetValue(f.ColumnName, out cf))
            {
                // remove alias
                f.ColumnName = cf.ColumnName;
            }
        }
Ejemplo n.º 18
0
        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);
        }
Ejemplo n.º 19
0
        protected internal void VisitAndReplaceTableName(SqlFragment sf, string oldTable, string newTable)
        {
            BinaryFragment bf = sf as BinaryFragment;
            ColumnFragment cf = sf as ColumnFragment;

            if (bf != null)
            {
                VisitAndReplaceTableName(bf.Left, oldTable, newTable);
                VisitAndReplaceTableName(bf.Right, oldTable, newTable);
            }
            else if ((cf != null) && (cf.TableName == oldTable))
            {
                cf.TableName = newTable;
            }
        }
Ejemplo n.º 20
0
        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);
        }
Ejemplo n.º 21
0
        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);
        }
Ejemplo n.º 22
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);
 }
Ejemplo n.º 23
0
        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);

                #region Correção devexpress
                if (fragment is ColumnFragment)
                {
                    col = fragment as ColumnFragment;
                    // Correção bug devexpress
                    if (col.ActualColumnName == "K1" && row.Properties[i].Name == "C1")
                    {
                        if (col.Literal != null || col.ColumnAlias != null)
                        {
                            col.ColumnAlias = row.Properties[i].Name;
                            select.Columns.Add(col);
                        }
                        else
                        {
                            col.ColumnAlias = row.Properties[i].Name;
#if EF6
                            if (row.Properties[i].TypeName != "DateTime")
                            {
                                col.Literal = new LiteralFragment("0");
                            }
#endif
                            select.Columns.Add(col);
                        }
                    }
                    else
                    {
                        col.ColumnAlias = row.Properties[i].Name;
                        select.Columns.Add(col);
                    }
                }
                else
                {
                    col         = new ColumnFragment(null, null);
                    col.Literal = fragment;
                    if (col.Literal != null)
                    {
                        col.ColumnAlias = row.Properties[i].Name;
                        select.Columns.Add(col);
                    }
                }
                #endregion

                #region Original

                //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);
                #endregion
            }
        }
Ejemplo n.º 24
0
 protected void GetBinaryFragmentPartsForIn(BinaryFragment bf, out LiteralFragment lf, out ColumnFragment cf)
 {
     cf = bf.Right as ColumnFragment;
     lf = bf.Left as LiteralFragment;
     if (lf == null)
     {
         lf = bf.Right as LiteralFragment;
         cf = bf.Left as ColumnFragment;
     }
 }
Ejemplo n.º 25
0
    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 != null) ? table.Name : table.Table);
        columns.Add(col);
      }
      return columns;
    }
Ejemplo n.º 26
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;
 }
Ejemplo n.º 27
0
        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)
            {
                if (!string.IsNullOrEmpty(input.Name))
                {
                    SelectStatement sf = scope.GetFragment(input.Name) as SelectStatement;
                    if (sf != null)
                    {
                        // Special case: undo alias in case of query fusing
                        for (int i = 0; i < sf.Columns.Count; i++)
                        {
                            ColumnFragment cf = sf.Columns[i];
                            if (column.ColumnName == cf.ColumnAlias)
                            {
                                column.ColumnName  = cf.ColumnName;
                                column.ColumnAlias = cf.ColumnAlias;
                                column.TableName   = input.Name;
                                return(column);
                            }
                        }
                    }
                }
                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);
        }
Ejemplo n.º 28
0
    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,
              string.IsNullOrEmpty(cf.ColumnAlias) ? cf.ActualColumnName : cf.ColumnAlias
              );
          newColumn.PushInput(cf.ActualColumnName);
          if (cf.TableName != null && cf.ColumnAlias == null)
            newColumn.PushInput(cf.TableName);
          if (select.Name != null)
          {
            newColumn.PushInput(select.Name);      // add the scope 
          }
          columns.Add(newColumn);
        }
        return columns;
      }
      else
        throw new NotImplementedException();
      if (!String.IsNullOrEmpty(input.Name) && input.Name != From.Name)
        foreach (ColumnFragment c in columns)
        {
            c.PushInput(input.Name);
        }
      return columns;
    }
Ejemplo n.º 29
0
        public override SqlFragment Visit(DbApplyExpression expression)
        {
            DbExpressionBinding inputBinding = expression.Input;
            InputFragment       input        = VisitInputExpression(inputBinding.Expression, inputBinding.VariableName, inputBinding.VariableType);
            DbExpressionBinding applyBinding = expression.Apply;
            InputFragment       apply        = VisitInputExpression(applyBinding.Expression, applyBinding.VariableName, applyBinding.VariableType);
            SelectStatement     select       = new SelectStatement(this);
            bool isInputSelect = false;

            if (!(input is TableFragment))
            {
                input.Wrap(scope);
                isInputSelect = true;
            }
            apply.Wrap(scope);
            select.From = input;
            select.Wrap(scope);
            if (apply is SelectStatement)
            {
                SelectStatement applySel = apply as SelectStatement;
                foreach (ColumnFragment f in applySel.Columns)
                {
                    SelectStatement newColSelect = new SelectStatement(this);
                    newColSelect.From  = applySel.From;
                    newColSelect.Where = applySel.Where;
                    if (isInputSelect)
                    {
                        VisitAndReplaceTableName(newColSelect.Where, (input as SelectStatement).From.Name, input.Name, null);
                    }
                    newColSelect.Limit      = applySel.Limit;
                    newColSelect.OrderBy    = applySel.OrderBy;
                    newColSelect.Skip       = applySel.Skip;
                    newColSelect.GroupBy    = applySel.GroupBy;
                    newColSelect.IsDistinct = applySel.IsDistinct;
                    newColSelect.Columns.Add(f);

                    newColSelect.Wrap(scope);
                    scope.Add(applySel.From.Name, applySel.From);

                    ColumnFragment newCol = new ColumnFragment(apply.Name, f.ColumnName);
                    newCol.Literal = newColSelect;
                    newCol.PushInput(newCol.ColumnName);
                    newCol.PushInput(apply.Name);
                    select.AddColumn(newCol, scope);
                    if (string.IsNullOrEmpty(newCol.ColumnAlias))
                    {
                        newColSelect.Name  = newCol.ColumnName;
                        newCol.ColumnAlias = newCol.ColumnName;
                    }
                    scope.Remove(newColSelect);
                }
                scope.Remove(applySel.From);
                scope.Remove(apply);
            }
            else if (apply is UnionFragment)
            {
                UnionFragment uf = apply as UnionFragment;
                if (uf.Left == null || uf.Right == null)
                {
                    throw new Exception("Union fragment is not properly formed.");
                }

                var left  = uf.Left as SelectStatement;
                var right = uf.Right as SelectStatement;

                if (left == null || right == null)
                {
                    throw new NotImplementedException();
                }

                var whereleft  = left.Where as BinaryFragment;
                var whereright = right.Where as BinaryFragment;

                if (whereleft == null || whereright == null)
                {
                    throw new NotImplementedException();
                }

                LiteralFragment literalFragmentWhere = null;

                //checking where left
                if (whereleft.Left is ColumnFragment && whereleft.Right is ColumnFragment)
                {
                    // we replace the where part for a dummy one
                    if (whereright.Left is ColumnFragment && whereright.Right is ColumnFragment)
                    {
                        literalFragmentWhere = new LiteralFragment("1 = 1");
                    }
                }

                if (literalFragmentWhere == null)
                {
                    throw new NotImplementedException();
                }

                var leftouterjoin = new JoinFragment();
                leftouterjoin.JoinType = Metadata.GetOperator(DbExpressionKind.LeftOuterJoin);
                leftouterjoin.Name     = apply.Name;

                // validating that column fragment on the left matches the name
                // for the input fragment
                var leftColumnFragment = whereleft.Left as ColumnFragment;

                if (leftColumnFragment == null)
                {
                    throw new NotImplementedException();
                }

                if (!leftColumnFragment.TableName.Equals(input.Name))
                {
                    new NotImplementedException();
                }

                var conditionJoin = new BinaryFragment();
                conditionJoin.Left = whereleft.Left;

                //update to the new reference
                var newColumnFragment = whereright.Right as ColumnFragment;
                if (newColumnFragment != null)
                {
                    newColumnFragment.TableName = uf.Name;
                }
                conditionJoin.Right    = newColumnFragment;
                conditionJoin.Operator = whereleft.Operator;

                leftouterjoin.Condition = conditionJoin;

                (uf.Left as SelectStatement).Where  = literalFragmentWhere;
                (uf.Right as SelectStatement).Where = literalFragmentWhere;

                leftouterjoin.Left  = input;
                leftouterjoin.Right = uf;

                return(leftouterjoin);
            }
            return(select);
        }
Ejemplo n.º 30
0
    public override SqlFragment Visit(DbApplyExpression expression)
    {

      DbExpressionBinding inputBinding = expression.Input;
      InputFragment input = VisitInputExpression(inputBinding.Expression, inputBinding.VariableName, inputBinding.VariableType);
      DbExpressionBinding applyBinding = expression.Apply;
      InputFragment apply = VisitInputExpression(applyBinding.Expression, applyBinding.VariableName, applyBinding.VariableType);
      SelectStatement select = new SelectStatement(this);
      bool isInputSelect = false;
      if (!(input is TableFragment))
      {
        input.Wrap(scope);
        isInputSelect = true;
      }
      apply.Wrap(scope);
      select.From = input;
      select.Wrap(scope);
      if (apply is SelectStatement)
      {
        SelectStatement applySel = apply as SelectStatement;
        foreach (ColumnFragment f in applySel.Columns)
        {
          SelectStatement newColSelect = new SelectStatement(this);
          newColSelect.From = applySel.From;
          newColSelect.Where = applySel.Where;
          if (isInputSelect)
          {
            VisitAndReplaceTableName(newColSelect.Where, (input as SelectStatement).From.Name, input.Name, null);
          }
          newColSelect.Limit = applySel.Limit;
          newColSelect.OrderBy = applySel.OrderBy;
          newColSelect.Skip = applySel.Skip;
          newColSelect.GroupBy = applySel.GroupBy;
          newColSelect.IsDistinct = applySel.IsDistinct;
          newColSelect.Columns.Add(f);

          newColSelect.Wrap(scope);
          scope.Add(applySel.From.Name, applySel.From);

          ColumnFragment newCol = new ColumnFragment(apply.Name, f.ColumnName);
          newCol.Literal = newColSelect;
          newCol.PushInput(newCol.ColumnName);
          newCol.PushInput(apply.Name);
          select.AddColumn(newCol, scope);
          if (string.IsNullOrEmpty(newCol.ColumnAlias))
          {
            newColSelect.Name = newCol.ColumnName;
            newCol.ColumnAlias = newCol.ColumnName;
          }
          scope.Remove(newColSelect);
        }
        scope.Remove(applySel.From);
        scope.Remove(apply);
      }
      else if (apply is UnionFragment)
      {
        UnionFragment uf = apply as UnionFragment;
        if (uf.Left == null || uf.Right == null)
          throw new Exception("Union fragment is not properly formed.");

        var left = uf.Left as SelectStatement;
        var right = uf.Right as SelectStatement;

        if (left == null || right == null)
          throw new NotImplementedException();

        var whereleft = left.Where as BinaryFragment;
        var whereright = right.Where as BinaryFragment;

        if (whereleft == null || whereright == null)
          throw new NotImplementedException();
        
        LiteralFragment literalFragmentWhere = null;        

        //checking where left
        if (whereleft.Left is ColumnFragment && whereleft.Right is ColumnFragment)
        {
          // we replace the where part for a dummy one 
          if (whereright.Left is ColumnFragment && whereright.Right is ColumnFragment)
          {
            literalFragmentWhere = new LiteralFragment("1 = 1");               
          }
        }

        if (literalFragmentWhere == null)
          throw new NotImplementedException();

        var leftouterjoin = new JoinFragment();
        leftouterjoin.JoinType = Metadata.GetOperator(DbExpressionKind.LeftOuterJoin);
        leftouterjoin.Name = apply.Name;        

        // validating that column fragment on the left matches the name
        // for the input fragment
        var leftColumnFragment = whereleft.Left as ColumnFragment;

        if (leftColumnFragment == null)
          throw new NotImplementedException();

        if (!leftColumnFragment.TableName.Equals(input.Name))
          new NotImplementedException();

        var conditionJoin = new BinaryFragment();
        conditionJoin.Left = whereleft.Left;

        //update to the new reference
        var newColumnFragment = whereright.Right as ColumnFragment;
        if (newColumnFragment != null)
        {          
          newColumnFragment.TableName = uf.Name;
        }
        conditionJoin.Right = newColumnFragment;
        conditionJoin.Operator = whereleft.Operator;

        leftouterjoin.Condition = conditionJoin;

        (uf.Left as SelectStatement).Where = literalFragmentWhere;
        (uf.Right as SelectStatement).Where = literalFragmentWhere;
        
        leftouterjoin.Left = input;
        leftouterjoin.Right = uf;

        return leftouterjoin;
      }
      return select;
    }
Ejemplo n.º 31
0
 public ColumnFragment Clone()
 {
     ColumnFragment cf = new ColumnFragment(TableName, ColumnName);
       cf.ColumnAlias = ColumnAlias;
       cf.Literal = Literal;
       return cf;
 }
Ejemplo n.º 32
0
        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;
        }
    internal void AddColumn(ColumnFragment column, Scope scope)
    {
      InputFragment input = scope.FindInputFromProperties(column.PropertyFragment);
      column.TableName = input.Name;

      // then we rename the column if necessary
      if (columnHash.ContainsKey(column.ColumnName.ToUpper()))
      {
        column.ColumnAlias = MakeColumnNameUnique(column.ColumnName);
        columnHash.Add(column.ColumnAlias, column);
      }
      else
      {
        if( !string.IsNullOrEmpty(column.ColumnAlias) )
          columnHash.Add(column.ColumnAlias.ToUpper(), column);
        else
          columnHash.Add(column.ColumnName.ToUpper(), column);
      }
      Columns.Add(column);
    }
Ejemplo n.º 34
0
 public void Visit(ColumnFragment f)
 {
   if ((f != null) && (f.TableName == _oldTableName))
     f.TableName = _newTableName;
 }
Ejemplo n.º 35
0
 public void Visit(ColumnFragment f)
 {
   ColumnFragment cf;
   if ((f != null) && (f.TableName == _oldTableName))
     f.TableName = _newTableName;
   if (_dicColumns != null && f.ColumnName != null && _dicColumns.TryGetValue(f.ColumnName, out cf))
   {
     // remove alias
     f.ColumnName = cf.ColumnName;
   }
 }
Ejemplo n.º 36
0
        public override SqlFragment Visit(DbGroupByExpression expression)
        {
            // first process the input
            DbGroupExpressionBinding e = expression.Input;
            SelectStatement innerSelect = VisitInputExpressionEnsureSelect(e.Expression, e.VariableName, e.VariableType);
            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)
            {
                select.AddGroupBy(key.Accept(this));
                propIndex++;
            }

            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;
        }
Ejemplo n.º 37
0
        void AddDefaultColumnsForTable(TableFragment table)
        {
            if (columnHash == null)
                columnHash = new Dictionary<string, ColumnFragment>();

            foreach (EdmProperty property in Metadata.GetProperties(table.Type.EdmType))
            {
                ColumnFragment col = new ColumnFragment(table.Name, property.Name);
                if (table.Columns == null)
                    table.Columns = new List<ColumnFragment>();
                table.Columns.Add(col);
                if (columnHash.ContainsKey(col.ColumnName))
                {
                    col.ColumnAlias = MakeColumnNameUnique(col.ColumnName);
                    columnHash.Add(col.ColumnAlias, col);
                }
                else
                    columnHash.Add(col.ColumnName, col);
                Columns.Add(col);
            }
        }
Ejemplo n.º 38
0
 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 override SqlFragment Visit(DbApplyExpression expression)
    {
      DbExpressionBinding inputBinding = expression.Input;
      InputFragment input = VisitInputExpression(inputBinding.Expression, inputBinding.VariableName, inputBinding.VariableType);
      DbExpressionBinding applyBinding = expression.Apply;
      InputFragment apply = VisitInputExpression(applyBinding.Expression, applyBinding.VariableName, applyBinding.VariableType);
      SelectStatement select = new SelectStatement(this);
      bool isInputSelect = false;
      if (!(input is TableFragment))
      {
        input.Wrap(scope);
        isInputSelect = true;
      }
      apply.Wrap(scope);
      select.From = input;
      select.Wrap(scope);
      if (apply is SelectStatement)
      {        
        SelectStatement applySel = apply as SelectStatement;
        foreach (ColumnFragment f in applySel.Columns)
        {
          SelectStatement newColSelect = new SelectStatement(this);
          newColSelect.From = applySel.From;
		  newColSelect.Where = applySel.Where;
          if (isInputSelect)
          {
            VisitAndReplaceTableName(newColSelect.Where, (input as SelectStatement).From.Name, input.Name);
          }
          newColSelect.Limit = applySel.Limit;
          newColSelect.Columns.Add( f );

          newColSelect.Wrap(scope);
          scope.Add(applySel.From.Name, applySel.From);
          
          ColumnFragment newCol = new ColumnFragment(apply.Name, f.ColumnName);
          newCol.Literal = newColSelect;
          newCol.PushInput(newCol.ColumnName);
          newCol.PushInput(apply.Name);
          select.AddColumn( newCol, scope);
          if (string.IsNullOrEmpty(newCol.ColumnAlias))
          {
            newColSelect.Name = newCol.ColumnName;
            newCol.ColumnAlias = newCol.ColumnName;
          }
          scope.Remove(newColSelect);
        }
        scope.Remove(applySel.From);
        scope.Remove(apply);
      }
      return select;
    }