Пример #1
0
        public static SqlQueryExpression Form(PlSqlParser.SubqueryContext context, out IntoClause into)
        {
            var query = Form(context.subqueryBasicElements(), out into);

            var opPart = context.subquery_operation_part();

            if (opPart.Length > 0)
            {
                if (into != null)
                {
                    throw new InvalidOperationException("Cannot SELECT INTO in a composite query.");
                }

                foreach (var part in opPart)
                {
                    CompositeFunction function;
                    if (part.MINUS() != null || part.EXCEPT() != null)
                    {
                        function = CompositeFunction.Except;
                    }
                    else if (part.UNION() != null)
                    {
                        function = CompositeFunction.Union;
                    }
                    else if (part.INTERSECT() != null)
                    {
                        function = CompositeFunction.Intersect;
                    }
                    else
                    {
                        throw new ParseCanceledException("Invalid composite function.");
                    }

                    bool isAll = part.ALL() != null;

                    var next = Form(part.subqueryBasicElements());
                    var prev = query.NextComposite;

                    if (prev == null)
                    {
                        prev = next;
                    }
                    else
                    {
                        prev.NextComposite = next;
                    }

                    query.IsCompositeAll    = isAll;
                    query.CompositeFunction = function;
                    query.NextComposite     = prev;
                }
            }

            return(query);
        }
Пример #2
0
        public static SqlQueryExpression Form(PlSqlParser.SubqueryContext context, out IntoClause into)
        {
            var query = Form(context.subqueryBasicElements(), out into);

            var opPart = context.subquery_operation_part();

            if (opPart.Length > 0) {
                if (into != null)
                    throw new InvalidOperationException("Cannot SELECT INTO in a composite query.");

                foreach (var part in opPart) {
                    CompositeFunction function;
                    if (part.MINUS() != null || part.EXCEPT() != null) {
                        function = CompositeFunction.Except;
                    } else if (part.UNION() != null) {
                        function = CompositeFunction.Union;
                    } else if (part.INTERSECT() != null) {
                        function = CompositeFunction.Intersect;
                    } else {
                        throw new ParseCanceledException("Invalid composite function.");
                    }

                    bool isAll = part.ALL() != null;

                    var next = Form(part.subqueryBasicElements());
                    var prev = query.NextComposite;

                    if (prev == null) {
                        prev = next;
                    } else {
                        prev.NextComposite = next;
                    }

                    query.IsCompositeAll = isAll;
                    query.CompositeFunction = function;
                    query.NextComposite = prev;
                }
            }

            return query;
        }
Пример #3
0
        private static SqlQueryExpression Form(PlSqlParser.SubqueryBasicElementsContext context, out IntoClause into)
        {
            var sub = context.subquery();

            if (sub != null && !sub.IsEmpty)
            {
                return(Form(sub, out into));
            }

            return(Form(context.queryBlock(), out into));
        }
Пример #4
0
        private static SqlQueryExpression Form(PlSqlParser.QueryBlockContext context, out IntoClause into)
        {
            var fromClause = FromClauseBuilder.Build(context.fromClause());

            SelectColumn[] columns;

            if (context.all != null)
            {
                columns = new[] { new SelectColumn(SqlExpression.Reference(new ObjectName("*"))) };
            }
            else
            {
                columns = context.selectedElement().Select(SelectElement.BuildColumn).ToArray();
            }

            var query = new SqlQueryExpression(columns);

            into = null;

            if (context.DISTINCT() != null ||
                context.UNIQUE() != null)
            {
                query.Distinct = true;
            }

            var intoClause = context.into_clause();

            if (intoClause != null)
            {
                into = new IntoClause();

                if (intoClause.objectName() != null)
                {
                    into.TableName = Name.Object(intoClause.objectName());
                }
                else if (intoClause.variable_name() != null)
                {
                    into.Variables = intoClause.variable_name().Select(Name.Variable).ToArray();
                }
            }

            if (fromClause != null)
            {
                query.FromClause = fromClause;
            }

            var groupBy = context.groupByClause();

            if (groupBy != null && !groupBy.IsEmpty)
            {
                query.GroupBy = groupBy.groupByElements().expression().Select(x => new SqlExpressionVisitor().Visit(x));

                var having = groupBy.havingClause();
                if (having != null)
                {
                    query.HavingExpression = new SqlExpressionVisitor().Visit(having.condition());
                }
            }

            var groupMax = context.groupMaxClause();

            if (groupMax != null && !groupMax.IsEmpty)
            {
                var maxColumn = Name.Object(groupMax.objectName());
                query.GroupMax = maxColumn;
            }

            var whereClause = context.whereClause();

            if (whereClause != null && !whereClause.IsEmpty)
            {
                var currentOf = whereClause.current_of_clause();
                if (currentOf != null && !currentOf.IsEmpty)
                {
                    var cursorName = Name.Simple(currentOf.cursor_name());
                    throw new NotImplementedException();
                }
                else
                {
                    query.WhereExpression = new SqlExpressionVisitor().Visit(whereClause.conditionWrapper());
                }
            }

            return(query);
        }
Пример #5
0
        private static SqlQueryExpression Form(PlSqlParser.QueryBlockContext context, out IntoClause into)
        {
            var fromClause = FromClauseBuilder.Build(context.fromClause());

            SelectColumn[] columns;

            if (context.all != null) {
                columns = new[] {new SelectColumn(SqlExpression.Reference(new ObjectName("*")))};
            } else {
                columns = context.selectedElement().Select(SelectElement.BuildColumn).ToArray();
            }

            var query = new SqlQueryExpression(columns);

            into = null;

            if (context.DISTINCT() != null ||
                context.UNIQUE() != null)
                query.Distinct = true;

            var intoClause = context.into_clause();
            if (intoClause != null) {
                into = new IntoClause();

                if (intoClause.objectName() != null) {
                    into.TableName = Name.Object(intoClause.objectName());
                } else if (intoClause.variable_name() != null) {
                    into.Variables = intoClause.variable_name().Select(Name.Variable).ToArray();
                }
            }

            if (fromClause != null)
                query.FromClause = fromClause;

            var groupBy = context.groupByClause();
            if (groupBy != null && !groupBy.IsEmpty) {
                query.GroupBy = groupBy.groupByElements().expression().Select(x => new SqlExpressionVisitor().Visit(x));

                var having = groupBy.havingClause();
                if (having != null)
                    query.HavingExpression = new SqlExpressionVisitor().Visit(having.condition());
            }

            var groupMax = context.groupMaxClause();
            if (groupMax != null && !groupMax.IsEmpty) {
                var maxColumn = Name.Object(groupMax.objectName());
                query.GroupMax = maxColumn;
            }

            var whereClause = context.whereClause();
            if (whereClause != null && !whereClause.IsEmpty) {
                var currentOf = whereClause.current_of_clause();
                if (currentOf != null && !currentOf.IsEmpty) {
                    var cursorName = Name.Simple(currentOf.cursor_name());
                    throw new NotImplementedException();
                } else {
                    query.WhereExpression = new SqlExpressionVisitor().Visit(whereClause.conditionWrapper());
                }
            }

            return query;
        }
Пример #6
0
        private static SqlQueryExpression Form(PlSqlParser.SubqueryBasicElementsContext context, out IntoClause into)
        {
            var sub = context.subquery();
            if (sub != null && !sub.IsEmpty)
                return Form(sub, out into);

            return Form(context.queryBlock(), out into);
        }