use of org.jooq.CreateTableFinalStep 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;
}
}
Aggregations