Search in sources :

Example 1 with Constraint

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

the class CreateTableImpl method accept0.

private final void accept0(Context<?> ctx) {
    if (select != null) {
        {
            acceptCreateTableAsSelect(ctx);
        }
    } else {
        ctx.start(CREATE_TABLE);
        toSQLCreateTableName(ctx);
        ctx.sql('(').start(CREATE_TABLE_COLUMNS).formatIndentStart().formatNewLine();
        boolean qualify = ctx.qualify();
        ctx.qualify(false);
        for (int i = 0; i < columnFields.size(); i++) {
            DataType<?> type = columnTypes.get(i);
            ctx.visit(columnFields.get(i)).sql(' ');
            Tools.toSQLDDLTypeDeclaration(ctx, type);
            //         NULL constraints clause
            if (asList(HSQLDB).contains(ctx.family()))
                acceptDefault(ctx, type);
            if (type.nullable()) {
                // [#4321] Not all dialects support explicit NULL type declarations
                if (!asList(DERBY, FIREBIRD).contains(ctx.family()))
                    ctx.sql(' ').keyword("null");
            } else {
                ctx.sql(' ').keyword("not null");
            }
            if (type.identity()) {
                // [#5062] H2's (and others') AUTO_INCREMENT flag is syntactically located *after* NULL flags.
                switch(ctx.family()) {
                    case H2:
                    case MARIADB:
                    case MYSQL:
                        ctx.sql(' ').keyword("auto_increment");
                        break;
                }
            }
            if (!asList(HSQLDB).contains(ctx.family()))
                acceptDefault(ctx, type);
            if (i < columnFields.size() - 1)
                ctx.sql(',').formatSeparator();
        }
        ctx.qualify(qualify);
        ctx.end(CREATE_TABLE_COLUMNS).start(CREATE_TABLE_CONSTRAINTS);
        if (!constraints.isEmpty())
            for (Constraint constraint : constraints) ctx.sql(',').formatSeparator().visit(constraint);
        ctx.end(CREATE_TABLE_CONSTRAINTS).formatIndentEnd().formatNewLine().sql(')');
        toSQLOnCommit(ctx);
        ctx.end(CREATE_TABLE);
    }
}
Also used : Constraint(org.jooq.Constraint) Constraint(org.jooq.Constraint)

Example 2 with Constraint

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

the class ParserImpl method parseCreateTable.

private static final DDLQuery parseCreateTable(ParserContext ctx) {
    boolean ifNotExists = parseKeywordIf(ctx, "IF NOT EXISTS");
    Table<?> tableName = parseTableName(ctx);
    if (parseKeywordIf(ctx, "AS")) {
        Select<?> select = parseSelect(ctx);
        CreateTableAsStep<Record> s1 = ifNotExists ? ctx.dsl.createTableIfNotExists(tableName) : ctx.dsl.createTable(tableName);
        CreateTableFinalStep s2 = s1.as(select);
        return s2;
    } else {
        List<Field<?>> fields = new ArrayList<Field<?>>();
        List<Constraint> constraints = new ArrayList<Constraint>();
        boolean primary = false;
        boolean noConstraint = true;
        parse(ctx, '(');
        do {
            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((Field) parseField(ctx));
                        defaultValue = true;
                        continue;
                    }
                }
                if (!unique) {
                    if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                        constraints.add(primaryKey(fieldName));
                        primary = true;
                        unique = true;
                        continue;
                    } else if (parseKeywordIf(ctx, "UNIQUE")) {
                        constraints.add(unique(fieldName));
                        unique = true;
                        continue;
                    }
                }
                if (parseKeywordIf(ctx, "CHECK")) {
                    parse(ctx, '(');
                    constraints.add(check(parseCondition(ctx)));
                    parse(ctx, ')');
                    continue;
                }
                break;
            }
            fields.add(field(name(fieldName), type));
        } while (parseIf(ctx, ',') && (noConstraint = !peekKeyword(ctx, "PRIMARY KEY") && !peekKeyword(ctx, "UNIQUE") && !peekKeyword(ctx, "FOREIGN KEY") && !peekKeyword(ctx, "CHECK") && !peekKeyword(ctx, "CONSTRAINT")));
        if (!noConstraint) {
            do {
                ConstraintTypeStep constraint = null;
                if (parseKeywordIf(ctx, "CONSTRAINT"))
                    constraint = constraint(parseIdentifier(ctx));
                if (parseKeywordIf(ctx, "PRIMARY KEY")) {
                    if (primary) {
                        throw ctx.exception();
                    } else {
                        primary = true;
                        parse(ctx, '(');
                        Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                        parse(ctx, ')');
                        constraints.add(constraint == null ? primaryKey(fieldNames) : constraint.primaryKey(fieldNames));
                    }
                } else if (parseKeywordIf(ctx, "UNIQUE")) {
                    parse(ctx, '(');
                    Field<?>[] fieldNames = parseFieldNames(ctx).toArray(EMPTY_FIELD);
                    parse(ctx, ')');
                    constraints.add(constraint == null ? unique(fieldNames) : 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();
                    constraints.add(constraint == null ? foreignKey(referencing).references(referencedTable, referencedFields) : constraint.foreignKey(referencing).references(referencedTable, referencedFields));
                } else if (parseKeywordIf(ctx, "CHECK")) {
                    parse(ctx, '(');
                    Condition condition = parseCondition(ctx);
                    parse(ctx, ')');
                    constraints.add(constraint == null ? check(condition) : constraint.check(condition));
                } else {
                    throw ctx.unexpectedToken();
                }
            } while (parseIf(ctx, ','));
        }
        parse(ctx, ')');
        CreateTableAsStep<Record> s1 = ifNotExists ? ctx.dsl.createTableIfNotExists(tableName) : ctx.dsl.createTable(tableName);
        CreateTableColumnStep s2 = s1.columns(fields);
        CreateTableConstraintStep s3 = constraints.isEmpty() ? s2 : s2.constraints(constraints);
        return s3;
    }
}
Also used : CreateTableFinalStep(org.jooq.CreateTableFinalStep) ConstraintTypeStep(org.jooq.ConstraintTypeStep) Condition(org.jooq.Condition) CreateTableConstraintStep(org.jooq.CreateTableConstraintStep) Constraint(org.jooq.Constraint) ArrayList(java.util.ArrayList) TableField(org.jooq.TableField) GroupField(org.jooq.GroupField) Field(org.jooq.Field) SortField(org.jooq.SortField) CreateTableColumnStep(org.jooq.CreateTableColumnStep) Record(org.jooq.Record)

Aggregations

Constraint (org.jooq.Constraint)2 ArrayList (java.util.ArrayList)1 Condition (org.jooq.Condition)1 ConstraintTypeStep (org.jooq.ConstraintTypeStep)1 CreateTableColumnStep (org.jooq.CreateTableColumnStep)1 CreateTableConstraintStep (org.jooq.CreateTableConstraintStep)1 CreateTableFinalStep (org.jooq.CreateTableFinalStep)1 Field (org.jooq.Field)1 GroupField (org.jooq.GroupField)1 Record (org.jooq.Record)1 SortField (org.jooq.SortField)1 TableField (org.jooq.TableField)1