Skip to content

Commit 9e683d3

Browse files
committed
support for named pks included
1 parent ffeb7b7 commit 9e683d3

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

src/main/java/net/sf/jsqlparser/statement/create/table/ForeignKeyIndex.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* Foreign Key Index
3030
* @author toben
3131
*/
32-
public class ForeignKeyIndex extends Index {
32+
public class ForeignKeyIndex extends NamedConstraint {
3333
private Table table;
3434
private List<String> referencedColumnNames;
3535

@@ -51,8 +51,7 @@ public void setReferencedColumnNames(List<String> referencedColumnNames) {
5151

5252
@Override
5353
public String toString() {
54-
return (getName()!=null?"CONSTRAINT " + getName() + " ":"")
55-
+ getType() + " " + PlainSelect.getStringList(getColumnsNames(), true, true)
54+
return super.toString()
5655
+ " REFERENCES " + table + PlainSelect.getStringList(getReferencedColumnNames(), true, true);
5756
}
5857
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2014 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
/*
23+
* Copyright (C) 2014 JSQLParser.
24+
*
25+
* This library is free software; you can redistribute it and/or
26+
* modify it under the terms of the GNU Lesser General Public
27+
* License as published by the Free Software Foundation; either
28+
* version 2.1 of the License, or (at your option) any later version.
29+
*
30+
* This library is distributed in the hope that it will be useful,
31+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
32+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33+
* Lesser General Public License for more details.
34+
*
35+
* You should have received a copy of the GNU Lesser General Public
36+
* License along with this library; if not, write to the Free Software
37+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
38+
* MA 02110-1301 USA
39+
*/
40+
41+
package net.sf.jsqlparser.statement.create.table;
42+
43+
import net.sf.jsqlparser.statement.select.PlainSelect;
44+
45+
/**
46+
*
47+
* @author toben
48+
*/
49+
public class NamedConstraint extends Index {
50+
@Override
51+
public String toString() {
52+
return (getName()!=null?"CONSTRAINT " + getName() + " ":"")
53+
+ getType() + " " + PlainSelect.getStringList(getColumnsNames(), true, true);
54+
}
55+
}

src/main/javacc/net/sf/jsqlparser/parser/JSqlParserCC.jj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,12 +2036,15 @@ CreateTable CreateTable():
20362036
}
20372037
)
20382038
|
2039-
(
2039+
LOOKAHEAD(3) (
2040+
{
2041+
index = new NamedConstraint();
2042+
}
2043+
[<K_CONSTRAINT> tk3=<S_IDENTIFIER> {index.setName(tk3.image);} ]
20402044
tk=<K_PRIMARY> tk2=<K_KEY>
20412045
colNames=ColumnsNamesList()
20422046
{
2043-
index = new Index();
2044-
index.setType(tk.image + " "+ tk2.image);
2047+
index.setType(tk.image + " " + tk2.image);
20452048
index.setColumnsNames(colNames);
20462049
indexes.add(index);
20472050
}

src/test/java/net/sf/jsqlparser/test/create/CreateTableTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public void testCreateTableForeignKey2() throws JSQLParserException {
5757
String statement = "CREATE TABLE test (id INT UNSIGNED NOT NULL AUTO_INCREMENT, string VARCHAR (20), user_id INT UNSIGNED, PRIMARY KEY (id), CONSTRAINT fkIdx FOREIGN KEY (user_id) REFERENCES ra_user(id))";
5858
assertSqlCanBeParsedAndDeparsed(statement);
5959
}
60+
61+
public void testCreateTablePrimaryKey() throws JSQLParserException {
62+
String statement = "CREATE TABLE test (id INT UNSIGNED NOT NULL AUTO_INCREMENT, string VARCHAR (20), user_id INT UNSIGNED, CONSTRAINT pk_name PRIMARY KEY (id))";
63+
assertSqlCanBeParsedAndDeparsed(statement);
64+
}
6065

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

0 commit comments

Comments
 (0)