[Feature] Add support for database views.
[Feature] Add support to manually set primary keys for destination structure fields.
This commit is contained in:
parent
5b08a1d240
commit
b88519bfd4
18 changed files with 462 additions and 128 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
. "github.com/go-jet/jet/postgres"
|
||||
"github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model"
|
||||
. "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/table"
|
||||
"github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/view"
|
||||
"github.com/go-jet/jet/tests/testdata/results/common"
|
||||
"github.com/google/uuid"
|
||||
"gotest.tools/assert"
|
||||
|
|
@ -23,6 +24,19 @@ func TestAllTypesSelect(t *testing.T) {
|
|||
assert.DeepEqual(t, dest[1], allTypesRow1)
|
||||
}
|
||||
|
||||
func TestAllTypesViewSelect(t *testing.T) {
|
||||
|
||||
type AllTypesView model.AllTypes
|
||||
|
||||
dest := []AllTypesView{}
|
||||
|
||||
err := view.AllTypesView.SELECT(view.AllTypesView.AllColumns).Query(db, &dest)
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.DeepEqual(t, dest[0], AllTypesView(allTypesRow0))
|
||||
assert.DeepEqual(t, dest[1], AllTypesView(allTypesRow1))
|
||||
}
|
||||
|
||||
func TestAllTypesInsertModel(t *testing.T) {
|
||||
query := AllTypes.INSERT(AllTypes.AllColumns).
|
||||
MODEL(allTypesRow0).
|
||||
|
|
@ -31,8 +45,8 @@ func TestAllTypesInsertModel(t *testing.T) {
|
|||
|
||||
dest := []model.AllTypes{}
|
||||
err := query.Query(db, &dest)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Equal(t, len(dest), 2)
|
||||
assert.DeepEqual(t, dest[0], allTypesRow0)
|
||||
assert.DeepEqual(t, dest[1], allTypesRow1)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/go-jet/jet/generator/postgres"
|
||||
"github.com/go-jet/jet/internal/testutils"
|
||||
"github.com/go-jet/jet/tests/dbconfig"
|
||||
"gotest.tools/assert"
|
||||
"io/ioutil"
|
||||
|
|
@ -99,53 +99,39 @@ func assertGeneratedFiles(t *testing.T) {
|
|||
tableSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/table")
|
||||
assert.NilError(t, err)
|
||||
|
||||
assertFileNameEqual(t, tableSQLBuilderFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
|
||||
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
|
||||
"customer.go", "film.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
|
||||
"payment.go", "rental.go", "staff.go", "store.go")
|
||||
|
||||
assertFileContent(t, "./.gentestdata2/jetdb/dvds/table/actor.go", "\npackage table", actorSQLBuilderFile)
|
||||
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/table/actor.go", "\npackage table", actorSQLBuilderFile)
|
||||
|
||||
// View SQL Builder files
|
||||
viewSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/view")
|
||||
assert.NilError(t, err)
|
||||
|
||||
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "actor_info.go", "film_list.go", "nicer_but_slower_film_list.go",
|
||||
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go", "staff_list.go")
|
||||
|
||||
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/view/actor_info.go", "\npackage view", actorInfoSQLBuilderFile)
|
||||
|
||||
// Enums SQL Builder files
|
||||
enumFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/enum")
|
||||
assert.NilError(t, err)
|
||||
|
||||
assertFileNameEqual(t, enumFiles, "mpaa_rating.go")
|
||||
assertFileContent(t, "./.gentestdata2/jetdb/dvds/enum/mpaa_rating.go", "\npackage enum", mpaaRatingEnumFile)
|
||||
testutils.AssertFileNamesEqual(t, enumFiles, "mpaa_rating.go")
|
||||
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/enum/mpaa_rating.go", "\npackage enum", mpaaRatingEnumFile)
|
||||
|
||||
// Model files
|
||||
modelFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/model")
|
||||
assert.NilError(t, err)
|
||||
|
||||
assertFileNameEqual(t, modelFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
|
||||
testutils.AssertFileNamesEqual(t, modelFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
|
||||
"customer.go", "film.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
|
||||
"payment.go", "rental.go", "staff.go", "store.go", "mpaa_rating.go")
|
||||
"payment.go", "rental.go", "staff.go", "store.go", "mpaa_rating.go",
|
||||
"actor_info.go", "film_list.go", "nicer_but_slower_film_list.go", "sales_by_film_category.go",
|
||||
"customer_list.go", "sales_by_store.go", "staff_list.go")
|
||||
|
||||
assertFileContent(t, "./.gentestdata2/jetdb/dvds/model/actor.go", "\npackage model", actorModelFile)
|
||||
}
|
||||
|
||||
func assertFileContent(t *testing.T, filePath string, contentBegin string, expectedContent string) {
|
||||
enumFileData, err := ioutil.ReadFile(filePath)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
beginIndex := bytes.Index(enumFileData, []byte(contentBegin))
|
||||
|
||||
//fmt.Println("-"+string(enumFileData[beginIndex:])+"-")
|
||||
|
||||
assert.DeepEqual(t, string(enumFileData[beginIndex:]), expectedContent)
|
||||
}
|
||||
|
||||
func assertFileNameEqual(t *testing.T, fileInfos []os.FileInfo, fileNames ...string) {
|
||||
|
||||
fileNamesMap := map[string]bool{}
|
||||
|
||||
for _, fileInfo := range fileInfos {
|
||||
fileNamesMap[fileInfo.Name()] = true
|
||||
}
|
||||
|
||||
for _, fileName := range fileNames {
|
||||
assert.Assert(t, fileNamesMap[fileName], fileName+" does not exist.")
|
||||
}
|
||||
testutils.AssertFileContent(t, "./.gentestdata2/jetdb/dvds/model/actor.go", "\npackage model", actorModelFile)
|
||||
}
|
||||
|
||||
var mpaaRatingEnumFile = `
|
||||
|
|
@ -236,3 +222,57 @@ type Actor struct {
|
|||
LastUpdate time.Time
|
||||
}
|
||||
`
|
||||
|
||||
var actorInfoSQLBuilderFile = `
|
||||
package view
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/postgres"
|
||||
)
|
||||
|
||||
var ActorInfo = newActorInfoTable()
|
||||
|
||||
type ActorInfoTable struct {
|
||||
postgres.Table
|
||||
|
||||
//Columns
|
||||
ActorID postgres.ColumnInteger
|
||||
FirstName postgres.ColumnString
|
||||
LastName postgres.ColumnString
|
||||
FilmInfo postgres.ColumnString
|
||||
|
||||
AllColumns postgres.IColumnList
|
||||
MutableColumns postgres.IColumnList
|
||||
}
|
||||
|
||||
// creates new ActorInfoTable with assigned alias
|
||||
func (a *ActorInfoTable) AS(alias string) *ActorInfoTable {
|
||||
aliasTable := newActorInfoTable()
|
||||
|
||||
aliasTable.Table.AS(alias)
|
||||
|
||||
return aliasTable
|
||||
}
|
||||
|
||||
func newActorInfoTable() *ActorInfoTable {
|
||||
var (
|
||||
ActorIDColumn = postgres.IntegerColumn("actor_id")
|
||||
FirstNameColumn = postgres.StringColumn("first_name")
|
||||
LastNameColumn = postgres.StringColumn("last_name")
|
||||
FilmInfoColumn = postgres.StringColumn("film_info")
|
||||
)
|
||||
|
||||
return &ActorInfoTable{
|
||||
Table: postgres.NewTable("dvds", "actor_info", ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn),
|
||||
|
||||
//Columns
|
||||
ActorID: ActorIDColumn,
|
||||
FirstName: FirstNameColumn,
|
||||
LastName: LastNameColumn,
|
||||
FilmInfo: FilmInfoColumn,
|
||||
|
||||
AllColumns: postgres.ColumnList(ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn),
|
||||
MutableColumns: postgres.ColumnList(ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn),
|
||||
}
|
||||
}
|
||||
`
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/enum"
|
||||
"github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/model"
|
||||
. "github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/table"
|
||||
"github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/view"
|
||||
"gotest.tools/assert"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -19,15 +20,15 @@ SELECT DISTINCT actor.actor_id AS "actor.actor_id",
|
|||
actor.last_name AS "actor.last_name",
|
||||
actor.last_update AS "actor.last_update"
|
||||
FROM dvds.actor
|
||||
WHERE actor.actor_id = 1;
|
||||
WHERE actor.actor_id = 2;
|
||||
`
|
||||
|
||||
query := Actor.
|
||||
SELECT(Actor.AllColumns).
|
||||
DISTINCT().
|
||||
WHERE(Actor.ActorID.EQ(Int(1)))
|
||||
WHERE(Actor.ActorID.EQ(Int(2)))
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(1))
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(2))
|
||||
|
||||
actor := model.Actor{}
|
||||
err := query.Query(db, &actor)
|
||||
|
|
@ -35,9 +36,9 @@ WHERE actor.actor_id = 1;
|
|||
assert.NilError(t, err)
|
||||
|
||||
expectedActor := model.Actor{
|
||||
ActorID: 1,
|
||||
FirstName: "Penelope",
|
||||
LastName: "Guiness",
|
||||
ActorID: 2,
|
||||
FirstName: "Nick",
|
||||
LastName: "Wahlberg",
|
||||
LastUpdate: *testutils.TimestampWithoutTimeZone("2013-05-26 14:47:57.62", 2),
|
||||
}
|
||||
|
||||
|
|
@ -1722,3 +1723,62 @@ ORDER BY payment.customer_id;
|
|||
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
func TestSimpleView(t *testing.T) {
|
||||
query := SELECT(
|
||||
view.ActorInfo.AllColumns,
|
||||
).
|
||||
FROM(view.ActorInfo).
|
||||
ORDER_BY(view.ActorInfo.ActorID).
|
||||
LIMIT(10)
|
||||
|
||||
type ActorInfo struct {
|
||||
ActorID int
|
||||
FirstName string
|
||||
LastName string
|
||||
FilmInfo string
|
||||
}
|
||||
|
||||
var dest []ActorInfo
|
||||
|
||||
err := query.Query(db, &dest)
|
||||
assert.NilError(t, err)
|
||||
|
||||
testutils.AssertJSON(t, dest[1:2], `
|
||||
[
|
||||
{
|
||||
"ActorID": 2,
|
||||
"FirstName": "Nick",
|
||||
"LastName": "Wahlberg",
|
||||
"FilmInfo": "Action: Bull Shawshank, Animation: Fight Jawbreaker, Children: Jersey Sassy, Classics: Dracula Crystal, Gilbert Pelican, Comedy: Mallrats United, Rushmore Mermaid, Documentary: Adaptation Holes, Drama: Wardrobe Phantom, Family: Apache Divine, Chisum Behavior, Indian Love, Maguire Apache, Foreign: Baby Hall, Happiness United, Games: Roof Champion, Music: Lucky Flying, New: Destiny Saturday, Flash Wars, Jekyll Frogmen, Mask Peach, Sci-Fi: Chainsaw Uptown, Goodfellas Salute, Travel: Liaisons Sweet, Smile Earring"
|
||||
}
|
||||
]
|
||||
`)
|
||||
|
||||
}
|
||||
|
||||
func TestJoinViewWithTable(t *testing.T) {
|
||||
query := SELECT(
|
||||
view.CustomerList.AllColumns,
|
||||
Rental.AllColumns,
|
||||
).
|
||||
FROM(view.CustomerList.
|
||||
INNER_JOIN(Rental, view.CustomerList.ID.EQ(Rental.CustomerID)),
|
||||
).
|
||||
ORDER_BY(view.CustomerList.ID).
|
||||
WHERE(view.CustomerList.ID.LT_EQ(Int(2)))
|
||||
|
||||
var dest []struct {
|
||||
model.CustomerList `sql:"primary_key=ID"`
|
||||
Rentals []model.Rental
|
||||
}
|
||||
|
||||
fmt.Println(query.DebugSql())
|
||||
|
||||
err := query.Query(db, &dest)
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.Equal(t, len(dest), 2)
|
||||
assert.Equal(t, len(dest[0].Rentals), 32)
|
||||
assert.Equal(t, len(dest[1].Rentals), 27)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue