|
| 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.expression.CastExpression; |
| 13 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 14 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 15 | +import net.sf.jsqlparser.statement.Statement; |
| 16 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 17 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.ValueSource; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | + |
| 24 | +class MySqlTypeCharacterSetTest { |
| 25 | + private Statement parse(String sql) throws Exception { |
| 26 | + return CCJSqlParserUtil.parse(sql, p -> p.withDialect(Dialect.MYSQL)); |
| 27 | + } |
| 28 | + |
| 29 | + @ParameterizedTest |
| 30 | + @ValueSource(strings = { |
| 31 | + "SELECT CAST('xx' AS CHAR(16) CHARSET BINARY)", |
| 32 | + "SELECT CAST('xx' AS CHAR(16) CHARACTER SET 'utf8mb4')", |
| 33 | + "SELECT CONVERT('xx' USING utf8mb4)", |
| 34 | + "CREATE TABLE t (c VARCHAR(20) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_bin')", |
| 35 | + "CREATE TABLE t (c TEXT CHARSET utf8mb4)", |
| 36 | + "CREATE TABLE t (c SET('a','b') CHARSET 'utf8mb4')", |
| 37 | + "CREATE TABLE t (c BINARY(16) AS (CAST('xx' AS CHAR(16) CHARSET BINARY)))", |
| 38 | + "ALTER TABLE t MODIFY c ENUM('a','b') CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci'", |
| 39 | + "ALTER TABLE t ADD c CHAR(4) CHARSET utf8mb4, ADD d VARCHAR(10) CHARACTER SET utf8mb4" |
| 40 | + }) |
| 41 | + void sharesTypeSyntaxAcrossCastsAndColumnDefinitions(String sql) throws Exception { |
| 42 | + Statement statement = parse(sql); |
| 43 | + StringBuilder output = new StringBuilder(); |
| 44 | + statement.accept(new StatementDeParser(output), null); |
| 45 | + assertEquals(statement.toString(), output.toString()); |
| 46 | + assertEquals(statement.toString(), parse(output.toString()).toString()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void exposesCharsetAndRetainsAbbreviationWhenMutated() throws Exception { |
| 51 | + CreateTable table = (CreateTable) parse("CREATE TABLE t (c VARCHAR(20) CHARSET 'utf8mb4')"); |
| 52 | + ColDataType type = table.getColumnDefinitions().get(0).getColDataType(); |
| 53 | + assertEquals("'utf8mb4'", type.getCharacterSet()); |
| 54 | + assertTrue(type.isUseCharsetKeyword()); |
| 55 | + type.setCharacterSet("latin1"); |
| 56 | + assertEquals("CREATE TABLE t (c VARCHAR (20) CHARSET latin1)", table.toString()); |
| 57 | + type.setUseCharsetKeyword(false); |
| 58 | + assertEquals("CREATE TABLE t (c VARCHAR (20) CHARACTER SET latin1)", table.toString()); |
| 59 | + type.setCharacterSet(null); |
| 60 | + assertEquals("CREATE TABLE t (c VARCHAR (20))", table.toString()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void preservesDefaultApiSpellingAndTypeEquality() throws Exception { |
| 65 | + ColDataType legacy = new ColDataType("CHAR").withCharacterSet("binary"); |
| 66 | + assertEquals("CHAR CHARACTER SET binary", legacy.toString()); |
| 67 | + PlainSelect select = (PlainSelect) parse("SELECT CAST('a' AS CHAR CHARACTER SET binary)"); |
| 68 | + ColDataType parsed = |
| 69 | + ((CastExpression) select.getSelectItem(0).getExpression()).getColDataType(); |
| 70 | + assertEquals(legacy, parsed); |
| 71 | + assertEquals(legacy.hashCode(), parsed.hashCode()); |
| 72 | + parsed.setUseCharsetKeyword(true); |
| 73 | + assertNotEquals(legacy, parsed); |
| 74 | + } |
| 75 | + |
| 76 | + @ParameterizedTest |
| 77 | + @ValueSource(strings = {"SELECT CAST('x' AS CHAR CHARSET)", |
| 78 | + "SELECT CAST('x' AS CHAR CHARACTER SET)", |
| 79 | + "SELECT CAST('x' AS CHAR CHARSET = utf8mb4)"}) |
| 80 | + void requiresCharsetName(String sql) { |
| 81 | + assertThrows(Exception.class, () -> parse(sql)); |
| 82 | + } |
| 83 | +} |
0 commit comments