Add support for NULLS_FIRST and NULLS_LAST sorting order.
This commit is contained in:
parent
0fc51cf402
commit
dab153a739
8 changed files with 882 additions and 19 deletions
|
|
@ -3,6 +3,7 @@ package mysql
|
|||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/postgres"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -296,6 +297,296 @@ ORDER BY inventory.film_id, inventory.store_id;
|
|||
`)
|
||||
}
|
||||
|
||||
func TestOrderBy(t *testing.T) {
|
||||
// NULLS_FIRST and NULLS_LAST are simulated using IS_NULL, ...
|
||||
|
||||
ensureNullsFirstRentalResult := func(t *testing.T, stmt postgres.Statement, asc bool) {
|
||||
var dest []model.Rental
|
||||
|
||||
err := stmt.Query(db, &dest)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, dest, 200)
|
||||
require.Nil(t, dest[0].ReturnDate)
|
||||
require.NotNil(t, dest[199].ReturnDate)
|
||||
|
||||
if asc {
|
||||
require.True(t, dest[198].ReturnDate.Before(*dest[199].ReturnDate))
|
||||
} else {
|
||||
require.True(t, dest[199].ReturnDate.Before(*dest[198].ReturnDate))
|
||||
}
|
||||
}
|
||||
|
||||
ensureNullsLastRentalResult := func(t *testing.T, stmt postgres.Statement, asc bool) {
|
||||
var dest []model.Rental
|
||||
|
||||
err := stmt.Query(db, &dest)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, dest, 200)
|
||||
require.NotNil(t, dest[0].ReturnDate)
|
||||
require.Nil(t, dest[199].ReturnDate)
|
||||
|
||||
if asc {
|
||||
require.True(t, dest[0].ReturnDate.Before(*dest[1].ReturnDate))
|
||||
} else {
|
||||
require.True(t, dest[1].ReturnDate.Before(*dest[0].ReturnDate))
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("default", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate,
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date
|
||||
LIMIT 200;
|
||||
`)
|
||||
ensureNullsFirstRentalResult(t, stmt, true)
|
||||
})
|
||||
|
||||
t.Run("NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date
|
||||
LIMIT 200;
|
||||
`)
|
||||
ensureNullsFirstRentalResult(t, stmt, true)
|
||||
})
|
||||
|
||||
t.Run("NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.NULLS_LAST(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date IS NULL, rental.return_date
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
ensureNullsLastRentalResult(t, stmt, true)
|
||||
})
|
||||
|
||||
t.Run("ASC", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date ASC
|
||||
LIMIT 200;
|
||||
`)
|
||||
ensureNullsFirstRentalResult(t, stmt, true)
|
||||
})
|
||||
|
||||
t.Run("ASC NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC().NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date ASC
|
||||
LIMIT 200;
|
||||
`)
|
||||
|
||||
ensureNullsFirstRentalResult(t, stmt, true)
|
||||
})
|
||||
|
||||
t.Run("ASC NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC().NULLS_LAST(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date IS NULL, rental.return_date ASC
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
ensureNullsLastRentalResult(t, stmt, true)
|
||||
})
|
||||
|
||||
t.Run("DESC", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date DESC
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
ensureNullsLastRentalResult(t, stmt, false)
|
||||
})
|
||||
|
||||
t.Run("DESC NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC().NULLS_LAST(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date DESC
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
ensureNullsLastRentalResult(t, stmt, false)
|
||||
})
|
||||
|
||||
t.Run("DESC NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC().NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date IS NOT NULL, rental.return_date DESC
|
||||
LIMIT 200;
|
||||
`)
|
||||
|
||||
ensureNullsFirstRentalResult(t, stmt, false)
|
||||
})
|
||||
|
||||
t.Run("complex", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.RentalID.DESC(),
|
||||
Rental.ReturnDate.DESC().NULLS_FIRST(),
|
||||
Rental.LastUpdate.ASC(),
|
||||
Rental.InventoryID.ADD(Rental.RentalID).ASC().NULLS_LAST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.rental_id DESC, rental.return_date IS NOT NULL, rental.return_date DESC, rental.last_update ASC, (rental.inventory_id + rental.rental_id) IS NULL, rental.inventory_id + rental.rental_id ASC
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestSubQuery(t *testing.T) {
|
||||
|
||||
rRatingFilms := SELECT(
|
||||
|
|
|
|||
|
|
@ -1151,7 +1151,7 @@ func TestSelectOrderByAscDesc(t *testing.T) {
|
|||
firstCustomerAsc := customersAsc[0]
|
||||
lastCustomerAsc := customersAsc[len(customersAsc)-1]
|
||||
|
||||
customersDesc := []model.Customer{}
|
||||
var customersDesc []model.Customer
|
||||
err = Customer.SELECT(Customer.CustomerID, Customer.FirstName, Customer.LastName).
|
||||
ORDER_BY(Customer.FirstName.DESC()).
|
||||
Query(db, &customersDesc)
|
||||
|
|
@ -1164,7 +1164,7 @@ func TestSelectOrderByAscDesc(t *testing.T) {
|
|||
testutils.AssertDeepEqual(t, firstCustomerAsc, lastCustomerDesc)
|
||||
testutils.AssertDeepEqual(t, lastCustomerAsc, firstCustomerDesc)
|
||||
|
||||
customersAscDesc := []model.Customer{}
|
||||
var customersAscDesc []model.Customer
|
||||
err = Customer.SELECT(Customer.CustomerID, Customer.FirstName, Customer.LastName).
|
||||
ORDER_BY(Customer.FirstName.ASC(), Customer.LastName.DESC()).
|
||||
Query(db, &customersAscDesc)
|
||||
|
|
@ -1187,6 +1187,233 @@ func TestSelectOrderByAscDesc(t *testing.T) {
|
|||
testutils.AssertDeepEqual(t, customerAscDesc327, customersAscDesc[327])
|
||||
}
|
||||
|
||||
func TestOrderBy(t *testing.T) {
|
||||
|
||||
t.Run("default", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate,
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date NULLS FIRST
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.NULLS_LAST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date NULLS LAST
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("ASC", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date ASC
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("ASC NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC().NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date ASC NULLS FIRST
|
||||
LIMIT 200;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("ASC NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC().NULLS_LAST(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date ASC NULLS LAST
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("DESC", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date DESC
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("DESC NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC().NULLS_LAST(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date DESC NULLS LAST
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("DESC NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC().NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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.rental
|
||||
ORDER BY rental.return_date DESC NULLS FIRST
|
||||
LIMIT 200;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
}
|
||||
|
||||
func TestSelectFullJoin(t *testing.T) {
|
||||
expectedSQL := `
|
||||
SELECT customer.customer_id AS "customer.customer_id",
|
||||
|
|
|
|||
|
|
@ -145,6 +145,233 @@ ORDER BY payment.customer_id, SUM(payment.amount) ASC;
|
|||
requireLogged(t, query)
|
||||
}
|
||||
|
||||
func TestOrderBy(t *testing.T) {
|
||||
|
||||
t.Run("default", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate,
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date NULLS FIRST
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.NULLS_LAST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date NULLS LAST
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("ASC", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date ASC
|
||||
LIMIT 200;
|
||||
`)
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("ASC NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC().NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date ASC NULLS FIRST
|
||||
LIMIT 200;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("ASC NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.ASC().NULLS_LAST(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date ASC NULLS LAST
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("DESC", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date DESC
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("DESC NULLS LAST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC().NULLS_LAST(),
|
||||
).LIMIT(200).OFFSET(15800)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date DESC NULLS LAST
|
||||
LIMIT 200
|
||||
OFFSET 15800;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
|
||||
t.Run("DESC NULLS FIRST", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Rental.AllColumns,
|
||||
).FROM(
|
||||
Rental,
|
||||
).ORDER_BY(
|
||||
Rental.ReturnDate.DESC().NULLS_FIRST(),
|
||||
).LIMIT(200)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT 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 rental
|
||||
ORDER BY rental.return_date DESC NULLS FIRST
|
||||
LIMIT 200;
|
||||
`)
|
||||
|
||||
require.NoError(t, stmt.Query(db, &struct{}{}))
|
||||
})
|
||||
}
|
||||
|
||||
func TestAggregateFunctionDistinct(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Payment.CustomerID,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue