|
| 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.util; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import net.sf.jsqlparser.JSQLParserException; |
| 18 | +import net.sf.jsqlparser.expression.Expression; |
| 19 | +import net.sf.jsqlparser.expression.Function; |
| 20 | +import net.sf.jsqlparser.expression.LongValue; |
| 21 | +import net.sf.jsqlparser.expression.TimeKeyExpression; |
| 22 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 23 | +import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
| 24 | +import net.sf.jsqlparser.schema.Column; |
| 25 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 26 | +import net.sf.jsqlparser.util.TemporalExpressionInfo.Kind; |
| 27 | +import net.sf.jsqlparser.util.deparser.StatementDeParser; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.CsvFileSource; |
| 31 | +import org.junit.jupiter.params.provider.EnumSource; |
| 32 | +import org.junit.jupiter.params.provider.ValueSource; |
| 33 | + |
| 34 | +class TemporalExpressionInfoTest { |
| 35 | + |
| 36 | + // Accepted forms from an execution matrix on MySQL 8.4.11 and PostgreSQL 18.6. |
| 37 | + @ParameterizedTest |
| 38 | + @CsvFileSource(resources = "temporal-expression-cases.tsv", delimiter = '\t') |
| 39 | + void recognizesServerAcceptedFormsWithoutChangingTheirAst(Dialect dialect, String expression, |
| 40 | + Kind kind, Integer precision) throws JSQLParserException { |
| 41 | + PlainSelect select = parse(expression, dialect); |
| 42 | + Expression node = select.getSelectItem(0).getExpression(); |
| 43 | + String original = select.toString(); |
| 44 | + TemporalExpressionInfo info = TemporalExpressionInfo.from(node, dialect).orElseThrow(); |
| 45 | + assertEquals(kind, info.getKind()); |
| 46 | + assertEquals(precision, info.getPrecision()); |
| 47 | + assertEquals(expression.contains("("), info.hasParentheses()); |
| 48 | + assertEquals(expression.split("\\(")[0], info.getName()); |
| 49 | + assertEquals(original, select.toString()); |
| 50 | + |
| 51 | + StringBuilder visitor = new StringBuilder(); |
| 52 | + select.accept(new StatementDeParser(visitor)); |
| 53 | + for (String sql : List.of(select.toString(), visitor.toString())) { |
| 54 | + PlainSelect reparsed = (PlainSelect) CCJSqlParserUtil.parse(sql, |
| 55 | + p -> p.withDialect(dialect)); |
| 56 | + Expression again = reparsed.getSelectItem(0).getExpression(); |
| 57 | + assertEquals(node.getClass(), again.getClass()); |
| 58 | + TemporalExpressionInfo after = |
| 59 | + TemporalExpressionInfo.from(again, dialect).orElseThrow(); |
| 60 | + assertEquals(kind, after.getKind()); |
| 61 | + assertEquals(precision, after.getPrecision()); |
| 62 | + assertEquals(info.hasParentheses(), after.hasParentheses()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void preservesLegacyNodeClassesAndNormalizesTheirMetadata() throws JSQLParserException { |
| 68 | + assertEquals(TimeKeyExpression.class, |
| 69 | + node("CURRENT_TIMESTAMP", Dialect.POSTGRESQL).getClass()); |
| 70 | + assertEquals(Function.class, |
| 71 | + node("CURRENT_TIMESTAMP(6)", Dialect.POSTGRESQL).getClass()); |
| 72 | + assertEquals(Column.class, node("LOCALTIME", Dialect.POSTGRESQL).getClass()); |
| 73 | + assertEquals(Kind.LOCAL_TIME, info("LOCALTIME", Dialect.POSTGRESQL).getKind()); |
| 74 | + assertEquals(Kind.CURRENT_TIMESTAMP, info("LOCALTIME", Dialect.MYSQL).getKind()); |
| 75 | + } |
| 76 | + |
| 77 | + @ParameterizedTest |
| 78 | + @EnumSource(value = Dialect.class, names = {"MYSQL", "POSTGRESQL"}) |
| 79 | + void distinguishesOmittedAndZeroPrecisionAndUnwrapsGrouping(Dialect dialect) |
| 80 | + throws JSQLParserException { |
| 81 | + assertNull(info("CURRENT_TIMESTAMP", dialect).getPrecision()); |
| 82 | + assertEquals(0, info("CURRENT_TIMESTAMP(0)", dialect).getPrecision()); |
| 83 | + assertEquals(6, info("((current_timestamp(6)))", dialect).getPrecision()); |
| 84 | + assertEquals("current_timestamp", info("((current_timestamp(6)))", dialect).getName()); |
| 85 | + } |
| 86 | + |
| 87 | + @ParameterizedTest |
| 88 | + @ValueSource(strings = {"\"LOCALTIME\"", "t.localtime", "t.current_timestamp", |
| 89 | + "app.now()", "\"now\"()", "'CURRENT_TIMESTAMP'", "CURRENT_TIMEZONE", |
| 90 | + "CURRENT_TIMESTAMP + INTERVAL '1' HOUR", "COALESCE(CURRENT_TIMESTAMP, NULL)", |
| 91 | + "CURRENT_TIMESTAMP()", "CURRENT_DATE(1)", "CURRENT_TIMESTAMP('6')", |
| 92 | + "CURRENT_TIMESTAMP(1, 2)", "CURRENT_TIMESTAMP(-1)", "NOW(6)", "CURDATE()"}) |
| 93 | + void doesNotClassifyUnrelatedOrUnsupportedPostgreSqlExpressions(String expression) |
| 94 | + throws JSQLParserException { |
| 95 | + assertTrue(TemporalExpressionInfo.from(node(expression, Dialect.POSTGRESQL), |
| 96 | + Dialect.POSTGRESQL).isEmpty(), expression); |
| 97 | + } |
| 98 | + |
| 99 | + @ParameterizedTest |
| 100 | + @ValueSource(strings = {"`LOCALTIME`", "\"LOCALTIME\"", "t.localtime", "app.now()", |
| 101 | + "'NOW()'", "CURRENT_DATE(6)", "CURRENT_TIMESTAMP(1 + 1)", |
| 102 | + "CURRENT_TIMESTAMP(1.5)", "CURRENT_TIMESTAMP(?)", "CURDATE(1)"}) |
| 103 | + void doesNotClassifyUnrelatedOrUnsupportedMySqlExpressions(String expression) |
| 104 | + throws JSQLParserException { |
| 105 | + assertTrue(TemporalExpressionInfo.from(node(expression, Dialect.MYSQL), |
| 106 | + Dialect.MYSQL).isEmpty(), expression); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void recognizesRequestedPrecisionWithoutApplyingDatabaseRangeRules() |
| 111 | + throws JSQLParserException { |
| 112 | + // PostgreSQL accepts 7 with a warning and clamps to 6; MySQL rejects 7. Metadata retains |
| 113 | + // the requested value, rather than pretending to perform execution-time validation. |
| 114 | + assertEquals(7, info("CURRENT_TIMESTAMP(7)", Dialect.POSTGRESQL).getPrecision()); |
| 115 | + assertEquals(7, info("CURRENT_TIMESTAMP(7)", Dialect.MYSQL).getPrecision()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void returnsSnapshotsAndIgnoresFunctionModifiers() { |
| 120 | + Function function = new Function("CURRENT_TIMESTAMP", new LongValue(6)); |
| 121 | + TemporalExpressionInfo before = TemporalExpressionInfo.from(function, Dialect.MYSQL) |
| 122 | + .orElseThrow(); |
| 123 | + ((LongValue) function.getParameters().get(0)).setValue(0); |
| 124 | + assertEquals(6, before.getPrecision()); |
| 125 | + assertEquals(0, TemporalExpressionInfo.from(function, Dialect.MYSQL) |
| 126 | + .orElseThrow().getPrecision()); |
| 127 | + function.setDistinct(true); |
| 128 | + assertTrue(TemporalExpressionInfo.from(function, Dialect.MYSQL).isEmpty()); |
| 129 | + assertTrue(TemporalExpressionInfo.from(new Column("NOW"), Dialect.POSTGRESQL).isEmpty()); |
| 130 | + assertTrue(TemporalExpressionInfo.from(new Column("CURTIME"), Dialect.MYSQL).isEmpty()); |
| 131 | + assertTrue(TemporalExpressionInfo.from(null, Dialect.MYSQL).isEmpty()); |
| 132 | + assertTrue(TemporalExpressionInfo.from(function, Dialect.ORACLE).isEmpty()); |
| 133 | + } |
| 134 | + |
| 135 | + private static TemporalExpressionInfo info(String expression, Dialect dialect) |
| 136 | + throws JSQLParserException { |
| 137 | + return TemporalExpressionInfo.from(node(expression, dialect), dialect).orElseThrow(); |
| 138 | + } |
| 139 | + |
| 140 | + private static Expression node(String expression, Dialect dialect) throws JSQLParserException { |
| 141 | + return parse(expression, dialect).getSelectItem(0).getExpression(); |
| 142 | + } |
| 143 | + |
| 144 | + private static PlainSelect parse(String expression, Dialect dialect) |
| 145 | + throws JSQLParserException { |
| 146 | + return (PlainSelect) CCJSqlParserUtil.parse("SELECT " + expression, |
| 147 | + p -> p.withDialect(dialect)); |
| 148 | + } |
| 149 | +} |
0 commit comments