Esempio n. 1
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();
        }
Esempio n. 2
0
        public static SqlStatement Build(PlSqlParser.DeleteStatementContext context)
        {
            var tableName = Name.Object(context.objectName());
            var whereClause = WhereClause.Form(context.whereClause());

            if (whereClause.CurrentOf != null)
                return new DeleteCurrentStatement(tableName, whereClause.CurrentOf);

            var statement = new DeleteStatement(tableName, whereClause.Expression);

            if (context.deleteLimit() != null) {
                var limit = Number.PositiveInteger(context.deleteLimit().numeric());
                if (limit == null)
                    throw new ParseCanceledException("Invalid delete limit.");

                statement.Limit = limit.Value;
            }

            return statement;
        }
Esempio n. 3
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;
        }