From 12dcb95ddb82aa4ce9b1cf4d11a329dd11fae515 Mon Sep 17 00:00:00 2001 From: bryanmcgrane Date: Sat, 19 Feb 2022 19:49:24 -0700 Subject: [PATCH] QuoteWrap columns in many-to-many eager join (#1094) * Fixed issue with quote generation Co-authored-by: Bryan McGrane --- boilingcore/templates.go | 8 +++++--- .../main/09_relationship_to_many_eager.go.tpl | 2 +- testdata/psql_test_schema.sql | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/boilingcore/templates.go b/boilingcore/templates.go index f4e497944..9787e5819 100644 --- a/boilingcore/templates.go +++ b/boilingcore/templates.go @@ -248,6 +248,7 @@ func (o once) Put(s string) bool { var templateStringMappers = map[string]func(string) string{ // String ops "quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) }, + "safeQuoteWrap": func(a string) string { return fmt.Sprintf(`\"%s\"`, a) }, "replaceReserved": strmangle.ReplaceReservedWords, // Casing @@ -262,9 +263,10 @@ var goVarnameReplacer = strings.NewReplacer("[", "_", "]", "_", ".", "_") // add a function pointer here. var templateFunctions = template.FuncMap{ // String ops - "quoteWrap": func(s string) string { return fmt.Sprintf(`"%s"`, s) }, - "id": strmangle.Identifier, - "goVarname": goVarnameReplacer.Replace, + "quoteWrap": func(s string) string { return fmt.Sprintf(`"%s"`, s) }, + "safeQuoteWrap": func(a string) string { return fmt.Sprintf(`\"%s\"`, a) }, + "id": strmangle.Identifier, + "goVarname": goVarnameReplacer.Replace, // Pluralization "singular": strmangle.Singular, diff --git a/templates/main/09_relationship_to_many_eager.go.tpl b/templates/main/09_relationship_to_many_eager.go.tpl index fab8a1544..b4eb9a65a 100644 --- a/templates/main/09_relationship_to_many_eager.go.tpl +++ b/templates/main/09_relationship_to_many_eager.go.tpl @@ -57,7 +57,7 @@ func ({{$ltable.DownSingular}}L) Load{{$relAlias.Local}}({{if $.NoContext}}e boi {{- $schemaJoinTable := .JoinTable | $.SchemaTable -}} {{- $foreignTable := getTable $.Tables .ForeignTable -}} query := NewQuery( - qm.Select("{{$foreignTable.Columns | columnNames | prefixStringSlice (print $schemaForeignTable ".") | join ", "}}, {{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}}"), + qm.Select("{{$foreignTable.Columns | columnNames | stringMap $.StringFuncs.safeQuoteWrap | prefixStringSlice (print $schemaForeignTable ".") | join ", "}}, {{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}}"), qm.From("{{$schemaForeignTable}}"), qm.InnerJoin("{{$schemaJoinTable}} as {{id 0 | $.Quotes}} on {{$schemaForeignTable}}.{{.ForeignColumn | $.Quotes}} = {{id 0 | $.Quotes}}.{{.JoinForeignColumn | $.Quotes}}"), qm.WhereIn("{{id 0 | $.Quotes}}.{{.JoinLocalColumn | $.Quotes}} in ?", args...), diff --git a/testdata/psql_test_schema.sql b/testdata/psql_test_schema.sql index 85e1365f2..232776dbb 100644 --- a/testdata/psql_test_schema.sql +++ b/testdata/psql_test_schema.sql @@ -2,6 +2,7 @@ CREATE EXTENSION IF NOT EXISTS citext; CREATE TYPE workday AS ENUM('monday', 'tuesday', 'wednesday', 'thursday', 'friday'); CREATE TYPE faceyface AS ENUM('angry', 'hungry', 'bitter'); +CREATE TYPE "UserRole" AS ENUM ('SUPER_ADMIN', 'BILLING_ADMIN', 'READ_ONLY_ADMIN', 'USER'); CREATE TABLE event_one ( id serial PRIMARY KEY NOT NULL, @@ -441,3 +442,16 @@ ALTER TABLE pilot_languages ADD CONSTRAINT languages_fkey FOREIGN KEY (language_ CREATE TABLE updates ( id integer PRIMARY KEY NOT NULL ); + +-- Create table that has a name with an uppercase letter and columns with uppercase letters +CREATE TABLE "User" ( + "id" UUID NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + "userId" VARCHAR(127) NOT NULL, + "role" "UserRole" NOT NULL DEFAULT E'USER', + "tenantId" UUID NOT NULL, + "clientId" UUID, + + PRIMARY KEY ("id") +);