|
12 | 12 | import net.sf.jsqlparser.JSQLParserException; |
13 | 13 | import net.sf.jsqlparser.expression.Expression; |
14 | 14 | import net.sf.jsqlparser.expression.LongValue; |
| 15 | +import net.sf.jsqlparser.expression.StringValue; |
15 | 16 | import net.sf.jsqlparser.expression.operators.arithmetic.Addition; |
16 | 17 | import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication; |
17 | 18 | import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList; |
|
20 | 21 | import net.sf.jsqlparser.statement.Statements; |
21 | 22 | import net.sf.jsqlparser.statement.UnsupportedStatement; |
22 | 23 | import net.sf.jsqlparser.statement.select.PlainSelect; |
| 24 | +import net.sf.jsqlparser.statement.select.Select; |
23 | 25 | import net.sf.jsqlparser.statement.select.TableStatement; |
24 | 26 | import net.sf.jsqlparser.test.MemoryLeakVerifier; |
25 | 27 | import net.sf.jsqlparser.test.TestUtils; |
|
44 | 46 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
45 | 47 | import static org.junit.jupiter.api.Assertions.assertNull; |
46 | 48 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 49 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
47 | 50 |
|
48 | 51 | public class CCJSqlParserUtilTest { |
49 | 52 |
|
@@ -611,4 +614,45 @@ public void testDialectPresetSwitchOverrides() throws Exception { |
611 | 614 | .withDialect(AbstractJSqlParser.Dialect.MYSQL)) |
612 | 615 | .toString()); |
613 | 616 | } |
| 617 | + |
| 618 | + @Test |
| 619 | + public void testDoubleQuotedStringsFeature() throws Exception { |
| 620 | + // default (flag off): double quotes are quoted identifiers, unchanged |
| 621 | + PlainSelect select = (PlainSelect) ((Select) CCJSqlParserUtil |
| 622 | + .parse("SELECT \"not an identifier\"")).getSelectBody(); |
| 623 | + assertTrue(select.getSelectItems().get(0).getExpression() instanceof Column); |
| 624 | + // on: the BigQuery/Spark/MySQL-default reading, double quotes are |
| 625 | + // string literals, the quote kept on round-trip |
| 626 | + select = (PlainSelect) ((Select) CCJSqlParserUtil.parse("SELECT \"not an identifier\"", |
| 627 | + p -> p.withDoubleQuotedStrings(true))).getSelectBody(); |
| 628 | + Expression expression = select.getSelectItems().get(0).getExpression(); |
| 629 | + assertTrue(expression instanceof StringValue); |
| 630 | + assertEquals("not an identifier", ((StringValue) expression).getValue()); |
| 631 | + assertEquals("SELECT \"not an identifier\"", select.toString()); |
| 632 | + // empty string and doubled quotes take the single-quote treatment |
| 633 | + assertEquals("SELECT \"\"", CCJSqlParserUtil |
| 634 | + .parse("SELECT \"\"", p -> p.withDoubleQuotedStrings(true)).toString()); |
| 635 | + assertEquals("SELECT \"a\"\"b\"", CCJSqlParserUtil |
| 636 | + .parse("SELECT \"a\"\"b\"", p -> p.withDoubleQuotedStrings(true)).toString()); |
| 637 | + // identifier positions: a `"..."` token follows the existing |
| 638 | + // string-as-table branch (`FROM 'file.csv'`), the same leniency |
| 639 | + // single quotes already have; MySQL itself would error here |
| 640 | + assertEquals("SELECT * FROM \"t\"", CCJSqlParserUtil |
| 641 | + .parse("SELECT * FROM \"t\"", p -> p.withDoubleQuotedStrings(true)).toString()); |
| 642 | + } |
| 643 | + |
| 644 | + @Test |
| 645 | + public void testDoubleQuotedStringsPreset() throws Exception { |
| 646 | + // MYSQL and MARIADB presets carry the switch (default sql_mode reading) |
| 647 | + for (AbstractJSqlParser.Dialect dialect : new AbstractJSqlParser.Dialect[] { |
| 648 | + AbstractJSqlParser.Dialect.MYSQL, AbstractJSqlParser.Dialect.MARIADB}) { |
| 649 | + PlainSelect select = (PlainSelect) ((Select) CCJSqlParserUtil |
| 650 | + .parse("SELECT \"abc\"", p -> p.withDialect(dialect))).getSelectBody(); |
| 651 | + assertTrue(select.getSelectItems().get(0).getExpression() instanceof StringValue); |
| 652 | + } |
| 653 | + // the identifier-default dialects keep the quoted identifier reading |
| 654 | + PlainSelect select = (PlainSelect) ((Select) CCJSqlParserUtil.parse("SELECT \"abc\"", |
| 655 | + p -> p.withDialect(AbstractJSqlParser.Dialect.SQLSERVER))).getSelectBody(); |
| 656 | + assertTrue(select.getSelectItems().get(0).getExpression() instanceof Column); |
| 657 | + } |
614 | 658 | } |
0 commit comments