Skip to content

Commit 125de36

Browse files
authored
Support MySQL CHECK constraint enforcement (#2528)
1 parent 433143e commit 125de36

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

‎src/main/java/net/sf/jsqlparser/statement/create/table/CheckConstraint.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class CheckConstraint extends NamedConstraint {
2121

2222
private Expression expression;
2323

24+
private Boolean enforced;
25+
2426
public Table getTable() {
2527
return table;
2628
}
@@ -37,13 +39,24 @@ public void setExpression(Expression expression) {
3739
this.expression = expression;
3840
}
3941

42+
public Boolean getEnforced() {
43+
return enforced;
44+
}
45+
46+
public void setEnforced(Boolean enforced) {
47+
this.enforced = enforced;
48+
}
49+
4050
@Override
4151
public String toString() {
4252
StringBuilder b = new StringBuilder();
4353
if (getName() != null) {
4454
b.append("CONSTRAINT ").append(getName()).append(" ");
4555
}
4656
b.append("CHECK (").append(expression).append(")");
57+
if (enforced != null) {
58+
b.append(enforced ? " ENFORCED" : " NOT ENFORCED");
59+
}
4760
return b.toString();
4861
}
4962

@@ -57,6 +70,11 @@ public CheckConstraint withExpression(Expression expression) {
5770
return this;
5871
}
5972

73+
public CheckConstraint withEnforced(Boolean enforced) {
74+
setEnforced(enforced);
75+
return this;
76+
}
77+
6078
public <E extends Expression> E getExpression(Class<E> type) {
6179
return type.cast(getExpression());
6280
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11950,11 +11950,17 @@ void ReferentialActionsOnIndex(ForeignKeyIndex fkIndex):
1195011950
CheckConstraint CheckConstraintSpec(String constraintName):
1195111951
{
1195211952
Expression exp = null;
11953+
Boolean enforced = null;
1195311954
}
1195411955
{
1195511956
<K_CHECK> ( LOOKAHEAD(2) "(" exp = Expression() ")" )*
11957+
[
11958+
[ <K_NOT> { enforced = false; } ]
11959+
<K_ENFORCED> { if (enforced == null) { enforced = true; } }
11960+
]
1195611961
{
11957-
return new CheckConstraint().withName(constraintName).withExpression(exp);
11962+
return new CheckConstraint().withName(constraintName).withExpression(exp)
11963+
.withEnforced(enforced);
1195811964
}
1195911965
}
1196011966

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2026 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.create;
11+
12+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
15+
import net.sf.jsqlparser.JSQLParserException;
16+
import net.sf.jsqlparser.statement.create.table.CheckConstraint;
17+
import net.sf.jsqlparser.statement.create.table.CreateTable;
18+
import org.junit.jupiter.api.Test;
19+
20+
/** MySQL {@code CREATE TABLE} constraint syntax verified against MySQL 8.4.11. */
21+
public class MySQLCreateTableConstraintTest {
22+
23+
@Test
24+
public void testCheckConstraintEnforcement() throws JSQLParserException {
25+
String sql = "CREATE TABLE mysql_check_enforcement (score INT, "
26+
+ "CONSTRAINT chk_positive CHECK (score >= 0) ENFORCED, "
27+
+ "CONSTRAINT chk_cap CHECK (score < 100) NOT ENFORCED)";
28+
29+
CreateTable createTable = (CreateTable) assertSqlCanBeParsedAndDeparsed(sql);
30+
31+
CheckConstraint positive = (CheckConstraint) createTable.getIndexes().get(0);
32+
assertEquals(Boolean.TRUE, positive.getEnforced());
33+
assertEquals("chk_positive", positive.getName());
34+
35+
CheckConstraint cap = (CheckConstraint) createTable.getIndexes().get(1);
36+
assertEquals(Boolean.FALSE, cap.getEnforced());
37+
assertEquals("chk_cap", cap.getName());
38+
}
39+
}

0 commit comments

Comments
 (0)