Example #1
0
        private IList <TableIdentifier> ProcessBinaryQueryExpression(BinaryQueryExpression binaryQueryExpression)
        {
            var fromIds = new List <TableIdentifier>();

            if (binaryQueryExpression != null)
            {
                var firstExpr  = binaryQueryExpression.FirstQueryExpression;
                var secondExpr = binaryQueryExpression.SecondQueryExpression;

                Func <QueryExpression, IList <TableIdentifier> > processExpr = (expr) =>
                {
                    if (expr is BinaryQueryExpression)
                    {
                        return(ProcessBinaryQueryExpression(expr as BinaryQueryExpression));
                    }
                    else if (expr is QuerySpecification)
                    {
                        return(ProcessSelect(expr));
                    }
                    return(null);
                };

                var firstExprTables = processExpr(firstExpr);
                if (firstExprTables != null && firstExprTables.Count > 0)
                {
                    fromIds.AddRange(firstExprTables);
                }
                var secondExprTables = processExpr(secondExpr);
                if (secondExprTables != null && secondExprTables.Count > 0)
                {
                    fromIds.AddRange(secondExprTables);
                }
            }
            return(fromIds);
        }
 private QuerySpecification GetQueryFromUnion(BinaryQueryExpression binaryQueryExpression)
 {
     while (binaryQueryExpression.FirstQueryExpression as BinaryQueryExpression != null)
     {
         binaryQueryExpression = binaryQueryExpression.FirstQueryExpression as BinaryQueryExpression;
     }
     return(binaryQueryExpression.FirstQueryExpression as QuerySpecification);
 }
Example #3
0
        private static QuerySpecification TopQuerySpecification(QueryExpression expr)
        {
            if (expr is QuerySpecification qspec)
            {
                return(qspec);
            }
            BinaryQueryExpression bqExpr = (BinaryQueryExpression)expr;

            return(TopQuerySpecification(bqExpr.FirstQueryExpression));
        }
Example #4
0
        public SqlServerBinaryTableNode VisitBinaryQueryExpression(BinaryQueryExpression binaryQueryExpression)
        {
            return(TreeHelper.Create <SqlServerBinaryTableNode>(n =>
            {
                n.Left.SetValue(VisitQueryExpression(binaryQueryExpression.FirstQueryExpression));
                n.Right.SetValue(VisitQueryExpression(binaryQueryExpression.SecondQueryExpression));

                n.BinaryTableType = binaryQueryExpression.BinaryQueryExpressionType switch
                {
                    BinaryQueryExpressionType.Except => SqlServerBinaryTableType.Except,
                    BinaryQueryExpressionType.Intersect => SqlServerBinaryTableType.Intersect,
                    BinaryQueryExpressionType.Union => SqlServerBinaryTableType.Union,
                    _ => throw new InvalidOperationException()
                };

                SqlServerTree.PutFragmentSpan(n, binaryQueryExpression);
            }));
        }
Example #5
0
        /// <summary>
        /// SELECT文 にコンストラクタで指定されたクエリを追加する。
        /// </summary>
        /// <param name="node">SELECT文</param>
        public override void ExplicitVisit(SelectStatement node)
        {
            try
            {
                var queryExpression = FindQueryExpression.Compile().Invoke(node);
                if (queryExpression != null && BinaryQueryExpressionType.HasValue)
                {
                    queryExpression = new BinaryQueryExpression()
                    {
                        BinaryQueryExpressionType = BinaryQueryExpressionType.Value,
                        FirstQueryExpression      = queryExpression,
                        SecondQueryExpression     = QueryExpression,
                        All           = All,
                        ForClause     = queryExpression.ForClause,
                        OffsetClause  = queryExpression.OffsetClause,
                        OrderByClause = queryExpression.OrderByClause,
                    };

                    var binaryQueryExpression = (BinaryQueryExpression)queryExpression;
                    binaryQueryExpression.FirstQueryExpression.OffsetClause   = null;
                    binaryQueryExpression.FirstQueryExpression.OrderByClause  = null;
                    binaryQueryExpression.FirstQueryExpression.ForClause      = null;
                    binaryQueryExpression.SecondQueryExpression.OffsetClause  = null;
                    binaryQueryExpression.SecondQueryExpression.OrderByClause = null;
                    binaryQueryExpression.SecondQueryExpression.ForClause     = null;
                }
                else
                {
                    queryExpression = QueryExpression;
                }

                var member = (MemberExpression)FindQueryExpression.Body;
                var param  = Expression.Parameter(typeof(QueryExpression), "value");
                var set    = Expression.Lambda <Action <SelectStatement, QueryExpression> >(
                    Expression.Assign(member, param), FindQueryExpression.Parameters[0], param);
                set.Compile().Invoke(node, queryExpression);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("指定されたクエリは追加できません。", e);
            }

            base.ExplicitVisit(node);
        }
Example #6
0
        protected void CheckForQueryExpression(object queryExpression, ref IList <TSqlFragment> selectStatements)
        {
            IList <TSqlFragment> statements = new List <TSqlFragment>();

            if (queryExpression.GetType().Equals(typeof(QueryExpression)) || queryExpression.GetType().Equals(typeof(QuerySpecification)))
            {
                statements = ((QuerySpecification)queryExpression).SelectElements;
                foreach (var stmt in statements)
                {
                    selectStatements.Add(stmt);
                }
                //selectStatements = statements;
            }
            else if (queryExpression.GetType().Equals(typeof(BinaryQueryExpression)))
            {
                BinaryQueryExpression experssion = (BinaryQueryExpression)(queryExpression);
                //statements = ((QuerySpecification)(experssion.FirstQueryExpression)).SelectElements;
                //selectStatements.Concat(statements);
                CheckForQueryExpression(experssion.FirstQueryExpression, ref selectStatements);
                CheckForQueryExpression(experssion.SecondQueryExpression, ref selectStatements);
            }
        }
Example #7
0
        private void recursifRecuperationProjection(QuerySpecification node)
        {
            bool starExpression = false;

            if (node.SelectElements != null)
            {
                foreach (TSqlFragment selectElement in node.SelectElements)
                {
                    if (selectElement is SelectStarExpression)
                    {
                        starExpression = true;
                        if (node.SelectElements.Count > 1)
                        {
                            //Lever une exception qui dit que c'est non géré*

                            break;
                        }
                        else
                        {
                            if (node.FromClause != null)
                            {
                                foreach (TSqlFragment fromElement in node.FromClause.TableReferences)
                                {
                                    if (fromElement is NamedTableReference)
                                    {
                                        String namedTableReferenceText = GetNodeTokenText(fromElement);
                                        projectionList.AddRange(GetProjectionsFromTablesReference(namedTableReferenceText));
                                    }
                                    if (fromElement is QualifiedJoin)
                                    {
                                        QualifiedJoin currQualifiedJoin = (QualifiedJoin)fromElement;
                                        if (currQualifiedJoin.FirstTableReference is NamedTableReference && currQualifiedJoin.SecondTableReference is NamedTableReference)
                                        {
                                            String namedTableReferenceText1 = GetNodeTokenText(currQualifiedJoin.FirstTableReference);
                                            projectionList.AddRange(GetProjectionsFromTablesReference(namedTableReferenceText1));

                                            String namedTableReferenceText2 = GetNodeTokenText(currQualifiedJoin.SecondTableReference);
                                            projectionList.AddRange(GetProjectionsFromTablesReference(namedTableReferenceText2));
                                        }
                                    }
                                    if (fromElement is PivotedTableReference)
                                    {
                                        PivotedTableReference currPivotedTable = (PivotedTableReference)fromElement;
                                        if (currPivotedTable.TableReference is NamedTableReference)
                                        {
                                            String namedTableReferenceText = GetNodeTokenText(currPivotedTable.TableReference);
                                            projectionList.AddRange(GetProjectionsFromTablesReference(namedTableReferenceText));
                                        }
                                    }
                                    if (fromElement is QueryDerivedTable)
                                    {
                                        QueryDerivedTable currQueryDerivedTable = (QueryDerivedTable)fromElement;
                                        if (currQueryDerivedTable.QueryExpression is QuerySpecification)
                                        {
                                            QuerySpecification currQuerySpecification = (QuerySpecification)currQueryDerivedTable.QueryExpression;
                                            recursifRecuperationProjection(currQuerySpecification);
                                        }
                                        if (currQueryDerivedTable.QueryExpression is BinaryQueryExpression)
                                        {
                                            //BinaryQueryExpression représente un union entre deux requête donc si on veut les projections dans le cas ou
                                            //Il y à un * il faut prendre les projections de la première requête d'union ou intersection
                                            BinaryQueryExpression currBinaryQueryExpression = (BinaryQueryExpression)currQueryDerivedTable.QueryExpression;


                                            while (currBinaryQueryExpression.FirstQueryExpression is BinaryQueryExpression)
                                            {
                                                currBinaryQueryExpression = (BinaryQueryExpression)currBinaryQueryExpression.FirstQueryExpression;
                                            }
                                            if (currBinaryQueryExpression.FirstQueryExpression is QuerySpecification)
                                            {
                                                QuerySpecification currQuerySpecification = (QuerySpecification)currBinaryQueryExpression.FirstQueryExpression;
                                                recursifRecuperationProjection(currQuerySpecification);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (starExpression == false)
                {
                    foreach (SelectElement selectElement in node.SelectElements)
                    {
                        projectionList.Add(GetNodeTokenText(selectElement));
                    }
                }
            }
        }
Example #8
0
 public override void Visit(BinaryQueryExpression node) { this.action(node); }
        public static EngineResult Evaluate(QueryExpression queryExpr, Scope scope)
        {
            var result = queryExpr switch
            {
                QuerySpecification querySpec => Evaluate(querySpec, scope),
                QueryParenthesisExpression paren => Evaluate(paren.QueryExpression, scope),
                BinaryQueryExpression binaryExpr => Evaluate(
                    binaryExpr.BinaryQueryExpressionType,
                    binaryExpr.All,
                    Evaluate(binaryExpr.FirstQueryExpression, scope).ResultSet,
                    Evaluate(binaryExpr.SecondQueryExpression, scope).ResultSet,
                    scope.Env),
                _ => throw FeatureNotSupportedException.Subtype(queryExpr)
            };

            if (result.ResultSet == null)
            {
                return(result);
            }

            var projection = result.ResultSet;

            // ORDER BY

            if (queryExpr.OrderByClause != null)
            {
                var elements     = queryExpr.OrderByClause.OrderByElements;
                var firstElement = elements.First();
                var restElements = elements.Skip(1);
                var temp         = projection.CopyLayout();

                foreach (var row in restElements.Aggregate(
                             Order(projection.Rows, firstElement, scope),
                             (orderedRows, element) => Order(orderedRows, element, scope)))
                {
                    CopyOnto(row, temp, scope.Env);
                }

                projection.Rows.Clear();
                CopyOnto(temp, projection, scope.Env);
            }

            // OFFSET

            if (queryExpr.OffsetClause != null)
            {
                var offset = Evaluate <int>(queryExpr.OffsetClause.OffsetExpression, NullArgument.It, scope);

                for (var i = 0; i < offset; ++i)
                {
                    projection.Rows.RemoveAt(0);
                }

                if (queryExpr.OffsetClause.FetchExpression != null)
                {
                    var fetch = Evaluate <int>(queryExpr.OffsetClause.FetchExpression, NullArgument.It, scope);

                    while (projection.Rows.Count > fetch)
                    {
                        projection.Rows.RemoveAt(projection.Rows.Count - 1);
                    }
                }
            }

            return(new EngineResult(projection));
        }
    }
 public override void ExplicitVisit(BinaryQueryExpression fragment)
 {
     _fragments.Add(fragment);
 }
 public override void ExplicitVisit(BinaryQueryExpression node)
 {
     base.ExplicitVisit(node);
 }
Example #12
0
 private QuerySpecification GetQueryFromUnion(BinaryQueryExpression binaryQueryExpression)
 {
     while (binaryQueryExpression.FirstQueryExpression as BinaryQueryExpression != null)
     {
         binaryQueryExpression = binaryQueryExpression.FirstQueryExpression as BinaryQueryExpression;
     }
     return binaryQueryExpression.FirstQueryExpression as QuerySpecification;
 }