Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: escape single quote with backslash #95

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ dependencies {
implementation("com.google.cloud:google-cloud-spanner-jdbc")

// Liquibase Core - needed for testing and docker container
implementation("org.liquibase:liquibase-core:4.2.0")
implementation("org.liquibase:liquibase-core:4.3.2")
implementation("org.yaml:snakeyaml:1.26")
implementation("org.apache.commons:commons-lang3:3.11")

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<liquibase.version>4.3.1</liquibase.version>
<liquibase.version>4.3.2</liquibase.version>
<snakeyaml.version>1.26</snakeyaml.version>

<gax-grpc.version>1.60.0</gax-grpc.version>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/liquibase/ext/spanner/CloudSpanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,9 @@ public boolean supportsDDLInTransaction() {
public boolean supportsPrimaryKeyNames() {
return false;
}

@Override
public String escapeStringForDatabase(String string) {
return string == null ? null : string.replace("'", "\\'");
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/google/spanner/liquibase/LiquibaseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,49 @@ void doLoadDataTest(TestHarness.Connection testHarness) throws Exception {
}
}

@Test
void doEmulatorLoadDataWithSingleQuotesTest() throws Exception {
doLoadDataWithSingleQuotesTest(getSpannerEmulator());
}

@Test
@Tag("integration")
void doRealSpannerLoadDataWithSingleQuotesTest() throws Exception {
doLoadDataWithSingleQuotesTest(getSpannerReal());
}

void doLoadDataWithSingleQuotesTest(TestHarness.Connection testHarness) throws Exception {
try (Connection con = DriverManager.getConnection(testHarness.getConnectionUrl())) {
try (Statement statement = con.createStatement()) {
statement.execute("START BATCH DDL");
statement.execute("CREATE TABLE TableWithEscapedStringData (" + "Id INT64,"
+ "ColString STRING(100),"
+ ") PRIMARY KEY (Id)");
statement.execute("RUN BATCH");
try (Liquibase liquibase =
getLiquibase(testHarness, "load-data-with-single-quotes.spanner.yaml")) {
liquibase.update(new Contexts("test"));

try (ResultSet rs = statement
.executeQuery("SELECT * FROM TableWithEscapedStringData ORDER BY Id")) {
int index = 0;
while (rs.next()) {
index++;
assertThat(rs.getLong("Id")).isEqualTo(index);
assertThat(rs.getString("ColString"))
.isEqualTo("Shouldn't have an issue inserting this as row " + index);
}
assertThat(index).isEqualTo(3);
}
} finally {
statement.execute("START BATCH DDL");
statement.execute("DROP TABLE TableWithEscapedStringData");
statement.execute("RUN BATCH");
}
}
}
}

@Test
void doEmulatorLoadUpdateDataTest() throws Exception {
doLoadUpdateDataTest(getSpannerEmulator());
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/data-with-single-quotes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Id;ColString
1;"Shouldn't have an issue inserting this as row 1"
2;"Shouldn't have an issue inserting this as row 2"
3;"Shouldn't have an issue inserting this as row 3"
40 changes: 40 additions & 0 deletions src/test/resources/load-data-with-single-quotes.spanner.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2020 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
#
# https://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.


databaseChangeLog:
- preConditions:
onFail: HALT
onError: HALT
- changeSet:
id: v0.1-load-data-with-single-quotes
author: spanner-liquibase-tests
changes:
- loadData:
tableName: TableWithEscapedStringData
usePreparedStatements: false
separator: ;
relativeToChangelogFile: true
file: data-with-single-quotes.csv
encoding: UTF-8
quotchar: '"'
columns:
- column:
header: Id
name: Id
type: NUMERIC
- column:
header: ColString
name: ColString
type: STRING