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; }
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; }
public static SqlStatement Drop(PlSqlParser.DropSequenceStatementContext context) { var names = context.objectName().Select(Name.Object).ToArray(); var ifExists = context.IF() != null && context.EXISTS() != null; if (names.Length == 1) return new DropSequenceStatement(names[0]); var sequence = new SequenceOfStatements(); foreach (var name in names) { sequence.Statements.Add(new DropSequenceStatement(name)); } return sequence; }
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; }