// This is the case where the PK/FK/UK is NOT NAMED
if (tokens.matches("UNIQUE")) {
String uc_name = "UC_1"; // UNIQUE CONSTRAINT NAME
tokens.consume(); // UNIQUE
AstNode constraintNode = nodeFactory().node(uc_name, tableNode, mixinType);
constraintNode.setProperty(CONSTRAINT_TYPE, UNIQUE);
// CONSUME COLUMNS
parseColumnNameList(tokens, constraintNode, TYPE_COLUMN_REFERENCE);
parseConstraintAttributes(tokens, constraintNode);
consumeComment(tokens);
} else if (tokens.matches("PRIMARY", "KEY")) {
String pk_name = "PK_1"; // PRIMARY KEY NAME
tokens.consume("PRIMARY", "KEY"); // PRIMARY KEY
AstNode constraintNode = nodeFactory().node(pk_name, tableNode, mixinType);
constraintNode.setProperty(CONSTRAINT_TYPE, PRIMARY_KEY);
// CONSUME COLUMNS
parseColumnNameList(tokens, constraintNode, TYPE_COLUMN_REFERENCE);
parseConstraintAttributes(tokens, constraintNode);
consumeComment(tokens);
} else if (tokens.matches("FOREIGN", "KEY")) {
String fk_name = "FK_1"; // FOREIGN KEY NAME
tokens.consume("FOREIGN", "KEY"); // FOREIGN KEY
if (!tokens.matches(L_PAREN)) {
// Assume the FK is Named here
fk_name = tokens.consume();
}
AstNode constraintNode = nodeFactory().node(fk_name, tableNode, mixinType);
constraintNode.setProperty(CONSTRAINT_TYPE, FOREIGN_KEY);
// CONSUME COLUMNS
parseColumnNameList(tokens, constraintNode, TYPE_COLUMN_REFERENCE);
// Parse the references to table and columns
parseReferences(tokens, constraintNode);
parseConstraintAttributes(tokens, constraintNode);
consumeComment(tokens);
}
} else if (tokens.matches("CONSTRAINT", TokenStream.ANY_VALUE, "UNIQUE")) {
// CONSTRAINT P_KEY_2a UNIQUE (PERMISSIONUID)
tokens.consume(); // CONSTRAINT
String uc_name = parseName(tokens); // UNIQUE CONSTRAINT NAME
tokens.consume("UNIQUE"); // UNIQUE
AstNode constraintNode = nodeFactory().node(uc_name, tableNode, mixinType);
constraintNode.setProperty(CONSTRAINT_TYPE, UNIQUE);
// CONSUME COLUMNS
parseColumnNameList(tokens, constraintNode, TYPE_COLUMN_REFERENCE);
parseConstraintAttributes(tokens, constraintNode);
consumeComment(tokens);
} else if (tokens.matches("CONSTRAINT", TokenStream.ANY_VALUE, "PRIMARY", "KEY")) {
// CONSTRAINT U_KEY_2a PRIMARY KEY (PERMISSIONUID)
tokens.consume("CONSTRAINT"); // CONSTRAINT
String pk_name = parseName(tokens); // PRIMARY KEY NAME
tokens.consume("PRIMARY", "KEY"); // PRIMARY KEY
AstNode constraintNode = nodeFactory().node(pk_name, tableNode, mixinType);
constraintNode.setProperty(CONSTRAINT_TYPE, PRIMARY_KEY);
// CONSUME COLUMNS
parseColumnNameList(tokens, constraintNode, TYPE_COLUMN_REFERENCE);
parseConstraintAttributes(tokens, constraintNode);
consumeComment(tokens);
} else if (tokens.matches("CONSTRAINT", TokenStream.ANY_VALUE, "FOREIGN", "KEY")) {
// CONSTRAINT F_KEY_2a FOREIGN KEY (PERMISSIONUID)
tokens.consume("CONSTRAINT"); // CONSTRAINT
String fk_name = parseName(tokens); // FOREIGN KEY NAME
tokens.consume("FOREIGN", "KEY"); // FOREIGN KEY
AstNode constraintNode = nodeFactory().node(fk_name, tableNode, mixinType);
constraintNode.setProperty(CONSTRAINT_TYPE, FOREIGN_KEY);
// CONSUME COLUMNS
parseColumnNameList(tokens, constraintNode, TYPE_COLUMN_REFERENCE);
// Parse the references to table and columns
parseReferences(tokens, constraintNode);
parseConstraintAttributes(tokens, constraintNode);
consumeComment(tokens);
} else if (tokens.matches("CONSTRAINT", TokenStream.ANY_VALUE, "CHECK")) {
// CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
tokens.consume("CONSTRAINT"); // CONSTRAINT
String ck_name = parseName(tokens); // NAME
tokens.consume("CHECK"); // CHECK
AstNode constraintNode = nodeFactory().node(ck_name, tableNode, mixinType);
constraintNode.setProperty(CONSTRAINT_TYPE, CHECK);
String clause = consumeParenBoundedTokens(tokens, true);
constraintNode.setProperty(CHECK_SEARCH_CONDITION, clause);
}
}