forked from googleapis/java-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: generalize skip methods (googleapis#2949)
Generalize the various skip methods so these can be used for both dialects. Each dialect implements a number of abstract methods to indicate what type of statements and constructs they support. These methods are used by the generalized skip methods to determine the start and end of literals, identifiers, and comments. This is step 2 of the refactor that is needed to share more of the code between the SpannerStatementParser and PostgreSQLStatementParser.
- Loading branch information
Showing
5 changed files
with
276 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...spanner/src/test/java/com/google/cloud/spanner/connection/SpannerStatementParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.spanner.connection; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.google.cloud.spanner.Dialect; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class SpannerStatementParserTest { | ||
|
||
static String skip(String sql) { | ||
return skip(sql, 0); | ||
} | ||
|
||
static String skip(String sql, int currentIndex) { | ||
int position = | ||
AbstractStatementParser.getInstance(Dialect.GOOGLE_STANDARD_SQL) | ||
.skip(sql, currentIndex, null); | ||
return sql.substring(currentIndex, position); | ||
} | ||
|
||
@Test | ||
public void testSkip() { | ||
assertEquals("", skip("")); | ||
assertEquals("1", skip("1 ")); | ||
assertEquals("1", skip("12 ")); | ||
assertEquals("2", skip("12 ", 1)); | ||
assertEquals("", skip("12", 2)); | ||
|
||
assertEquals("'foo'", skip("'foo' ", 0)); | ||
assertEquals("'foo'", skip("'foo''bar' ", 0)); | ||
assertEquals("'foo'", skip("'foo' 'bar' ", 0)); | ||
assertEquals("'bar'", skip("'foo''bar' ", 5)); | ||
assertEquals("'foo\"bar\"'", skip("'foo\"bar\"' ", 0)); | ||
assertEquals("\"foo'bar'\"", skip("\"foo'bar'\" ", 0)); | ||
assertEquals("`foo'bar'`", skip("`foo'bar'` ", 0)); | ||
|
||
assertEquals("'''foo'bar'''", skip("'''foo'bar''' ", 0)); | ||
assertEquals("'''foo\\'bar'''", skip("'''foo\\'bar''' ", 0)); | ||
assertEquals("'''foo\\'\\'bar'''", skip("'''foo\\'\\'bar''' ", 0)); | ||
assertEquals("'''foo\\'\\'\\'bar'''", skip("'''foo\\'\\'\\'bar''' ", 0)); | ||
assertEquals("\"\"\"foo'bar\"\"\"", skip("\"\"\"foo'bar\"\"\"", 0)); | ||
assertEquals("```foo'bar```", skip("```foo'bar```", 0)); | ||
|
||
assertEquals("-- comment\n", skip("-- comment\nselect * from foo", 0)); | ||
assertEquals("# comment\n", skip("# comment\nselect * from foo", 0)); | ||
assertEquals("/* comment */", skip("/* comment */ select * from foo", 0)); | ||
assertEquals( | ||
"/* comment /* GoogleSQL does not support nested comments */", | ||
skip("/* comment /* GoogleSQL does not support nested comments */ select * from foo", 0)); | ||
// GoogleSQL does not support dollar-quoted strings. | ||
assertEquals("$", skip("$tag$not a string$tag$ select * from foo", 0)); | ||
|
||
assertEquals("/* 'test' */", skip("/* 'test' */ foo")); | ||
assertEquals("-- 'test' \n", skip("-- 'test' \n foo")); | ||
assertEquals("'/* test */'", skip("'/* test */' foo")); | ||
|
||
// Raw strings do not consider '\' as something that starts an escape sequence, but any | ||
// quote character following it is still preserved within the string, as the definition of a | ||
// raw string says that 'both characters are preserved'. | ||
assertEquals("'foo\\''", skip("'foo\\'' ", 0)); | ||
assertEquals("'foo\\''", skip("r'foo\\'' ", 1)); | ||
assertEquals("'''foo\\'\\'\\'bar'''", skip("'''foo\\'\\'\\'bar''' ", 0)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters