Search in sources :

Example 1 with AlterTableDropStep

use of org.jooq.AlterTableDropStep in project jOOQ by jOOQ.

the class DefaultParseContext method parseAlterTable.

private final DDLQuery parseAlterTable() {
    boolean ifTableExists = parseKeywordIf("IF EXISTS");
    Table<?> tableName;
    if (peekKeyword("ONLY")) {
        // [#7751] ONLY is only supported by PostgreSQL. In other RDBMS, it
        // corresponds to a table name.
        Name only = parseIdentifier();
        int p = position();
        if ((tableName = parseTableNameIf()) == null || (!tableName.getQualifiedName().qualified() && tableName.getUnqualifiedName().quoted() == Quoted.UNQUOTED && ALTER_KEYWORDS.contains(tableName.getName().toUpperCase()))) {
            tableName = table(only);
            position(p);
        }
    } else {
        tableName = parseTableName();
    }
    AlterTableStep s1 = ifTableExists ? dsl.alterTableIfExists(tableName) : dsl.alterTable(tableName);
    switch(characterUpper()) {
        case 'A':
            if (parseKeywordIf("ADD"))
                return parseAlterTableAdd(s1, tableName);
            else if (parseKeywordIf("ALTER"))
                if (parseKeywordIf("CONSTRAINT"))
                    return parseAlterTableAlterConstraint(s1);
                else if ((parseKeywordIf("COLUMN") || true))
                    return parseAlterTableAlterColumn(s1);
            break;
        case 'C':
            // TODO: support all of the storageLoop from the CREATE TABLE statement
            if (parseKeywordIf("COMMENT")) {
                if (!parseIf('='))
                    parseKeywordIf("IS");
                return dsl.commentOnTable(tableName).is(parseStringLiteral());
            }
            break;
        case 'D':
            if (parseKeywordIf("DROP")) {
                if (parseKeywordIf("CONSTRAINT")) {
                    return parseCascadeRestrictIf(parseIfExists(this::parseIdentifier, s1::dropConstraintIfExists, s1::dropConstraint), AlterTableDropStep::cascade, AlterTableDropStep::restrict);
                } else if (parseKeywordIf("UNIQUE")) {
                    return parseCascadeRestrictIf(s1.dropUnique(peek('(') ? unique(parseKeyColumnList()) : constraint(parseIdentifier())), AlterTableDropStep::cascade, AlterTableDropStep::restrict);
                } else if (parseKeywordIf("PRIMARY KEY")) {
                    Name identifier = parseIdentifierIf();
                    return parseCascadeRestrictIf(identifier == null ? s1.dropPrimaryKey() : s1.dropPrimaryKey(identifier), AlterTableDropStep::cascade, AlterTableDropStep::restrict);
                } else if (parseKeywordIf("FOREIGN KEY")) {
                    return s1.dropForeignKey(parseIdentifier());
                } else if (parseKeywordIf("INDEX") || parseKeywordIf("KEY")) {
                    return dsl.dropIndex(parseIdentifier()).on(tableName);
                } else {
                    parseKeywordIf("COLUMN");
                    boolean ifColumnExists = parseKeywordIf("IF EXISTS");
                    boolean parens = parseIf('(');
                    Field<?> field = parseFieldName();
                    List<Field<?>> fields = null;
                    if (!ifColumnExists) {
                        while (parseIf(',') || parseKeywordIf("DROP") && (parseKeywordIf("COLUMN") || true)) {
                            if (fields == null) {
                                fields = new ArrayList<>();
                                fields.add(field);
                            }
                            fields.add(parseFieldName());
                        }
                    }
                    if (parens)
                        parse(')');
                    return parseCascadeRestrictIf(fields == null ? ifColumnExists ? s1.dropColumnIfExists(field) : s1.dropColumn(field) : s1.dropColumns(fields), AlterTableDropStep::cascade, AlterTableDropStep::restrict);
                }
            }
            break;
        case 'M':
            if (parseKeywordIf("MODIFY"))
                if (parseKeywordIf("CONSTRAINT"))
                    return parseAlterTableAlterConstraint(s1);
                else if ((parseKeywordIf("COLUMN") || true))
                    return parseAlterTableAlterColumn(s1);
            break;
        case 'O':
            if (parseKeywordIf("OWNER TO") && parseUser() != null)
                return IGNORE;
            break;
        case 'R':
            if (parseKeywordIf("RENAME")) {
                if (parseKeywordIf("AS") || parseKeywordIf("TO")) {
                    Table<?> newName = parseTableName();
                    return s1.renameTo(newName);
                } else if (parseKeywordIf("COLUMN")) {
                    Name oldName = parseIdentifier();
                    parseKeyword("AS", "TO");
                    Name newName = parseIdentifier();
                    return s1.renameColumn(oldName).to(newName);
                } else if (parseKeywordIf("INDEX")) {
                    Name oldName = parseIdentifier();
                    parseKeyword("AS", "TO");
                    Name newName = parseIdentifier();
                    return s1.renameIndex(oldName).to(newName);
                } else if (parseKeywordIf("CONSTRAINT")) {
                    Name oldName = parseIdentifier();
                    parseKeyword("AS", "TO");
                    Name newName = parseIdentifier();
                    return s1.renameConstraint(oldName).to(newName);
                }
            }
            break;
        case 'S':
            if (parseKeywordIf("SET"))
                return s1.comment(parseOptionsDescription());
            break;
    }
    throw expected("ADD", "ALTER", "COMMENT", "DROP", "MODIFY", "OWNER TO", "RENAME", "SET");
}
Also used : GroupField(org.jooq.GroupField) TableField(org.jooq.TableField) Field(org.jooq.Field) SortField(org.jooq.SortField) SelectField(org.jooq.SelectField) AlterTableStep(org.jooq.AlterTableStep) AlterTableDropStep(org.jooq.AlterTableDropStep) Constraint(org.jooq.Constraint) DSL.constraint(org.jooq.impl.DSL.constraint) Name(org.jooq.Name) DSL.systemName(org.jooq.impl.DSL.systemName)

Example 2 with AlterTableDropStep

use of org.jooq.AlterTableDropStep in project jOOQ by jOOQ.

the class ParserImpl method parseAlterTable.

private static final DDLQuery parseAlterTable(ParserContext ctx) {
    boolean ifExists = parseKeywordIf(ctx, "IF EXISTS");
    Table<?> tableName = parseTableName(ctx);
    parseWhitespaceIf(ctx);
    AlterTableStep s1 = ifExists ? ctx.dsl.alterTableIfExists(tableName) : ctx.dsl.alterTable(tableName);
    switch(ctx.character()) {
        case 'a':
        case 'A':
            if (parseKeywordIf(ctx, "ADD")) {
                ConstraintTypeStep constraint = null;
                if (parseKeywordIf(ctx, "CONSTRAINT"))
                    constraint = constraint(parseIdentifier(ctx));
                if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                    parse(ctx, '(');
                    Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    return constraint == null ? s1.add(primaryKey(fieldNames)) : s1.add(constraint.primaryKey(fieldNames));
                } else if (parseKeywordIf(ctx, "UNIQUE")) {
                    parse(ctx, '(');
                    Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    return constraint == null ? s1.add(unique(fieldNames)) : s1.add(constraint.unique(fieldNames));
                } else if (parseKeywordIf(ctx, "FOREIGN KEY")) {
                    parse(ctx, '(');
                    Field<?>[] referencing = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    parseKeyword(ctx, "REFERENCES");
                    Table<?> referencedTable = parseTableName(ctx);
                    parse(ctx, '(');
                    Field<?>[] referencedFields = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    if (referencing.length != referencedFields.length)
                        throw ctx.exception();
                    return constraint == null ? s1.add(foreignKey(referencing).references(referencedTable, referencedFields)) : s1.add(constraint.foreignKey(referencing).references(referencedTable, referencedFields));
                } else if (parseKeywordIf(ctx, "CHECK")) {
                    parse(ctx, '(');
                    Condition condition = parseCondition(ctx);
                    parse(ctx, ')');
                    return constraint == null ? s1.add(check(condition)) : s1.add(constraint.check(condition));
                } else if (constraint != null) {
                    throw ctx.unexpectedToken();
                } else {
                    parseKeywordIf(ctx, "COLUMN");
                    // The below code is taken from CREATE TABLE, with minor modifications as
                    // https://github.com/jOOQ/jOOQ/issues/5317 has not yet been implemented
                    // Once implemented, we might be able to factor out the common logic into
                    // a new parseXXX() method.
                    String fieldName = parseIdentifier(ctx);
                    DataType type = parseDataType(ctx);
                    boolean nullable = false;
                    boolean defaultValue = false;
                    boolean unique = false;
                    for (; ; ) {
                        if (!nullable) {
                            if (parseKeywordIf(ctx, "NULL")) {
                                type = type.nullable(true);
                                nullable = true;
                                continue;
                            } else if (parseKeywordIf(ctx, "NOT NULL")) {
                                type = type.nullable(false);
                                nullable = true;
                                continue;
                            }
                        }
                        if (!defaultValue) {
                            if (parseKeywordIf(ctx, "DEFAULT")) {
                                type = type.defaultValue(parseField(ctx));
                                defaultValue = true;
                                continue;
                            }
                        }
                        if (!unique) {
                            if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                                throw ctx.unexpectedToken();
                            } else if (parseKeywordIf(ctx, "UNIQUE")) {
                                throw ctx.unexpectedToken();
                            }
                        }
                        if (parseKeywordIf(ctx, "CHECK")) {
                            throw ctx.unexpectedToken();
                        }
                        break;
                    }
                    return s1.add(field(name(fieldName), type), type);
                }
            }
            break;
        case 'd':
        case 'D':
            if (parseKeywordIf(ctx, "DROP")) {
                if (parseKeywordIf(ctx, "COLUMN")) {
                    Field<?> field = parseFieldName(ctx);
                    boolean cascade = parseKeywordIf(ctx, "CASCADE");
                    boolean restrict = !cascade && parseKeywordIf(ctx, "RESTRICT");
                    AlterTableDropStep s2 = s1.dropColumn(field);
                    AlterTableFinalStep s3 = cascade ? s2.cascade() : restrict ? s2.restrict() : s2;
                    return s3;
                } else if (parseKeywordIf(ctx, "CONSTRAINT")) {
                    String constraint = parseIdentifier(ctx);
                    return s1.dropConstraint(constraint);
                }
            }
            break;
        case 'r':
        case 'R':
            if (parseKeywordIf(ctx, "RENAME")) {
                if (parseKeywordIf(ctx, "TO")) {
                    String newName = parseIdentifier(ctx);
                    return s1.renameTo(newName);
                } else if (parseKeywordIf(ctx, "COLUMN")) {
                    String oldName = parseIdentifier(ctx);
                    parseKeyword(ctx, "TO");
                    String newName = parseIdentifier(ctx);
                    return s1.renameColumn(oldName).to(newName);
                } else if (parseKeywordIf(ctx, "CONSTRAINT")) {
                    String oldName = parseIdentifier(ctx);
                    parseKeyword(ctx, "TO");
                    String newName = parseIdentifier(ctx);
                    return s1.renameConstraint(oldName).to(newName);
                }
            }
            break;
    }
    throw ctx.unexpectedToken();
}
Also used : ConstraintTypeStep(org.jooq.ConstraintTypeStep) Condition(org.jooq.Condition) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) AlterTableFinalStep(org.jooq.AlterTableFinalStep) AlterTableStep(org.jooq.AlterTableStep) DataType(org.jooq.DataType) AlterTableDropStep(org.jooq.AlterTableDropStep)

Aggregations

AlterTableDropStep (org.jooq.AlterTableDropStep)2 AlterTableStep (org.jooq.AlterTableStep)2 Field (org.jooq.Field)2 GroupField (org.jooq.GroupField)2 SortField (org.jooq.SortField)2 TableField (org.jooq.TableField)2 AlterTableFinalStep (org.jooq.AlterTableFinalStep)1 Condition (org.jooq.Condition)1 Constraint (org.jooq.Constraint)1 ConstraintTypeStep (org.jooq.ConstraintTypeStep)1 DataType (org.jooq.DataType)1 Name (org.jooq.Name)1 SelectField (org.jooq.SelectField)1 DSL.constraint (org.jooq.impl.DSL.constraint)1 DSL.systemName (org.jooq.impl.DSL.systemName)1