private static SqlQueryExpression Form(IContext context, PlSqlParser.QueryBlockContext queryBlock,
                                                   out SqlParseIntoClause into)
            {
                var fromClause = FromClauseBuilder.Build(context, queryBlock.fromClause());

                SqlQueryExpressionItem[] columns;

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

                var query = new SqlQueryExpression();

                foreach (var column in columns)
                {
                    query.Items.Add(column);
                }

                @into = null;

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

                var intoClause = queryBlock.into_clause();

                if (intoClause != null)
                {
                    @into = new SqlParseIntoClause();

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

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

                var groupBy = queryBlock.groupByClause();

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

                    var having = groupBy.havingClause();
                    if (having != null)
                    {
                        query.Having = new PlSqlExpressionVisitor(context).Visit(having.condition());
                    }
                }

                var groupMax = queryBlock.groupMaxClause();

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

                var whereClause = queryBlock.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.Where = new PlSqlExpressionVisitor(context).Visit(whereClause.conditionWrapper());
                    }
                }

                //TODO: in case of a SELECT INTO cause create a statement

                return(query);
            }
Esempio n. 2
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);
        }