|
| 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