|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2019 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.parser; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | +import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed; |
| 14 | +import net.sf.jsqlparser.parser.AbstractJSqlParser.Dialect; |
| 15 | +import net.sf.jsqlparser.statement.ConnectionDefinition; |
| 16 | +import net.sf.jsqlparser.statement.CloudConnectionDefinition; |
| 17 | +import net.sf.jsqlparser.statement.export.DBMSDestination; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.ValueSource; |
| 20 | + |
| 21 | +class LookaheadCleanupTest { |
| 22 | + @ParameterizedTest |
| 23 | + @ValueSource(strings = {"EXA AT conn TABLE t", "ORA AT conn TABLE schemaName.tableName", |
| 24 | + "JDBC DRIVER = 'driverName' AT conn TABLE \"t\"", |
| 25 | + "EXA AT conn TABLE t (c1, c2) REPLACE TRUNCATE"}) |
| 26 | + void dispatchesTableDestinationsWithCompleteConsumption(String sql) throws Exception { |
| 27 | + CCJSqlParser parser = CCJSqlParserUtil.newParser(sql); |
| 28 | + DBMSDestination destination = parser.DBMSDestination(); |
| 29 | + assertNotNull(destination.getTable()); |
| 30 | + assertNull(destination.getStatement()); |
| 31 | + assertNotNull(destination.getConnectionDefinition()); |
| 32 | + assertEquals(CCJSqlParserConstants.EOF, parser.getToken(1).kind); |
| 33 | + assertSqlCanBeParsedAndDeparsed("EXPORT sourceTable INTO " + sql, true, |
| 34 | + p -> p.withDialect(Dialect.EXASOL)); |
| 35 | + } |
| 36 | + |
| 37 | + @ParameterizedTest |
| 38 | + @ValueSource(strings = {"EXA AT conn STATEMENT 'insert into t values (?)'", |
| 39 | + "ORA AT conn STATEMENT 'insert into t values (?)'"}) |
| 40 | + void preservesStatementDestinations(String sql) throws Exception { |
| 41 | + CCJSqlParser parser = CCJSqlParserUtil.newParser(sql); |
| 42 | + DBMSDestination destination = parser.DBMSDestination(); |
| 43 | + assertNull(destination.getTable()); |
| 44 | + assertEquals("insert into t values (?)", destination.getStatement().getValue()); |
| 45 | + assertEquals(CCJSqlParserConstants.EOF, parser.getToken(1).kind); |
| 46 | + assertSqlCanBeParsedAndDeparsed("EXPORT sourceTable INTO " + sql, true, |
| 47 | + p -> p.withDialect(Dialect.EXASOL)); |
| 48 | + } |
| 49 | + |
| 50 | + @ParameterizedTest |
| 51 | + @ValueSource(strings = {"EXA AT conn TABLE", "ORA AT conn STATEMENT", "EXA AT conn SELECT t"}) |
| 52 | + void rejectsIncompleteAndWrongDestinationPrefixes(String sql) { |
| 53 | + CCJSqlParser parser = CCJSqlParserUtil.newParser(sql); |
| 54 | + assertThrows(ParseException.class, parser::DBMSDestination); |
| 55 | + if (sql.endsWith("TABLE")) { |
| 56 | + assertEquals(CCJSqlParserConstants.K_TABLE, parser.getToken(0).kind); |
| 57 | + assertEquals(CCJSqlParserConstants.EOF, parser.getToken(1).kind); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest |
| 62 | + @ValueSource(strings = {"AT conn", "AT '127.0.0.1'", "AT CLOUD NONE conn", |
| 63 | + "AT CLOUD AZURE BLOBSTORAGE conn"}) |
| 64 | + void preservesCloudAndOrdinaryConnections(String sql) throws Exception { |
| 65 | + CCJSqlParser parser = CCJSqlParserUtil.newParser(sql); |
| 66 | + ConnectionDefinition connection = parser.ConnectionOrCloudConnectionDefinition(); |
| 67 | + if (sql.startsWith("AT CLOUD")) { |
| 68 | + assertInstanceOf(CloudConnectionDefinition.class, connection); |
| 69 | + } else { |
| 70 | + assertEquals(ConnectionDefinition.class, connection.getClass()); |
| 71 | + } |
| 72 | + assertEquals(CCJSqlParserConstants.EOF, parser.getToken(1).kind); |
| 73 | + assertSqlCanBeParsedAndDeparsed("EXPORT sourceTable INTO CSV " + sql + " FILE 'file.csv'", |
| 74 | + true, p -> p.withDialect(Dialect.EXASOL)); |
| 75 | + } |
| 76 | +} |
0 commit comments