[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
|
|
@ -5,6 +5,7 @@ import (
|
|||
"github.com/go-jet/jet/internal/testutils"
|
||||
"github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/model"
|
||||
. "github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/table"
|
||||
"github.com/go-jet/jet/tests/.gentestdata/mysql/test_sample/view"
|
||||
"github.com/go-jet/jet/tests/testdata/results/common"
|
||||
"github.com/google/uuid"
|
||||
"time"
|
||||
|
|
@ -36,6 +37,23 @@ func TestAllTypes(t *testing.T) {
|
|||
testutils.AssertJSON(t, dest, allTypesJson)
|
||||
}
|
||||
|
||||
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.Equal(t, len(dest), 2)
|
||||
|
||||
if sourceIsMariaDB() { // MariaDB saves current timestamp in a case of NULL value insert
|
||||
return
|
||||
}
|
||||
|
||||
testutils.AssertJSON(t, dest, allTypesJson)
|
||||
}
|
||||
|
||||
func TestUUID(t *testing.T) {
|
||||
|
||||
query := AllTypes.
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/go-jet/jet/generator/mysql"
|
||||
"github.com/go-jet/jet/internal/testutils"
|
||||
"github.com/go-jet/jet/tests/dbconfig"
|
||||
"gotest.tools/assert"
|
||||
"io/ioutil"
|
||||
|
|
@ -63,53 +63,40 @@ func assertGeneratedFiles(t *testing.T) {
|
|||
tableSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/table")
|
||||
assert.NilError(t, err)
|
||||
|
||||
assertFileNameEqual(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",
|
||||
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "actor.go", "address.go", "category.go", "city.go", "country.go",
|
||||
"customer.go", "film.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
|
||||
"payment.go", "rental.go", "staff.go", "store.go")
|
||||
|
||||
assertFileContent(t, genTestDir3+"/dvds/table/actor.go", "\npackage table", actorSQLBuilderFile)
|
||||
testutils.AssertFileContent(t, genTestDir3+"/dvds/table/actor.go", "\npackage table", actorSQLBuilderFile)
|
||||
|
||||
// View SQL Builder files
|
||||
viewSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/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, genTestDir3+"/dvds/view/actor_info.go", "\npackage view", actorInfoSQLBuilerFile)
|
||||
|
||||
// Enums SQL Builder files
|
||||
enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
|
||||
assert.NilError(t, err)
|
||||
|
||||
assertFileNameEqual(t, enumFiles, "film_rating.go")
|
||||
assertFileContent(t, genTestDir3+"/dvds/enum/film_rating.go", "\npackage enum", mpaaRatingEnumFile)
|
||||
testutils.AssertFileNamesEqual(t, enumFiles, "film_rating.go", "film_list_rating.go", "nicer_but_slower_film_list_rating.go")
|
||||
testutils.AssertFileContent(t, genTestDir3+"/dvds/enum/film_rating.go", "\npackage enum", mpaaRatingEnumFile)
|
||||
|
||||
// Model files
|
||||
modelFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/model")
|
||||
assert.NilError(t, err)
|
||||
|
||||
assertFileNameEqual(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", "film_rating.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", "film_text.go", "inventory.go", "language.go",
|
||||
"payment.go", "rental.go", "staff.go", "store.go",
|
||||
"film_rating.go", "film_list_rating.go", "nicer_but_slower_film_list_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, genTestDir3+"/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, genTestDir3+"/dvds/model/actor.go", "\npackage model", actorModelFile)
|
||||
}
|
||||
|
||||
var mpaaRatingEnumFile = `
|
||||
|
|
@ -200,3 +187,57 @@ type Actor struct {
|
|||
LastUpdate time.Time
|
||||
}
|
||||
`
|
||||
|
||||
var actorInfoSQLBuilerFile = `
|
||||
package view
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/mysql"
|
||||
)
|
||||
|
||||
var ActorInfo = newActorInfoTable()
|
||||
|
||||
type ActorInfoTable struct {
|
||||
mysql.Table
|
||||
|
||||
//Columns
|
||||
ActorID mysql.ColumnInteger
|
||||
FirstName mysql.ColumnString
|
||||
LastName mysql.ColumnString
|
||||
FilmInfo mysql.ColumnString
|
||||
|
||||
AllColumns mysql.IColumnList
|
||||
MutableColumns mysql.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 = mysql.IntegerColumn("actor_id")
|
||||
FirstNameColumn = mysql.StringColumn("first_name")
|
||||
LastNameColumn = mysql.StringColumn("last_name")
|
||||
FilmInfoColumn = mysql.StringColumn("film_info")
|
||||
)
|
||||
|
||||
return &ActorInfoTable{
|
||||
Table: mysql.NewTable("dvds", "actor_info", ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn),
|
||||
|
||||
//Columns
|
||||
ActorID: ActorIDColumn,
|
||||
FirstName: FirstNameColumn,
|
||||
LastName: LastNameColumn,
|
||||
FilmInfo: FilmInfoColumn,
|
||||
|
||||
AllColumns: mysql.ColumnList(ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn),
|
||||
MutableColumns: mysql.ColumnList(ActorIDColumn, FirstNameColumn, LastNameColumn, FilmInfoColumn),
|
||||
}
|
||||
}
|
||||
`
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/go-jet/jet/tests/.gentestdata/mysql/dvds/enum"
|
||||
"github.com/go-jet/jet/tests/.gentestdata/mysql/dvds/model"
|
||||
. "github.com/go-jet/jet/tests/.gentestdata/mysql/dvds/table"
|
||||
"github.com/go-jet/jet/tests/.gentestdata/mysql/dvds/view"
|
||||
"gotest.tools/assert"
|
||||
|
||||
"testing"
|
||||
|
|
@ -16,7 +17,7 @@ func TestSelect_ScanToStruct(t *testing.T) {
|
|||
query := Actor.
|
||||
SELECT(Actor.AllColumns).
|
||||
DISTINCT().
|
||||
WHERE(Actor.ActorID.EQ(Int(1)))
|
||||
WHERE(Actor.ActorID.EQ(Int(2)))
|
||||
|
||||
testutils.AssertStatementSql(t, query, `
|
||||
SELECT DISTINCT actor.actor_id AS "actor.actor_id",
|
||||
|
|
@ -25,20 +26,20 @@ SELECT DISTINCT actor.actor_id AS "actor.actor_id",
|
|||
actor.last_update AS "actor.last_update"
|
||||
FROM dvds.actor
|
||||
WHERE actor.actor_id = ?;
|
||||
`, int64(1))
|
||||
`, int64(2))
|
||||
|
||||
actor := model.Actor{}
|
||||
err := query.Query(db, &actor)
|
||||
|
||||
assert.NilError(t, err)
|
||||
|
||||
assert.DeepEqual(t, actor, actor1)
|
||||
assert.DeepEqual(t, actor, actor2)
|
||||
}
|
||||
|
||||
var actor1 = model.Actor{
|
||||
ActorID: 1,
|
||||
FirstName: "PENELOPE",
|
||||
LastName: "GUINESS",
|
||||
var actor2 = model.Actor{
|
||||
ActorID: 2,
|
||||
FirstName: "NICK",
|
||||
LastName: "WAHLBERG",
|
||||
LastUpdate: *testutils.TimestampWithoutTimeZone("2006-02-15 04:34:33", 2),
|
||||
}
|
||||
|
||||
|
|
@ -62,7 +63,7 @@ ORDER BY actor.actor_id;
|
|||
assert.NilError(t, err)
|
||||
|
||||
assert.Equal(t, len(dest), 200)
|
||||
assert.DeepEqual(t, dest[0], actor1)
|
||||
assert.DeepEqual(t, dest[1], actor2)
|
||||
|
||||
//testutils.PrintJson(dest)
|
||||
//testutils.SaveJsonFile(dest, "mysql/testdata/all_actors.json")
|
||||
|
|
@ -640,3 +641,60 @@ 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)
|
||||
|
||||
assert.Equal(t, len(dest), 10)
|
||||
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
|
||||
}
|
||||
|
||||
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