Skip to content

Commit

Permalink
Added reference options foreign keys support (ON UPDATE/DELETE NO ACT…
Browse files Browse the repository at this point in the history
…ION/CASCADE) and Full text indexes (FULLTEXT idx(text1))
  • Loading branch information
pabloa committed Feb 1, 2016
1 parent bcfa257 commit c29565b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
public class ForeignKeyIndex extends NamedConstraint {
private Table table;
private List<String> referencedColumnNames;
private String onDeleteReferenceOption;
private String onUpdateReferenceOption;

public Table getTable() {
return table;
Expand All @@ -49,9 +51,33 @@ public void setReferencedColumnNames(List<String> referencedColumnNames) {
this.referencedColumnNames = referencedColumnNames;
}

public String getOnDeleteReferenceOption() {
return onDeleteReferenceOption;
}

public void setOnDeleteReferenceOption(String onDeleteReferenceOption) {
this.onDeleteReferenceOption = onDeleteReferenceOption;
}

public String getOnUpdateReferenceOption() {
return onUpdateReferenceOption;
}

public void setOnUpdateReferenceOption(String onUpdateReferenceOption) {
this.onUpdateReferenceOption = onUpdateReferenceOption;
}

@Override
public String toString() {
String referenceOptions = "";
if(onDeleteReferenceOption != null) {
referenceOptions += " ON DELETE " + onDeleteReferenceOption;
}
if(onUpdateReferenceOption != null) {
referenceOptions += " ON UPDATE " + onUpdateReferenceOption;
}
return super.toString()
+ " REFERENCES " + table + PlainSelect.getStringList(getReferencedColumnNames(), true, true);
+ " REFERENCES " + table + PlainSelect.getStringList(getReferencedColumnNames(), true, true)
+ referenceOptions;
}
}
6 changes: 5 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_VALUES:"VALUES">
| <K_ESCAPE:"ESCAPE">
| <K_PRIMARY:"PRIMARY">
| <K_FULLTEXT:"FULLTEXT">
| <K_NATURAL:"NATURAL">
| <K_REPLACE:"REPLACE">
| <K_BETWEEN:"BETWEEN">
Expand Down Expand Up @@ -227,6 +228,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_MATCHED: "MATCHED">
| <K_CASCADE: "CASCADE">
| <K_RESTRICT: "RESTRICT">
| <K_NO_ACTION: "NO ACTION">
| <K_DUPLICATE: "DUPLICATE">
| <K_LOW_PRIORITY : "LOW_PRIORITY">
| <K_DELAYED : "DELAYED">
Expand Down Expand Up @@ -2589,7 +2591,7 @@ CreateTable CreateTable():
)
|
LOOKAHEAD(3) ( {tk=null;}
[ tk=<K_UNIQUE> ] tk2=<K_KEY>
[ tk=<K_UNIQUE> ] [ tk=<K_FULLTEXT> ] tk2=<K_KEY>
sk3=RelObjectName()
colNames=ColumnsNamesList()
{
Expand Down Expand Up @@ -2618,6 +2620,8 @@ CreateTable CreateTable():
fkIndex.setReferencedColumnNames(colNames);
indexes.add(fkIndex);
}
[LOOKAHEAD(2) (<K_ON> <K_DELETE> (<K_CASCADE> {fkIndex.setOnDeleteReferenceOption("CASCADE");}|<K_NO_ACTION> {fkIndex.setOnDeleteReferenceOption("NO ACTION");}))]
[LOOKAHEAD(2) (<K_ON> <K_UPDATE> (<K_CASCADE> {fkIndex.setOnUpdateReferenceOption("CASCADE");}|<K_NO_ACTION> {fkIndex.setOnUpdateReferenceOption("NO ACTION");}))]
)
|
(
Expand Down
30 changes: 21 additions & 9 deletions src/test/java/net/sf/jsqlparser/test/create/CreateTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,28 @@ public void testCreateTableColumnValue() throws JSQLParserException {
}

public void testCreateTableForeignKey5() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE IF NOT EXISTS table1 (id INTEGER PRIMARY KEY AUTO_INCREMENT, aid INTEGER REFERENCES accounts ON aid ON DELETE CASCADE, name STRING, lastname STRING)");
}

public void testCreateTableForeignKey6() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE test (id long, fkey long references another_table (id))");
}
assertSqlCanBeParsedAndDeparsed("CREATE TABLE IF NOT EXISTS table1 (id INTEGER PRIMARY KEY AUTO_INCREMENT, aid INTEGER REFERENCES accounts ON aid ON DELETE CASCADE, name STRING, lastname STRING)");
}

public void testCreateTableMySqlOnUpdateCurrentTimestamp() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE test (applied timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)");
}
public void testCreateTableForeignKey6() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE test (id long, fkey long references another_table (id))");
}

public void testMySqlCreateTableOnUpdateCurrentTimestamp() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE test (applied timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)");
}

public void testMySqlCreateTableWithConstraintWithCascade() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table1 (id INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, t2_id INT (10) UNSIGNED DEFAULT NULL, t3_id INT (10) UNSIGNED DEFAULT NULL, t4_id INT (10) UNSIGNED NOT NULL, PRIMARY KEY (id), KEY fkc_table1_t4 (t4_id), KEY fkc_table1_t2 (t2_id), KEY fkc_table1_t3 (t3_id), CONSTRAINT fkc_table1_t2 FOREIGN KEY (t2_id) REFERENCES table_two(t2o_id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT fkc_table1_t3 FOREIGN KEY (t3_id) REFERENCES table_three(t3o_id) ON UPDATE CASCADE, CONSTRAINT fkc_table1_t4 FOREIGN KEY (t4_id) REFERENCES table_four(t4o_id) ON DELETE CASCADE) ENGINE = InnoDB AUTO_INCREMENT = 8761 DEFAULT CHARSET = utf8");
}

public void testMySqlCreateTableWithConstraintWithNoAction() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table1 (id INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, t2_id INT (10) UNSIGNED DEFAULT NULL, t3_id INT (10) UNSIGNED DEFAULT NULL, t4_id INT (10) UNSIGNED NOT NULL, PRIMARY KEY (id), KEY fkc_table1_t4 (t4_id), KEY fkc_table1_t2 (t2_id), KEY fkc_table1_t3 (t3_id), CONSTRAINT fkc_table1_t2 FOREIGN KEY (t2_id) REFERENCES table_two(t2o_id) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fkc_table1_t3 FOREIGN KEY (t3_id) REFERENCES table_three(t3o_id) ON UPDATE NO ACTION, CONSTRAINT fkc_table1_t4 FOREIGN KEY (t4_id) REFERENCES table_four(t4o_id) ON DELETE NO ACTION) ENGINE = InnoDB AUTO_INCREMENT = 8761 DEFAULT CHARSET = utf8");
}

public void testMySqlCreateTableWithTextIndexes() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table2 (id INT (10) UNSIGNED NOT NULL AUTO_INCREMENT, name TEXT, url TEXT, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), FULLTEXT KEY idx_table2_name (name)) ENGINE = InnoDB AUTO_INCREMENT = 7334 DEFAULT CHARSET = utf8");
}

public void testRUBiSCreateList() throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(CreateTableTest.class.getResourceAsStream("/RUBiS-create-requests.txt")));
Expand Down

0 comments on commit c29565b

Please sign in to comment.