From 1c8a9d2e398dea22c6e6083f08fcf235834f6710 Mon Sep 17 00:00:00 2001 From: Patrick O'brien Date: Fri, 9 Sep 2016 07:23:10 +1000 Subject: [PATCH] Add schema feature to everything (except rels) * Add strmangle SchemaTable helper --- README.md | 2 +- bdb/drivers/mock.go | 8 +++--- bdb/drivers/postgres.go | 30 ++++++++++---------- bdb/interface.go | 22 +++++++------- bdb/table.go | 7 +++-- config.go | 1 + main.go | 2 ++ sqlboiler.go | 8 ++++-- strmangle/strmangle.go | 12 ++++++++ templates.go | 2 ++ templates/04_relationship_to_one.tpl | 5 ++-- templates/05_relationship_to_many.tpl | 6 ++-- templates/06_relationship_to_one_eager.tpl | 7 +++-- templates/07_relationship_to_many_eager.tpl | 4 +-- templates/09_relationship_to_many_setops.tpl | 8 +++--- templates/10_all.tpl | 2 +- templates/11_find.tpl | 2 +- templates/12_insert.tpl | 4 +-- templates/13_update.tpl | 4 +-- templates/15_delete.tpl | 4 +-- templates/16_reload.tpl | 2 +- templates/17_exists.tpl | 2 +- templates_test/relationship_to_many.tpl | 4 +-- 23 files changed, 88 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index 485fc25..eb6576a 100644 --- a/README.md +++ b/README.md @@ -1065,7 +1065,7 @@ fake schemas (like MySQL). If you'd like to run the benchmarks yourself check out our [boilbench](https://github.com/vattle/boilbench) repo. -Here are the results **(lower is better)**: +Here are the results (lower is better): `go test -bench . -benchmem` ``` diff --git a/bdb/drivers/mock.go b/bdb/drivers/mock.go index 93d18cc..4371e6f 100644 --- a/bdb/drivers/mock.go +++ b/bdb/drivers/mock.go @@ -9,7 +9,7 @@ import ( type MockDriver struct{} // TableNames returns a list of mock table names -func (m *MockDriver) TableNames(whitelist, exclude []string) ([]string, error) { +func (m *MockDriver) TableNames(schema string, whitelist, exclude []string) ([]string, error) { if len(whitelist) > 0 { return whitelist, nil } @@ -18,7 +18,7 @@ func (m *MockDriver) TableNames(whitelist, exclude []string) ([]string, error) { } // Columns returns a list of mock columns -func (m *MockDriver) Columns(tableName string) ([]bdb.Column, error) { +func (m *MockDriver) Columns(schema, tableName string) ([]bdb.Column, error) { return map[string][]bdb.Column{ "pilots": { {Name: "id", Type: "int", DBType: "integer"}, @@ -59,7 +59,7 @@ func (m *MockDriver) Columns(tableName string) ([]bdb.Column, error) { } // ForeignKeyInfo returns a list of mock foreignkeys -func (m *MockDriver) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) { +func (m *MockDriver) ForeignKeyInfo(schema, tableName string) ([]bdb.ForeignKey, error) { return map[string][]bdb.ForeignKey{ "jets": { {Table: "jets", Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", ForeignColumnUnique: true}, @@ -82,7 +82,7 @@ func (m *MockDriver) TranslateColumnType(c bdb.Column) bdb.Column { } // PrimaryKeyInfo returns mock primary key info for the passed in table name -func (m *MockDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) { +func (m *MockDriver) PrimaryKeyInfo(schema, tableName string) (*bdb.PrimaryKey, error) { return map[string]*bdb.PrimaryKey{ "pilots": { Name: "pilot_id_pkey", diff --git a/bdb/drivers/postgres.go b/bdb/drivers/postgres.go index be69816..dace79e 100644 --- a/bdb/drivers/postgres.go +++ b/bdb/drivers/postgres.go @@ -81,11 +81,11 @@ func (p *PostgresDriver) UseLastInsertID() bool { // TableNames connects to the postgres database and // retrieves all table names from the information_schema where the -// table schema is public. It uses a whitelist and exclude list. -func (p *PostgresDriver) TableNames(whitelist, exclude []string) ([]string, error) { +// table schema is schema. It uses a whitelist and exclude list. +func (p *PostgresDriver) TableNames(schema string, whitelist, exclude []string) ([]string, error) { var names []string - query := `select table_name from information_schema.tables where table_schema = 'public'` + query := fmt.Sprintf(`select table_name from information_schema.tables where table_schema = '%s'`, schema) if len(whitelist) > 0 { query = query + fmt.Sprintf("and table_name in ('%s');", strings.Join(whitelist, "','")) } else if len(exclude) > 0 { @@ -114,7 +114,7 @@ func (p *PostgresDriver) TableNames(whitelist, exclude []string) ([]string, erro // from the database information_schema.columns. It retrieves the column names // and column types and returns those as a []Column after TranslateColumnType() // converts the SQL types to Go types, for example: "varchar" to "string" -func (p *PostgresDriver) Columns(tableName string) ([]bdb.Column, error) { +func (p *PostgresDriver) Columns(schema, tableName string) ([]bdb.Column, error) { var columns []bdb.Column rows, err := p.dbConn.Query(` @@ -132,11 +132,11 @@ func (p *PostgresDriver) Columns(tableName string) ([]bdb.Column, error) { inner join pg_index pgi on pgi.indexrelid = pgc.oid inner join pg_attribute pga on pga.attrelid = pgi.indrelid and pga.attnum = ANY(pgi.indkey) where - pgix.schemaname = 'public' and pgix.tablename = c.table_name and pga.attname = c.column_name and pgi.indisunique = true + pgix.schemaname = $1 and pgix.tablename = c.table_name and pga.attname = c.column_name and pgi.indisunique = true )) as is_unique from information_schema.columns as c - where table_name=$1 and table_schema = 'public'; - `, tableName) + where table_name=$2 and table_schema = $3; + `, schema, tableName, schema) if err != nil { return nil, err @@ -172,16 +172,16 @@ func (p *PostgresDriver) Columns(tableName string) ([]bdb.Column, error) { } // PrimaryKeyInfo looks up the primary key for a table. -func (p *PostgresDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) { +func (p *PostgresDriver) PrimaryKeyInfo(schema, tableName string) (*bdb.PrimaryKey, error) { pkey := &bdb.PrimaryKey{} var err error query := ` select tc.constraint_name from information_schema.table_constraints as tc - where tc.table_name = $1 and tc.constraint_type = 'PRIMARY KEY' and tc.table_schema = 'public';` + where tc.table_name = $1 and tc.constraint_type = 'PRIMARY KEY' and tc.table_schema = $2;` - row := p.dbConn.QueryRow(query, tableName) + row := p.dbConn.QueryRow(query, tableName, schema) if err = row.Scan(&pkey.Name); err != nil { if err == sql.ErrNoRows { return nil, nil @@ -192,10 +192,10 @@ func (p *PostgresDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, erro queryColumns := ` select kcu.column_name from information_schema.key_column_usage as kcu - where constraint_name = $1 and table_schema = 'public';` + where constraint_name = $1 and table_schema = $2;` var rows *sql.Rows - if rows, err = p.dbConn.Query(queryColumns, pkey.Name); err != nil { + if rows, err = p.dbConn.Query(queryColumns, pkey.Name, schema); err != nil { return nil, err } defer rows.Close() @@ -222,7 +222,7 @@ func (p *PostgresDriver) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, erro } // ForeignKeyInfo retrieves the foreign keys for a given table name. -func (p *PostgresDriver) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) { +func (p *PostgresDriver) ForeignKeyInfo(schema, tableName string) ([]bdb.ForeignKey, error) { var fkeys []bdb.ForeignKey query := ` @@ -235,11 +235,11 @@ func (p *PostgresDriver) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, err from information_schema.table_constraints as tc inner join information_schema.key_column_usage as kcu ON tc.constraint_name = kcu.constraint_name inner join information_schema.constraint_column_usage as ccu ON tc.constraint_name = ccu.constraint_name - where tc.table_name = $1 and tc.constraint_type = 'FOREIGN KEY' and tc.table_schema = 'public';` + where tc.table_name = $1 and tc.constraint_type = 'FOREIGN KEY' and tc.table_schema = $2;` var rows *sql.Rows var err error - if rows, err = p.dbConn.Query(query, tableName); err != nil { + if rows, err = p.dbConn.Query(query, tableName, schema); err != nil { return nil, err } diff --git a/bdb/interface.go b/bdb/interface.go index b59dbe1..a7f14e0 100644 --- a/bdb/interface.go +++ b/bdb/interface.go @@ -6,10 +6,10 @@ import "github.com/pkg/errors" // Interface for a database driver. Functionality required to support a specific // database type (eg, MySQL, Postgres etc.) type Interface interface { - TableNames(whitelist, exclude []string) ([]string, error) - Columns(tableName string) ([]Column, error) - PrimaryKeyInfo(tableName string) (*PrimaryKey, error) - ForeignKeyInfo(tableName string) ([]ForeignKey, error) + TableNames(schema string, whitelist, exclude []string) ([]string, error) + Columns(schema, tableName string) ([]Column, error) + PrimaryKeyInfo(schema, tableName string) (*PrimaryKey, error) + ForeignKeyInfo(schema, tableName string) ([]ForeignKey, error) // TranslateColumnType takes a Database column type and returns a go column type. TranslateColumnType(Column) Column @@ -26,19 +26,21 @@ type Interface interface { // Tables returns the metadata for all tables, minus the tables // specified in the exclude slice. -func Tables(db Interface, whitelist, exclude []string) ([]Table, error) { +func Tables(db Interface, schema string, whitelist, exclude []string) ([]Table, error) { var err error - names, err := db.TableNames(whitelist, exclude) + names, err := db.TableNames(schema, whitelist, exclude) if err != nil { return nil, errors.Wrap(err, "unable to get table names") } var tables []Table for _, name := range names { - t := Table{Name: name} + t := Table{ + Name: name, + } - if t.Columns, err = db.Columns(name); err != nil { + if t.Columns, err = db.Columns(schema, name); err != nil { return nil, errors.Wrapf(err, "unable to fetch table column info (%s)", name) } @@ -46,11 +48,11 @@ func Tables(db Interface, whitelist, exclude []string) ([]Table, error) { t.Columns[i] = db.TranslateColumnType(c) } - if t.PKey, err = db.PrimaryKeyInfo(name); err != nil { + if t.PKey, err = db.PrimaryKeyInfo(schema, name); err != nil { return nil, errors.Wrapf(err, "unable to fetch table pkey info (%s)", name) } - if t.FKeys, err = db.ForeignKeyInfo(name); err != nil { + if t.FKeys, err = db.ForeignKeyInfo(schema, name); err != nil { return nil, errors.Wrapf(err, "unable to fetch table fkey info (%s)", name) } diff --git a/bdb/table.go b/bdb/table.go index 82e872d..28fa624 100644 --- a/bdb/table.go +++ b/bdb/table.go @@ -4,8 +4,11 @@ import "fmt" // Table metadata from the database schema. type Table struct { - Name string - Columns []Column + Name string + // For dbs with real schemas, like Postgres. + // Example value: "schema_name"."table_name" + SchemaName string + Columns []Column PKey *PrimaryKey FKeys []ForeignKey diff --git a/config.go b/config.go index 662747d..2c5a2b3 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main // Config for the running of the commands type Config struct { DriverName string + Schema string PkgName string OutFolder string BaseDir string diff --git a/main.go b/main.go index 058e617..2b79923 100644 --- a/main.go +++ b/main.go @@ -61,6 +61,7 @@ func main() { // Set up the cobra root command flags rootCmd.PersistentFlags().StringP("output", "o", "models", "The name of the folder to output to") + rootCmd.PersistentFlags().StringP("schema", "s", "public", "The name of your database schema, for databases that support real schemas") rootCmd.PersistentFlags().StringP("pkgname", "p", "models", "The name you wish to assign to your generated package") rootCmd.PersistentFlags().StringP("basedir", "b", "", "The base directory has the templates and templates_test folders") rootCmd.PersistentFlags().StringSliceP("exclude", "x", nil, "Tables to be excluded from the generated package") @@ -108,6 +109,7 @@ func preRun(cmd *cobra.Command, args []string) error { cmdConfig = &Config{ DriverName: driverName, OutFolder: viper.GetString("output"), + Schema: viper.GetString("schema"), PkgName: viper.GetString("pkgname"), Debug: viper.GetBool("debug"), NoTests: viper.GetBool("no-tests"), diff --git a/sqlboiler.go b/sqlboiler.go index fcecb0d..0757c3c 100644 --- a/sqlboiler.go +++ b/sqlboiler.go @@ -59,7 +59,7 @@ func New(config *Config) (*State, error) { return nil, errors.Wrap(err, "unable to connect to the database") } - err = s.initTables(config.WhitelistTables, config.ExcludeTables) + err = s.initTables(config.Schema, config.WhitelistTables, config.ExcludeTables) if err != nil { return nil, errors.Wrap(err, "unable to initialize tables") } @@ -96,6 +96,7 @@ func New(config *Config) (*State, error) { func (s *State) Run(includeTests bool) error { singletonData := &templateData{ Tables: s.Tables, + Schema: s.Config.Schema, DriverName: s.Config.DriverName, UseLastInsertID: s.Driver.UseLastInsertID(), PkgName: s.Config.PkgName, @@ -127,6 +128,7 @@ func (s *State) Run(includeTests bool) error { data := &templateData{ Tables: s.Tables, Table: table, + Schema: s.Config.Schema, DriverName: s.Config.DriverName, UseLastInsertID: s.Driver.UseLastInsertID(), PkgName: s.Config.PkgName, @@ -239,9 +241,9 @@ func (s *State) initDriver(driverName string) error { } // initTables retrieves all "public" schema table names from the database. -func (s *State) initTables(whitelist, exclude []string) error { +func (s *State) initTables(schema string, whitelist, exclude []string) error { var err error - s.Tables, err = bdb.Tables(s.Driver, whitelist, exclude) + s.Tables, err = bdb.Tables(s.Driver, schema, whitelist, exclude) if err != nil { return errors.Wrap(err, "unable to fetch table data") } diff --git a/strmangle/strmangle.go b/strmangle/strmangle.go index 86cab33..5bda771 100644 --- a/strmangle/strmangle.go +++ b/strmangle/strmangle.go @@ -34,6 +34,18 @@ func init() { boilRuleset = newBoilRuleset() } +// SchemaTable returns a table name with a schema prefixed if +// using a database that supports real schemas, for example, +// for Postgres: "schema_name"."table_name", versus +// simply "table_name" for MySQL (because it does not support real schemas) +func SchemaTable(driver string, schema string, table string) string { + if driver == "postgres" { + return fmt.Sprintf(`"%s"."%s"`, schema, table) + } + + return fmt.Sprintf(`"%s"`, table) +} + // IdentQuote attempts to quote simple identifiers in SQL tatements func IdentQuote(s string) string { if strings.ToLower(s) == "null" { diff --git a/templates.go b/templates.go index e760711..8d171f2 100644 --- a/templates.go +++ b/templates.go @@ -15,6 +15,7 @@ import ( type templateData struct { Tables []bdb.Table Table bdb.Table + Schema string DriverName string UseLastInsertID bool PkgName string @@ -141,6 +142,7 @@ var templateFunctions = template.FuncMap{ // Database related mangling "whereClause": strmangle.WhereClause, + "schemaTable": strmangle.SchemaTable, // Text helpers "textsFromForeignKey": textsFromForeignKey, diff --git a/templates/04_relationship_to_one.tpl b/templates/04_relationship_to_one.tpl index 5081f39..9b75a26 100644 --- a/templates/04_relationship_to_one.tpl +++ b/templates/04_relationship_to_one.tpl @@ -14,12 +14,13 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(exec bo queryMods = append(queryMods, mods...) query := {{.ForeignTable.NamePluralGo}}(exec, queryMods...) - boil.SetFrom(query.Query, "{{.ForeignTable.Name}}") + boil.SetFrom(query.Query, `{{schemaTable .DriverName .Schema .ForeignTable.Name}}`) return query } +{{end -}}{{/* end define */}} -{{end -}} +{{/* Begin execution of template for one-to-one relationship. */}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- $dot := . -}} diff --git a/templates/05_relationship_to_many.tpl b/templates/05_relationship_to_many.tpl index e886258..dd08d32 100644 --- a/templates/05_relationship_to_many.tpl +++ b/templates/05_relationship_to_many.tpl @@ -5,8 +5,10 @@ {{- range .Table.ToManyRelationships -}} {{- $varNameSingular := .ForeignTable | singular | camelCase -}} {{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}} +{{- /* Begin execution of template for many-to-one relationship. */ -}} {{- template "relationship_to_one_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}} {{- else -}} +{{- /* Begin execution of template for many-to-many relationship. */ -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}} // {{$rel.Function.Name}}G retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. @@ -27,7 +29,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na {{if .ToJoinTable -}} queryMods = append(queryMods, - qm.InnerJoin(`"{{.JoinTable}}" as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}"`), + qm.InnerJoin(`{{schemaTable $dot.DriverName $dot.Schema .JoinTable}} as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}"`), qm.Where(`"{{id 1}}"."{{.JoinLocalColumn}}"=$1`, {{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}), ) {{else -}} @@ -37,7 +39,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na {{end}} query := {{$rel.ForeignTable.NamePluralGo}}(exec, queryMods...) - boil.SetFrom(query.Query, `"{{.ForeignTable}}" as "{{id 0}}"`) + boil.SetFrom(query.Query, `{{schemaTable $dot.DriverName $dot.Schema .ForeignTable}} as "{{id 0}}"`) return query } diff --git a/templates/06_relationship_to_one_eager.tpl b/templates/06_relationship_to_one_eager.tpl index 8d6a514..e7bcef7 100644 --- a/templates/06_relationship_to_one_eager.tpl +++ b/templates/06_relationship_to_one_eager.tpl @@ -28,7 +28,7 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo } query := fmt.Sprintf( - `select * from "{{.ForeignKey.ForeignTable}}" where "{{.ForeignKey.ForeignColumn}}" in (%s)`, + `select * from {{schemaTable .DriverName .Schema .ForeignKey.ForeignTable}} where "{{.ForeignKey.ForeignColumn}}" in (%s)`, strmangle.Placeholders(count, 1, 1), ) @@ -79,8 +79,9 @@ func ({{$varNameSingular}}L) Load{{.Function.Name}}(e boil.Executor, singular bo return nil } - {{- end -}} -{{end -}} + {{- end -}}{{- /* end with */ -}} +{{end -}}{{- /* end define */ -}} + {{- if .Table.IsJoinTable -}} {{- else -}} {{- $dot := . -}} diff --git a/templates/07_relationship_to_many_eager.tpl b/templates/07_relationship_to_many_eager.tpl index b8276c6..3d9dea7 100644 --- a/templates/07_relationship_to_many_eager.tpl +++ b/templates/07_relationship_to_many_eager.tpl @@ -35,12 +35,12 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula {{if .ToJoinTable -}} query := fmt.Sprintf( - `select "{{id 0}}".*, "{{id 1}}"."{{.JoinLocalColumn}}" from "{{.ForeignTable}}" as "{{id 0}}" inner join "{{.JoinTable}}" as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}" in (%s)`, + `select "{{id 0}}".*, "{{id 1}}"."{{.JoinLocalColumn}}" from {{schemaTable .DriverName .Schema .ForeignTable}} as "{{id 0}}" inner join {{schemaTable .DriverName .Schema .JoinTable}} as "{{id 1}}" on "{{id 0}}"."{{.ForeignColumn}}" = "{{id 1}}"."{{.JoinForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}" in (%s)`, strmangle.Placeholders(count, 1, 1), ) {{else -}} query := fmt.Sprintf( - `select * from "{{.ForeignTable}}" where "{{.ForeignColumn}}" in (%s)`, + `select * from {{schemaTable $dot.DriverName $dot.Schema .ForeignTable}} where "{{.ForeignColumn}}" in (%s)`, strmangle.Placeholders(count, 1, 1), ) {{end -}} diff --git a/templates/09_relationship_to_many_setops.tpl b/templates/09_relationship_to_many_setops.tpl index d2d2922..860387a 100644 --- a/templates/09_relationship_to_many_setops.tpl +++ b/templates/09_relationship_to_many_setops.tpl @@ -37,7 +37,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function {{if .ToJoinTable -}} for _, rel := range related { - query := `insert into "{{.JoinTable}}" ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)` + query := `insert into {{schemaTable .DriverName .Schema .JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)` values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}, rel.{{$rel.ForeignTable.ColumnNameGo}}} if boil.DebugMode { @@ -94,10 +94,10 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Add{{$rel.Function // Sets related.R.{{$rel.Function.ForeignName}}'s {{$rel.Function.Name}} accordingly. func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Set{{$rel.Function.Name}}(exec boil.Executor, insert bool, related ...*{{$rel.ForeignTable.NameGo}}) error { {{if .ToJoinTable -}} - query := `delete from "{{.JoinTable}}" where "{{.JoinLocalColumn}}" = $1` + query := `delete from {{schemaTable $dot.DriverName $dot.Schema .JoinTable}} where "{{.JoinLocalColumn}}" = $1` values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}} {{else -}} - query := `update "{{.ForeignTable}}" set "{{.ForeignColumn}}" = null where "{{.ForeignColumn}}" = $1` + query := `update {{schemaTable $dot.DriverName $dot.Schema .ForeignTable}} set "{{.ForeignColumn}}" = null where "{{.ForeignColumn}}" = $1` values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}} {{end -}} if boil.DebugMode { @@ -138,7 +138,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) Remove{{$rel.Funct var err error {{if .ToJoinTable -}} query := fmt.Sprintf( - `delete from "{{.JoinTable}}" where "{{.JoinLocalColumn}}" = $1 and "{{.JoinForeignColumn}}" in (%s)`, + `delete from {{schemaTable .DriverName .Schema .JoinTable}} where "{{.JoinLocalColumn}}" = $1 and "{{.JoinForeignColumn}}" in (%s)`, strmangle.Placeholders(len(related), 1, 1), ) values := []interface{}{{"{"}}{{$rel.Function.Receiver}}.{{$rel.LocalTable.ColumnNameGo}}} diff --git a/templates/10_all.tpl b/templates/10_all.tpl index a9470b1..0148925 100644 --- a/templates/10_all.tpl +++ b/templates/10_all.tpl @@ -8,6 +8,6 @@ func {{$tableNamePlural}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query { // {{$tableNamePlural}} retrieves all the records using an executor. func {{$tableNamePlural}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query { - mods = append(mods, qm.From("{{.Table.Name}}")) + mods = append(mods, qm.From(`{{schemaTable .DriverName .Schema .Table.Name}}`)) return {{$varNameSingular}}Query{NewQuery(exec, mods...)} } diff --git a/templates/11_find.tpl b/templates/11_find.tpl index 987afcf..6cbe974 100644 --- a/templates/11_find.tpl +++ b/templates/11_find.tpl @@ -29,7 +29,7 @@ func Find{{$tableNameSingular}}(exec boil.Executor, {{$pkArgs}}, selectCols ...s sel = strings.Join(strmangle.IdentQuoteSlice(selectCols), ",") } query := fmt.Sprintf( - `select %s from "{{.Table.Name}}" where {{whereClause 1 .Table.PKey.Columns}}`, sel, + `select %s from {{schemaTable .DriverName .Schema .Table.Name}} where {{whereClause 1 .Table.PKey.Columns}}`, sel, ) q := boil.SQL(exec, query, {{$pkNames | join ", "}}) diff --git a/templates/12_insert.tpl b/templates/12_insert.tpl index aa7958d..4300124 100644 --- a/templates/12_insert.tpl +++ b/templates/12_insert.tpl @@ -64,11 +64,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string if err != nil { return err } - cache.query = fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), strmangle.Placeholders(len(wl), 1, 1)) + cache.query = fmt.Sprintf(`INSERT INTO {{schemaTable .DriverName .Schema .Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), strmangle.Placeholders(len(wl), 1, 1)) if len(cache.retMapping) != 0 { {{if .UseLastInsertID -}} - cache.retQuery = fmt.Sprintf(`SELECT %s FROM {{.Table.Name}} WHERE %s`, strings.Join(returnColumns, `","`), strmangle.WhereClause(1, {{$varNameSingular}}PrimaryKeyColumns)) + cache.retQuery = fmt.Sprintf(`SELECT %s FROM {{schemaTable .DriverName .Schema .Table.Name}} WHERE %s`, strings.Join(returnColumns, `","`), strmangle.WhereClause(1, {{$varNameSingular}}PrimaryKeyColumns)) {{else -}} cache.query += fmt.Sprintf(` RETURNING %s`, strings.Join(returnColumns, ",")) {{end -}} diff --git a/templates/13_update.tpl b/templates/13_update.tpl index 8483d11..ef623ac 100644 --- a/templates/13_update.tpl +++ b/templates/13_update.tpl @@ -52,7 +52,7 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string if !cached { wl := strmangle.UpdateColumnSet({{$varNameSingular}}Columns, {{$varNameSingular}}PrimaryKeyColumns, whitelist) - cache.query = fmt.Sprintf(`UPDATE "{{.Table.Name}}" SET %s WHERE %s`, strmangle.SetParamNames(wl), strmangle.WhereClause(len(wl)+1, {{$varNameSingular}}PrimaryKeyColumns)) + cache.query = fmt.Sprintf(`UPDATE {{schemaTable .DriverName .Schema .Table.Name}} SET %s WHERE %s`, strmangle.SetParamNames(wl), strmangle.WhereClause(len(wl)+1, {{$varNameSingular}}PrimaryKeyColumns)) cache.valueMapping, err = boil.BindMapping({{$varNameSingular}}Type, {{$varNameSingular}}Mapping, append(wl, {{$varNameSingular}}PrimaryKeyColumns...)) if err != nil { return err @@ -155,7 +155,7 @@ func (o {{$tableNameSingular}}Slice) UpdateAll(exec boil.Executor, cols M) error args = append(args, o.inPrimaryKeyArgs()...) sql := fmt.Sprintf( - `UPDATE {{.Table.Name}} SET (%s) = (%s) WHERE (%s) IN (%s)`, + `UPDATE {{schemaTable .DriverName .Schema .Table.Name}} SET (%s) = (%s) WHERE (%s) IN (%s)`, strings.Join(colNames, ", "), strmangle.Placeholders(len(colNames), 1, 1), strings.Join(strmangle.IdentQuoteSlice({{$varNameSingular}}PrimaryKeyColumns), ","), diff --git a/templates/15_delete.tpl b/templates/15_delete.tpl index a3c8b51..ebcc744 100644 --- a/templates/15_delete.tpl +++ b/templates/15_delete.tpl @@ -43,7 +43,7 @@ func (o *{{$tableNameSingular}}) Delete(exec boil.Executor) error { args := o.inPrimaryKeyArgs() - sql := `DELETE FROM {{.Table.Name}} WHERE {{whereClause 1 .Table.PKey.Columns}}` + sql := `DELETE FROM {{schemaTable .DriverName .Schema .Table.Name}} WHERE {{whereClause 1 .Table.PKey.Columns}}` if boil.DebugMode { fmt.Fprintln(boil.DebugWriter, sql) @@ -132,7 +132,7 @@ func (o {{$tableNameSingular}}Slice) DeleteAll(exec boil.Executor) error { args := o.inPrimaryKeyArgs() sql := fmt.Sprintf( - `DELETE FROM {{.Table.Name}} WHERE (%s) IN (%s)`, + `DELETE FROM {{schemaTable .DriverName .Schema .Table.Name}} WHERE (%s) IN (%s)`, strings.Join(strmangle.IdentQuoteSlice({{$varNameSingular}}PrimaryKeyColumns), ","), strmangle.Placeholders(len(o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)), ) diff --git a/templates/16_reload.tpl b/templates/16_reload.tpl index 38e8306..f2d18a9 100644 --- a/templates/16_reload.tpl +++ b/templates/16_reload.tpl @@ -67,7 +67,7 @@ func (o *{{$tableNameSingular}}Slice) ReloadAll(exec boil.Executor) error { args := o.inPrimaryKeyArgs() sql := fmt.Sprintf( - `SELECT {{.Table.Name}}.* FROM {{.Table.Name}} WHERE (%s) IN (%s)`, + `SELECT {{schemaTable .DriverName .Schema .Table.Name}}.* FROM {{schemaTable .DriverName .Schema .Table.Name}} WHERE (%s) IN (%s)`, strings.Join(strmangle.IdentQuoteSlice({{$varNameSingular}}PrimaryKeyColumns), ","), strmangle.Placeholders(len(*o) * len({{$varNameSingular}}PrimaryKeyColumns), 1, len({{$varNameSingular}}PrimaryKeyColumns)), ) diff --git a/templates/17_exists.tpl b/templates/17_exists.tpl index 899bec0..68b9729 100644 --- a/templates/17_exists.tpl +++ b/templates/17_exists.tpl @@ -6,7 +6,7 @@ func {{$tableNameSingular}}Exists(exec boil.Executor, {{$pkArgs}}) (bool, error) { var exists bool - sql := `select exists(select 1 from "{{.Table.Name}}" where {{whereClause 1 .Table.PKey.Columns}} limit 1)` + sql := `select exists(select 1 from {{schemaTable .DriverName .Schema .Table.Name}} where {{whereClause 1 .Table.PKey.Columns}} limit 1)` if boil.DebugMode { fmt.Fprintln(boil.DebugWriter, sql) diff --git a/templates_test/relationship_to_many.tpl b/templates_test/relationship_to_many.tpl index cb9ee36..bf74cef 100644 --- a/templates_test/relationship_to_many.tpl +++ b/templates_test/relationship_to_many.tpl @@ -41,11 +41,11 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { } {{if .ToJoinTable -}} - _, err = tx.Exec(`insert into "{{.JoinTable}}" ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{$rel.LocalTable.ColumnNameGo}}, b.{{$rel.ForeignTable.ColumnNameGo}}) + _, err = tx.Exec(`insert into "{{schemaTable .DriverName .Schema .JoinTable}}" ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{$rel.LocalTable.ColumnNameGo}}, b.{{$rel.ForeignTable.ColumnNameGo}}) if err != nil { t.Fatal(err) } - _, err = tx.Exec(`insert into "{{.JoinTable}}" ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{$rel.LocalTable.ColumnNameGo}}, c.{{$rel.ForeignTable.ColumnNameGo}}) + _, err = tx.Exec(`insert into "{{schemaTable .DriverName .Schema .JoinTable}}" ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{$rel.LocalTable.ColumnNameGo}}, c.{{$rel.ForeignTable.ColumnNameGo}}) if err != nil { t.Fatal(err) }