Skip to content

Commit

Permalink
QuoteWrap columns in many-to-many eager join (volatiletech#1094)
Browse files Browse the repository at this point in the history

* Fixed issue with quote generation

Co-authored-by: Bryan McGrane <bmcgrane@sensory.com>
  • Loading branch information
bryanmcgrane and Bryan McGrane authored Feb 20, 2022
1 parent 73a4ee0 commit 12dcb95
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 5 additions & 3 deletions boilingcore/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion templates/main/09_relationship_to_many_eager.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
Expand Down
14 changes: 14 additions & 0 deletions testdata/psql_test_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
);

0 comments on commit 12dcb95

Please sign in to comment.