|
14 | 14 | import static org.junit.jupiter.api.Assertions.assertEquals; |
15 | 15 | import static org.junit.jupiter.api.Assertions.assertFalse; |
16 | 16 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertNull; |
17 | 18 | import static org.junit.jupiter.api.Assertions.assertTrue; |
18 | 19 |
|
19 | 20 | import java.io.BufferedReader; |
|
35 | 36 | import net.sf.jsqlparser.statement.create.table.ColumnDefinition; |
36 | 37 | import net.sf.jsqlparser.statement.create.table.CreateTable; |
37 | 38 | import net.sf.jsqlparser.statement.create.table.ExcludeConstraint; |
| 39 | +import net.sf.jsqlparser.statement.create.table.ForeignKeyIndex; |
38 | 40 | import net.sf.jsqlparser.statement.create.table.Index; |
| 41 | +import net.sf.jsqlparser.statement.create.table.NamedConstraint; |
39 | 42 | import net.sf.jsqlparser.statement.create.table.RowMovementMode; |
40 | 43 | import net.sf.jsqlparser.test.TestException; |
41 | 44 | import org.assertj.core.api.Assertions; |
@@ -1247,4 +1250,44 @@ void testUniqueIndexIssue1893() throws JSQLParserException { |
1247 | 1250 | // A plain INDEX must still parse unchanged. |
1248 | 1251 | assertSqlCanBeParsedAndDeparsed("CREATE TABLE t (a int, INDEX idx (a))", true); |
1249 | 1252 | } |
| 1253 | + |
| 1254 | + @Test |
| 1255 | + void testMySqlConstraintSymbolAndIndexNameIssue1570() throws JSQLParserException { |
| 1256 | + String uniqueSql = "CREATE TABLE table1 (col1 INT, col2 INT UNIQUE, " |
| 1257 | + + "CONSTRAINT my_constraint UNIQUE KEY index_name (col1))"; |
| 1258 | + CreateTable uniqueTable = (CreateTable) assertSqlCanBeParsedAndDeparsed(uniqueSql, true); |
| 1259 | + NamedConstraint unique = (NamedConstraint) uniqueTable.getIndexes().get(0); |
| 1260 | + |
| 1261 | + assertEquals("my_constraint", unique.getName()); |
| 1262 | + assertEquals("index_name", unique.getIndexName()); |
| 1263 | + assertEquals("UNIQUE KEY", unique.getType()); |
| 1264 | + |
| 1265 | + String foreignKeySql = "CREATE TABLE table2 (id INT, parent_id INT, " |
| 1266 | + + "CONSTRAINT fk_parent FOREIGN KEY fk_parent_idx (parent_id) " |
| 1267 | + + "REFERENCES parent (id))"; |
| 1268 | + CreateTable foreignKeyTable = |
| 1269 | + (CreateTable) assertSqlCanBeParsedAndDeparsed(foreignKeySql, true); |
| 1270 | + ForeignKeyIndex foreignKey = (ForeignKeyIndex) foreignKeyTable.getIndexes().get(0); |
| 1271 | + |
| 1272 | + assertEquals("fk_parent", foreignKey.getName()); |
| 1273 | + assertEquals("fk_parent_idx", foreignKey.getIndexName()); |
| 1274 | + } |
| 1275 | + |
| 1276 | + @Test |
| 1277 | + void testMySqlUnnamedConstraintAndUniqueIndexOptionsIssues1570And538() |
| 1278 | + throws JSQLParserException { |
| 1279 | + assertSqlCanBeParsedAndDeparsed( |
| 1280 | + "CREATE TABLE table1 (col1 INT, CONSTRAINT UNIQUE KEY (col1))", true); |
| 1281 | + |
| 1282 | + String sql = "CREATE TABLE g_platform_payway (id INT, platform_code VARCHAR (45), " |
| 1283 | + + "pay_way VARCHAR (32), UNIQUE uniq_platform_payway USING BTREE " |
| 1284 | + + "(platform_code, pay_way) COMMENT 'should be unique')"; |
| 1285 | + CreateTable createTable = (CreateTable) assertSqlCanBeParsedAndDeparsed(sql, true); |
| 1286 | + NamedConstraint unique = (NamedConstraint) createTable.getIndexes().get(0); |
| 1287 | + |
| 1288 | + assertNull(unique.getName()); |
| 1289 | + assertEquals("uniq_platform_payway", unique.getIndexName()); |
| 1290 | + assertEquals("BTREE", unique.getUsing()); |
| 1291 | + assertEquals(List.of("COMMENT", "'should be unique'"), unique.getIndexSpec()); |
| 1292 | + } |
1250 | 1293 | } |
0 commit comments