public override SqlStatement VisitLockTableStatement(PlSqlParser.LockTableStatementContext context)
        {
            var         tableNames = context.objectName().Select(Name.Object).ToArray();
            LockingMode mode;

            if (context.SHARED() != null)
            {
                mode = LockingMode.Shared;
            }
            else if (context.EXCLUSIVE() != null)
            {
                mode = LockingMode.Exclusive;
            }
            else
            {
                throw new ParseCanceledException("Invalid locking mode");
            }

            int timeout = Timeout.Infinite;

            if (context.WAIT() != null)
            {
                var wait = Number.PositiveInteger(context.numeric());
                if (wait == null)
                {
                    throw new ParseCanceledException("Invalid timeout wait value");
                }

                timeout = wait.Value;
            }
            else if (context.NOWAIT() != null)
            {
                timeout = 0;
            }

            if (tableNames.Length == 1)
            {
                return(new LockTableStatement(tableNames[0], mode, timeout));
            }

            var seq = new SequenceOfStatements();

            foreach (var tableName in tableNames)
            {
                seq.Statements.Add(new LockTableStatement(tableName, mode, timeout));
            }

            return(seq);
        }
        public override SqlStatement VisitRevokeRoleStatement(PlSqlParser.RevokeRoleStatementContext context)
        {
            var grantee   = Name.Simple(context.granteeName());
            var roleNames = context.roleName().Select(Name.Simple).ToArray();

            if (roleNames.Length == 1)
            {
                return(new RevokeRoleStatement(grantee, roleNames[0]));
            }

            var seq = new SequenceOfStatements();

            foreach (var roleName in roleNames)
            {
                seq.Statements.Add(new RevokeRoleStatement(grantee, roleName));
            }

            return(seq);
        }
Beispiel #3
0
        public static SqlStatement Drop(PlSqlParser.DropViewStatementContext context)
        {
            var names    = context.objectName().Select(Name.Object).ToArray();
            var ifExists = context.IF() != null && context.EXISTS() != null;

            if (names.Length == 1)
            {
                return(new DropViewStatement(names[0], ifExists));
            }

            var sequence = new SequenceOfStatements();

            foreach (var name in names)
            {
                sequence.Statements.Add(new DropViewStatement(name, ifExists));
            }

            return(sequence);
        }
Beispiel #4
0
        public static SqlStatement Build(PlSqlParser.DropTableStatementContext context)
        {
            var  tableNames = context.objectName().Select(Name.Object).ToArray();
            bool ifExists   = context.IF() != null && context.EXISTS() != null;

            if (tableNames.Length == 1)
            {
                return(new DropTableStatement(tableNames[0], ifExists));
            }

            var list = new SequenceOfStatements();

            foreach (var tableName in tableNames)
            {
                list.Statements.Add(new DropTableStatement(tableName, ifExists));
            }

            return(list);
        }
        public static SqlStatement Build(PlSqlParser.AlterTableStatementContext context)
        {
            var tableName = Name.Object(context.objectName());

            var actions = context.alterTableAction().Select(ActionBuilder.Build).ToArray();

            if (actions.Length == 1)
            {
                return(new AlterTableStatement(tableName, actions[0]));
            }

            var list = new SequenceOfStatements();

            foreach (var action in actions)
            {
                list.Statements.Add(new AlterTableStatement(tableName, action));
            }

            return(list);
        }
        public override SqlStatement VisitCompilationUnit(PlSqlParser.CompilationUnitContext context)
        {
            var sequence = new SequenceOfStatements();

            foreach (var statementContext in context.unitStatement())
            {
                var statement = Visit(statementContext);
                if (statement is SequenceOfStatements)
                {
                    var childSequence = (SequenceOfStatements)statement;
                    foreach (var childStatement in childSequence.Statements)
                    {
                        sequence.Statements.Add(childStatement);
                    }
                }
                else
                {
                    sequence.Statements.Add(statement);
                }
            }

            return(sequence);
        }
        public override SqlStatement VisitDropTriggerStatement(PlSqlParser.DropTriggerStatementContext context)
        {
            var names    = context.objectName().Select(Name.Object).ToArray();
            var callback = context.CALLBACK() != null;

            if (callback)
            {
                if (names.Length == 1)
                {
                    return(new DropCallbackTriggersStatement(names[0].Name));
                }

                var seq = new SequenceOfStatements();
                foreach (var name in names)
                {
                    seq.Statements.Add(new DropCallbackTriggersStatement(name.Name));
                }

                return(seq);
            }
            else
            {
                if (names.Length == 1)
                {
                    return(new DropTriggerStatement(names[0]));
                }

                var seq = new SequenceOfStatements();
                foreach (var name in names)
                {
                    seq.Statements.Add(new DropTriggerStatement(name));
                }

                return(seq);
            }
        }
Beispiel #8
0
        public static SqlStatement Alter(PlSqlParser.AlterUserStatementContext context)
        {
            var userName = context.userName().GetText();

            var actions = new List <IAlterUserAction>();

            foreach (var actionContext in context.alterUserAction())
            {
                if (actionContext.alterUserIdAction() != null)
                {
                    actions.Add(SetPassword(actionContext.alterUserIdAction()));
                }
                else if (actionContext.setAccountAction() != null)
                {
                    actions.Add(SetAccount(actionContext.setAccountAction()));
                }
                else if (actionContext.setRoleAction() != null)
                {
                    actions.Add(SetRole(actionContext.setRoleAction()));
                }
            }

            if (actions.Count == 1)
            {
                return(new AlterUserStatement(userName, actions[0]));
            }

            var seq = new SequenceOfStatements();

            foreach (var action in actions)
            {
                seq.Statements.Add(new AlterUserStatement(userName, action));
            }

            return(seq);
        }
Beispiel #9
0
        public static SqlStatement Build(PlSqlParser.CreateTableStatementContext context)
        {
            var tableName           = Name.Object(context.objectName());
            var columnOrConstraints = context.columnOrConstraintList().columnOrConstraint();

            bool ifNotExists = context.IF() != null && context.NOT() != null && context.EXISTS() != null;
            bool temporary   = context.TEMPORARY() != null;

            var tableColumns      = new List <SqlTableColumn>();
            var tableConstraints  = new List <SqlTableConstraint>();
            var columnConstraints = new List <ColumnConstraint>();

            foreach (var columnOrConstraint in columnOrConstraints)
            {
                if (columnOrConstraint.tableColumn() != null)
                {
                    tableColumns.Add(TableColumn.Form(columnOrConstraint.tableColumn(), columnConstraints));
                }
                else if (columnOrConstraint.tableConstraint() != null)
                {
                    tableConstraints.Add(TableConstraint.Form(columnOrConstraint.tableConstraint()));
                }
            }

            if (columnConstraints.Count > 0)
            {
                var constraintGroups = columnConstraints.GroupBy(x => x.Type);

                foreach (var constraintGroup in constraintGroups)
                {
                    var columnNames = constraintGroup.Select(x => x.ColumnName).ToArray();
                    var index       = tableConstraints.FindIndex(x => String.IsNullOrEmpty(x.ConstraintName) &&
                                                                 x.ConstraintType == constraintGroup.Key);
                    if (index != -1)
                    {
                        var unnamedConstraint = tableConstraints[index];
                        var columns           = new List <string>(unnamedConstraint.Columns);
                        foreach (var columnName in columnNames)
                        {
                            if (!columns.Contains(columnName))
                            {
                                columns.Add(columnName);
                            }
                        }

                        // TODO: set the new columns
                    }
                    else
                    {
                        tableConstraints.Add(new SqlTableConstraint(constraintGroup.Key, columnNames));
                    }
                }
            }

            var block = new SequenceOfStatements();

            block.Statements.Add(new CreateTableStatement(tableName, tableColumns)
            {
                IfNotExists = ifNotExists,
                Temporary   = temporary
            });

            foreach (var constraint in tableConstraints)
            {
                block.Statements.Add(new AlterTableStatement(tableName, new AddConstraintAction(constraint)));
            }

            return(block);
        }