|
| 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.table; |
| 11 | + |
| 12 | +import net.sf.jsqlparser.JSQLParserException; |
| 13 | +import net.sf.jsqlparser.expression.CastExpression; |
| 14 | +import net.sf.jsqlparser.expression.JsonFunction; |
| 15 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 16 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 17 | +import net.sf.jsqlparser.statement.create.table.ColDataType.CharacterSetSyntax; |
| 18 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 19 | +import net.sf.jsqlparser.test.TestUtils; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.CsvSource; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.*; |
| 25 | + |
| 26 | +class CharacterSetShorthandTest { |
| 27 | + @ParameterizedTest |
| 28 | + @CsvSource({"ASCII,latin1", "UNICODE,ucs2"}) |
| 29 | + void shorthandsExposeCharacterSetAndRetainSpelling(String shorthand, String charset) |
| 30 | + throws JSQLParserException { |
| 31 | + PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 32 | + "SELECT CAST('ab' AS CHAR(10) " + shorthand + "), " |
| 33 | + + "JSON_VALUE('{\"v\":\"ab\"}', '$.v' RETURNING CHAR(10) " + shorthand |
| 34 | + + ")", |
| 35 | + true, parser -> parser.withDialect(Dialect.MYSQL)); |
| 36 | + ColDataType castType = |
| 37 | + ((CastExpression) select.getSelectItem(0).getExpression()).getColDataType(); |
| 38 | + ColDataType jsonType = |
| 39 | + ((JsonFunction) select.getSelectItem(1).getExpression()).getReturningType(); |
| 40 | + for (ColDataType type : new ColDataType[] {castType, jsonType}) { |
| 41 | + assertEquals("CHAR", type.getBaseTypeName()); |
| 42 | + assertEquals(charset, type.getCharacterSet()); |
| 43 | + assertEquals(CharacterSetSyntax.valueOf(shorthand), type.getCharacterSetSyntax()); |
| 44 | + } |
| 45 | + TestUtils.assertSqlCanBeParsedAndDeparsed(select.toString(), false, |
| 46 | + parser -> parser.withDialect(Dialect.MYSQL)); |
| 47 | + TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 48 | + "CREATE TABLE t (j JSON, g CHAR(10) AS (JSON_VALUE(j, '$.v' RETURNING CHAR(10) " |
| 49 | + + shorthand + ")) STORED)", |
| 50 | + true, parser -> parser.withDialect(Dialect.MYSQL)); |
| 51 | + CreateTable create = (CreateTable) TestUtils.assertSqlCanBeParsedAndDeparsed( |
| 52 | + "CREATE TABLE t (c CHAR(10) " + shorthand + ")", true, |
| 53 | + parser -> parser.withDialect(Dialect.MYSQL)); |
| 54 | + assertEquals(charset, |
| 55 | + create.getColumnDefinitions().get(0).getColDataType().getCharacterSet()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void parsedReturningTypeCanBeChanged() throws JSQLParserException { |
| 60 | + String sql = "SELECT JSON_VALUE('{\"v\":\"ab\"}', '$.v' RETURNING CHAR(10) ASCII)"; |
| 61 | + PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sql, |
| 62 | + parser -> parser.withDialect(Dialect.MYSQL)); |
| 63 | + ColDataType type = |
| 64 | + ((JsonFunction) select.getSelectItem(0).getExpression()).getReturningType(); |
| 65 | + type.setCharacterSetSyntax(CharacterSetSyntax.UNICODE); |
| 66 | + TestUtils.assertStatementCanBeDeparsedAs(select, sql.replace("ASCII", "UNICODE"), true); |
| 67 | + type.setCharacterSet("utf8mb4"); |
| 68 | + TestUtils.assertStatementCanBeDeparsedAs(select, |
| 69 | + sql.replace("ASCII", "CHARACTER SET utf8mb4"), true); |
| 70 | + TestUtils.assertSqlCanBeParsedAndDeparsed(select.toString(), false, |
| 71 | + parser -> parser.withDialect(Dialect.MYSQL)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void characterSetSyntaxCanBeConstructedChangedAndCleared() throws JSQLParserException { |
| 76 | + ColDataType type = new ColDataType("CHAR").addArgumentsStringList("10") |
| 77 | + .withCharacterSetSyntax(CharacterSetSyntax.ASCII); |
| 78 | + assertEquals("latin1", type.getCharacterSet()); |
| 79 | + assertEquals("CHAR (10) ASCII", type.toString()); |
| 80 | + type.setCharacterSetSyntax(CharacterSetSyntax.UNICODE); |
| 81 | + assertEquals("ucs2", type.getCharacterSet()); |
| 82 | + assertEquals("CHAR (10) UNICODE", type.toString()); |
| 83 | + type.setCharacterSet("utf8mb4"); |
| 84 | + assertEquals(CharacterSetSyntax.CHARACTER_SET, type.getCharacterSetSyntax()); |
| 85 | + assertEquals("CHAR (10) CHARACTER SET utf8mb4", type.toString()); |
| 86 | + type.setUseCharsetKeyword(true); |
| 87 | + assertTrue(type.isUseCharsetKeyword()); |
| 88 | + type.setCharacterSet("latin1"); |
| 89 | + assertEquals("CHAR (10) CHARSET latin1", type.toString()); |
| 90 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT CAST('ab' AS " + type + ")", true, |
| 91 | + parser -> parser.withDialect(Dialect.MYSQL)); |
| 92 | + type.setCharacterSetSyntax(CharacterSetSyntax.ASCII); |
| 93 | + type.setUseCharsetKeyword(false); |
| 94 | + assertEquals("CHAR (10) CHARACTER SET latin1", type.toString()); |
| 95 | + type.setCharacterSetSyntax(CharacterSetSyntax.UNICODE); |
| 96 | + type.setCharacterSet(null); |
| 97 | + assertEquals("CHAR (10)", type.toString()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void syntaxParticipatesInEqualityAndHashCode() { |
| 102 | + ColDataType ascii = |
| 103 | + new ColDataType("CHAR").withCharacterSetSyntax(CharacterSetSyntax.ASCII); |
| 104 | + ColDataType same = new ColDataType("char").withCharacterSetSyntax(CharacterSetSyntax.ASCII); |
| 105 | + ColDataType explicit = new ColDataType("CHAR").withCharacterSet("latin1"); |
| 106 | + assertEquals(ascii, same); |
| 107 | + assertEquals(ascii.hashCode(), same.hashCode()); |
| 108 | + assertNotEquals(ascii, explicit); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void shorthandIsMySqlSpecificAndDoesNotReserveIdentifiers() throws JSQLParserException { |
| 113 | + for (String shorthand : new String[] {"ASCII", "UNICODE"}) { |
| 114 | + String sql = "SELECT CAST('ab' AS CHAR(10) " + shorthand + ")"; |
| 115 | + assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(sql)); |
| 116 | + assertThrows(JSQLParserException.class, |
| 117 | + () -> CCJSqlParserUtil.parse(sql, |
| 118 | + parser -> parser.withDialect(Dialect.POSTGRESQL))); |
| 119 | + TestUtils.assertSqlCanBeParsedAndDeparsed("SELECT " + shorthand + " FROM t", true, |
| 120 | + parser -> parser.withDialect(Dialect.MYSQL)); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments