Beispiel #1
0
        public override SqlExpression VisitTimeStampFunction(PlSqlParser.TimeStampFunctionContext context)
        {
            SqlExpression arg;

            if (context.bind_variable() != null)
            {
                arg = SqlExpression.Variable(SqlParseName.Variable(context.bind_variable()));
            }
            else if (context.argString != null)
            {
                arg = SqlExpression.Constant(SqlObject.String(SqlParseInputString.AsNotQuoted(context.argString)));
            }
            else
            {
                throw new ParseCanceledException("Invalid argument in a TIMESTAMP implicit function");
            }

            SqlExpression tzArg = null;

            if (context.tzString != null)
            {
                tzArg = SqlExpression.Constant(SqlObject.String(SqlParseInputString.AsNotQuoted(context.tzString)));
            }

            var args = tzArg != null
                ? new SqlExpression[] { arg, tzArg }
                : new SqlExpression[] { arg };

            return(SqlExpression.Function("TOTIMESTAMP", args));
        }
Beispiel #2
0
        public override SqlExpression VisitNextValueFunction(PlSqlParser.NextValueFunctionContext context)
        {
            var seqName = SqlParseName.Object(context.objectName());

            return(SqlExpression.Function("NEXTVAL",
                                          new SqlExpression[] { SqlExpression.Constant(SqlObject.String(seqName.ToString())) }));
        }
            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);
            }
Beispiel #4
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)) }));
        }
Beispiel #5
0
            public override SqlTypeResolveInfo VisitRowRefType(PlSqlParser.RowRefTypeContext context)
            {
                var objName = SqlParseName.Object(context.objectName());

                return(new SqlTypeResolveInfo("%ROWTYPE",
                                              new Dictionary <string, object> {
                    { "ObjectName", objName.FullName }
                }));
            }
Beispiel #6
0
            public override SqlTypeResolveInfo VisitColumnRefType(PlSqlParser.ColumnRefTypeContext context)
            {
                var fieldName = SqlParseName.Object(context.objectName());

                return(new SqlTypeResolveInfo("%TYPE",
                                              new Dictionary <string, object> {
                    { "FieldName", fieldName.FullName }
                }));
            }
Beispiel #7
0
            public override SqlTypeResolveInfo VisitUserDataType(PlSqlParser.UserDataTypeContext context)
            {
                var name = SqlParseName.Object(context.objectName());
                var args = context.typeArgument();

                if (args != null && args.typeArgumentSpec().Length > 0)
                {
                    throw new NotSupportedException("Arguments to user-defined type are not supported yet.");
                }

                return(new SqlTypeResolveInfo(name.FullName));
            }
Beispiel #8
0
        public override SqlExpression VisitInvokedFunction(PlSqlParser.InvokedFunctionContext context)
        {
            var name = SqlParseName.Object(context.objectName());

            InvokeArgument[] args = null;

            if (context.argument() != null)
            {
                args = context.argument().Select(SqlParseFunctionArgument.Form)
                       .Select(x => new InvokeArgument(x.Id, x.Expression)).ToArray();
            }

            return(SqlExpression.Function(name, args));
        }
        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
            });
        }
        public static SqlParseElementNode Form(IContext context, PlSqlParser.General_elementContext element)
        {
            var id  = SqlParseName.Object(element.objectName());
            var arg = element.function_argument();
            IEnumerable <SqlParseFunctionArgument> argNodes = null;

            if (arg != null)
            {
                argNodes = arg.argument().Select(x => SqlParseFunctionArgument.Form(context, x));
            }

            return(new SqlParseElementNode {
                Id = id,
                Argument = argNodes != null?argNodes.ToArray() : null
            });
        }
            private static FromSource FormSource(IContext context, PlSqlParser.QueryExpressionClauseContext clause)
            {
                var tableName = SqlParseName.Object(clause.objectName());
                var query     = clause.subquery();

                var source = new FromSource();

                if (tableName != null)
                {
                    source.TableName = ObjectName.Parse(tableName.ToString());
                }
                else if (!query.IsEmpty)
                {
                    source.SubQuery = Form(context, query);
                }

                if (clause.alias != null && !clause.alias.IsEmpty)
                {
                    source.Alias = clause.alias.GetText();
                }

                return(source);
            }
Beispiel #12
0
        public override SqlExpression VisitBind_variable(PlSqlParser.Bind_variableContext context)
        {
            var varRef = SqlParseName.Variable(context);

            return(SqlExpression.Variable(varRef));
        }
        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);
        }