Skip to content

Commit 8777a8e

Browse files
authored
Merge pull request #336 from wrobstory/pg-create-table
Add support for more Postgres Create Table options
2 parents fefe4ac + 04c56f2 commit 8777a8e

File tree

3 files changed

+109
-5
lines changed

3 files changed

+109
-5
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2016 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+
package net.sf.jsqlparser.statement.create.table;
23+
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
28+
import net.sf.jsqlparser.expression.Expression;
29+
import net.sf.jsqlparser.statement.select.PlainSelect;
30+
31+
/**
32+
* Table Exclusion Constraint
33+
* Eg. 'EXCLUDE WHERE (col1 > 100)'
34+
*
35+
* @author wrobstory
36+
*/
37+
public class ExcludeConstraint extends Index{
38+
39+
private Expression expression;
40+
41+
public Expression getExpression() {
42+
return expression;
43+
}
44+
45+
public void setExpression(Expression expression) {
46+
this.expression = expression;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
StringBuilder exclusionStatement = new StringBuilder("EXCLUDE WHERE ");
52+
exclusionStatement.append("(");
53+
exclusionStatement.append(expression);
54+
exclusionStatement.append(")");
55+
return exclusionStatement.toString();
56+
}
57+
58+
}

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
253253
| <K_DOUBLE : "DOUBLE">
254254
| <K_PRECISION : "PRECISION">
255255
| <K_TABLESPACE : "TABLESPACE">
256+
| <K_EXCLUDE : "EXCLUDE">
256257
}
257258

258259
TOKEN : /* Stuff */
@@ -2653,6 +2654,7 @@ CreateTable CreateTable():
26532654
Table fkTable = null;
26542655
Select select = null;
26552656
CheckConstraint checkCs = null;
2657+
ExcludeConstraint excludeC = null;
26562658
}
26572659
{
26582660
<K_CREATE>
@@ -2771,6 +2773,13 @@ CreateTable CreateTable():
27712773
indexes.add(checkCs);
27722774
}
27732775
)
2776+
|
2777+
tk=<K_EXCLUDE> {excludeC = new ExcludeConstraint(); Expression exp = null;}
2778+
(tk2=<K_WHERE>
2779+
("(" exp = Expression() ")")* {excludeC.setExpression(exp);})
2780+
{
2781+
indexes.add(excludeC);
2782+
}
27742783
|
27752784
(
27762785
columnName=RelObjectName()
@@ -2879,12 +2888,16 @@ List<String> CreateParameter():
28792888
{
28802889
String retval = "";
28812890
Token tk = null;
2882-
Token sign = null;
2891+
Token tk2 = null;
2892+
StringBuilder identifier = new StringBuilder("");
2893+
Expression exp = null;
28832894
List<String> param = new ArrayList<String>();
28842895
}
28852896
{
28862897
(
2887-
tk=<S_IDENTIFIER> { param.add(tk.image); }
2898+
((tk=<S_IDENTIFIER> { identifier.append(tk.image); }
2899+
["." tk2=<S_IDENTIFIER> { identifier.append("."); identifier.append(tk2.image); }])
2900+
{ param.add(identifier.toString()); })
28882901
|
28892902
tk=<K_NULL> { param.add(tk.image); }
28902903
|
@@ -2925,12 +2938,23 @@ List<String> CreateParameter():
29252938
tk=<K_TIME_KEY_EXPR> { param.add(new TimeKeyExpression(tk.image).toString()); }
29262939
|
29272940
"=" { param.add("="); }
2928-
|
2929-
<K_USING> <K_INDEX> <K_TABLESPACE> retval=RelObjectName() { param.add("USING"); param.add("INDEX"); param.add("TABLESPACE"); param.add(retval); }
2941+
|
2942+
LOOKAHEAD(3) <K_USING> <K_INDEX> <K_TABLESPACE> retval=RelObjectName() { param.add("USING"); param.add("INDEX"); param.add("TABLESPACE"); param.add(retval); }
29302943
|
29312944
<K_TABLESPACE> retval=RelObjectName() { param.add("TABLESPACE"); param.add(retval); }
29322945
|
29332946
retval=AList() { param.add(retval); }
2947+
|
2948+
<K_CHECK> ("(" exp = Expression() ")") { param.add("CHECK"); param.add("(" + exp.toString() + ")");}
2949+
|
2950+
tk=<K_CONSTRAINT> { param.add(tk.image); }
2951+
|
2952+
tk=<K_WITH> { param.add(tk.image); }
2953+
|
2954+
tk=<K_EXCLUDE> { param.add(tk.image); }
2955+
|
2956+
tk=<K_WHERE> { param.add(tk.image); }
2957+
29342958
)
29352959
{return param;}
29362960
}
@@ -2939,11 +2963,13 @@ String AList():
29392963
{
29402964
StringBuilder retval = new StringBuilder("(");
29412965
Token tk = null;
2966+
Token tk2 = null;
29422967
}
29432968
{
29442969
"("
29452970

2946-
( (tk=<S_LONG> | tk=<S_DOUBLE> | tk=<S_CHAR_LITERAL> | tk=<S_IDENTIFIER>) { retval.append(tk.image); } ["," {retval.append(",");}] )*
2971+
( (tk=<S_LONG> | tk=<S_DOUBLE> | tk=<S_CHAR_LITERAL> | tk=<S_IDENTIFIER>) { retval.append(tk.image); }
2972+
[("," {retval.append(",");} | "=" {retval.append("=");})] )*
29472973

29482974
")"
29492975
{

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ public void testOnDeleteSetNull() throws JSQLParserException {
220220
assertSqlCanBeParsedAndDeparsed("CREATE TABLE inventory (inventory_id INT PRIMARY KEY, product_id INT, CONSTRAINT fk_inv_product_id FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE SET NULL)");
221221
}
222222

223+
public void testColumnCheck() throws JSQLParserException {
224+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table1 (col1 INTEGER CHECK (col1 > 100))");
225+
}
226+
227+
public void testTableReferenceWithSchema() throws JSQLParserException {
228+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table1 (col1 INTEGER REFERENCES schema1.table1)");
229+
}
230+
231+
public void testNamedColumnConstraint() throws JSQLParserException {
232+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE foo (col1 integer CONSTRAINT no_null NOT NULL)");
233+
}
234+
235+
public void testColumnConstraintWith() throws JSQLParserException {
236+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE foo (col1 integer) WITH (fillfactor=70)");
237+
}
238+
239+
public void testExcludeWhereConstraint() throws JSQLParserException {
240+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE foo (col1 integer, EXCLUDE WHERE (col1 > 100))");
241+
}
242+
223243
public void testRUBiSCreateList() throws Exception {
224244
BufferedReader in = new BufferedReader(new InputStreamReader(CreateTableTest.class.getResourceAsStream("/RUBiS-create-requests.txt")));
225245
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();

0 commit comments

Comments
 (0)