2019-03-03 17:54:43 +01:00
|
|
|
package tests
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2019-05-27 13:11:15 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-06-21 13:56:57 +02:00
|
|
|
. "github.com/go-jet/jet"
|
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"
|
|
|
|
|
model2 "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/model"
|
|
|
|
|
. "github.com/go-jet/jet/tests/.gentestdata/jetdb/test_sample/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-05-12 18:15:23 +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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-05-27 13:11:15 +02:00
|
|
|
LastUpdate: *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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
query := SELECT(Payment.AllColumns, Customer.AllColumns).
|
2019-05-29 14:03:38 +02:00
|
|
|
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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-05-12 18:15:23 +02:00
|
|
|
fmt.Println(query.Sql())
|
2019-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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) {
|
|
|
|
|
|
|
|
|
|
expectedSql := `
|
|
|
|
|
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-06-20 11:19:23 +02:00
|
|
|
assertStatementSql(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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-06-03 17:05:29 +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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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]
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
func TestSelecSelfJoin1(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
var expectedSql = `
|
|
|
|
|
SELECT employee.employee_id AS "employee.employee_id",
|
|
|
|
|
employee.first_name AS "employee.first_name",
|
|
|
|
|
employee.last_name AS "employee.last_name",
|
2019-06-12 12:47:30 +02:00
|
|
|
employee.employment_date AS "employee.employment_date",
|
2019-05-20 17:37:55 +02:00
|
|
|
employee.manager_id AS "employee.manager_id",
|
|
|
|
|
manager.employee_id AS "manager.employee_id",
|
|
|
|
|
manager.first_name AS "manager.first_name",
|
|
|
|
|
manager.last_name AS "manager.last_name",
|
2019-06-12 12:47:30 +02:00
|
|
|
manager.employment_date AS "manager.employment_date",
|
2019-05-20 17:37:55 +02:00
|
|
|
manager.manager_id AS "manager.manager_id"
|
|
|
|
|
FROM test_sample.employee
|
2019-05-31 14:37:51 +02:00
|
|
|
LEFT JOIN test_sample.employee AS manager ON (manager.employee_id = employee.manager_id)
|
2019-05-20 17:37:55 +02:00
|
|
|
ORDER BY employee.employee_id;
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
manager := Employee.AS("manager")
|
|
|
|
|
query := Employee.
|
2019-05-29 14:03:38 +02:00
|
|
|
LEFT_JOIN(manager, manager.EmployeeID.EQ(Employee.ManagerID)).
|
2019-05-20 17:37:55 +02:00
|
|
|
SELECT(Employee.AllColumns, manager.AllColumns).
|
|
|
|
|
ORDER_BY(Employee.EmployeeID)
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(t, query, expectedSql)
|
2019-05-20 17:37:55 +02:00
|
|
|
|
|
|
|
|
var dest []struct {
|
|
|
|
|
model2.Employee
|
|
|
|
|
|
|
|
|
|
Manager *model2.Employee
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := query.Query(db, &dest)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
assert.Equal(t, len(dest), 8)
|
|
|
|
|
assert.DeepEqual(t, dest[0].Employee, model2.Employee{
|
2019-06-12 12:47:30 +02:00
|
|
|
EmployeeID: 1,
|
|
|
|
|
FirstName: "Windy",
|
|
|
|
|
LastName: "Hays",
|
2019-06-21 16:16:57 +02:00
|
|
|
EmploymentDate: timestampWithTimeZone("1999-01-08 04:05:06.1 +0100 CET", 1),
|
2019-06-12 12:47:30 +02:00
|
|
|
ManagerID: nil,
|
2019-05-20 17:37:55 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
assert.Assert(t, dest[0].Manager == nil)
|
|
|
|
|
|
|
|
|
|
assert.DeepEqual(t, dest[7].Employee, model2.Employee{
|
2019-06-12 12:47:30 +02:00
|
|
|
EmployeeID: 8,
|
|
|
|
|
FirstName: "Salley",
|
|
|
|
|
LastName: "Lester",
|
|
|
|
|
EmploymentDate: timestampWithTimeZone("1999-01-08 04:05:06 +0100 CET", 1),
|
|
|
|
|
ManagerID: int32Ptr(3),
|
2019-05-20 17:37:55 +02:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-16 20:41:06 +01:00
|
|
|
func TestSelectSelfJoin(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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})
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 17:37:55 +02:00
|
|
|
//
|
|
|
|
|
//type Manager staff
|
|
|
|
|
//
|
|
|
|
|
//type staff struct {
|
|
|
|
|
// StaffID int32 `sql:"unique"`
|
|
|
|
|
// FirstName string
|
|
|
|
|
// LastName string
|
|
|
|
|
// //Address *model.Address
|
|
|
|
|
// //Email *string
|
|
|
|
|
// //StoreID int16
|
|
|
|
|
// //Active bool
|
|
|
|
|
// //Username string
|
|
|
|
|
// //Password *string
|
|
|
|
|
// //LastUpdate time.Time
|
|
|
|
|
// *Manager //`sqlbuilder:"manager"`
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func TestSelectSelfReferenceType(t *testing.T) {
|
|
|
|
|
//
|
|
|
|
|
// expectedSql := `
|
|
|
|
|
//SELECT DISTINCT staff.staff_id AS "staff.staff_id",
|
|
|
|
|
// staff.first_name AS "staff.first_name",
|
|
|
|
|
// staff.last_name AS "staff.last_name",
|
|
|
|
|
// 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",
|
|
|
|
|
// manager.staff_id AS "manager.staff_id",
|
|
|
|
|
// manager.first_name AS "manager.first_name"
|
|
|
|
|
//FROM dvds.staff
|
|
|
|
|
// JOIN dvds.address ON staff.address_id = address.address_id
|
|
|
|
|
// JOIN dvds.staff AS manager ON staff.staff_id = manager.staff_id;
|
|
|
|
|
//`
|
|
|
|
|
// manager := Staff.AS("manager")
|
|
|
|
|
//
|
|
|
|
|
// query := Staff.
|
2019-06-05 17:15:20 +02:00
|
|
|
// innerJoin(Address, Staff.AddressID.EQ(Address.AddressID)).
|
|
|
|
|
// innerJoin(manager, Staff.StaffID.EQ(manager.StaffID)).
|
2019-05-20 17:37:55 +02:00
|
|
|
// SELECT(Staff.StaffID, Staff.FirstName, Staff.LastName, Address.AllColumns, manager.StaffID, manager.FirstName).
|
|
|
|
|
// DISTINCT()
|
|
|
|
|
//
|
2019-06-14 14:35:50 +02:00
|
|
|
// assertStatementSql(t, query, expectedSql)
|
2019-05-20 17:37:55 +02:00
|
|
|
//
|
|
|
|
|
// staffs := []staff{}
|
|
|
|
|
//
|
|
|
|
|
// err := query.Query(db, &staffs)
|
|
|
|
|
//
|
|
|
|
|
// assert.NilError(t, err)
|
|
|
|
|
//
|
|
|
|
|
// fmt.Println(query.DebugSql())
|
|
|
|
|
// //spew.Dump(staffs)
|
|
|
|
|
//}
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
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-17 12:05:52 +02:00
|
|
|
"rFilms"."film.title" AS "film.title"
|
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-06-08 16:34:15 +02:00
|
|
|
rFilmId := Film.FilmID.From(rRatingFilms)
|
|
|
|
|
rTitle := Film.Title.From(rRatingFilms)
|
2019-06-07 14:23:14 +02:00
|
|
|
|
|
|
|
|
query := Actor.
|
|
|
|
|
INNER_JOIN(FilmActor, Actor.ActorID.EQ(FilmActor.FilmID)).
|
2019-06-08 16:34:15 +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-08 16:34:15 +02:00
|
|
|
rTitle.AS("film.title"),
|
2019-03-30 10:17:32 +01:00
|
|
|
)
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
fmt.Println(query.Sql())
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-06-07 14:23:14 +02:00
|
|
|
)::double precision
|
2019-05-12 18:15:23 +02:00
|
|
|
ORDER BY film.film_id ASC;
|
|
|
|
|
`
|
|
|
|
|
|
2019-06-07 14:23:14 +02:00
|
|
|
maxFilmRentalRate := Film.SELECT(MAXf(Film.RentalRate)).TO_DOUBLE()
|
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-05-12 18:15:23 +02:00
|
|
|
fmt.Println(query.Sql())
|
2019-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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",
|
|
|
|
|
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,
|
|
|
|
|
Length: int16Ptr(48),
|
|
|
|
|
ReplacementCost: 12.99,
|
2019-04-03 19:21:46 +02:00
|
|
|
Rating: &gRating,
|
2019-03-30 10:17:32 +01:00
|
|
|
RentalDuration: 3,
|
2019-05-27 13:11:15 +02:00
|
|
|
LastUpdate: *timestampWithoutTimeZone("2013-05-26 14:50:58.951", 3),
|
2019-03-30 10:17:32 +01:00
|
|
|
SpecialFeatures: stringPtr("{Trailers,\"Deleted Scenes\"}"),
|
|
|
|
|
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-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
SELECT payment.customer_id AS "customer_payment_sum.customer_id",
|
2019-06-03 18:28:16 +02:00
|
|
|
SUM(payment.amount) AS "customer_payment_sum.amount_sum",
|
|
|
|
|
AVG(payment.amount) AS "customer_payment_sum.amount_avg",
|
|
|
|
|
MAX(payment.amount) AS "customer_payment_sum.amount_max",
|
|
|
|
|
MIN(payment.amount) AS "customer_payment_sum.amount_min",
|
|
|
|
|
COUNT(payment.amount) AS "customer_payment_sum.amount_count"
|
2019-05-12 18:15:23 +02:00
|
|
|
FROM dvds.payment
|
|
|
|
|
GROUP BY payment.customer_id
|
|
|
|
|
HAVING SUM(payment.amount) > 100
|
|
|
|
|
ORDER BY SUM(payment.amount) ASC;
|
|
|
|
|
`
|
2019-03-30 10:17:32 +01:00
|
|
|
customersPaymentQuery := Payment.
|
2019-04-03 11:03:07 +02:00
|
|
|
SELECT(
|
2019-05-05 18:03:30 +02:00
|
|
|
Payment.CustomerID.AS("customer_payment_sum.customer_id"),
|
2019-05-31 12:59:57 +02:00
|
|
|
SUMf(Payment.Amount).AS("customer_payment_sum.amount_sum"),
|
2019-06-21 12:30:32 +02:00
|
|
|
AVG(Payment.Amount).AS("customer_payment_sum.amount_avg"),
|
2019-06-03 18:28:16 +02:00
|
|
|
MAXf(Payment.Amount).AS("customer_payment_sum.amount_max"),
|
|
|
|
|
MINf(Payment.Amount).AS("customer_payment_sum.amount_min"),
|
|
|
|
|
COUNT(Payment.Amount).AS("customer_payment_sum.amount_count"),
|
2019-03-30 10:17:32 +01:00
|
|
|
).
|
2019-04-29 14:39:48 +02:00
|
|
|
GROUP_BY(Payment.CustomerID).
|
2019-05-29 14:03:38 +02:00
|
|
|
ORDER_BY(
|
2019-05-31 12:59:57 +02:00
|
|
|
SUMf(Payment.Amount).ASC(),
|
2019-05-29 14:03:38 +02:00
|
|
|
).
|
|
|
|
|
HAVING(
|
2019-05-31 12:59:57 +02:00
|
|
|
SUMf(Payment.Amount).GT(Float(100)),
|
2019-05-29 14:03:38 +02:00
|
|
|
)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(t, customersPaymentQuery, expectedSql, float64(100))
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
type CustomerPaymentSum struct {
|
2019-06-03 18:28:16 +02:00
|
|
|
CustomerID int16
|
|
|
|
|
AmountSum float64
|
|
|
|
|
AmountAvg float64
|
|
|
|
|
AmountMax float64
|
|
|
|
|
AmountMin float64
|
|
|
|
|
AmountCount int64
|
2019-03-30 10:17:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
customerPaymentSum := []CustomerPaymentSum{}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
err := customersPaymentQuery.Query(db, &customerPaymentSum)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, len(customerPaymentSum), 296)
|
|
|
|
|
assert.DeepEqual(t, customerPaymentSum[0], CustomerPaymentSum{
|
2019-06-03 18:28:16 +02:00
|
|
|
CustomerID: 135,
|
|
|
|
|
AmountSum: 100.72,
|
|
|
|
|
AmountAvg: 3.597142857142857,
|
|
|
|
|
AmountMax: 7.99,
|
|
|
|
|
AmountMin: 0.99,
|
|
|
|
|
AmountCount: 28,
|
2019-03-30 10:17:32 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSelectGroupBy2(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
|
|
|
|
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-06-08 16:34:15 +02:00
|
|
|
customerId := Payment.CustomerID.From(customersPayments)
|
|
|
|
|
amountSum := FloatColumn("amount_sum").From(customersPayments)
|
2019-03-30 10:17:32 +01:00
|
|
|
|
|
|
|
|
query := Customer.
|
2019-06-08 16:34:15 +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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-03-30 10:17:32 +01:00
|
|
|
Email: stringPtr("brian.wyman@sakilacustomer.org"),
|
|
|
|
|
Activebool: true,
|
2019-05-27 13:11:15 +02:00
|
|
|
CreateDate: *timestampWithoutTimeZone("2006-02-14 00:00:00", 0),
|
|
|
|
|
LastUpdate: timestampWithoutTimeZone("2013-05-26 14:49:45.738", 3),
|
2019-03-30 10:17:32 +01:00
|
|
|
Active: int32Ptr(1),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
spew.Dump(staffs)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 14:18:58 +02:00
|
|
|
func TestSelectTimeColumns(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
|
|
|
|
|
expectedSql := `
|
|
|
|
|
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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-05-27 13:11:15 +02:00
|
|
|
PaymentDate: *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-05-12 18:15:23 +02:00
|
|
|
queryStr, _, _ := query.Sql()
|
2019-05-03 11:59:18 +02:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
fmt.Println("-" + queryStr + "-")
|
2019-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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-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-06-14 14:35:50 +02:00
|
|
|
assertStatementSql(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 {
|
|
|
|
|
StaffIdNum string
|
|
|
|
|
}{}
|
|
|
|
|
|
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)
|
|
|
|
|
assert.Equal(t, dest[0].StaffIdNum, "TWO")
|
|
|
|
|
assert.Equal(t, dest[1].StaffIdNum, "ONE")
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 13:44:30 +02:00
|
|
|
func TestLockTable(t *testing.T) {
|
2019-05-12 18:15:23 +02:00
|
|
|
expectedSql := `
|
2019-06-15 13:58:45 +02:00
|
|
|
LOCK TABLE dvds.address IN`
|
|
|
|
|
|
|
|
|
|
var testData = []TableLockMode{
|
|
|
|
|
LOCK_ACCESS_SHARE,
|
|
|
|
|
LOCK_ROW_SHARE,
|
|
|
|
|
LOCK_ROW_EXCLUSIVE,
|
|
|
|
|
LOCK_SHARE_UPDATE_EXCLUSIVE,
|
|
|
|
|
LOCK_SHARE,
|
|
|
|
|
LOCK_SHARE_ROW_EXCLUSIVE,
|
|
|
|
|
LOCK_EXCLUSIVE,
|
|
|
|
|
LOCK_ACCESS_EXCLUSIVE,
|
|
|
|
|
}
|
2019-05-07 13:44:30 +02:00
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
for _, lockMode := range testData {
|
|
|
|
|
query := Address.LOCK().IN(lockMode)
|
2019-05-07 13:44:30 +02:00
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
assertStatementSql(t, query, expectedSql+" "+string(lockMode)+" MODE;\n")
|
2019-05-07 13:44:30 +02:00
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
tx, _ := db.Begin()
|
2019-05-07 13:44:30 +02:00
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
_, err := query.Exec(tx)
|
2019-05-07 13:44:30 +02:00
|
|
|
|
2019-06-15 13:58:45 +02:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
err = tx.Rollback()
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
2019-06-15 13:58:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, lockMode := range testData {
|
|
|
|
|
query := Address.LOCK().IN(lockMode).NOWAIT()
|
|
|
|
|
|
|
|
|
|
assertStatementSql(t, query, expectedSql+" "+string(lockMode)+" MODE NOWAIT;\n")
|
|
|
|
|
|
|
|
|
|
tx, _ := db.Begin()
|
|
|
|
|
|
|
|
|
|
_, err := query.Exec(tx)
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
2019-06-21 13:56:57 +02:00
|
|
|
err = tx.Rollback()
|
|
|
|
|
|
|
|
|
|
assert.NilError(t, err)
|
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) {
|
|
|
|
|
expectedSql := `
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM dvds.address
|
|
|
|
|
LIMIT 3
|
|
|
|
|
FOR`
|
|
|
|
|
query := Address.
|
|
|
|
|
SELECT(STAR).
|
|
|
|
|
LIMIT(3)
|
|
|
|
|
|
|
|
|
|
for lockType, lockTypeStr := range getRowLockTestData() {
|
|
|
|
|
query.FOR(lockType)
|
|
|
|
|
|
|
|
|
|
assertStatementSql(t, query, expectedSql+" "+lockTypeStr+";\n", int64(3))
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
|
|
assertStatementSql(t, query, expectedSql+" "+lockTypeStr+" NOWAIT;\n", int64(3))
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
|
|
|
|
|
assertStatementSql(t, query, expectedSql+" "+lockTypeStr+" SKIP LOCKED;\n", int64(3))
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|