|
| 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.expression; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | +import net.sf.jsqlparser.JSQLParserException; |
| 14 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 15 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 16 | +import net.sf.jsqlparser.schema.Column; |
| 17 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 18 | +import net.sf.jsqlparser.util.deparser.ExpressionDeParser; |
| 19 | +import net.sf.jsqlparser.util.deparser.SelectDeParser; |
| 20 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.ValueSource; |
| 24 | + |
| 25 | +class MySqlConvertTest { |
| 26 | + @ParameterizedTest |
| 27 | + @ValueSource(strings = {"'42', SIGNED", "'-1', UNSIGNED INTEGER", |
| 28 | + "'3.14', DECIMAL(10,2)", "'xx', CHAR(16) CHARACTER SET 'utf8mb4'", |
| 29 | + "'xx', CHAR(16) CHARSET binary", "'2026-09-21', DATE", |
| 30 | + "NULL, CHAR", "amount, DECIMAL(12,4)", "COALESCE(amount, 0), SIGNED", |
| 31 | + "CONVERT('42', SIGNED), CHAR(10)", "?, SIGNED"}) |
| 32 | + void representsExpressionBeforeType(String arguments) throws Exception { |
| 33 | + PlainSelect select = parse("SELECT CONVERT(" + arguments + ") FROM t", Dialect.MYSQL); |
| 34 | + TranscodingFunction convert = conversion(select); |
| 35 | + assertEquals(TranscodingFunction.Syntax.TYPE_LAST, convert.getSyntax()); |
| 36 | + assertFalse(convert.isTranscodeStyle()); |
| 37 | + assertNotNull(convert.getColDataType()); |
| 38 | + roundTrip(select, Dialect.MYSQL); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void distinguishesDialectDependentOrderAndLegacyAccessors() throws Exception { |
| 43 | + TranscodingFunction mysql = conversion(parse("SELECT CONVERT(value, CHAR)", Dialect.MYSQL)); |
| 44 | + assertInstanceOf(Column.class, mysql.getExpression()); |
| 45 | + assertEquals("CHAR", mysql.getColDataType().getDataType()); |
| 46 | + TranscodingFunction sqlserver = conversion( |
| 47 | + parse("SELECT CONVERT(VARCHAR(10), value, 120)", Dialect.SQLSERVER)); |
| 48 | + assertEquals(TranscodingFunction.Syntax.TYPE_FIRST, sqlserver.getSyntax()); |
| 49 | + assertEquals("120", sqlserver.getTranscodingName()); |
| 50 | + roundTrip(parse("SELECT TRY_CONVERT(INT, value)", Dialect.SQLSERVER), Dialect.SQLSERVER); |
| 51 | + roundTrip(parse("SELECT CONVERT(VARCHAR(10), value, 120)", Dialect.SQLSERVER), |
| 52 | + Dialect.SQLSERVER); |
| 53 | + TranscodingFunction using = |
| 54 | + conversion(parse("SELECT CONVERT(value USING utf8mb4)", Dialect.MYSQL)); |
| 55 | + assertTrue(using.isTranscodeStyle()); |
| 56 | + using.setTranscodeStyle(false); |
| 57 | + assertEquals(TranscodingFunction.Syntax.TYPE_FIRST, using.getSyntax()); |
| 58 | + using.setTranscodeStyle(true); |
| 59 | + assertEquals(TranscodingFunction.Syntax.USING, using.getSyntax()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void visitsOperandAndRendersTypeMutations() throws Exception { |
| 64 | + PlainSelect select = parse("SELECT CONVERT(42, SIGNED)", Dialect.MYSQL); |
| 65 | + conversion(select).getColDataType().setDataType("UNSIGNED"); |
| 66 | + StringBuilder out = new StringBuilder(); |
| 67 | + ExpressionDeParser visitor = new ExpressionDeParser() { |
| 68 | + @Override |
| 69 | + public <S> StringBuilder visit(LongValue value, S context) { |
| 70 | + return getBuilder().append(value.getValue() + 1); |
| 71 | + } |
| 72 | + }; |
| 73 | + select.accept(new StatementDeParser(visitor, new SelectDeParser(), out), null); |
| 74 | + assertEquals("SELECT CONVERT( 43, UNSIGNED )", out.toString()); |
| 75 | + roundTrip(select, Dialect.MYSQL); |
| 76 | + } |
| 77 | + |
| 78 | + @ParameterizedTest |
| 79 | + @ValueSource(strings = {"SELECT CONVERT(1,)", "SELECT CONVERT(, SIGNED)", |
| 80 | + "SELECT CONVERT(1, SIGNED, 120)", "SELECT CONVERT(1 USING)"}) |
| 81 | + void rejectsIncompleteArguments(String sql) { |
| 82 | + assertThrows(JSQLParserException.class, () -> parse(sql, Dialect.MYSQL)); |
| 83 | + } |
| 84 | + |
| 85 | + private static PlainSelect parse(String sql, Dialect dialect) throws JSQLParserException { |
| 86 | + return (PlainSelect) CCJSqlParserUtil.parse(sql, p -> p.withDialect(dialect)); |
| 87 | + } |
| 88 | + |
| 89 | + private static TranscodingFunction conversion(PlainSelect select) { |
| 90 | + return assertInstanceOf(TranscodingFunction.class, select.getSelectItem(0).getExpression()); |
| 91 | + } |
| 92 | + |
| 93 | + private static void roundTrip(PlainSelect select, Dialect dialect) throws Exception { |
| 94 | + StringBuilder out = new StringBuilder(); |
| 95 | + select.accept(new StatementDeParser(out), null); |
| 96 | + assertEquals(select.toString(), out.toString()); |
| 97 | + assertEquals(select.toString(), parse(out.toString(), dialect).toString()); |
| 98 | + } |
| 99 | +} |
0 commit comments