Beispiel #1
0
        public override SqlExpression VisitExtractFunction(PlSqlParser.ExtractFunctionContext context)
        {
            var part = SqlParseName.Simple(context.regular_id());
            var exp  = Visit(context.concatenationWrapper());

            return(SqlExpression.Function("SQL_EXTRACT", new[] { exp, SqlExpression.Constant(SqlObject.String(part)) }));
        }
            public static SqlQueryExpressionItem BuildColumn(IContext context, PlSqlParser.SelectedElementContext selectedElement)
            {
                string alias = null;

                if (selectedElement.column_alias() != null &&
                    !selectedElement.column_alias().IsEmpty)
                {
                    alias = SqlParseName.Simple(selectedElement.column_alias());
                }

                SqlQueryExpressionItem column;

                if (selectedElement.expression() != null &&
                    !selectedElement.expression().IsEmpty)
                {
                    column = new SqlQueryExpressionItem(SqlParseExpression.Build(context, selectedElement.expression()), alias);
                }
                else if (selectedElement.selectedColumn() != null &&
                         !selectedElement.selectedColumn().IsEmpty)
                {
                    bool       glob = selectedElement.selectedColumn().glob != null;
                    ObjectName name = SqlParseName.Select(selectedElement.selectedColumn().objectName(), glob);

                    var exp = SqlExpression.Reference(name);
                    column = new SqlQueryExpressionItem(exp, alias);
                }
                else
                {
                    throw new ParseCanceledException();
                }

                return(column);
            }
        public static SqlParseFunctionArgument Form(IContext context, PlSqlParser.ArgumentContext argument)
        {
            if (argument == null)
            {
                return(null);
            }

            var id  = SqlParseName.Simple(argument.id());
            var exp = Parsing.SqlParseExpression.Build(context, argument.expressionWrapper());

            return(new SqlParseFunctionArgument {
                Id = id,
                Expression = exp
            });
        }
        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 = SqlParseName.Object(intoClause.objectName());
                }
                else if (intoClause.variable_name() != null)
                {
                    into.Variables = intoClause.variable_name().Select(SqlParseName.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 SqlExpressionVisitor(context).Visit(x)).ToList();

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

            var groupMax = queryBlock.groupMaxClause();

            if (groupMax != null && !groupMax.IsEmpty)
            {
                var maxColumn = SqlParseName.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 = SqlParseName.Simple(currentOf.cursor_name());
                    throw new NotImplementedException();
                }
                else
                {
                    query.Where = new SqlExpressionVisitor(context).Visit(whereClause.conditionWrapper());
                }
            }

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

            return(query);
        }