2019-07-30 11:45:10 +02:00
|
|
|
package postgres
|
2019-03-03 17:54:43 +01:00
|
|
|
|
|
|
|
|
import (
|
2019-07-29 18:08:53 +02:00
|
|
|
"github.com/go-jet/jet/internal/testutils"
|
2019-07-31 18:43:54 +02:00
|
|
|
. "github.com/go-jet/jet/postgres"
|
2019-06-24 10:01:34 +02:00
|
|
|
"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"
|
2019-03-03 17:54:43 +01:00
|
|
|
"gotest.tools/assert"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2019-03-14 09:18:23 +01:00
|
|
|
func TestSelect_ScanToStruct(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-06-17 12:05:52 +02:00
|
|
|
SELECT DISTINCT actor.actor_id AS "actor.actor_id",
|
2019-05-12 18:15:23 +02:00
|
|
|
actor.first_name AS "actor.first_name",
|
|
|
|
|
actor.last_name AS "actor.last_name",
|
|
|
|
|
actor.last_update AS "actor.last_update"
|
|
|
|
|
FROM dvds.actor
|
2019-05-20 17:37:55 +02:00
|
|
|
WHERE actor.actor_id = 1;
|
2019-05-12 18:15:23 +02:00
|
|
|
`
|
|
|
|
|
|
2019-05-08 12:49:36 +02:00
|
|
|
query := Actor.
|
|
|
|
|
SELECT(Actor.AllColumns).
|
2019-06-17 12:05:52 +02:00
|
|
|
DISTINCT().
|
2019-05-29 14:03:38 +02:00
|
|
|
WHERE(Actor.ActorID.EQ(Int(1)))
|
2019-04-03 11:03:07 +02:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(1))
|
2019-04-03 11:03:07 +02:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
actor := model.Actor{}
|
|
|
|
|
err := query.Query(db, &actor)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
2019-03-05 18:55:47 +01:00
|
|
|
|
2019-03-14 09:18:23 +01:00
|
|
|
expectedActor := model.Actor{
|
|
|
|
|
ActorID: 1,
|
|
|
|
|
FirstName: "Penelope",
|
|
|
|
|
LastName: "Guiness",
|
2019-07-29 18:08:53 +02:00
|
|
|
LastUpdate: *testutils.TimestampWithoutTimeZone("2013-05-26 14:47:57.62", 2),
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.DeepEqual(t, actor, expectedActor)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 12:37:23 +02:00
|
|
|
func TestClassicSelect(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT payment.payment_id AS "payment.payment_id",
|
|
|
|
|
payment.customer_id AS "payment.customer_id",
|
|
|
|
|
payment.staff_id AS "payment.staff_id",
|
|
|
|
|
payment.rental_id AS "payment.rental_id",
|
|
|
|
|
payment.amount AS "payment.amount",
|
|
|
|
|
payment.payment_date AS "payment.payment_date",
|
|
|
|
|
customer.customer_id AS "customer.customer_id",
|
|
|
|
|
customer.store_id AS "customer.store_id",
|
|
|
|
|
customer.first_name AS "customer.first_name",
|
|
|
|
|
customer.last_name AS "customer.last_name",
|
|
|
|
|
customer.email AS "customer.email",
|
|
|
|
|
customer.address_id AS "customer.address_id",
|
|
|
|
|
customer.activebool AS "customer.activebool",
|
|
|
|
|
customer.create_date AS "customer.create_date",
|
|
|
|
|
customer.last_update AS "customer.last_update",
|
|
|
|
|
customer.active AS "customer.active"
|
|
|
|
|
FROM dvds.payment
|
2019-06-05 17:15:20 +02:00
|
|
|
INNER JOIN dvds.customer ON (payment.customer_id = customer.customer_id)
|
2019-05-12 18:15:23 +02:00
|
|
|
ORDER BY payment.payment_id ASC
|
|
|
|
|
LIMIT 30;
|
|
|
|
|
`
|
|
|
|
|
|
2019-06-29 16:58:41 +02:00
|
|
|
query := SELECT(
|
|
|
|
|
Payment.AllColumns,
|
|
|
|
|
Customer.AllColumns,
|
|
|
|
|
).
|
|
|
|
|
FROM(Payment.
|
|
|
|
|
INNER_JOIN(Customer, Payment.CustomerID.EQ(Customer.CustomerID))).
|
2019-05-05 18:03:30 +02:00
|
|
|
ORDER_BY(Payment.PaymentID.ASC()).
|
2019-05-05 12:37:23 +02:00
|
|
|
LIMIT(30)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(30))
|
2019-05-05 12:37:23 +02:00
|
|
|
|
|
|
|
|
dest := []model.Payment{}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &dest)
|
2019-05-05 12:37:23 +02:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
2019-05-12 18:15:23 +02:00
|
|
|
assert.Equal(t, len(dest), 30)
|
2019-05-05 12:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-14 09:18:23 +01:00
|
|
|
func TestSelect_ScanToSlice(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT customer.customer_id AS "customer.customer_id",
|
|
|
|
|
customer.store_id AS "customer.store_id",
|
|
|
|
|
customer.first_name AS "customer.first_name",
|
|
|
|
|
customer.last_name AS "customer.last_name",
|
|
|
|
|
customer.email AS "customer.email",
|
|
|
|
|
customer.address_id AS "customer.address_id",
|
|
|
|
|
customer.activebool AS "customer.activebool",
|
|
|
|
|
customer.create_date AS "customer.create_date",
|
|
|
|
|
customer.last_update AS "customer.last_update",
|
|
|
|
|
customer.active AS "customer.active"
|
|
|
|
|
FROM dvds.customer
|
|
|
|
|
ORDER BY customer.customer_id ASC;
|
|
|
|
|
`
|
2019-03-05 18:55:47 +01:00
|
|
|
customers := []model.Customer{}
|
|
|
|
|
|
2019-05-05 18:03:30 +02:00
|
|
|
query := Customer.SELECT(Customer.AllColumns).ORDER_BY(Customer.CustomerID.ASC())
|
2019-03-05 18:55:47 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL)
|
2019-03-05 18:55:47 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &customers)
|
2019-03-05 18:55:47 +01:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-03-14 09:18:23 +01:00
|
|
|
assert.Equal(t, len(customers), 599)
|
2019-03-03 17:54:43 +01:00
|
|
|
|
2019-03-14 09:18:23 +01:00
|
|
|
assert.DeepEqual(t, customer0, customers[0])
|
|
|
|
|
assert.DeepEqual(t, customer1, customers[1])
|
|
|
|
|
assert.DeepEqual(t, lastCustomer, customers[598])
|
2019-03-03 17:54:43 +01:00
|
|
|
}
|
2019-03-09 14:20:44 +01:00
|
|
|
|
2019-05-05 12:37:23 +02:00
|
|
|
func TestSelectAndUnionInProjection(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT payment.payment_id AS "payment.payment_id",
|
|
|
|
|
(
|
|
|
|
|
SELECT customer.customer_id AS "customer.customer_id"
|
|
|
|
|
FROM dvds.customer
|
|
|
|
|
LIMIT 1
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
(
|
|
|
|
|
(
|
|
|
|
|
SELECT payment.payment_id AS "payment.payment_id"
|
|
|
|
|
FROM dvds.payment
|
|
|
|
|
LIMIT 1
|
|
|
|
|
OFFSET 10
|
|
|
|
|
)
|
|
|
|
|
UNION
|
|
|
|
|
(
|
|
|
|
|
SELECT payment.payment_id AS "payment.payment_id"
|
|
|
|
|
FROM dvds.payment
|
|
|
|
|
LIMIT 1
|
|
|
|
|
OFFSET 2
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
LIMIT 1
|
|
|
|
|
)
|
|
|
|
|
FROM dvds.payment
|
|
|
|
|
LIMIT 12;
|
|
|
|
|
`
|
2019-05-05 12:37:23 +02:00
|
|
|
|
|
|
|
|
query := Payment.
|
|
|
|
|
SELECT(
|
|
|
|
|
Payment.PaymentID,
|
|
|
|
|
Customer.SELECT(Customer.CustomerID).LIMIT(1),
|
2019-05-12 18:15:23 +02:00
|
|
|
UNION(
|
|
|
|
|
Payment.SELECT(Payment.PaymentID).LIMIT(1).OFFSET(10),
|
|
|
|
|
Payment.SELECT(Payment.PaymentID).LIMIT(1).OFFSET(2),
|
|
|
|
|
).LIMIT(1),
|
2019-05-05 12:37:23 +02:00
|
|
|
).
|
|
|
|
|
LIMIT(12)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(1), int64(1), int64(10), int64(1), int64(2), int64(1), int64(12))
|
2019-05-05 12:37:23 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func TestJoinQueryStruct(t *testing.T) {
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-20 17:37:55 +02:00
|
|
|
SELECT film_actor.actor_id AS "film_actor.actor_id",
|
|
|
|
|
film_actor.film_id AS "film_actor.film_id",
|
|
|
|
|
film_actor.last_update AS "film_actor.last_update",
|
|
|
|
|
film.film_id AS "film.film_id",
|
|
|
|
|
film.title AS "film.title",
|
|
|
|
|
film.description AS "film.description",
|
|
|
|
|
film.release_year AS "film.release_year",
|
|
|
|
|
film.language_id AS "film.language_id",
|
|
|
|
|
film.rental_duration AS "film.rental_duration",
|
|
|
|
|
film.rental_rate AS "film.rental_rate",
|
|
|
|
|
film.length AS "film.length",
|
|
|
|
|
film.replacement_cost AS "film.replacement_cost",
|
|
|
|
|
film.rating AS "film.rating",
|
|
|
|
|
film.last_update AS "film.last_update",
|
|
|
|
|
film.special_features AS "film.special_features",
|
|
|
|
|
film.fulltext AS "film.fulltext",
|
|
|
|
|
language.language_id AS "language.language_id",
|
|
|
|
|
language.name AS "language.name",
|
|
|
|
|
language.last_update AS "language.last_update",
|
|
|
|
|
actor.actor_id AS "actor.actor_id",
|
|
|
|
|
actor.first_name AS "actor.first_name",
|
|
|
|
|
actor.last_name AS "actor.last_name",
|
|
|
|
|
actor.last_update AS "actor.last_update",
|
|
|
|
|
inventory.inventory_id AS "inventory.inventory_id",
|
|
|
|
|
inventory.film_id AS "inventory.film_id",
|
|
|
|
|
inventory.store_id AS "inventory.store_id",
|
|
|
|
|
inventory.last_update AS "inventory.last_update",
|
|
|
|
|
rental.rental_id AS "rental.rental_id",
|
|
|
|
|
rental.rental_date AS "rental.rental_date",
|
|
|
|
|
rental.inventory_id AS "rental.inventory_id",
|
|
|
|
|
rental.customer_id AS "rental.customer_id",
|
|
|
|
|
rental.return_date AS "rental.return_date",
|
|
|
|
|
rental.staff_id AS "rental.staff_id",
|
|
|
|
|
rental.last_update AS "rental.last_update"
|
|
|
|
|
FROM dvds.film_actor
|
2019-06-05 17:15:20 +02:00
|
|
|
INNER JOIN dvds.actor ON (film_actor.actor_id = actor.actor_id)
|
|
|
|
|
INNER JOIN dvds.film ON (film_actor.film_id = film.film_id)
|
|
|
|
|
INNER JOIN dvds.language ON (film.language_id = language.language_id)
|
|
|
|
|
INNER JOIN dvds.inventory ON (inventory.film_id = film.film_id)
|
|
|
|
|
INNER JOIN dvds.rental ON (rental.inventory_id = inventory.inventory_id)
|
2019-05-20 17:37:55 +02:00
|
|
|
ORDER BY film.film_id ASC
|
2019-06-20 11:19:23 +02:00
|
|
|
LIMIT 1000;
|
2019-05-20 17:37:55 +02:00
|
|
|
`
|
2019-06-20 11:19:23 +02:00
|
|
|
for i := 0; i < 2; i++ {
|
2019-05-20 17:37:55 +02:00
|
|
|
query := FilmActor.
|
2019-05-29 14:03:38 +02:00
|
|
|
INNER_JOIN(Actor, FilmActor.ActorID.EQ(Actor.ActorID)).
|
|
|
|
|
INNER_JOIN(Film, FilmActor.FilmID.EQ(Film.FilmID)).
|
|
|
|
|
INNER_JOIN(Language, Film.LanguageID.EQ(Language.LanguageID)).
|
|
|
|
|
INNER_JOIN(Inventory, Inventory.FilmID.EQ(Film.FilmID)).
|
|
|
|
|
INNER_JOIN(Rental, Rental.InventoryID.EQ(Inventory.InventoryID)).
|
2019-05-20 17:37:55 +02:00
|
|
|
SELECT(
|
|
|
|
|
FilmActor.AllColumns,
|
|
|
|
|
Film.AllColumns,
|
|
|
|
|
Language.AllColumns,
|
|
|
|
|
Actor.AllColumns,
|
|
|
|
|
Inventory.AllColumns,
|
|
|
|
|
Rental.AllColumns,
|
|
|
|
|
).
|
|
|
|
|
//WHERE(FilmActor.ActorID.GtEqL(1).AND(FilmActor.ActorID.LtEqL(2))).
|
|
|
|
|
ORDER_BY(Film.FilmID.ASC()).
|
2019-06-20 11:19:23 +02:00
|
|
|
LIMIT(1000)
|
2019-05-20 17:37:55 +02:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(1000))
|
2019-05-20 17:37:55 +02:00
|
|
|
|
|
|
|
|
var languageActorFilm []struct {
|
|
|
|
|
model.Language
|
|
|
|
|
|
|
|
|
|
Films []struct {
|
|
|
|
|
model.Film
|
|
|
|
|
Actors []struct {
|
|
|
|
|
model.Actor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Inventory []struct {
|
|
|
|
|
model.Inventory
|
|
|
|
|
|
|
|
|
|
Rental []model.Rental
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := query.Query(db, &languageActorFilm)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(languageActorFilm), 1)
|
2019-06-20 11:19:23 +02:00
|
|
|
assert.Equal(t, len(languageActorFilm[0].Films), 10)
|
2019-05-20 17:37:55 +02:00
|
|
|
assert.Equal(t, len(languageActorFilm[0].Films[0].Actors), 10)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
func TestJoinQuerySlice(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT language.language_id AS "language.language_id",
|
|
|
|
|
language.name AS "language.name",
|
|
|
|
|
language.last_update AS "language.last_update",
|
|
|
|
|
film.film_id AS "film.film_id",
|
|
|
|
|
film.title AS "film.title",
|
|
|
|
|
film.description AS "film.description",
|
|
|
|
|
film.release_year AS "film.release_year",
|
|
|
|
|
film.language_id AS "film.language_id",
|
|
|
|
|
film.rental_duration AS "film.rental_duration",
|
|
|
|
|
film.rental_rate AS "film.rental_rate",
|
|
|
|
|
film.length AS "film.length",
|
|
|
|
|
film.replacement_cost AS "film.replacement_cost",
|
|
|
|
|
film.rating AS "film.rating",
|
|
|
|
|
film.last_update AS "film.last_update",
|
|
|
|
|
film.special_features AS "film.special_features",
|
|
|
|
|
film.fulltext AS "film.fulltext"
|
|
|
|
|
FROM dvds.film
|
2019-06-05 17:15:20 +02:00
|
|
|
INNER JOIN dvds.language ON (film.language_id = language.language_id)
|
2019-05-12 18:15:23 +02:00
|
|
|
WHERE film.rating = 'NC-17'
|
|
|
|
|
LIMIT 15;
|
|
|
|
|
`
|
|
|
|
|
|
2019-03-14 09:18:23 +01:00
|
|
|
type FilmsPerLanguage struct {
|
|
|
|
|
Language *model.Language
|
2019-04-03 19:21:46 +02:00
|
|
|
Film []model.Film
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filmsPerLanguage := []FilmsPerLanguage{}
|
|
|
|
|
limit := 15
|
|
|
|
|
|
2019-04-03 11:03:07 +02:00
|
|
|
query := Film.
|
2019-05-29 14:03:38 +02:00
|
|
|
INNER_JOIN(Language, Film.LanguageID.EQ(Language.LanguageID)).
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(Language.AllColumns, Film.AllColumns).
|
2019-07-03 16:27:14 +02:00
|
|
|
WHERE(Film.Rating.EQ(enum.MpaaRating.Nc17)).
|
2019-04-29 14:39:48 +02:00
|
|
|
LIMIT(15)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(15))
|
2019-03-15 21:25:24 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &filmsPerLanguage)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(filmsPerLanguage), 1)
|
2019-04-03 19:21:46 +02:00
|
|
|
assert.Equal(t, len(filmsPerLanguage[0].Film), limit)
|
|
|
|
|
|
|
|
|
|
englishFilms := filmsPerLanguage[0]
|
|
|
|
|
|
2019-07-03 16:27:14 +02:00
|
|
|
assert.Equal(t, *englishFilms.Film[0].Rating, model.MpaaRating_Nc17)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
filmsPerLanguageWithPtrs := []*FilmsPerLanguage{}
|
2019-04-20 19:49:29 +02:00
|
|
|
err = query.Query(db, &filmsPerLanguageWithPtrs)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(filmsPerLanguage), 1)
|
2019-04-03 19:21:46 +02:00
|
|
|
assert.Equal(t, len(filmsPerLanguage[0].Film), limit)
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 15:13:00 +02:00
|
|
|
func TestExecution1(t *testing.T) {
|
|
|
|
|
stmt := City.
|
|
|
|
|
INNER_JOIN(Address, Address.CityID.EQ(City.CityID)).
|
|
|
|
|
INNER_JOIN(Customer, Customer.AddressID.EQ(Address.AddressID)).
|
|
|
|
|
SELECT(
|
|
|
|
|
City.CityID,
|
|
|
|
|
City.City,
|
|
|
|
|
Address.AddressID,
|
|
|
|
|
Address.Address,
|
|
|
|
|
Customer.CustomerID,
|
|
|
|
|
Customer.LastName,
|
|
|
|
|
).
|
|
|
|
|
WHERE(City.City.EQ(String("London")).OR(City.City.EQ(String("York")))).
|
|
|
|
|
ORDER_BY(City.CityID, Address.AddressID, Customer.CustomerID)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, stmt, `
|
2019-07-05 15:13:00 +02:00
|
|
|
SELECT city.city_id AS "city.city_id",
|
|
|
|
|
city.city AS "city.city",
|
|
|
|
|
address.address_id AS "address.address_id",
|
|
|
|
|
address.address AS "address.address",
|
|
|
|
|
customer.customer_id AS "customer.customer_id",
|
|
|
|
|
customer.last_name AS "customer.last_name"
|
|
|
|
|
FROM dvds.city
|
|
|
|
|
INNER JOIN dvds.address ON (address.city_id = city.city_id)
|
|
|
|
|
INNER JOIN dvds.customer ON (customer.address_id = address.address_id)
|
|
|
|
|
WHERE (city.city = 'London') OR (city.city = 'York')
|
|
|
|
|
ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|
|
|
|
`, "London", "York")
|
|
|
|
|
|
|
|
|
|
var dest []struct {
|
|
|
|
|
model.City
|
|
|
|
|
|
|
|
|
|
Customers []struct {
|
|
|
|
|
model.Customer
|
|
|
|
|
|
|
|
|
|
Address model.Address
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := stmt.Query(db, &dest)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(dest), 2)
|
|
|
|
|
assert.Equal(t, dest[0].City.City, "London")
|
|
|
|
|
assert.Equal(t, dest[1].City.City, "York")
|
|
|
|
|
assert.Equal(t, len(dest[0].Customers), 2)
|
|
|
|
|
assert.Equal(t, dest[0].Customers[0].LastName, "Hoffman")
|
|
|
|
|
assert.Equal(t, dest[0].Customers[1].LastName, "Vines")
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExecution2(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
type MyAddress struct {
|
|
|
|
|
ID int32 `sql:"primary_key"`
|
|
|
|
|
AddressLine string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MyCustomer struct {
|
|
|
|
|
ID int32 `sql:"primary_key"`
|
|
|
|
|
LastName *string
|
|
|
|
|
|
|
|
|
|
Address MyAddress
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MyCity struct {
|
|
|
|
|
ID int32 `sql:"primary_key"`
|
|
|
|
|
Name string
|
|
|
|
|
|
|
|
|
|
Customers []MyCustomer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dest := []MyCity{}
|
|
|
|
|
|
|
|
|
|
stmt := City.
|
|
|
|
|
INNER_JOIN(Address, Address.CityID.EQ(City.CityID)).
|
|
|
|
|
INNER_JOIN(Customer, Customer.AddressID.EQ(Address.AddressID)).
|
|
|
|
|
SELECT(
|
|
|
|
|
City.CityID.AS("my_city.id"),
|
|
|
|
|
City.City.AS("myCity.Name"),
|
|
|
|
|
Address.AddressID.AS("My_Address.id"),
|
|
|
|
|
Address.Address.AS("my address.address line"),
|
|
|
|
|
Customer.CustomerID.AS("my_customer.id"),
|
|
|
|
|
Customer.LastName.AS("my_customer.last_name"),
|
|
|
|
|
).
|
|
|
|
|
WHERE(City.City.EQ(String("London")).OR(City.City.EQ(String("York")))).
|
|
|
|
|
ORDER_BY(City.CityID, Address.AddressID, Customer.CustomerID)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, stmt, `
|
2019-07-05 15:13:00 +02:00
|
|
|
SELECT city.city_id AS "my_city.id",
|
|
|
|
|
city.city AS "myCity.Name",
|
|
|
|
|
address.address_id AS "My_Address.id",
|
|
|
|
|
address.address AS "my address.address line",
|
|
|
|
|
customer.customer_id AS "my_customer.id",
|
|
|
|
|
customer.last_name AS "my_customer.last_name"
|
|
|
|
|
FROM dvds.city
|
|
|
|
|
INNER JOIN dvds.address ON (address.city_id = city.city_id)
|
|
|
|
|
INNER JOIN dvds.customer ON (customer.address_id = address.address_id)
|
|
|
|
|
WHERE (city.city = 'London') OR (city.city = 'York')
|
|
|
|
|
ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|
|
|
|
`, "London", "York")
|
|
|
|
|
|
|
|
|
|
err := stmt.Query(db, &dest)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(dest), 2)
|
|
|
|
|
assert.Equal(t, dest[0].Name, "London")
|
|
|
|
|
assert.Equal(t, dest[1].Name, "York")
|
|
|
|
|
assert.Equal(t, len(dest[0].Customers), 2)
|
|
|
|
|
assert.Equal(t, *dest[0].Customers[0].LastName, "Hoffman")
|
|
|
|
|
assert.Equal(t, *dest[0].Customers[1].LastName, "Vines")
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExecution3(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
var dest []struct {
|
|
|
|
|
CityID int32 `sql:"primary_key"`
|
|
|
|
|
CityName string
|
|
|
|
|
|
|
|
|
|
Customers []struct {
|
|
|
|
|
CustomerID int32 `sql:"primary_key"`
|
|
|
|
|
LastName *string
|
|
|
|
|
|
|
|
|
|
Address struct {
|
|
|
|
|
AddressID int32 `sql:"primary_key"`
|
|
|
|
|
AddressLine string
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := City.
|
|
|
|
|
INNER_JOIN(Address, Address.CityID.EQ(City.CityID)).
|
|
|
|
|
INNER_JOIN(Customer, Customer.AddressID.EQ(Address.AddressID)).
|
|
|
|
|
SELECT(
|
|
|
|
|
City.CityID.AS("city_id"),
|
|
|
|
|
City.City.AS("city_name"),
|
|
|
|
|
Customer.CustomerID.AS("customer_id"),
|
|
|
|
|
Customer.LastName.AS("last_name"),
|
|
|
|
|
Address.AddressID.AS("address_id"),
|
|
|
|
|
Address.Address.AS("address_line"),
|
|
|
|
|
).
|
|
|
|
|
WHERE(City.City.EQ(String("London")).OR(City.City.EQ(String("York")))).
|
|
|
|
|
ORDER_BY(City.CityID, Address.AddressID, Customer.CustomerID)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, stmt, `
|
2019-07-05 15:13:00 +02:00
|
|
|
SELECT city.city_id AS "city_id",
|
|
|
|
|
city.city AS "city_name",
|
|
|
|
|
customer.customer_id AS "customer_id",
|
|
|
|
|
customer.last_name AS "last_name",
|
|
|
|
|
address.address_id AS "address_id",
|
|
|
|
|
address.address AS "address_line"
|
|
|
|
|
FROM dvds.city
|
|
|
|
|
INNER JOIN dvds.address ON (address.city_id = city.city_id)
|
|
|
|
|
INNER JOIN dvds.customer ON (customer.address_id = address.address_id)
|
|
|
|
|
WHERE (city.city = 'London') OR (city.city = 'York')
|
|
|
|
|
ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|
|
|
|
`, "London", "York")
|
|
|
|
|
|
|
|
|
|
err := stmt.Query(db, &dest)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(dest), 2)
|
|
|
|
|
assert.Equal(t, dest[0].CityName, "London")
|
|
|
|
|
assert.Equal(t, dest[1].CityName, "York")
|
|
|
|
|
assert.Equal(t, len(dest[0].Customers), 2)
|
|
|
|
|
assert.Equal(t, *dest[0].Customers[0].LastName, "Hoffman")
|
|
|
|
|
assert.Equal(t, *dest[0].Customers[1].LastName, "Vines")
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 13:17:28 +02:00
|
|
|
func TestExecution4(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
var dest []struct {
|
|
|
|
|
CityID int32 `sql:"primary_key" alias:"city.city_id"`
|
|
|
|
|
CityName string `alias:"city.city"`
|
|
|
|
|
|
|
|
|
|
Customers []struct {
|
|
|
|
|
CustomerID int32 `sql:"primary_key" alias:"customer_id"`
|
|
|
|
|
LastName *string `alias:"last_name"`
|
|
|
|
|
|
|
|
|
|
Address struct {
|
|
|
|
|
AddressID int32 `sql:"primary_key" alias:"AddressId"`
|
|
|
|
|
AddressLine string `alias:"address.address"`
|
|
|
|
|
} `alias:"address.*"`
|
|
|
|
|
} `alias:"customer"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := City.
|
|
|
|
|
INNER_JOIN(Address, Address.CityID.EQ(City.CityID)).
|
|
|
|
|
INNER_JOIN(Customer, Customer.AddressID.EQ(Address.AddressID)).
|
|
|
|
|
SELECT(
|
|
|
|
|
City.CityID,
|
|
|
|
|
City.City,
|
|
|
|
|
Customer.CustomerID,
|
|
|
|
|
Customer.LastName,
|
|
|
|
|
Address.AddressID,
|
|
|
|
|
Address.Address,
|
|
|
|
|
).
|
|
|
|
|
WHERE(City.City.EQ(String("London")).OR(City.City.EQ(String("York")))).
|
|
|
|
|
ORDER_BY(City.CityID, Address.AddressID, Customer.CustomerID)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, stmt, `
|
2019-07-13 13:17:28 +02:00
|
|
|
SELECT city.city_id AS "city.city_id",
|
|
|
|
|
city.city AS "city.city",
|
|
|
|
|
customer.customer_id AS "customer.customer_id",
|
|
|
|
|
customer.last_name AS "customer.last_name",
|
|
|
|
|
address.address_id AS "address.address_id",
|
|
|
|
|
address.address AS "address.address"
|
|
|
|
|
FROM dvds.city
|
|
|
|
|
INNER JOIN dvds.address ON (address.city_id = city.city_id)
|
|
|
|
|
INNER JOIN dvds.customer ON (customer.address_id = address.address_id)
|
|
|
|
|
WHERE (city.city = 'London') OR (city.city = 'York')
|
|
|
|
|
ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|
|
|
|
`, "London", "York")
|
|
|
|
|
|
|
|
|
|
err := stmt.Query(db, &dest)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(dest), 2)
|
2019-08-06 11:41:45 +02:00
|
|
|
testutils.AssertJSON(t, dest, `
|
2019-07-13 13:17:28 +02:00
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"CityID": 312,
|
|
|
|
|
"CityName": "London",
|
|
|
|
|
"Customers": [
|
|
|
|
|
{
|
|
|
|
|
"CustomerID": 252,
|
|
|
|
|
"LastName": "Hoffman",
|
|
|
|
|
"Address": {
|
|
|
|
|
"AddressID": 256,
|
|
|
|
|
"AddressLine": "1497 Yuzhou Drive"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"CustomerID": 512,
|
|
|
|
|
"LastName": "Vines",
|
|
|
|
|
"Address": {
|
|
|
|
|
"AddressID": 517,
|
|
|
|
|
"AddressLine": "548 Uruapan Street"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"CityID": 589,
|
|
|
|
|
"CityName": "York",
|
|
|
|
|
"Customers": [
|
|
|
|
|
{
|
|
|
|
|
"CustomerID": 497,
|
|
|
|
|
"LastName": "Sledge",
|
|
|
|
|
"Address": {
|
|
|
|
|
"AddressID": 502,
|
|
|
|
|
"AddressLine": "1515 Korla Way"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]
|
2019-08-06 11:41:45 +02:00
|
|
|
`)
|
2019-07-13 13:17:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-14 09:18:23 +01:00
|
|
|
func TestJoinQuerySliceWithPtrs(t *testing.T) {
|
|
|
|
|
type FilmsPerLanguage struct {
|
|
|
|
|
Language model.Language
|
2019-03-30 10:17:32 +01:00
|
|
|
Film *[]*model.Film
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
limit := int64(3)
|
|
|
|
|
|
2019-05-29 14:03:38 +02:00
|
|
|
query := Film.INNER_JOIN(Language, Film.LanguageID.EQ(Language.LanguageID)).
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(Language.AllColumns, Film.AllColumns).
|
2019-04-29 14:39:48 +02:00
|
|
|
LIMIT(limit)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
filmsPerLanguageWithPtrs := []*FilmsPerLanguage{}
|
2019-04-20 19:49:29 +02:00
|
|
|
err := query.Query(db, &filmsPerLanguageWithPtrs)
|
2019-03-14 09:18:23 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(filmsPerLanguageWithPtrs), 1)
|
2019-03-30 10:17:32 +01:00
|
|
|
assert.Equal(t, len(*filmsPerLanguageWithPtrs[0].Film), int(limit))
|
2019-03-14 09:18:23 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-15 21:55:43 +01:00
|
|
|
func TestSelect_WithoutUniqueColumnSelected(t *testing.T) {
|
2019-04-03 11:03:07 +02:00
|
|
|
query := Customer.SELECT(Customer.FirstName, Customer.LastName, Customer.Email)
|
2019-03-15 21:55:43 +01:00
|
|
|
|
|
|
|
|
customers := []model.Customer{}
|
|
|
|
|
|
2019-04-20 19:49:29 +02:00
|
|
|
err := query.Query(db, &customers)
|
2019-03-15 21:55:43 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
//spew.Dump(customers)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(customers), 599)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectOrderByAscDesc(t *testing.T) {
|
|
|
|
|
customersAsc := []model.Customer{}
|
|
|
|
|
|
2019-04-03 11:03:07 +02:00
|
|
|
err := Customer.SELECT(Customer.CustomerID, Customer.FirstName, Customer.LastName).
|
2019-05-05 18:03:30 +02:00
|
|
|
ORDER_BY(Customer.FirstName.ASC()).
|
2019-04-20 19:49:29 +02:00
|
|
|
Query(db, &customersAsc)
|
2019-03-15 21:55:43 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
firstCustomerAsc := customersAsc[0]
|
|
|
|
|
lastCustomerAsc := customersAsc[len(customersAsc)-1]
|
|
|
|
|
|
|
|
|
|
customersDesc := []model.Customer{}
|
2019-04-03 11:03:07 +02:00
|
|
|
err = Customer.SELECT(Customer.CustomerID, Customer.FirstName, Customer.LastName).
|
2019-05-05 18:03:30 +02:00
|
|
|
ORDER_BY(Customer.FirstName.DESC()).
|
2019-04-20 19:49:29 +02:00
|
|
|
Query(db, &customersDesc)
|
2019-03-15 21:55:43 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
firstCustomerDesc := customersDesc[0]
|
|
|
|
|
lastCustomerDesc := customersDesc[len(customersAsc)-1]
|
|
|
|
|
|
|
|
|
|
assert.DeepEqual(t, firstCustomerAsc, lastCustomerDesc)
|
|
|
|
|
assert.DeepEqual(t, lastCustomerAsc, firstCustomerDesc)
|
|
|
|
|
|
|
|
|
|
customersAscDesc := []model.Customer{}
|
2019-04-03 11:03:07 +02:00
|
|
|
err = Customer.SELECT(Customer.CustomerID, Customer.FirstName, Customer.LastName).
|
2019-05-05 18:03:30 +02:00
|
|
|
ORDER_BY(Customer.FirstName.ASC(), Customer.LastName.DESC()).
|
2019-04-20 19:49:29 +02:00
|
|
|
Query(db, &customersAscDesc)
|
2019-03-15 21:55:43 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
customerAscDesc326 := model.Customer{
|
|
|
|
|
CustomerID: 67,
|
|
|
|
|
FirstName: "Kelly",
|
|
|
|
|
LastName: "Torres",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
customerAscDesc327 := model.Customer{
|
|
|
|
|
CustomerID: 546,
|
|
|
|
|
FirstName: "Kelly",
|
|
|
|
|
LastName: "Knott",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert.DeepEqual(t, customerAscDesc326, customersAscDesc[326])
|
|
|
|
|
assert.DeepEqual(t, customerAscDesc327, customersAscDesc[327])
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-16 14:13:35 +01:00
|
|
|
func TestSelectFullJoin(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT customer.customer_id AS "customer.customer_id",
|
|
|
|
|
customer.store_id AS "customer.store_id",
|
|
|
|
|
customer.first_name AS "customer.first_name",
|
|
|
|
|
customer.last_name AS "customer.last_name",
|
|
|
|
|
customer.email AS "customer.email",
|
|
|
|
|
customer.address_id AS "customer.address_id",
|
|
|
|
|
customer.activebool AS "customer.activebool",
|
|
|
|
|
customer.create_date AS "customer.create_date",
|
|
|
|
|
customer.last_update AS "customer.last_update",
|
|
|
|
|
customer.active AS "customer.active",
|
|
|
|
|
address.address_id AS "address.address_id",
|
|
|
|
|
address.address AS "address.address",
|
|
|
|
|
address.address2 AS "address.address2",
|
|
|
|
|
address.district AS "address.district",
|
|
|
|
|
address.city_id AS "address.city_id",
|
|
|
|
|
address.postal_code AS "address.postal_code",
|
|
|
|
|
address.phone AS "address.phone",
|
|
|
|
|
address.last_update AS "address.last_update"
|
|
|
|
|
FROM dvds.customer
|
2019-05-31 14:37:51 +02:00
|
|
|
FULL JOIN dvds.address ON (customer.address_id = address.address_id)
|
2019-05-12 18:15:23 +02:00
|
|
|
ORDER BY customer.customer_id ASC;
|
|
|
|
|
`
|
2019-03-16 14:13:35 +01:00
|
|
|
query := Customer.
|
2019-05-29 14:03:38 +02:00
|
|
|
FULL_JOIN(Address, Customer.AddressID.EQ(Address.AddressID)).
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(Customer.AllColumns, Address.AllColumns).
|
2019-05-05 18:03:30 +02:00
|
|
|
ORDER_BY(Customer.CustomerID.ASC())
|
2019-03-16 14:13:35 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL)
|
2019-03-16 14:13:35 +01:00
|
|
|
|
|
|
|
|
allCustomersAndAddress := []struct {
|
|
|
|
|
Address *model.Address
|
|
|
|
|
Customer *model.Customer
|
|
|
|
|
}{}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &allCustomersAndAddress)
|
2019-03-16 14:13:35 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(allCustomersAndAddress), 603)
|
|
|
|
|
|
|
|
|
|
assert.DeepEqual(t, allCustomersAndAddress[0].Customer, &customer0)
|
|
|
|
|
assert.Assert(t, allCustomersAndAddress[0].Address != nil)
|
|
|
|
|
|
|
|
|
|
lastCustomerAddress := allCustomersAndAddress[len(allCustomersAndAddress)-1]
|
|
|
|
|
|
|
|
|
|
assert.Assert(t, lastCustomerAddress.Customer == nil)
|
|
|
|
|
assert.Assert(t, lastCustomerAddress.Address != nil)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectFullCrossJoin(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT customer.customer_id AS "customer.customer_id",
|
|
|
|
|
customer.store_id AS "customer.store_id",
|
|
|
|
|
customer.first_name AS "customer.first_name",
|
|
|
|
|
customer.last_name AS "customer.last_name",
|
|
|
|
|
customer.email AS "customer.email",
|
|
|
|
|
customer.address_id AS "customer.address_id",
|
|
|
|
|
customer.activebool AS "customer.activebool",
|
|
|
|
|
customer.create_date AS "customer.create_date",
|
|
|
|
|
customer.last_update AS "customer.last_update",
|
|
|
|
|
customer.active AS "customer.active",
|
|
|
|
|
address.address_id AS "address.address_id",
|
|
|
|
|
address.address AS "address.address",
|
|
|
|
|
address.address2 AS "address.address2",
|
|
|
|
|
address.district AS "address.district",
|
|
|
|
|
address.city_id AS "address.city_id",
|
|
|
|
|
address.postal_code AS "address.postal_code",
|
|
|
|
|
address.phone AS "address.phone",
|
|
|
|
|
address.last_update AS "address.last_update"
|
|
|
|
|
FROM dvds.customer
|
|
|
|
|
CROSS JOIN dvds.address
|
|
|
|
|
ORDER BY customer.customer_id ASC
|
|
|
|
|
LIMIT 1000;
|
|
|
|
|
`
|
2019-03-16 14:13:35 +01:00
|
|
|
query := Customer.
|
2019-05-05 12:37:23 +02:00
|
|
|
CROSS_JOIN(Address).
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(Customer.AllColumns, Address.AllColumns).
|
2019-05-05 18:03:30 +02:00
|
|
|
ORDER_BY(Customer.CustomerID.ASC()).
|
2019-04-29 14:39:48 +02:00
|
|
|
LIMIT(1000)
|
2019-03-16 14:13:35 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(1000))
|
2019-03-16 14:13:35 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
var customerAddresCrosJoined []struct {
|
|
|
|
|
model.Customer
|
|
|
|
|
model.Address
|
|
|
|
|
}
|
2019-03-16 14:13:35 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &customerAddresCrosJoined)
|
2019-03-16 14:13:35 +01:00
|
|
|
|
2019-03-17 10:21:44 +01:00
|
|
|
assert.Equal(t, len(customerAddresCrosJoined), 1000)
|
|
|
|
|
|
2019-03-16 14:13:35 +01:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
}
|
2019-03-16 20:41:06 +01:00
|
|
|
|
|
|
|
|
func TestSelectSelfJoin(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT f1.film_id AS "f1.film_id",
|
|
|
|
|
f1.title AS "f1.title",
|
|
|
|
|
f1.description AS "f1.description",
|
|
|
|
|
f1.release_year AS "f1.release_year",
|
|
|
|
|
f1.language_id AS "f1.language_id",
|
|
|
|
|
f1.rental_duration AS "f1.rental_duration",
|
|
|
|
|
f1.rental_rate AS "f1.rental_rate",
|
|
|
|
|
f1.length AS "f1.length",
|
|
|
|
|
f1.replacement_cost AS "f1.replacement_cost",
|
|
|
|
|
f1.rating AS "f1.rating",
|
|
|
|
|
f1.last_update AS "f1.last_update",
|
|
|
|
|
f1.special_features AS "f1.special_features",
|
|
|
|
|
f1.fulltext AS "f1.fulltext",
|
|
|
|
|
f2.film_id AS "f2.film_id",
|
|
|
|
|
f2.title AS "f2.title",
|
|
|
|
|
f2.description AS "f2.description",
|
|
|
|
|
f2.release_year AS "f2.release_year",
|
|
|
|
|
f2.language_id AS "f2.language_id",
|
|
|
|
|
f2.rental_duration AS "f2.rental_duration",
|
|
|
|
|
f2.rental_rate AS "f2.rental_rate",
|
|
|
|
|
f2.length AS "f2.length",
|
|
|
|
|
f2.replacement_cost AS "f2.replacement_cost",
|
|
|
|
|
f2.rating AS "f2.rating",
|
|
|
|
|
f2.last_update AS "f2.last_update",
|
|
|
|
|
f2.special_features AS "f2.special_features",
|
|
|
|
|
f2.fulltext AS "f2.fulltext"
|
|
|
|
|
FROM dvds.film AS f1
|
2019-06-05 17:15:20 +02:00
|
|
|
INNER JOIN dvds.film AS f2 ON ((f1.film_id < f2.film_id) AND (f1.length = f2.length))
|
2019-05-20 17:37:55 +02:00
|
|
|
ORDER BY f1.film_id ASC;
|
2019-05-12 18:15:23 +02:00
|
|
|
`
|
2019-05-05 18:03:30 +02:00
|
|
|
f1 := Film.AS("f1")
|
2019-03-16 20:41:06 +01:00
|
|
|
|
2019-05-05 18:03:30 +02:00
|
|
|
f2 := Film.AS("f2")
|
2019-03-16 20:41:06 +01:00
|
|
|
|
|
|
|
|
query := f1.
|
2019-05-29 14:03:38 +02:00
|
|
|
INNER_JOIN(f2, f1.FilmID.LT(f2.FilmID).AND(f1.Length.EQ(f2.Length))).
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(f1.AllColumns, f2.AllColumns).
|
2019-05-20 17:37:55 +02:00
|
|
|
ORDER_BY(f1.FilmID.ASC())
|
2019-03-16 20:41:06 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL)
|
2019-03-16 20:41:06 +01:00
|
|
|
|
|
|
|
|
type F1 model.Film
|
|
|
|
|
type F2 model.Film
|
|
|
|
|
|
|
|
|
|
theSameLengthFilms := []struct {
|
|
|
|
|
F1 F1
|
|
|
|
|
F2 F2
|
|
|
|
|
}{}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &theSameLengthFilms)
|
2019-03-16 20:41:06 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
//spew.Dump(theSameLengthFilms)
|
|
|
|
|
|
|
|
|
|
//assert.Equal(t, len(theSameLengthFilms), 100)
|
2019-03-16 20:41:06 +01:00
|
|
|
}
|
|
|
|
|
|
2019-03-17 20:41:03 +01:00
|
|
|
func TestSelectAliasColumn(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT f1.title AS "thesame_length_films.title1",
|
|
|
|
|
f2.title AS "thesame_length_films.title2",
|
|
|
|
|
f1.length AS "thesame_length_films.length"
|
|
|
|
|
FROM dvds.film AS f1
|
2019-06-05 17:15:20 +02:00
|
|
|
INNER JOIN dvds.film AS f2 ON ((f1.film_id != f2.film_id) AND (f1.length = f2.length))
|
2019-05-12 18:15:23 +02:00
|
|
|
ORDER BY f1.length ASC, f1.title ASC, f2.title ASC
|
|
|
|
|
LIMIT 1000;
|
|
|
|
|
`
|
2019-05-05 18:03:30 +02:00
|
|
|
f1 := Film.AS("f1")
|
|
|
|
|
f2 := Film.AS("f2")
|
2019-03-17 20:41:03 +01:00
|
|
|
|
2019-05-31 12:59:57 +02:00
|
|
|
f1.FilmID.EQ(Int(11))
|
2019-05-29 14:03:38 +02:00
|
|
|
|
2019-03-17 20:41:03 +01:00
|
|
|
query := f1.
|
2019-05-29 14:03:38 +02:00
|
|
|
INNER_JOIN(f2, f1.FilmID.NOT_EQ(f2.FilmID).AND(f1.Length.EQ(f2.Length))).
|
2019-05-05 18:03:30 +02:00
|
|
|
SELECT(f1.Title.AS("thesame_length_films.title1"),
|
|
|
|
|
f2.Title.AS("thesame_length_films.title2"),
|
|
|
|
|
f1.Length.AS("thesame_length_films.length")).
|
|
|
|
|
ORDER_BY(f1.Length.ASC(), f1.Title.ASC(), f2.Title.ASC()).
|
2019-04-29 14:39:48 +02:00
|
|
|
LIMIT(1000)
|
2019-03-17 20:41:03 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(1000))
|
2019-03-17 20:41:03 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
type thesameLengthFilms struct {
|
|
|
|
|
Title1 string
|
|
|
|
|
Title2 string
|
|
|
|
|
Length int16
|
|
|
|
|
}
|
2019-03-17 20:41:03 +01:00
|
|
|
films := []thesameLengthFilms{}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &films)
|
2019-03-17 20:41:03 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
//spew.Dump(films)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(films), 1000)
|
2019-03-30 10:17:32 +01:00
|
|
|
assert.DeepEqual(t, films[0], thesameLengthFilms{"Alien Center", "Iron Moon", 46})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSubQuery(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedQuery := `
|
|
|
|
|
SELECT actor.actor_id AS "actor.actor_id",
|
|
|
|
|
actor.first_name AS "actor.first_name",
|
|
|
|
|
actor.last_name AS "actor.last_name",
|
|
|
|
|
actor.last_update AS "actor.last_update",
|
|
|
|
|
film_actor.actor_id AS "film_actor.actor_id",
|
|
|
|
|
film_actor.film_id AS "film_actor.film_id",
|
|
|
|
|
film_actor.last_update AS "film_actor.last_update",
|
2019-06-29 16:58:41 +02:00
|
|
|
"rFilms"."film.film_id" AS "film.film_id",
|
|
|
|
|
"rFilms"."film.title" AS "film.title",
|
|
|
|
|
"rFilms"."film.rating" AS "film.rating"
|
2019-05-12 18:15:23 +02:00
|
|
|
FROM dvds.actor
|
2019-06-05 17:15:20 +02:00
|
|
|
INNER JOIN dvds.film_actor ON (actor.actor_id = film_actor.film_id)
|
|
|
|
|
INNER JOIN (
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT film.film_id AS "film.film_id",
|
|
|
|
|
film.title AS "film.title",
|
|
|
|
|
film.rating AS "film.rating"
|
|
|
|
|
FROM dvds.film
|
|
|
|
|
WHERE film.rating = 'R'
|
2019-06-17 12:05:52 +02:00
|
|
|
) AS "rFilms" ON (film_actor.film_id = "rFilms"."film.film_id");
|
2019-05-12 18:15:23 +02:00
|
|
|
`
|
|
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
rRatingFilms := Film.
|
2019-06-07 14:23:14 +02:00
|
|
|
SELECT(
|
|
|
|
|
Film.FilmID,
|
|
|
|
|
Film.Title,
|
|
|
|
|
Film.Rating,
|
|
|
|
|
).
|
2019-06-03 17:05:29 +02:00
|
|
|
WHERE(Film.Rating.EQ(enum.MpaaRating.R)).
|
2019-06-17 12:05:52 +02:00
|
|
|
AsTable("rFilms")
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
rFilmID := Film.FilmID.From(rRatingFilms)
|
2019-06-07 14:23:14 +02:00
|
|
|
|
|
|
|
|
query := Actor.
|
|
|
|
|
INNER_JOIN(FilmActor, Actor.ActorID.EQ(FilmActor.FilmID)).
|
2019-07-18 18:42:03 +02:00
|
|
|
INNER_JOIN(rRatingFilms, FilmActor.FilmID.EQ(rFilmID)).
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(
|
2019-03-30 10:17:32 +01:00
|
|
|
Actor.AllColumns,
|
|
|
|
|
FilmActor.AllColumns,
|
2019-06-29 16:58:41 +02:00
|
|
|
rRatingFilms.AllColumns(),
|
2019-03-30 10:17:32 +01:00
|
|
|
)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedQuery)
|
2019-05-12 18:15:23 +02:00
|
|
|
|
|
|
|
|
dest := []model.Actor{}
|
|
|
|
|
|
|
|
|
|
err := query.Query(db, &dest)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
assert.NilError(t, err)
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectFunctions(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedQuery := `
|
|
|
|
|
SELECT MAX(film.rental_rate) AS "max_film_rate"
|
|
|
|
|
FROM dvds.film;
|
|
|
|
|
`
|
2019-05-31 12:59:57 +02:00
|
|
|
query := Film.SELECT(
|
|
|
|
|
MAXf(Film.RentalRate).AS("max_film_rate"),
|
|
|
|
|
)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedQuery)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
ret := struct {
|
|
|
|
|
MaxFilmRate float64
|
|
|
|
|
}{}
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &ret)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, ret.MaxFilmRate, 4.99)
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectQueryScalar(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT film.film_id AS "film.film_id",
|
|
|
|
|
film.title AS "film.title",
|
|
|
|
|
film.description AS "film.description",
|
|
|
|
|
film.release_year AS "film.release_year",
|
|
|
|
|
film.language_id AS "film.language_id",
|
|
|
|
|
film.rental_duration AS "film.rental_duration",
|
|
|
|
|
film.rental_rate AS "film.rental_rate",
|
|
|
|
|
film.length AS "film.length",
|
|
|
|
|
film.replacement_cost AS "film.replacement_cost",
|
|
|
|
|
film.rating AS "film.rating",
|
|
|
|
|
film.last_update AS "film.last_update",
|
|
|
|
|
film.special_features AS "film.special_features",
|
|
|
|
|
film.fulltext AS "film.fulltext"
|
|
|
|
|
FROM dvds.film
|
|
|
|
|
WHERE film.rental_rate = (
|
|
|
|
|
SELECT MAX(film.rental_rate)
|
|
|
|
|
FROM dvds.film
|
2019-07-08 13:00:44 +02:00
|
|
|
)
|
2019-05-12 18:15:23 +02:00
|
|
|
ORDER BY film.film_id ASC;
|
|
|
|
|
`
|
|
|
|
|
|
2019-07-08 13:00:44 +02:00
|
|
|
maxFilmRentalRate := FloatExp(
|
2019-07-07 12:19:05 +02:00
|
|
|
Film.
|
|
|
|
|
SELECT(MAXf(Film.RentalRate)),
|
2019-07-08 13:00:44 +02:00
|
|
|
)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
query := Film.
|
|
|
|
|
SELECT(Film.AllColumns).
|
2019-05-29 14:03:38 +02:00
|
|
|
WHERE(Film.RentalRate.EQ(maxFilmRentalRate)).
|
2019-05-05 18:03:30 +02:00
|
|
|
ORDER_BY(Film.FilmID.ASC())
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
maxRentalRateFilms := []model.Film{}
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &maxRentalRateFilms)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(maxRentalRateFilms), 336)
|
|
|
|
|
|
2019-04-03 19:21:46 +02:00
|
|
|
gRating := model.MpaaRating_G
|
|
|
|
|
|
2019-03-30 10:17:32 +01:00
|
|
|
assert.DeepEqual(t, maxRentalRateFilms[0], model.Film{
|
|
|
|
|
FilmID: 2,
|
|
|
|
|
Title: "Ace Goldfinger",
|
2019-07-29 18:08:53 +02:00
|
|
|
Description: StringPtr("A Astounding Epistle of a Database Administrator And a Explorer who must Find a Car in Ancient China"),
|
|
|
|
|
ReleaseYear: Int32Ptr(2006),
|
2019-05-20 17:37:55 +02:00
|
|
|
LanguageID: 1,
|
2019-03-30 10:17:32 +01:00
|
|
|
RentalRate: 4.99,
|
2019-07-29 18:08:53 +02:00
|
|
|
Length: Int16Ptr(48),
|
2019-03-30 10:17:32 +01:00
|
|
|
ReplacementCost: 12.99,
|
2019-04-03 19:21:46 +02:00
|
|
|
Rating: &gRating,
|
2019-03-30 10:17:32 +01:00
|
|
|
RentalDuration: 3,
|
2019-07-29 18:08:53 +02:00
|
|
|
LastUpdate: *testutils.TimestampWithoutTimeZone("2013-05-26 14:50:58.951", 3),
|
|
|
|
|
SpecialFeatures: StringPtr("{Trailers,\"Deleted Scenes\"}"),
|
2019-03-30 10:17:32 +01:00
|
|
|
Fulltext: "'ace':1 'administr':9 'ancient':19 'astound':4 'car':17 'china':20 'databas':8 'epistl':5 'explor':12 'find':15 'goldfing':2 'must':14",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectGroupByHaving(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-08-01 16:56:54 +02:00
|
|
|
SELECT customer.customer_id AS "customer.customer_id",
|
|
|
|
|
customer.store_id AS "customer.store_id",
|
|
|
|
|
customer.first_name AS "customer.first_name",
|
|
|
|
|
customer.last_name AS "customer.last_name",
|
|
|
|
|
customer.email AS "customer.email",
|
|
|
|
|
customer.address_id AS "customer.address_id",
|
|
|
|
|
customer.activebool AS "customer.activebool",
|
|
|
|
|
customer.create_date AS "customer.create_date",
|
|
|
|
|
customer.last_update AS "customer.last_update",
|
|
|
|
|
customer.active AS "customer.active",
|
|
|
|
|
SUM(payment.amount) AS "amount.sum",
|
|
|
|
|
AVG(payment.amount) AS "amount.avg",
|
|
|
|
|
MAX(payment.amount) AS "amount.max",
|
|
|
|
|
MIN(payment.amount) AS "amount.min",
|
|
|
|
|
COUNT(payment.amount) AS "amount.count"
|
2019-05-12 18:15:23 +02:00
|
|
|
FROM dvds.payment
|
2019-08-01 16:56:54 +02:00
|
|
|
INNER JOIN dvds.customer ON (customer.customer_id = payment.customer_id)
|
|
|
|
|
GROUP BY customer.customer_id
|
|
|
|
|
HAVING SUM(payment.amount) > 125.6
|
|
|
|
|
ORDER BY customer.customer_id, SUM(payment.amount) ASC;
|
2019-05-12 18:15:23 +02:00
|
|
|
`
|
2019-08-01 16:56:54 +02:00
|
|
|
query := Payment.
|
|
|
|
|
INNER_JOIN(Customer, Customer.CustomerID.EQ(Payment.CustomerID)).
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(
|
2019-08-01 16:56:54 +02:00
|
|
|
Customer.AllColumns,
|
|
|
|
|
|
|
|
|
|
SUMf(Payment.Amount).AS("amount.sum"),
|
|
|
|
|
AVG(Payment.Amount).AS("amount.avg"),
|
|
|
|
|
MAXf(Payment.Amount).AS("amount.max"),
|
|
|
|
|
MINf(Payment.Amount).AS("amount.min"),
|
|
|
|
|
COUNT(Payment.Amount).AS("amount.count"),
|
2019-05-29 14:03:38 +02:00
|
|
|
).
|
2019-08-01 16:56:54 +02:00
|
|
|
GROUP_BY(Customer.CustomerID).
|
2019-05-29 14:03:38 +02:00
|
|
|
HAVING(
|
2019-08-01 16:56:54 +02:00
|
|
|
SUMf(Payment.Amount).GT(Float(125.6)),
|
|
|
|
|
).
|
|
|
|
|
ORDER_BY(
|
|
|
|
|
Customer.CustomerID, SUMf(Payment.Amount).ASC(),
|
2019-05-29 14:03:38 +02:00
|
|
|
)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-08-01 16:56:54 +02:00
|
|
|
//fmt.Println(query.DebugSql())
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-08-01 16:56:54 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, float64(125.6))
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-08-01 16:56:54 +02:00
|
|
|
var dest []struct {
|
|
|
|
|
model.Customer
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-08-01 16:56:54 +02:00
|
|
|
Amount struct {
|
|
|
|
|
Sum float64
|
|
|
|
|
Avg float64
|
|
|
|
|
Max float64
|
|
|
|
|
Min float64
|
|
|
|
|
Count int64
|
|
|
|
|
} `alias:"amount"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := query.Query(db, &dest)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-08-01 16:56:54 +02:00
|
|
|
//testutils.JsonPrint(dest)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(dest), 104)
|
|
|
|
|
|
|
|
|
|
//testutils.JsonSave(dest, "postgres/testdata/customer_payment_sum.json")
|
|
|
|
|
testutils.AssertJSONFile(t, dest, "postgres/testdata/customer_payment_sum.json")
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectGroupBy2(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT customer.customer_id AS "customer.customer_id",
|
|
|
|
|
customer.store_id AS "customer.store_id",
|
|
|
|
|
customer.first_name AS "customer.first_name",
|
|
|
|
|
customer.last_name AS "customer.last_name",
|
|
|
|
|
customer.email AS "customer.email",
|
|
|
|
|
customer.address_id AS "customer.address_id",
|
|
|
|
|
customer.activebool AS "customer.activebool",
|
|
|
|
|
customer.create_date AS "customer.create_date",
|
|
|
|
|
customer.last_update AS "customer.last_update",
|
|
|
|
|
customer.active AS "customer.active",
|
2019-06-18 14:35:32 +02:00
|
|
|
customer_payment_sum."amount_sum" AS "CustomerWithAmounts.AmountSum"
|
2019-05-12 18:15:23 +02:00
|
|
|
FROM dvds.customer
|
2019-06-05 17:15:20 +02:00
|
|
|
INNER JOIN (
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT payment.customer_id AS "payment.customer_id",
|
|
|
|
|
SUM(payment.amount) AS "amount_sum"
|
|
|
|
|
FROM dvds.payment
|
|
|
|
|
GROUP BY payment.customer_id
|
2019-05-31 14:37:51 +02:00
|
|
|
) AS customer_payment_sum ON (customer.customer_id = customer_payment_sum."payment.customer_id")
|
2019-06-18 14:35:32 +02:00
|
|
|
ORDER BY customer_payment_sum."amount_sum" ASC;
|
2019-05-12 18:15:23 +02:00
|
|
|
`
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-06-08 16:34:15 +02:00
|
|
|
customersPayments := Payment.
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(
|
2019-03-30 10:17:32 +01:00
|
|
|
Payment.CustomerID,
|
2019-05-31 12:59:57 +02:00
|
|
|
SUMf(Payment.Amount).AS("amount_sum"),
|
2019-03-30 10:17:32 +01:00
|
|
|
).
|
2019-06-08 16:34:15 +02:00
|
|
|
GROUP_BY(Payment.CustomerID).
|
|
|
|
|
AsTable("customer_payment_sum")
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
customerID := Payment.CustomerID.From(customersPayments)
|
2019-06-08 16:34:15 +02:00
|
|
|
amountSum := FloatColumn("amount_sum").From(customersPayments)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
query := Customer.
|
2019-07-18 18:42:03 +02:00
|
|
|
INNER_JOIN(customersPayments, Customer.CustomerID.EQ(customerID)).
|
2019-06-07 14:23:14 +02:00
|
|
|
SELECT(
|
|
|
|
|
Customer.AllColumns,
|
2019-06-18 14:35:32 +02:00
|
|
|
amountSum.AS("CustomerWithAmounts.AmountSum"),
|
2019-06-07 14:23:14 +02:00
|
|
|
).
|
2019-06-08 16:34:15 +02:00
|
|
|
ORDER_BY(amountSum.ASC())
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
type CustomerWithAmounts struct {
|
|
|
|
|
Customer *model.Customer
|
|
|
|
|
AmountSum float64
|
|
|
|
|
}
|
|
|
|
|
customersWithAmounts := []CustomerWithAmounts{}
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &customersWithAmounts)
|
|
|
|
|
assert.NilError(t, err)
|
2019-03-30 10:17:32 +01:00
|
|
|
assert.Equal(t, len(customersWithAmounts), 599)
|
|
|
|
|
|
|
|
|
|
assert.DeepEqual(t, customersWithAmounts[0].Customer, &model.Customer{
|
|
|
|
|
CustomerID: 318,
|
|
|
|
|
StoreID: 1,
|
|
|
|
|
FirstName: "Brian",
|
|
|
|
|
LastName: "Wyman",
|
2019-05-20 17:37:55 +02:00
|
|
|
AddressID: 323,
|
2019-07-29 18:08:53 +02:00
|
|
|
Email: StringPtr("brian.wyman@sakilacustomer.org"),
|
2019-03-30 10:17:32 +01:00
|
|
|
Activebool: true,
|
2019-07-29 18:08:53 +02:00
|
|
|
CreateDate: *testutils.TimestampWithoutTimeZone("2006-02-14 00:00:00", 0),
|
|
|
|
|
LastUpdate: testutils.TimestampWithoutTimeZone("2013-05-26 14:49:45.738", 3),
|
|
|
|
|
Active: Int32Ptr(1),
|
2019-03-30 10:17:32 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, customersWithAmounts[0].AmountSum, 27.93)
|
2019-04-03 14:18:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-27 13:11:15 +02:00
|
|
|
func TestSelectStaff(t *testing.T) {
|
|
|
|
|
staffs := []model.Staff{}
|
|
|
|
|
|
|
|
|
|
err := Staff.SELECT(Staff.AllColumns).Query(db, &staffs)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-08-06 11:41:45 +02:00
|
|
|
testutils.AssertJSON(t, staffs, `
|
2019-07-13 13:17:28 +02:00
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"StaffID": 1,
|
|
|
|
|
"FirstName": "Mike",
|
|
|
|
|
"LastName": "Hillyer",
|
|
|
|
|
"AddressID": 3,
|
|
|
|
|
"Email": "Mike.Hillyer@sakilastaff.com",
|
|
|
|
|
"StoreID": 1,
|
|
|
|
|
"Active": true,
|
|
|
|
|
"Username": "Mike",
|
|
|
|
|
"Password": "8cb2237d0679ca88db6464eac60da96345513964",
|
|
|
|
|
"LastUpdate": "2006-05-16T16:13:11.79328Z",
|
|
|
|
|
"Picture": "iVBORw0KWgo="
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"StaffID": 2,
|
|
|
|
|
"FirstName": "Jon",
|
|
|
|
|
"LastName": "Stephens",
|
|
|
|
|
"AddressID": 4,
|
|
|
|
|
"Email": "Jon.Stephens@sakilastaff.com",
|
|
|
|
|
"StoreID": 2,
|
|
|
|
|
"Active": true,
|
|
|
|
|
"Username": "Jon",
|
|
|
|
|
"Password": "8cb2237d0679ca88db6464eac60da96345513964",
|
|
|
|
|
"LastUpdate": "2006-05-16T16:13:11.79328Z",
|
|
|
|
|
"Picture": null
|
|
|
|
|
}
|
|
|
|
|
]
|
2019-08-06 11:41:45 +02:00
|
|
|
`)
|
2019-05-27 13:11:15 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-03 14:18:58 +02:00
|
|
|
func TestSelectTimeColumns(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-05-12 18:15:23 +02:00
|
|
|
SELECT payment.payment_id AS "payment.payment_id",
|
|
|
|
|
payment.customer_id AS "payment.customer_id",
|
|
|
|
|
payment.staff_id AS "payment.staff_id",
|
|
|
|
|
payment.rental_id AS "payment.rental_id",
|
|
|
|
|
payment.amount AS "payment.amount",
|
|
|
|
|
payment.payment_date AS "payment.payment_date"
|
|
|
|
|
FROM dvds.payment
|
2019-06-03 14:41:39 +02:00
|
|
|
WHERE payment.payment_date < '2007-02-14 22:16:01.000'::timestamp without time zone
|
2019-05-12 18:15:23 +02:00
|
|
|
ORDER BY payment.payment_date ASC;
|
|
|
|
|
`
|
|
|
|
|
|
2019-04-03 14:18:58 +02:00
|
|
|
query := Payment.SELECT(Payment.AllColumns).
|
2019-05-30 14:49:36 +02:00
|
|
|
WHERE(Payment.PaymentDate.LT(Timestamp(2007, 02, 14, 22, 16, 01, 0))).
|
2019-05-05 18:03:30 +02:00
|
|
|
ORDER_BY(Payment.PaymentDate.ASC())
|
2019-04-03 14:18:58 +02:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL, "2007-02-14 22:16:01.000")
|
2019-04-03 14:18:58 +02:00
|
|
|
|
|
|
|
|
payments := []model.Payment{}
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &payments)
|
2019-04-03 14:18:58 +02:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
//spew.Dump(payments)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(payments), 9)
|
|
|
|
|
assert.DeepEqual(t, payments[0], model.Payment{
|
|
|
|
|
PaymentID: 17793,
|
2019-05-20 17:37:55 +02:00
|
|
|
CustomerID: 416,
|
|
|
|
|
StaffID: 2,
|
|
|
|
|
RentalID: 1158,
|
2019-04-03 14:18:58 +02:00
|
|
|
Amount: 2.99,
|
2019-07-29 18:08:53 +02:00
|
|
|
PaymentDate: *testutils.TimestampWithoutTimeZone("2007-02-14 21:21:59.996577", 6),
|
2019-04-03 14:18:58 +02:00
|
|
|
})
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-03 11:59:18 +02:00
|
|
|
func TestUnion(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedQuery := `
|
|
|
|
|
(
|
|
|
|
|
(
|
|
|
|
|
SELECT payment.payment_id AS "payment.payment_id",
|
|
|
|
|
payment.amount AS "payment.amount"
|
|
|
|
|
FROM dvds.payment
|
|
|
|
|
WHERE payment.amount <= 100
|
|
|
|
|
)
|
|
|
|
|
UNION ALL
|
|
|
|
|
(
|
|
|
|
|
SELECT payment.payment_id AS "payment.payment_id",
|
|
|
|
|
payment.amount AS "payment.amount"
|
|
|
|
|
FROM dvds.payment
|
|
|
|
|
WHERE payment.amount >= 200
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
ORDER BY "payment.payment_id" ASC, "payment.amount" DESC
|
|
|
|
|
LIMIT 10
|
|
|
|
|
OFFSET 20;
|
|
|
|
|
`
|
|
|
|
|
query := UNION_ALL(
|
2019-05-03 11:59:18 +02:00
|
|
|
Payment.
|
2019-05-05 18:03:30 +02:00
|
|
|
SELECT(Payment.PaymentID.AS("payment.payment_id"), Payment.Amount).
|
2019-05-31 12:59:57 +02:00
|
|
|
WHERE(Payment.Amount.LT_EQ(Float(100))),
|
2019-05-03 11:59:18 +02:00
|
|
|
Payment.
|
|
|
|
|
SELECT(Payment.PaymentID, Payment.Amount).
|
2019-05-31 12:59:57 +02:00
|
|
|
WHERE(Payment.Amount.GT_EQ(Float(200))),
|
2019-05-03 11:59:18 +02:00
|
|
|
).
|
2019-06-08 16:34:15 +02:00
|
|
|
ORDER_BY(IntegerColumn("payment.payment_id").ASC(), Payment.Amount.DESC()).
|
2019-05-12 18:15:23 +02:00
|
|
|
LIMIT(10).
|
|
|
|
|
OFFSET(20)
|
2019-05-03 11:59:18 +02:00
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedQuery, float64(100), float64(200), int64(10), int64(20))
|
2019-05-03 11:59:18 +02:00
|
|
|
|
|
|
|
|
dest := []model.Payment{}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &dest)
|
2019-05-03 11:59:18 +02:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(dest), 10)
|
|
|
|
|
assert.DeepEqual(t, dest[0], model.Payment{
|
|
|
|
|
PaymentID: 17523,
|
|
|
|
|
Amount: 4.99,
|
|
|
|
|
})
|
|
|
|
|
assert.DeepEqual(t, dest[1], model.Payment{
|
|
|
|
|
PaymentID: 17524,
|
|
|
|
|
Amount: 0.99,
|
|
|
|
|
})
|
|
|
|
|
assert.DeepEqual(t, dest[9], model.Payment{
|
|
|
|
|
PaymentID: 17532,
|
|
|
|
|
Amount: 8.99,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-01 19:41:49 +02:00
|
|
|
func TestAllSetOperators(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
select1 := Payment.SELECT(Payment.AllColumns).WHERE(Payment.PaymentID.GT_EQ(Int(17600)).AND(Payment.PaymentID.LT(Int(17610))))
|
|
|
|
|
select2 := Payment.SELECT(Payment.AllColumns).WHERE(Payment.PaymentID.GT_EQ(Int(17620)).AND(Payment.PaymentID.LT(Int(17630))))
|
|
|
|
|
|
2019-08-03 14:10:47 +02:00
|
|
|
type setOperator func(lhs, rhs SelectStatement, selects ...SelectStatement) SelectStatement
|
2019-07-01 19:41:49 +02:00
|
|
|
operators := []setOperator{
|
|
|
|
|
UNION,
|
|
|
|
|
UNION_ALL,
|
|
|
|
|
INTERSECT,
|
|
|
|
|
INTERSECT_ALL,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedDestLen := []int{
|
|
|
|
|
20,
|
|
|
|
|
20,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
10,
|
|
|
|
|
10,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, operator := range operators {
|
|
|
|
|
query := operator(select1, select2)
|
|
|
|
|
|
|
|
|
|
dest := []model.Payment{}
|
|
|
|
|
err := query.Query(db, &dest)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(dest), expectedDestLen[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 12:42:15 +02:00
|
|
|
func TestSelectWithCase(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedQuery := `
|
|
|
|
|
SELECT (CASE payment.staff_id WHEN 1 THEN 'ONE' WHEN 2 THEN 'TWO' WHEN 3 THEN 'THREE' ELSE 'OTHER' END) AS "staff_id_num"
|
|
|
|
|
FROM dvds.payment
|
|
|
|
|
ORDER BY payment.payment_id ASC
|
|
|
|
|
LIMIT 20;
|
|
|
|
|
`
|
2019-05-06 12:42:15 +02:00
|
|
|
query := Payment.SELECT(
|
2019-05-12 18:15:23 +02:00
|
|
|
CASE(Payment.StaffID).
|
2019-06-04 11:52:37 +02:00
|
|
|
WHEN(Int(1)).THEN(String("ONE")).
|
|
|
|
|
WHEN(Int(2)).THEN(String("TWO")).
|
|
|
|
|
WHEN(Int(3)).THEN(String("THREE")).
|
|
|
|
|
ELSE(String("OTHER")).AS("staff_id_num"),
|
2019-05-06 12:42:15 +02:00
|
|
|
).
|
|
|
|
|
ORDER_BY(Payment.PaymentID.ASC()).
|
|
|
|
|
LIMIT(20)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedQuery, int64(1), "ONE", int64(2), "TWO", int64(3), "THREE", "OTHER", int64(20))
|
2019-05-06 12:42:15 +02:00
|
|
|
|
|
|
|
|
dest := []struct {
|
2019-07-18 18:42:03 +02:00
|
|
|
StaffIDNum string
|
2019-05-06 12:42:15 +02:00
|
|
|
}{}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := query.Query(db, &dest)
|
2019-05-06 12:42:15 +02:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(dest), 20)
|
2019-07-18 18:42:03 +02:00
|
|
|
assert.Equal(t, dest[0].StaffIDNum, "TWO")
|
|
|
|
|
assert.Equal(t, dest[1].StaffIDNum, "ONE")
|
2019-05-06 12:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
func getRowLockTestData() map[SelectLock]string {
|
|
|
|
|
return map[SelectLock]string{
|
|
|
|
|
UPDATE(): "UPDATE",
|
|
|
|
|
NO_KEY_UPDATE(): "NO KEY UPDATE",
|
|
|
|
|
SHARE(): "SHARE",
|
|
|
|
|
KEY_SHARE(): "KEY SHARE",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRowLock(t *testing.T) {
|
2019-07-18 17:43:11 +02:00
|
|
|
expectedSQL := `
|
2019-06-15 13:58:45 +02:00
|
|
|
SELECT *
|
|
|
|
|
FROM dvds.address
|
|
|
|
|
LIMIT 3
|
|
|
|
|
FOR`
|
|
|
|
|
query := Address.
|
|
|
|
|
SELECT(STAR).
|
|
|
|
|
LIMIT(3)
|
|
|
|
|
|
|
|
|
|
for lockType, lockTypeStr := range getRowLockTestData() {
|
|
|
|
|
query.FOR(lockType)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+";\n", int64(3))
|
2019-06-15 13:58:45 +02:00
|
|
|
|
|
|
|
|
tx, _ := db.Begin()
|
|
|
|
|
|
|
|
|
|
res, err := query.Exec(tx)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
rowsAffected, _ := res.RowsAffected()
|
|
|
|
|
assert.Equal(t, rowsAffected, int64(3))
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
err = tx.Rollback()
|
|
|
|
|
assert.NilError(t, err)
|
2019-06-15 13:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for lockType, lockTypeStr := range getRowLockTestData() {
|
|
|
|
|
query.FOR(lockType.NOWAIT())
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+" NOWAIT;\n", int64(3))
|
2019-06-15 13:58:45 +02:00
|
|
|
|
|
|
|
|
tx, _ := db.Begin()
|
|
|
|
|
|
|
|
|
|
res, err := query.Exec(tx)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
rowsAffected, _ := res.RowsAffected()
|
|
|
|
|
assert.Equal(t, rowsAffected, int64(3))
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
err = tx.Rollback()
|
|
|
|
|
assert.NilError(t, err)
|
2019-06-15 13:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for lockType, lockTypeStr := range getRowLockTestData() {
|
|
|
|
|
query.FOR(lockType.SKIP_LOCKED())
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+" SKIP LOCKED;\n", int64(3))
|
2019-06-15 13:58:45 +02:00
|
|
|
|
|
|
|
|
tx, _ := db.Begin()
|
|
|
|
|
|
|
|
|
|
res, err := query.Exec(tx)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
rowsAffected, _ := res.RowsAffected()
|
|
|
|
|
assert.Equal(t, rowsAffected, int64(3))
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
err = tx.Rollback()
|
|
|
|
|
assert.NilError(t, err)
|
2019-06-15 13:58:45 +02:00
|
|
|
}
|
2019-05-07 13:44:30 +02:00
|
|
|
}
|
2019-06-24 18:22:55 +02:00
|
|
|
|
2019-06-26 10:30:31 +02:00
|
|
|
func TestQuickStart(t *testing.T) {
|
2019-06-24 18:22:55 +02:00
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
var expectedSQL = `
|
2019-06-24 18:22:55 +02:00
|
|
|
SELECT actor.actor_id AS "actor.actor_id",
|
|
|
|
|
actor.first_name AS "actor.first_name",
|
|
|
|
|
actor.last_name AS "actor.last_name",
|
|
|
|
|
actor.last_update AS "actor.last_update",
|
|
|
|
|
film.film_id AS "film.film_id",
|
|
|
|
|
film.title AS "film.title",
|
|
|
|
|
film.description AS "film.description",
|
|
|
|
|
film.release_year AS "film.release_year",
|
|
|
|
|
film.language_id AS "film.language_id",
|
|
|
|
|
film.rental_duration AS "film.rental_duration",
|
|
|
|
|
film.rental_rate AS "film.rental_rate",
|
|
|
|
|
film.length AS "film.length",
|
|
|
|
|
film.replacement_cost AS "film.replacement_cost",
|
|
|
|
|
film.rating AS "film.rating",
|
|
|
|
|
film.last_update AS "film.last_update",
|
|
|
|
|
film.special_features AS "film.special_features",
|
|
|
|
|
film.fulltext AS "film.fulltext",
|
|
|
|
|
language.language_id AS "language.language_id",
|
|
|
|
|
language.name AS "language.name",
|
|
|
|
|
language.last_update AS "language.last_update",
|
|
|
|
|
category.category_id AS "category.category_id",
|
|
|
|
|
category.name AS "category.name",
|
|
|
|
|
category.last_update AS "category.last_update"
|
|
|
|
|
FROM dvds.actor
|
|
|
|
|
INNER JOIN dvds.film_actor ON (actor.actor_id = film_actor.actor_id)
|
|
|
|
|
INNER JOIN dvds.film ON (film.film_id = film_actor.film_id)
|
|
|
|
|
INNER JOIN dvds.language ON (language.language_id = film.language_id)
|
|
|
|
|
INNER JOIN dvds.film_category ON (film_category.film_id = film.film_id)
|
|
|
|
|
INNER JOIN dvds.category ON (category.category_id = film_category.category_id)
|
|
|
|
|
WHERE ((language.name = 'English') AND (category.name != 'Action')) AND (film.length > 180)
|
|
|
|
|
ORDER BY actor.actor_id ASC, film.film_id ASC;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
stmt := SELECT(
|
|
|
|
|
Actor.ActorID, Actor.FirstName, Actor.LastName, Actor.LastUpdate, // list of all actor columns (equivalent to Actor.AllColumns)
|
|
|
|
|
Film.AllColumns, // list of all film columns (equivalent to Film.FilmID, Film.Title, ...)
|
|
|
|
|
Language.AllColumns,
|
|
|
|
|
Category.AllColumns,
|
|
|
|
|
).FROM(
|
|
|
|
|
Actor.
|
|
|
|
|
INNER_JOIN(FilmActor, Actor.ActorID.EQ(FilmActor.ActorID)). // INNER JOIN Actor with FilmActor on condition Actor.ActorID = FilmActor.ActorID
|
|
|
|
|
INNER_JOIN(Film, Film.FilmID.EQ(FilmActor.FilmID)). // then with Film, Language, FilmCategory and Category.
|
|
|
|
|
INNER_JOIN(Language, Language.LanguageID.EQ(Film.LanguageID)).
|
|
|
|
|
INNER_JOIN(FilmCategory, FilmCategory.FilmID.EQ(Film.FilmID)).
|
|
|
|
|
INNER_JOIN(Category, Category.CategoryID.EQ(FilmCategory.CategoryID)),
|
|
|
|
|
).WHERE(
|
|
|
|
|
Language.Name.EQ(String("English")). // note that every column has type.
|
|
|
|
|
AND(Category.Name.NOT_EQ(String("Action"))). // String column Language.Name and Category.Name can be compared only with string expression
|
|
|
|
|
AND(Film.Length.GT(Int(180))), // Film.Length is integer column and can be compared only with integer expression
|
|
|
|
|
).ORDER_BY(
|
|
|
|
|
Actor.ActorID.ASC(),
|
|
|
|
|
Film.FilmID.ASC(),
|
|
|
|
|
)
|
|
|
|
|
|
2019-07-30 11:18:12 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, stmt, expectedSQL, "English", "Action", int64(180))
|
2019-06-24 18:22:55 +02:00
|
|
|
|
|
|
|
|
var dest []struct {
|
|
|
|
|
model.Actor
|
|
|
|
|
|
|
|
|
|
Films []struct {
|
|
|
|
|
model.Film
|
|
|
|
|
|
|
|
|
|
Language model.Language
|
|
|
|
|
|
2019-06-26 10:30:31 +02:00
|
|
|
Categories []model.Category
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := stmt.Query(db, &dest)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
//jsonSave("./testdata/quick-start-dest.json", dest)
|
2019-08-01 16:56:54 +02:00
|
|
|
testutils.AssertJSONFile(t, dest, "./postgres/testdata/quick-start-dest.json")
|
2019-06-26 10:30:31 +02:00
|
|
|
|
|
|
|
|
var dest2 []struct {
|
|
|
|
|
model.Category
|
|
|
|
|
|
|
|
|
|
Films []model.Film
|
|
|
|
|
Actors []model.Actor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = stmt.Query(db, &dest2)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
//jsonSave("./testdata/quick-start-dest2.json", dest2)
|
2019-08-01 16:56:54 +02:00
|
|
|
testutils.AssertJSONFile(t, dest2, "./postgres/testdata/quick-start-dest2.json")
|
2019-06-26 10:30:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestQuickStartWithSubQueries(t *testing.T) {
|
2019-07-03 16:27:14 +02:00
|
|
|
|
2019-06-26 10:30:31 +02:00
|
|
|
filmLogerThan180 := Film.
|
|
|
|
|
SELECT(Film.AllColumns).
|
|
|
|
|
WHERE(Film.Length.GT(Int(180))).
|
|
|
|
|
AsTable("films")
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
filmID := Film.FilmID.From(filmLogerThan180)
|
|
|
|
|
filmLanguageID := Film.LanguageID.From(filmLogerThan180)
|
2019-06-26 10:30:31 +02:00
|
|
|
|
|
|
|
|
categoriesNotAction := Category.
|
|
|
|
|
SELECT(Category.AllColumns).
|
|
|
|
|
WHERE(Category.Name.NOT_EQ(String("Action"))).
|
|
|
|
|
AsTable("categories")
|
|
|
|
|
|
2019-07-18 18:42:03 +02:00
|
|
|
categoryID := Category.CategoryID.From(categoriesNotAction)
|
2019-06-26 10:30:31 +02:00
|
|
|
|
|
|
|
|
stmt := Actor.
|
|
|
|
|
INNER_JOIN(FilmActor, Actor.ActorID.EQ(FilmActor.ActorID)).
|
2019-07-18 18:42:03 +02:00
|
|
|
INNER_JOIN(filmLogerThan180, filmID.EQ(FilmActor.FilmID)).
|
|
|
|
|
INNER_JOIN(Language, Language.LanguageID.EQ(filmLanguageID)).
|
|
|
|
|
INNER_JOIN(FilmCategory, FilmCategory.FilmID.EQ(filmID)).
|
|
|
|
|
INNER_JOIN(categoriesNotAction, categoryID.EQ(FilmCategory.CategoryID)).
|
2019-06-26 10:30:31 +02:00
|
|
|
SELECT(
|
|
|
|
|
Actor.AllColumns,
|
|
|
|
|
filmLogerThan180.AllColumns(),
|
|
|
|
|
Language.AllColumns,
|
|
|
|
|
categoriesNotAction.AllColumns(),
|
|
|
|
|
).ORDER_BY(
|
|
|
|
|
Actor.ActorID.ASC(),
|
2019-07-18 18:42:03 +02:00
|
|
|
filmID.ASC(),
|
2019-06-26 10:30:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var dest []struct {
|
|
|
|
|
model.Actor
|
|
|
|
|
|
|
|
|
|
Films []struct {
|
|
|
|
|
model.Film
|
|
|
|
|
|
|
|
|
|
Language model.Language
|
|
|
|
|
|
|
|
|
|
Categories []model.Category
|
2019-06-24 18:22:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := stmt.Query(db, &dest)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-06-26 10:30:31 +02:00
|
|
|
//jsonSave("./testdata/quick-start-dest.json", dest)
|
2019-08-01 16:56:54 +02:00
|
|
|
testutils.AssertJSONFile(t, dest, "./postgres/testdata/quick-start-dest.json")
|
2019-06-24 18:22:55 +02:00
|
|
|
|
|
|
|
|
var dest2 []struct {
|
|
|
|
|
model.Category
|
|
|
|
|
|
2019-06-26 10:30:31 +02:00
|
|
|
Films []model.Film
|
|
|
|
|
Actors []model.Actor
|
2019-06-24 18:22:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = stmt.Query(db, &dest2)
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-06-26 10:30:31 +02:00
|
|
|
//jsonSave("./testdata/quick-start-dest2.json", dest2)
|
2019-08-01 16:56:54 +02:00
|
|
|
testutils.AssertJSONFile(t, dest2, "./postgres/testdata/quick-start-dest2.json")
|
2019-06-24 18:22:55 +02:00
|
|
|
}
|