Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class CheckConstraint extends NamedConstraint {

private Expression expression;

private Boolean enforced;

public Table getTable() {
return table;
}
Expand All @@ -37,13 +39,24 @@ public void setExpression(Expression expression) {
this.expression = expression;
}

public Boolean getEnforced() {
return enforced;
}

public void setEnforced(Boolean enforced) {
this.enforced = enforced;
}

@Override
public String toString() {
StringBuilder b = new StringBuilder();
if (getName() != null) {
b.append("CONSTRAINT ").append(getName()).append(" ");
}
b.append("CHECK (").append(expression).append(")");
if (enforced != null) {
b.append(enforced ? " ENFORCED" : " NOT ENFORCED");
}
return b.toString();
}

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

public CheckConstraint withEnforced(Boolean enforced) {
setEnforced(enforced);
return this;
}

public <E extends Expression> E getExpression(Class<E> type) {
return type.cast(getExpression());
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -11833,11 +11833,17 @@ void ReferentialActionsOnIndex(ForeignKeyIndex fkIndex):
CheckConstraint CheckConstraintSpec(String constraintName):
{
Expression exp = null;
Boolean enforced = null;
}
{
<K_CHECK> ( LOOKAHEAD(2) "(" exp = Expression() ")" )*
[
[ <K_NOT> { enforced = false; } ]
<K_ENFORCED> { if (enforced == null) { enforced = true; } }
]
{
return new CheckConstraint().withName(constraintName).withExpression(exp);
return new CheckConstraint().withName(constraintName).withExpression(exp)
.withEnforced(enforced);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.create;

import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static org.junit.jupiter.api.Assertions.assertEquals;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.statement.create.table.CheckConstraint;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import org.junit.jupiter.api.Test;

/** MySQL {@code CREATE TABLE} constraint syntax verified against MySQL 8.4.11. */
public class MySQLCreateTableConstraintTest {

@Test
public void testCheckConstraintEnforcement() throws JSQLParserException {
String sql = "CREATE TABLE mysql_check_enforcement (score INT, "
+ "CONSTRAINT chk_positive CHECK (score >= 0) ENFORCED, "
+ "CONSTRAINT chk_cap CHECK (score < 100) NOT ENFORCED)";

CreateTable createTable = (CreateTable) assertSqlCanBeParsedAndDeparsed(sql);

CheckConstraint positive = (CheckConstraint) createTable.getIndexes().get(0);
assertEquals(Boolean.TRUE, positive.getEnforced());
assertEquals("chk_positive", positive.getName());

CheckConstraint cap = (CheckConstraint) createTable.getIndexes().get(1);
assertEquals(Boolean.FALSE, cap.getEnforced());
assertEquals("chk_cap", cap.getName());
}
}
Loading