From 0d19fb5d60554b9a90fac52918f784e6c3e13918 Mon Sep 17 00:00:00 2001 From: James Romeril Date: Tue, 5 Jul 2022 16:00:54 +1000 Subject: [PATCH] fix(spanner/spansql): Add tests for INSERT parsing (#6303) Fix bug where 'INSERT' statement was generated incorrectly --- spanner/spansql/parser_test.go | 7 +++++++ spanner/spansql/sql.go | 3 ++- spanner/spansql/sql_test.go | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/spanner/spansql/parser_test.go b/spanner/spansql/parser_test.go index d42dcf7427bc..83f827bd7ae3 100644 --- a/spanner/spansql/parser_test.go +++ b/spanner/spansql/parser_test.go @@ -312,6 +312,13 @@ func TestParseDMLStmt(t *testing.T) { Input: Values{{IntegerLiteral(1), StringLiteral("Marc"), StringLiteral("Richards")}}, }, }, + {"INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (1, 'Marc', 'Richards')", + &Insert{ + Table: "Singers", + Columns: []ID{ID("SingerId"), ID("FirstName"), ID("LastName")}, + Input: Values{{IntegerLiteral(1), StringLiteral("Marc"), StringLiteral("Richards")}}, + }, + }, {"INSERT Singers (SingerId, FirstName, LastName) SELECT * FROM UNNEST ([1, 2, 3]) AS data", &Insert{ Table: "Singers", diff --git a/spanner/spansql/sql.go b/spanner/spansql/sql.go index 32e498d1ac6b..1f92deee2458 100644 --- a/spanner/spansql/sql.go +++ b/spanner/spansql/sql.go @@ -157,6 +157,7 @@ func (rrdp ReplaceRowDeletionPolicy) SQL() string { func (drdp DropRowDeletionPolicy) SQL() string { return "DROP ROW DELETION POLICY" } + func (sct SetColumnType) SQL() string { str := sct.Type.SQL() if sct.NotNull { @@ -261,7 +262,7 @@ func (u *Update) SQL() string { } func (i *Insert) SQL() string { - str := "INSERT " + i.Table.SQL() + " INTO (" + str := "INSERT INTO " + i.Table.SQL() + " (" for i, column := range i.Columns { if i > 0 { str += ", " diff --git a/spanner/spansql/sql_test.go b/spanner/spansql/sql_test.go index 45f768592cc9..febbbda63daf 100644 --- a/spanner/spansql/sql_test.go +++ b/spanner/spansql/sql_test.go @@ -411,6 +411,15 @@ func TestSQL(t *testing.T) { "ALTER DATABASE dbname SET OPTIONS (optimizer_version=null, version_retention_period=null, enable_key_visualizer=null)", reparseDDL, }, + { + &Insert{ + Table: "Singers", + Columns: []ID{ID("SingerId"), ID("FirstName"), ID("LastName")}, + Input: Values{{IntegerLiteral(1), StringLiteral("Marc"), StringLiteral("Richards")}}, + }, + `INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (1, "Marc", "Richards")`, + reparseDML, + }, { &Delete{ Table: "Ta",