Skip to content

Commit 401d279

Browse files
Megan Woodsmeganwoods
authored andcommitted
Implemented table check constraint for named constraints.
1. Added named constraint to create table. 2. Added check constraint to alter table statement. 3. Added CheckConstraint type. Tests: 4. Added create table test. 5. Added alter table test.
1 parent 04fc3aa commit 401d279

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2013 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 net.sf.jsqlparser.expression.Expression;
25+
import net.sf.jsqlparser.schema.Table;
26+
27+
/**
28+
* Table Check Constraint
29+
* Eg. ' CONSTRAINT less_than_ten CHECK (count < 10) '
30+
* @author mw
31+
*/
32+
public class CheckConstraint extends NamedConstraint {
33+
private Table table;
34+
private Expression expression;
35+
36+
37+
public Table getTable() {
38+
return table;
39+
}
40+
41+
public void setTable(Table table) {
42+
this.table = table;
43+
}
44+
45+
public Expression getExpression() {
46+
return expression;
47+
}
48+
49+
public void setExpression(Expression expression) {
50+
this.expression = expression;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "CONSTRAINT " + getName() + " CHECK (" + expression + ")";
56+
}
57+
}

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
182182
| <K_FOREIGN:"FOREIGN">
183183
| <K_CONSTRAINT:"CONSTRAINT">
184184
| <K_REFERENCES:"REFERENCES">
185+
| <K_CHECK:"CHECK">
185186
| <K_CHARACTER:"CHARACTER">
186187
| <K_VARYING:"VARYING">
187188
| <K_START:"START">
@@ -1537,6 +1538,20 @@ First First():
15371538
}
15381539
}
15391540

1541+
1542+
Expression MandatoryExpression() #Expression :
1543+
{
1544+
Expression retval = null;
1545+
}
1546+
{
1547+
(
1548+
LOOKAHEAD(3) retval=OrExpression() | "(" retval=MandatoryExpression() ")" {retval = new Parenthesis(retval); }
1549+
)
1550+
1551+
{ return retval; }
1552+
}
1553+
1554+
15401555
Expression Expression() #Expression :
15411556
{
15421557
Expression retval = null;
@@ -2572,6 +2587,7 @@ CreateTable CreateTable():
25722587
String parameter = null;
25732588
Table fkTable = null;
25742589
Select select = null;
2590+
CheckConstraint checkCs = null;
25752591
}
25762592
{
25772593
<K_CREATE>
@@ -2650,7 +2666,7 @@ CreateTable CreateTable():
26502666
}
26512667
)
26522668
|
2653-
(
2669+
LOOKAHEAD(3)(
26542670
{
26552671
fkIndex = new ForeignKeyIndex();
26562672
}
@@ -2671,6 +2687,17 @@ CreateTable CreateTable():
26712687
[LOOKAHEAD(2) (<K_ON> <K_UPDATE> (<K_CASCADE> {fkIndex.setOnUpdateReferenceOption("CASCADE");}|<K_NO> <K_ACTION> {fkIndex.setOnUpdateReferenceOption("NO ACTION");}))]
26722688
)
26732689
|
2690+
LOOKAHEAD(3)(
2691+
[<K_CONSTRAINT> sk3 = RelObjectName()]
2692+
{Expression exp = null;}
2693+
<K_CHECK> ("(" exp = Expression() ")")* {
2694+
checkCs = new CheckConstraint();
2695+
checkCs.setName(sk3);
2696+
checkCs.setExpression(exp);
2697+
indexes.add(checkCs);
2698+
}
2699+
)
2700+
|
26742701
(
26752702
columnName=RelObjectName()
26762703

@@ -2951,7 +2978,7 @@ Alter AlterTable():
29512978
(
29522979
<K_CONSTRAINT> sk3=RelObjectName()
29532980

2954-
( ( tk=<K_FOREIGN> tk2=<K_KEY>
2981+
( ( tk=<K_FOREIGN> tk2=<K_KEY>
29552982
columnNames=ColumnsNamesList()
29562983
{
29572984
fkIndex = new ForeignKeyIndex();
@@ -2976,7 +3003,16 @@ Alter AlterTable():
29763003
index.setColumnsNames(columnNames);
29773004
alter.setIndex(index);
29783005
}
2979-
) )
3006+
)
3007+
|
3008+
(
3009+
<K_CHECK> {Expression exp = null;} ("(" exp = Expression() ")")* {
3010+
CheckConstraint checkCs = new CheckConstraint();
3011+
checkCs.setName(sk3);
3012+
checkCs.setExpression(exp);
3013+
alter.setIndex(checkCs);
3014+
}
3015+
) )
29803016
)
29813017
)
29823018
)

src/test/java/net/sf/jsqlparser/test/alter/AlterTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@ public void testAlterTableDropConstraint() throws JSQLParserException {
6565
public void testAlterTablePK() throws JSQLParserException {
6666
assertSqlCanBeParsedAndDeparsed("ALTER TABLE `Author` ADD CONSTRAINT `AuthorPK` PRIMARY KEY (`ID`)");
6767
}
68+
69+
public void testAlterTableCheckConstraint() throws JSQLParserException {
70+
assertSqlCanBeParsedAndDeparsed("ALTER TABLE `Author` ADD CONSTRAINT name_not_empty CHECK (`NAME` <> '')");
71+
}
72+
6873
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public void testMySqlCreateTableWithTextIndexes() throws JSQLParserException {
192192
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");
193193
}
194194

195+
public void testCreateTableWithCheck() throws JSQLParserException {
196+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE table2 (id INT (10) NOT NULL, name TEXT, url TEXT, CONSTRAINT name_not_empty CHECK (name <> ''))");
197+
}
198+
195199
public void testRUBiSCreateList() throws Exception {
196200
BufferedReader in = new BufferedReader(new InputStreamReader(CreateTableTest.class.getResourceAsStream("/RUBiS-create-requests.txt")));
197201
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();

0 commit comments

Comments
 (0)