Inheritance: InputFragment
        public void Remove(string Name, InputFragment fragment)
        {
            if (fragment == null)
            {
                return;
            }
            if (Name != null)
            {
                scopeTable.Remove(Name);
            }

            if (fragment is SelectStatement)
            {
                Remove((fragment as SelectStatement).From);
            }
            else if (fragment is JoinFragment)
            {
                JoinFragment j = fragment as JoinFragment;
                Remove(j.Left);
                Remove(j.Right);
            }
            else if (fragment is UnionFragment)
            {
                UnionFragment u = fragment as UnionFragment;
                Remove(u.Left);
                Remove(u.Right);
            }
        }
Exemple #2
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);
        }
        public override SqlFragment Visit(DbUnionAllExpression expression)
        {
            UnionFragment f = new UnionFragment();

            Debug.Assert(expression.Left is DbProjectExpression);
            Debug.Assert(expression.Right is DbProjectExpression);

            SelectStatement left = VisitInputExpressionEnsureSelect(expression.Left, null, null);

            Debug.Assert(left.Name == null);
            left.Wrap(null);

            SelectStatement right = VisitInputExpressionEnsureSelect(expression.Right, null, null);

            Debug.Assert(right.Name == null);
            right.Wrap(null);

            f.Left  = left;
            f.Right = right;
            return(f);
        }
        public override SqlFragment Visit(DbUnionAllExpression expression)
        {
            UnionFragment f = new UnionFragment();
            Debug.Assert(expression.Left is DbProjectExpression);
            Debug.Assert(expression.Right is DbProjectExpression);

            SelectStatement left = VisitInputExpressionEnsureSelect(expression.Left, null, null);
            Debug.Assert(left.Name == null);
            left.Wrap(null);

            SelectStatement right = VisitInputExpressionEnsureSelect(expression.Right, null, null);
            Debug.Assert(right.Name == null);
            right.Wrap(null);

            f.Left = left;
            f.Right = right;
            return f;
        }
 public void Visit(UnionFragment f)
 {
 }
 public void Visit(UnionFragment f)
 {
 }
Exemple #7
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);
        }
Exemple #8
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);
        }