From b375733dfa6cc82bc0111fbf5ec2c16fb35a0dc6 Mon Sep 17 00:00:00 2001 From: go-jet Date: Sun, 21 Mar 2021 17:17:44 +0100 Subject: [PATCH] Add schema rename support Using SchemaFrom("schemaName") it is possible to set SQL builder table to point to a different schema. --- generator/internal/template/templates.go | 46 ++++++---------- mysql/table.go | 4 +- mysql/utils_test.go | 34 ++---------- postgres/dialect_test.go | 8 +-- postgres/table.go | 4 +- postgres/utils_test.go | 25 ++------- tests/init/init.go | 2 + tests/mysql/alltypes_test.go | 7 ++- tests/mysql/generator_test.go | 34 +++++++----- tests/mysql/select_test.go | 43 +++++++++++++++ tests/mysql/update_test.go | 3 +- tests/postgres/chinook_db_test.go | 68 +++++++++++++++++++++-- tests/postgres/generator_test.go | 69 +++++++++++++----------- tests/postgres/sample_test.go | 2 +- tests/postgres/scan_test.go | 3 +- tests/postgres/select_test.go | 5 +- tests/testdata | 2 +- 17 files changed, 206 insertions(+), 153 deletions(-) diff --git a/generator/internal/template/templates.go b/generator/internal/template/templates.go index 5288ef4..186ade0 100644 --- a/generator/internal/template/templates.go +++ b/generator/internal/template/templates.go @@ -23,7 +23,7 @@ import ( "github.com/go-jet/jet/v2/{{dialect.PackageName}}" ) -var {{ToGoIdentifier .Name}} = new{{.GoStructName}}() +var {{ToGoIdentifier .Name}} = new{{.GoStructName}}("{{.SchemaName}}", "{{.Name}}", "") type {{.GoStructName}} struct { {{dialect.PackageName}}.Table @@ -38,22 +38,16 @@ type {{.GoStructName}} struct { } // AS creates new {{.GoStructName}} with assigned alias -func (a *{{.GoStructName}}) AS(alias string) {{.GoStructName}} { - aliasTable := new{{.GoStructName}}() - aliasTable.Table.AS(alias) - aliasTable.Table.Schema(a.Table.SchemaName()) - return aliasTable +func (a {{.GoStructName}}) AS(alias string) {{.GoStructName}} { + return new{{.GoStructName}}(a.SchemaName(), a.TableName(), alias) } // Schema creates new {{.GoStructName}} with assigned schema name -func (a *{{.GoStructName}}) Schema(schemaName string) {{.GoStructName}} { - aliasTable := new{{.GoStructName}}() - aliasTable.Table.AS(a.Table.Alias()) - aliasTable.Table.Schema(schemaName) - return aliasTable +func (a {{.GoStructName}}) FromSchema(schemaName string) {{.GoStructName}} { + return new{{.GoStructName}}(schemaName, a.TableName(), a.Alias()) } -func new{{.GoStructName}}() {{.GoStructName}} { +func new{{.GoStructName}}(schemaName, tableName, alias string) {{.GoStructName}} { var ( {{- range .Columns}} {{ToGoIdentifier .Name}}Column = {{dialect.PackageName}}.{{.SqlBuilderColumnType}}Column("{{.Name}}") @@ -63,7 +57,7 @@ func new{{.GoStructName}}() {{.GoStructName}} { ) return {{.GoStructName}}{ - Table: {{dialect.PackageName}}.NewTable("{{.SchemaName}}", "{{.Name}}", allColumns...), + Table: {{dialect.PackageName}}.NewTable(schemaName, tableName, alias, allColumns...), //Columns {{- range .Columns}} @@ -89,7 +83,7 @@ import ( "github.com/go-jet/jet/v2/{{dialect.PackageName}}" ) -var {{ToGoIdentifier .Name}} = new{{.GoStructName}}() +var {{ToGoIdentifier .Name}} = new{{.GoStructName}}("{{.SchemaName}}", "{{.Name}}", "") type {{.GoStructImplName}} struct { {{dialect.PackageName}}.Table @@ -110,29 +104,23 @@ type {{.GoStructName}} struct { } // AS creates new {{.GoStructName}} with assigned alias -func (a *{{.GoStructName}}) AS(alias string) *{{.GoStructName}} { - aliasTable := new{{.GoStructName}}() - aliasTable.Table.AS(alias) - aliasTable.Table.Schema(a.Table.SchemaName()) - return aliasTable +func (a {{.GoStructName}}) AS(alias string) *{{.GoStructName}} { + return new{{.GoStructName}}(a.SchemaName(), a.TableName(), alias) } // Schema creates new {{.GoStructName}} with assigned schema name -func (a *{{.GoStructName}}) Schema(schemaName string) *{{.GoStructName}} { - aliasTable := new{{.GoStructName}}() - aliasTable.Table.AS(a.Table.Alias()) - aliasTable.Table.Schema(schemaName) - return aliasTable +func (a {{.GoStructName}}) FromSchema(schemaName string) *{{.GoStructName}} { + return new{{.GoStructName}}(schemaName, a.TableName(), a.Alias()) } -func new{{.GoStructName}}() *{{.GoStructName}} { +func new{{.GoStructName}}(schemaName, tableName, alias string) *{{.GoStructName}} { return &{{.GoStructName}}{ - {{.GoStructImplName}}: new{{.GoStructName}}Impl("{{.SchemaName}}", "{{.Name}}"), - EXCLUDED: new{{.GoStructName}}Impl("", "excluded"), + {{.GoStructImplName}}: new{{.GoStructName}}Impl(schemaName, tableName, alias), + EXCLUDED: new{{.GoStructName}}Impl("", "excluded", ""), } } -func new{{.GoStructName}}Impl(schemaName, tableName string) {{.GoStructImplName}} { +func new{{.GoStructName}}Impl(schemaName, tableName, alias string) {{.GoStructImplName}} { var ( {{- range .Columns}} {{ToGoIdentifier .Name}}Column = {{dialect.PackageName}}.{{.SqlBuilderColumnType}}Column("{{.Name}}") @@ -142,7 +130,7 @@ func new{{.GoStructName}}Impl(schemaName, tableName string) {{.GoStructImplName} ) return {{.GoStructImplName}}{ - Table: {{dialect.PackageName}}.NewTable(schemaName, tableName, allColumns...), + Table: {{dialect.PackageName}}.NewTable(schemaName, tableName, alias, allColumns...), //Columns {{- range .Columns}} diff --git a/mysql/table.go b/mysql/table.go index 5630966..0ae7ee8 100644 --- a/mysql/table.go +++ b/mysql/table.go @@ -77,9 +77,9 @@ func (r readableTableInterfaceImpl) CROSS_JOIN(table ReadableTable) joinSelectUp } // NewTable creates new table with schema Name, table Name and list of columns -func NewTable(schemaName, name string, columns ...jet.ColumnExpression) Table { +func NewTable(schemaName, name, alias string, columns ...jet.ColumnExpression) Table { t := &tableImpl{ - SerializerTable: jet.NewTable(schemaName, name, columns...), + SerializerTable: jet.NewTable(schemaName, name, alias, columns...), } t.readableTableInterfaceImpl.parent = t diff --git a/mysql/utils_test.go b/mysql/utils_test.go index cc8b3c3..d95638a 100644 --- a/mysql/utils_test.go +++ b/mysql/utils_test.go @@ -16,19 +16,7 @@ var table1ColTimestamp = TimestampColumn("col_timestamp") var table1ColDate = DateColumn("col_date") var table1ColTime = TimeColumn("col_time") -var table1 = NewTable( - "db", - "table1", - table1Col1, - table1ColInt, - table1ColFloat, - table1ColString, - table1Col3, - table1ColBool, - table1ColDate, - table1ColTimestamp, - table1ColTime, -) +var table1 = NewTable("db", "table1", "", table1Col1, table1ColInt, table1ColFloat, table1ColString, table1Col3, table1ColBool, table1ColDate, table1ColTimestamp, table1ColTime) var table2Col3 = IntegerColumn("col3") var table2Col4 = IntegerColumn("col4") @@ -39,28 +27,12 @@ var table2ColBool = BoolColumn("col_bool") var table2ColTimestamp = TimestampColumn("col_timestamp") var table2ColDate = DateColumn("col_date") -var table2 = NewTable( - "db", - "table2", - table2Col3, - table2Col4, - table2ColInt, - table2ColFloat, - table2ColStr, - table2ColBool, - table2ColDate, - table2ColTimestamp, -) +var table2 = NewTable("db", "table2", "", table2Col3, table2Col4, table2ColInt, table2ColFloat, table2ColStr, table2ColBool, table2ColDate, table2ColTimestamp) var table3Col1 = IntegerColumn("col1") var table3ColInt = IntegerColumn("col_int") var table3StrCol = StringColumn("col2") -var table3 = NewTable( - "db", - "table3", - table3Col1, - table3ColInt, - table3StrCol) +var table3 = NewTable("db", "table3", "", table3Col1, table3ColInt, table3StrCol) func assertSerialize(t *testing.T, clause jet.Serializer, query string, args ...interface{}) { testutils.AssertSerialize(t, Dialect, clause, query, args...) diff --git a/postgres/dialect_test.go b/postgres/dialect_test.go index 7bf9242..9b7b3d1 100644 --- a/postgres/dialect_test.go +++ b/postgres/dialect_test.go @@ -80,13 +80,7 @@ func TestReservedWordEscaped(t *testing.T) { var table1ColVariadic = IntervalColumn("VARIADIC") var table1ColProcedure = IntervalColumn("procedure") - _ = NewTable( - "db", - "table1", - table1ColUser, - table1ColVariadic, - table1ColProcedure, - ) + _ = NewTable("db", "table1", "", table1ColUser, table1ColVariadic, table1ColProcedure) assertSerialize(t, table1ColUser, `table1."user"`) assertSerialize(t, table1ColVariadic, `table1."VARIADIC"`) diff --git a/postgres/table.go b/postgres/table.go index ac153d3..f90c114 100644 --- a/postgres/table.go +++ b/postgres/table.go @@ -109,10 +109,10 @@ type tableImpl struct { } // NewTable creates new table with schema Name, table Name and list of columns -func NewTable(schemaName, name string, columns ...jet.ColumnExpression) Table { +func NewTable(schemaName, name, alias string, columns ...jet.ColumnExpression) Table { t := &tableImpl{ - SerializerTable: jet.NewTable(schemaName, name, columns...), + SerializerTable: jet.NewTable(schemaName, name, alias, columns...), } t.readableTableInterfaceImpl.parent = t diff --git a/postgres/utils_test.go b/postgres/utils_test.go index ef5f72e..bd59b43 100644 --- a/postgres/utils_test.go +++ b/postgres/utils_test.go @@ -21,6 +21,7 @@ var table1ColInterval = IntervalColumn("col_interval") var table1 = NewTable( "db", "table1", + "", table1Col1, table1ColInt, table1ColFloat, @@ -46,32 +47,12 @@ var table2ColTimestampz = TimestampzColumn("col_timestampz") var table2ColDate = DateColumn("col_date") var table2ColInterval = IntervalColumn("col_interval") -var table2 = NewTable( - "db", - "table2", - table2Col3, - table2Col4, - table2ColInt, - table2ColFloat, - table2ColStr, - table2ColBool, - table2ColTime, - table2ColTimez, - table2ColDate, - table2ColTimestamp, - table2ColTimestampz, - table2ColInterval, -) +var table2 = NewTable("db", "table2", "", table2Col3, table2Col4, table2ColInt, table2ColFloat, table2ColStr, table2ColBool, table2ColTime, table2ColTimez, table2ColDate, table2ColTimestamp, table2ColTimestampz, table2ColInterval) var table3Col1 = IntegerColumn("col1") var table3ColInt = IntegerColumn("col_int") var table3StrCol = StringColumn("col2") -var table3 = NewTable( - "db", - "table3", - table3Col1, - table3ColInt, - table3StrCol) +var table3 = NewTable("db", "table3", "", table3Col1, table3ColInt, table3StrCol) func assertSerialize(t *testing.T, serializer jet.Serializer, query string, args ...interface{}) { testutils.AssertSerialize(t, Dialect, serializer, query, args...) diff --git a/tests/init/init.go b/tests/init/init.go index 356806d..a2f6eb3 100644 --- a/tests/init/init.go +++ b/tests/init/init.go @@ -46,6 +46,7 @@ func initMySQLDB() { mySQLDBs := []string{ "dvds", + "dvds2", "test_sample", } @@ -89,6 +90,7 @@ func initPostgresDB() { "dvds", "test_sample", "chinook", + "chinook2", "northwind", } diff --git a/tests/mysql/alltypes_test.go b/tests/mysql/alltypes_test.go index 5728725..6fbb136 100644 --- a/tests/mysql/alltypes_test.go +++ b/tests/mysql/alltypes_test.go @@ -1,7 +1,6 @@ package mysql import ( - "fmt" "github.com/stretchr/testify/require" "strings" "testing" @@ -974,7 +973,7 @@ func TestAllTypesInsert(t *testing.T) { stmt := AllTypes.INSERT(AllTypes.AllColumns). MODEL(toInsert) - fmt.Println(stmt.DebugSql()) + //fmt.Println(stmt.DebugSql()) testutils.AssertExec(t, stmt, tx, 1) @@ -1028,7 +1027,7 @@ func TestAllTypesInsertOnDuplicateKeyUpdate(t *testing.T) { AllTypes.Date.SET(DateT(time.Now())), ) - fmt.Println(stmt.DebugSql()) + //fmt.Println(stmt.DebugSql()) _, err = stmt.Exec(tx) require.NoError(t, err) @@ -1257,7 +1256,7 @@ FROM test_sample.user; err := stmt.Query(db, &dest) require.NoError(t, err) - testutils.PrintJson(dest) + //testutils.PrintJson(dest) testutils.AssertJSON(t, dest, ` [ diff --git a/tests/mysql/generator_test.go b/tests/mysql/generator_test.go index 1e9a51d..c9dcc1a 100644 --- a/tests/mysql/generator_test.go +++ b/tests/mysql/generator_test.go @@ -135,7 +135,7 @@ import ( "github.com/go-jet/jet/v2/mysql" ) -var Actor = newActorTable() +var Actor = newActorTable("dvds", "actor", "") type ActorTable struct { mysql.Table @@ -151,13 +151,16 @@ type ActorTable struct { } // AS creates new ActorTable with assigned alias -func (a *ActorTable) AS(alias string) ActorTable { - aliasTable := newActorTable() - aliasTable.Table.AS(alias) - return aliasTable +func (a ActorTable) AS(alias string) ActorTable { + return newActorTable(a.SchemaName(), a.TableName(), alias) } -func newActorTable() ActorTable { +// Schema creates new ActorTable with assigned schema name +func (a ActorTable) FromSchema(schemaName string) ActorTable { + return newActorTable(schemaName, a.TableName(), a.Alias()) +} + +func newActorTable(schemaName, tableName, alias string) ActorTable { var ( ActorIDColumn = mysql.IntegerColumn("actor_id") FirstNameColumn = mysql.StringColumn("first_name") @@ -168,7 +171,7 @@ func newActorTable() ActorTable { ) return ActorTable{ - Table: mysql.NewTable("dvds", "actor", allColumns...), + Table: mysql.NewTable(schemaName, tableName, alias, allColumns...), //Columns ActorID: ActorIDColumn, @@ -218,7 +221,7 @@ import ( "github.com/go-jet/jet/v2/mysql" ) -var ActorInfo = newActorInfoTable() +var ActorInfo = newActorInfoTable("dvds", "actor_info", "") type ActorInfoTable struct { mysql.Table @@ -234,13 +237,16 @@ type ActorInfoTable struct { } // AS creates new ActorInfoTable with assigned alias -func (a *ActorInfoTable) AS(alias string) ActorInfoTable { - aliasTable := newActorInfoTable() - aliasTable.Table.AS(alias) - return aliasTable +func (a ActorInfoTable) AS(alias string) ActorInfoTable { + return newActorInfoTable(a.SchemaName(), a.TableName(), alias) } -func newActorInfoTable() ActorInfoTable { +// Schema creates new ActorInfoTable with assigned schema name +func (a ActorInfoTable) FromSchema(schemaName string) ActorInfoTable { + return newActorInfoTable(schemaName, a.TableName(), a.Alias()) +} + +func newActorInfoTable(schemaName, tableName, alias string) ActorInfoTable { var ( ActorIDColumn = mysql.IntegerColumn("actor_id") FirstNameColumn = mysql.StringColumn("first_name") @@ -251,7 +257,7 @@ func newActorInfoTable() ActorInfoTable { ) return ActorInfoTable{ - Table: mysql.NewTable("dvds", "actor_info", allColumns...), + Table: mysql.NewTable(schemaName, tableName, alias, allColumns...), //Columns ActorID: ActorIDColumn, diff --git a/tests/mysql/select_test.go b/tests/mysql/select_test.go index dc6af0d..f447218 100644 --- a/tests/mysql/select_test.go +++ b/tests/mysql/select_test.go @@ -744,3 +744,46 @@ LIMIT 3; require.Equal(t, len(dest), 3) } + +func Test_SchemaRename(t *testing.T) { + Film := Film.FromSchema("dvds2") + Language := Language.FromSchema("dvds2") + + stmt := SELECT( + Film.FilmID, + Film.Title, + Language.LanguageID, + Language.Name, + ).FROM( + Language. + INNER_JOIN(Film, Film.LanguageID.EQ(Language.LanguageID)), + ).WHERE( + Language.LanguageID.EQ(Int(1)), + ).ORDER_BY( + Language.LanguageID, Film.FilmID, + ).LIMIT(5) + + testutils.AssertDebugStatementSql(t, stmt, ` +SELECT film.film_id AS "film.film_id", + film.title AS "film.title", + language.language_id AS "language.language_id", + language.name AS "language.name" +FROM dvds2.language + INNER JOIN dvds2.film ON (film.language_id = language.language_id) +WHERE language.language_id = 1 +ORDER BY language.language_id, film.film_id +LIMIT 5; +`) + + dest := struct { + model.Language + Films []model.Film + }{} + + err := stmt.Query(db, &dest) + require.NoError(t, err) + require.Len(t, dest.Films, 5) + require.Equal(t, dest.Films[0].Title, "ACADEMY DINOSAUR") + require.Equal(t, dest.Films[1].Title, "ACE GOLDFINGER") + require.Equal(t, dest.Films[4].Title, "AFRICAN EGG") +} diff --git a/tests/mysql/update_test.go b/tests/mysql/update_test.go index bef22f9..3b6aa75 100644 --- a/tests/mysql/update_test.go +++ b/tests/mysql/update_test.go @@ -2,7 +2,6 @@ package mysql import ( "context" - "fmt" "github.com/go-jet/jet/v2/internal/testutils" . "github.com/go-jet/jet/v2/mysql" "github.com/go-jet/jet/v2/tests/.gentestdata/mysql/dvds/table" @@ -193,7 +192,7 @@ SET url = 'http://www.duckduckgo.com', description = NULL WHERE link.id = 201; ` - fmt.Println(stmt.DebugSql()) + //fmt.Println(stmt.DebugSql()) testutils.AssertDebugStatementSql(t, stmt, expectedSQL, "http://www.duckduckgo.com", "DuckDuckGo", nil, int64(201)) testutils.AssertExec(t, stmt, db) diff --git a/tests/postgres/chinook_db_test.go b/tests/postgres/chinook_db_test.go index 50f5e60..553f920 100644 --- a/tests/postgres/chinook_db_test.go +++ b/tests/postgres/chinook_db_test.go @@ -2,7 +2,6 @@ package postgres import ( "context" - "fmt" "github.com/go-jet/jet/v2/internal/testutils" . "github.com/go-jet/jet/v2/postgres" "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/chinook/model" @@ -17,7 +16,7 @@ func TestSelect(t *testing.T) { SELECT(Album.AllColumns). ORDER_BY(Album.AlbumId.ASC()) - fmt.Println(stmt.DebugSql()) + //fmt.Println(stmt.DebugSql()) testutils.AssertDebugStatementSql(t, stmt, ` SELECT "Album"."AlbumId" AS "Album.AlbumId", @@ -330,8 +329,71 @@ ORDER BY "first10Artist"."Artist.ArtistId"; err := stmt.Query(db, &dest) require.NoError(t, err) +} - //spew.Dump(dest) +func Test_SchemaRename(t *testing.T) { + + Artist2 := Artist.FromSchema("chinook2") + Album2 := Album.FromSchema("chinook2") + + first10Artist := Artist2. + SELECT(Artist2.AllColumns). + ORDER_BY(Artist2.ArtistId). + LIMIT(10). + AsTable("first10Artist") + + artistID := Artist2.ArtistId.From(first10Artist) + + first10Albums := Album2. + SELECT(Album2.AllColumns). + ORDER_BY(Album2.AlbumId). + LIMIT(10). + AsTable("first10Albums") + + albumArtistID := Album2.ArtistId.From(first10Albums) + + stmt := SELECT(first10Artist.AllColumns(), first10Albums.AllColumns()). + FROM(first10Artist. + INNER_JOIN(first10Albums, artistID.EQ(albumArtistID))). + ORDER_BY(artistID) + + testutils.AssertDebugStatementSql(t, stmt, ` +SELECT "first10Artist"."Artist.ArtistId" AS "Artist.ArtistId", + "first10Artist"."Artist.Name" AS "Artist.Name", + "first10Albums"."Album.AlbumId" AS "Album.AlbumId", + "first10Albums"."Album.Title" AS "Album.Title", + "first10Albums"."Album.ArtistId" AS "Album.ArtistId" +FROM ( + SELECT "Artist"."ArtistId" AS "Artist.ArtistId", + "Artist"."Name" AS "Artist.Name" + FROM chinook2."Artist" + ORDER BY "Artist"."ArtistId" + LIMIT 10 + ) AS "first10Artist" + INNER JOIN ( + SELECT "Album"."AlbumId" AS "Album.AlbumId", + "Album"."Title" AS "Album.Title", + "Album"."ArtistId" AS "Album.ArtistId" + FROM chinook2."Album" + ORDER BY "Album"."AlbumId" + LIMIT 10 + ) AS "first10Albums" ON ("first10Artist"."Artist.ArtistId" = "first10Albums"."Album.ArtistId") +ORDER BY "first10Artist"."Artist.ArtistId"; +`) + + var dest []struct { + model.Artist + + Album []model.Album + } + + err := stmt.Query(db, &dest) + require.NoError(t, err) + + require.Len(t, dest, 2) + require.Equal(t, *dest[0].Artist.Name, "Apocalyptica") + require.Len(t, dest[0].Album, 1) + require.Equal(t, dest[0].Album[0].Title, "Plays Metallica By Four Cellos") } var album1 = model.Album{ diff --git a/tests/postgres/generator_test.go b/tests/postgres/generator_test.go index 1e23016..833e2a4 100644 --- a/tests/postgres/generator_test.go +++ b/tests/postgres/generator_test.go @@ -168,7 +168,7 @@ import ( "github.com/go-jet/jet/v2/postgres" ) -var Actor = newActorTable() +var Actor = newActorTable("dvds", "actor", "") type actorTable struct { postgres.Table @@ -190,20 +190,23 @@ type ActorTable struct { } // AS creates new ActorTable with assigned alias -func (a *ActorTable) AS(alias string) *ActorTable { - aliasTable := newActorTable() - aliasTable.Table.AS(alias) - return aliasTable +func (a ActorTable) AS(alias string) *ActorTable { + return newActorTable(a.SchemaName(), a.TableName(), alias) } -func newActorTable() *ActorTable { +// Schema creates new ActorTable with assigned schema name +func (a ActorTable) FromSchema(schemaName string) *ActorTable { + return newActorTable(schemaName, a.TableName(), a.Alias()) +} + +func newActorTable(schemaName, tableName, alias string) *ActorTable { return &ActorTable{ - actorTable: newActorTableImpl("dvds", "actor"), - EXCLUDED: newActorTableImpl("", "excluded"), + actorTable: newActorTableImpl(schemaName, tableName, alias), + EXCLUDED: newActorTableImpl("", "excluded", ""), } } -func newActorTableImpl(schemaName, tableName string) actorTable { +func newActorTableImpl(schemaName, tableName, alias string) actorTable { var ( ActorIDColumn = postgres.IntegerColumn("actor_id") FirstNameColumn = postgres.StringColumn("first_name") @@ -214,7 +217,7 @@ func newActorTableImpl(schemaName, tableName string) actorTable { ) return actorTable{ - Table: postgres.NewTable(schemaName, tableName, allColumns...), + Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), //Columns ActorID: ActorIDColumn, @@ -264,7 +267,7 @@ import ( "github.com/go-jet/jet/v2/postgres" ) -var ActorInfo = newActorInfoTable() +var ActorInfo = newActorInfoTable("dvds", "actor_info", "") type actorInfoTable struct { postgres.Table @@ -286,20 +289,23 @@ type ActorInfoTable struct { } // AS creates new ActorInfoTable with assigned alias -func (a *ActorInfoTable) AS(alias string) *ActorInfoTable { - aliasTable := newActorInfoTable() - aliasTable.Table.AS(alias) - return aliasTable +func (a ActorInfoTable) AS(alias string) *ActorInfoTable { + return newActorInfoTable(a.SchemaName(), a.TableName(), alias) } -func newActorInfoTable() *ActorInfoTable { +// Schema creates new ActorInfoTable with assigned schema name +func (a ActorInfoTable) FromSchema(schemaName string) *ActorInfoTable { + return newActorInfoTable(schemaName, a.TableName(), a.Alias()) +} + +func newActorInfoTable(schemaName, tableName, alias string) *ActorInfoTable { return &ActorInfoTable{ - actorInfoTable: newActorInfoTableImpl("dvds", "actor_info"), - EXCLUDED: newActorInfoTableImpl("", "excluded"), + actorInfoTable: newActorInfoTableImpl(schemaName, tableName, alias), + EXCLUDED: newActorInfoTableImpl("", "excluded", ""), } } -func newActorInfoTableImpl(schemaName, tableName string) actorInfoTable { +func newActorInfoTableImpl(schemaName, tableName, alias string) actorInfoTable { var ( ActorIDColumn = postgres.IntegerColumn("actor_id") FirstNameColumn = postgres.StringColumn("first_name") @@ -310,7 +316,7 @@ func newActorInfoTableImpl(schemaName, tableName string) actorInfoTable { ) return actorInfoTable{ - Table: postgres.NewTable(schemaName, tableName, allColumns...), + Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), //Columns ActorID: ActorIDColumn, @@ -497,7 +503,7 @@ import ( "github.com/go-jet/jet/v2/postgres" ) -var AllTypes = newAllTypesTable() +var AllTypes = newAllTypesTable("test_sample", "all_types", "") type allTypesTable struct { postgres.Table @@ -576,20 +582,23 @@ type AllTypesTable struct { } // AS creates new AllTypesTable with assigned alias -func (a *AllTypesTable) AS(alias string) *AllTypesTable { - aliasTable := newAllTypesTable() - aliasTable.Table.AS(alias) - return aliasTable +func (a AllTypesTable) AS(alias string) *AllTypesTable { + return newAllTypesTable(a.SchemaName(), a.TableName(), alias) } -func newAllTypesTable() *AllTypesTable { +// Schema creates new AllTypesTable with assigned schema name +func (a AllTypesTable) FromSchema(schemaName string) *AllTypesTable { + return newAllTypesTable(schemaName, a.TableName(), a.Alias()) +} + +func newAllTypesTable(schemaName, tableName, alias string) *AllTypesTable { return &AllTypesTable{ - allTypesTable: newAllTypesTableImpl("test_sample", "all_types"), - EXCLUDED: newAllTypesTableImpl("", "excluded"), + allTypesTable: newAllTypesTableImpl(schemaName, tableName, alias), + EXCLUDED: newAllTypesTableImpl("", "excluded", ""), } } -func newAllTypesTableImpl(schemaName, tableName string) allTypesTable { +func newAllTypesTableImpl(schemaName, tableName, alias string) allTypesTable { var ( SmallIntPtrColumn = postgres.IntegerColumn("small_int_ptr") SmallIntColumn = postgres.IntegerColumn("small_int") @@ -657,7 +666,7 @@ func newAllTypesTableImpl(schemaName, tableName string) allTypesTable { ) return allTypesTable{ - Table: postgres.NewTable(schemaName, tableName, allColumns...), + Table: postgres.NewTable(schemaName, tableName, alias, allColumns...), //Columns SmallIntPtr: SmallIntPtrColumn, diff --git a/tests/postgres/sample_test.go b/tests/postgres/sample_test.go index bcb7361..327604f 100644 --- a/tests/postgres/sample_test.go +++ b/tests/postgres/sample_test.go @@ -364,7 +364,7 @@ FROM test_sample."User"; err := stmt.Query(db, &dest) require.NoError(t, err) - testutils.PrintJson(dest) + //testutils.PrintJson(dest) testutils.AssertJSON(t, dest, ` [ diff --git a/tests/postgres/scan_test.go b/tests/postgres/scan_test.go index 8eb3276..def3097 100644 --- a/tests/postgres/scan_test.go +++ b/tests/postgres/scan_test.go @@ -1,7 +1,6 @@ package postgres import ( - "fmt" "github.com/go-jet/jet/v2/internal/testutils" . "github.com/go-jet/jet/v2/postgres" "github.com/go-jet/jet/v2/qrm" @@ -93,7 +92,7 @@ func TestScanToStruct(t *testing.T) { SELECT(Inventory.AllColumns). ORDER_BY(Inventory.InventoryID) - fmt.Println(query.DebugSql()) + //fmt.Println(query.DebugSql()) t.Run("one struct", func(t *testing.T) { dest := model.Inventory{} diff --git a/tests/postgres/select_test.go b/tests/postgres/select_test.go index e9dd7d2..8bd6f53 100644 --- a/tests/postgres/select_test.go +++ b/tests/postgres/select_test.go @@ -1,7 +1,6 @@ package postgres import ( - "fmt" "github.com/go-jet/jet/v2/internal/testutils" . "github.com/go-jet/jet/v2/postgres" "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/dvds/enum" @@ -1255,7 +1254,7 @@ OFFSET 20; LIMIT(10). OFFSET(20) - fmt.Println(query.DebugSql()) + //fmt.Println(query.DebugSql()) testutils.AssertDebugStatementSql(t, query, expectedQuery, float64(100), float64(200), int64(10), int64(20)) @@ -1788,7 +1787,7 @@ func TestJoinViewWithTable(t *testing.T) { Rentals []model.Rental } - fmt.Println(query.DebugSql()) + //fmt.Println(query.DebugSql()) err := query.Query(db, &dest) require.NoError(t, err) diff --git a/tests/testdata b/tests/testdata index ed53a50..391d936 160000 --- a/tests/testdata +++ b/tests/testdata @@ -1 +1 @@ -Subproject commit ed53a505eb738d1be457877eee251f9ba0418df1 +Subproject commit 391d936515d2f826df073707697de44907a7f67d