public override SqlExpression VisitQuantifiedExpression(PlSqlParser.QuantifiedExpressionContext context)
        {
            SqlExpression arg;

            if (context.subquery() != null)
            {
                arg = Subquery.Form(context.subquery());
            }
            else if (context.expression_list() != null)
            {
                var elements = context.expression_list().expression().Select(Visit).ToArray();
                arg = SqlExpression.Constant(Field.Array(elements));
            }
            else
            {
                throw new ParseCanceledException("Invalid argument in a quantified expression.");
            }

            if (context.ALL() != null)
            {
                return(SqlExpression.All(arg));
            }
            if (context.ANY() != null ||
                context.SOME() != null)
            {
                return(SqlExpression.Any(arg));
            }

            return(base.VisitQuantifiedExpression(context));
        }
        public override SqlStatement VisitInsertStatement(PlSqlParser.InsertStatementContext context)
        {
            var singleTableInsert = context.singleTableInsert();
            var insertInto        = singleTableInsert.insertIntoClause();

            if (insertInto != null)
            {
                var tableName   = Name.Object(insertInto.objectName());
                var columnNames = new string[0];

                if (insertInto.columnName() != null)
                {
                    columnNames = insertInto.columnName().Select(Name.Simple).ToArray();
                }

                if (singleTableInsert.valuesClause() != null)
                {
                    var values = new List <SqlExpression[]>();
                    foreach (var listContext in singleTableInsert.valuesClause().expression_list())
                    {
                        var array = listContext.expression().Select(Expression.Build).ToArray();
                        values.Add(array);
                    }

                    return(new InsertStatement(tableName, columnNames, values));
                }

                if (singleTableInsert.subquery() != null)
                {
                    var query = Subquery.Form(singleTableInsert.subquery());
                    return(new InsertSelectStatement(tableName, columnNames, query));
                }
            }
            else if (singleTableInsert.insertSetClause() != null)
            {
                var tableName = Name.Object(singleTableInsert.insertSetClause().objectName());

                var columns = new List <string>();
                var values  = new List <SqlExpression>();

                foreach (var assignmentContext in singleTableInsert.insertSetClause().insertAssignment())
                {
                    var columnName = Name.Simple(assignmentContext.columnName());
                    var value      = Expression.Build(assignmentContext.expression());

                    columns.Add(columnName);
                    values.Add(value);
                }

                return(new InsertStatement(tableName, columns.ToArray(), new[] { values.ToArray() }));
            }

            return(base.VisitInsertStatement(context));
        }
Пример #3
0
        public static UpdateStatement Build(PlSqlParser.UpdateStatementContext context)
        {
            var tableName   = Name.Object(context.objectName());
            var setClause   = context.updateSetClause();
            var limitClause = context.updateLimitClause();

            if (setClause != null)
            {
                var assignments = new List <SqlColumnAssignment>();
                var whereClause = context.whereClause();
                int limit       = -1;

                if (limitClause != null)
                {
                    limit = Number.PositiveInteger(limitClause.numeric()) ?? -1;
                }

                SqlExpression whereExpression = null;
                if (whereClause != null)
                {
                    whereExpression = Expression.Build(whereClause.conditionWrapper());
                }

                if (setClause.VALUE() != null)
                {
                    var columnName = Name.Simple(setClause.columnName());
                    var value      = Expression.Build(setClause.expression());

                    assignments.Add(new SqlColumnAssignment(columnName, value));
                }
                else
                {
                    var pairs = setClause.columnBasedUpdateClause().Select(x => new {
                        columnName = Name.Simple(x.columnName()),
                        value      = Expression.Build(x.expression())
                    });

                    assignments = pairs.Select(x => new SqlColumnAssignment(x.columnName, x.value)).ToList();
                }

                return(new UpdateStatement(tableName, whereExpression, assignments)
                {
                    Limit = limit
                });
            }
            if (context.updateFromClause() != null)
            {
                var query = Subquery.Form(context.updateFromClause().subquery());
            }

            throw new NotSupportedException();
        }
Пример #4
0
        public static SqlStatement Declare(PlSqlParser.CursorDeclarationContext context)
        {
            var cursorName = Name.Simple(context.cursor_name());
            var query      = Subquery.Form(context.subquery());

            CursorParameter[] parameters = null;
            if (context.parameterSpec() != null)
            {
                parameters = context.parameterSpec().Select(Parameter.Form).ToArray();
            }

            return(new DeclareCursorStatement(cursorName, parameters, query));
        }
Пример #5
0
        public static SqlStatement Build(PlSqlParser.SelectStatementContext context)
        {
            IntoClause into;
            var        query = Subquery.Form(context.subquery(), out into);

            if (into != null)
            {
                SqlExpression reference;
                if (into.TableName != null)
                {
                    reference = SqlExpression.Reference(into.TableName);
                }
                else
                {
                    var vars = into.Variables;
                    reference = SqlExpression.Tuple(vars.Select(SqlExpression.VariableReference).Cast <SqlExpression>().ToArray());
                }

                return(new SelectIntoStatement(query, reference));
            }

            var statement = new SelectStatement(query);

            var orderBy   = context.orderByClause();
            var forUpdate = context.forUpdateClause();

            if (orderBy != null)
            {
                var sortColumns = orderBy.orderByElements().orderByElement().Select(x => {
                    bool asc = x.DESC() == null;
                    var exp  = Expression.Build(x.expression());
                    return(new SortColumn(exp, asc));
                });

                statement.OrderBy = sortColumns;
            }

            statement.ForUpdate = forUpdate != null;

            var limit = context.queryLimitClause();

            if (limit != null)
            {
                var n1 = Number.PositiveInteger(limit.n1);
                var n2 = Number.PositiveInteger(limit.n2);

                if (n1 == null)
                {
                    throw new ParseCanceledException("Invalid LIMIT clause");
                }

                if (n2 != null)
                {
                    statement.Limit = new QueryLimit(n1.Value, n2.Value);
                }
                else
                {
                    statement.Limit = new QueryLimit(n1.Value);
                }
            }

            return(statement);
        }
 public override SqlExpression VisitSubquery(PlSqlParser.SubqueryContext context)
 {
     return(Subquery.Form(context));
 }