Add schema rename support
Using SchemaFrom("schemaName") it is possible to set SQL builder table to point to a different schema.
This commit is contained in:
parent
38776e35ab
commit
fae8dde639
17 changed files with 206 additions and 153 deletions
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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, `
|
||||
[
|
||||
|
|
|
|||
|
|
@ -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{}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue