use of org.jooq.ConstraintTypeStep 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;
}
}
use of org.jooq.ConstraintTypeStep in project jOOQ by jOOQ.
the class DefaultParseContext method parseCreateDomain.
private final DDLQuery parseCreateDomain() {
boolean ifNotExists = parseKeywordIf("IF NOT EXISTS");
Domain<?> domainName = parseDomainName();
parseKeyword("AS");
DataType<?> dataType = parseDataType();
CreateDomainDefaultStep<?> s1 = ifNotExists ? dsl.createDomainIfNotExists(domainName).as(dataType) : dsl.createDomain(domainName).as(dataType);
CreateDomainConstraintStep s2 = parseKeywordIf("DEFAULT") ? s1.default_((Field) parseField()) : s1;
List<Constraint> constraints = new ArrayList<>();
constraintLoop: for (; ; ) {
ConstraintTypeStep constraint = parseConstraintNameSpecification();
// TODO: NOT NULL constraints
if (parseKeywordIf("CHECK")) {
constraints.add(parseCheckSpecification(constraint));
continue constraintLoop;
} else if (constraint != null)
throw expected("CHECK", "CONSTRAINT");
break;
}
if (!constraints.isEmpty())
s2 = s2.constraints(constraints);
return s2;
}
use of org.jooq.ConstraintTypeStep in project jOOQ by jOOQ.
the class DefaultParseContext method parseForeignKeySpecification.
private final Constraint parseForeignKeySpecification(ConstraintTypeStep constraint) {
Name constraintName;
if ((constraintName = parseIdentifierIf()) != null)
if (constraint == null)
constraint = constraint(constraintName);
parse('(');
Field<?>[] referencing = parseList(',', c -> parseFieldName()).toArray(EMPTY_FIELD);
parse(')');
parseKeyword("REFERENCES");
return parseForeignKeyReferenceSpecification(constraint, referencing);
}
use of org.jooq.ConstraintTypeStep 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();
}
use of org.jooq.ConstraintTypeStep in project jOOQ by jOOQ.
the class DefaultParseContext method parseInlineConstraints.
private final ParseInlineConstraints parseInlineConstraints(Name fieldName, DataType<?> type, List<? super Constraint> constraints, boolean primary, boolean identity, boolean readonly) {
boolean nullable = false;
boolean defaultValue = false;
boolean computed = false;
boolean onUpdate = false;
boolean unique = false;
boolean comment = false;
boolean compress = false;
Comment fieldComment = null;
identity |= type.identity();
readonly |= type.readonly();
for (; ; ) {
if (!nullable) {
if (parseKeywordIf("NULL")) {
type = type.nullable(true);
nullable = true;
continue;
} else if (parseNotNullOptionalEnable()) {
type = type.nullable(false);
nullable = true;
continue;
}
}
if (!defaultValue) {
if (!identity && parseKeywordIf("IDENTITY")) {
if (parseIf('(')) {
parseSignedIntegerLiteral();
parse(',');
parseSignedIntegerLiteral();
parse(')');
}
type = type.identity(true);
defaultValue = true;
identity = true;
continue;
} else if (!ignoreProEdition() && parseKeywordIf("READONLY") && requireProEdition()) {
} else if (parseKeywordIf("DEFAULT")) {
// [#10963] Special case nextval('<id>_seq'::regclass)
if (parseSerialIf()) {
type = type.identity(true);
} else {
// TODO: [#10116] Support this clause also in the jOOQ API
parseKeywordIf("ON NULL");
type = type.defaultValue((Field) toField(parseConcat()));
// TODO: [#10115] Support this clause also in the jOOQ API
parseKeywordIf("WITH VALUES");
defaultValue = true;
}
continue;
} else if (!computed && !ignoreProEdition() && (parseKeywordIf("AS") || parseKeywordIf("COMPUTED") && (parseKeywordIf("BY") || true)) && requireProEdition()) {
} else if (!identity && !computed && parseKeywordIf("GENERATED")) {
boolean always;
if (!(always = parseKeywordIf("ALWAYS"))) {
parseKeyword("BY DEFAULT");
// TODO: Ignored keyword from Oracle
parseKeywordIf("ON NULL");
}
if (always ? parseKeywordIf("AS IDENTITY") : parseKeyword("AS IDENTITY")) {
parseIdentityOptionIf();
type = type.identity(true);
identity = true;
} else if (!ignoreProEdition() && parseKeyword("AS") && requireProEdition()) {
}
defaultValue = true;
continue;
}
}
if (!onUpdate) {
if (parseKeywordIf("ON UPDATE")) {
// [#6132] TODO: Support this feature in the jOOQ DDL API
parseConcat();
onUpdate = true;
continue;
}
}
ConstraintTypeStep inlineConstraint = parseConstraintNameSpecification();
if (!unique) {
if (!primary && parsePrimaryKeyClusteredNonClusteredKeywordIf()) {
if (!parseKeywordIf("CLUSTERED"))
parseKeywordIf("NONCLUSTERED");
constraints.add(parseConstraintEnforcementIf(inlineConstraint == null ? primaryKey(fieldName) : inlineConstraint.primaryKey(fieldName)));
primary = true;
unique = true;
continue;
} else if (parseKeywordIf("UNIQUE")) {
if (!parseKeywordIf("KEY"))
parseKeywordIf("INDEX");
constraints.add(parseConstraintEnforcementIf(inlineConstraint == null ? unique(fieldName) : inlineConstraint.unique(fieldName)));
unique = true;
continue;
}
}
if (parseKeywordIf("CHECK")) {
constraints.add(parseCheckSpecification(inlineConstraint));
continue;
}
if (parseKeywordIf("FOREIGN KEY REFERENCES", "REFERENCES")) {
constraints.add(parseForeignKeyReferenceSpecification(inlineConstraint, new Field[] { field(fieldName) }));
continue;
}
if (!nullable) {
if (parseKeywordIf("NULL")) {
type = type.nullable(true);
nullable = true;
continue;
} else if (parseNotNullOptionalEnable()) {
type = type.nullable(false);
nullable = true;
continue;
}
}
if (inlineConstraint != null)
throw expected("CHECK", "NOT NULL", "NULL", "PRIMARY KEY", "REFERENCES", "UNIQUE");
if (!identity) {
if (parseKeywordIf("AUTO_INCREMENT") || parseKeywordIf("AUTOINCREMENT")) {
type = type.identity(true);
identity = true;
continue;
}
}
if (!comment) {
// [#10164] In a statement batch, this could already be the next statement
if (!peekKeyword("COMMENT ON") && parseKeywordIf("COMMENT")) {
if (!parseIf('='))
parseKeywordIf("IS");
fieldComment = parseComment();
comment = true;
continue;
} else if (peekKeyword("OPTIONS")) {
fieldComment = parseOptionsDescription();
comment = true;
continue;
}
}
if (!compress) {
if (!ignoreProEdition() && parseKeywordIf("NO COMPRESS") && requireProEdition()) {
} else if (!ignoreProEdition() && parseKeywordIf("COMPRESS") && requireProEdition()) {
}
}
break;
}
return new ParseInlineConstraints(type, fieldComment, primary, identity, readonly);
}
Aggregations