Add support for prepared statement caching.
This commit is contained in:
parent
1b63280b74
commit
0918e5503e
30 changed files with 603 additions and 289 deletions
|
|
@ -1,7 +1,7 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -71,7 +71,7 @@ func TestAllTypesInsertModel(t *testing.T) {
|
|||
MODEL(&allTypesRow1).
|
||||
RETURNING(AllTypes.AllColumns)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []model.AllTypes
|
||||
err := query.Query(tx, &dest)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -94,7 +94,7 @@ func TestAllTypesInsertQuery(t *testing.T) {
|
|||
).
|
||||
RETURNING(AllTypesAllColumns)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []model.AllTypes
|
||||
err := query.Query(tx, &dest)
|
||||
|
||||
|
|
@ -1293,7 +1293,7 @@ WHERE all_types.small_int = 14
|
|||
RETURNING all_types.json AS "all_types.json";
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var res model.AllTypes
|
||||
|
||||
err := stmt.Query(tx, &res)
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func TestSelect(t *testing.T) {
|
||||
func TestSelectAlbum(t *testing.T) {
|
||||
stmt := SELECT(Album.AllColumns).
|
||||
FROM(Album).
|
||||
ORDER_BY(Album.AlbumId.ASC())
|
||||
|
|
@ -782,9 +782,11 @@ func TestQueryWithContext(t *testing.T) {
|
|||
return // context cancellation doesn't work for pq driver
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
|
||||
defer cancel()
|
||||
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
|
||||
var dest []model.Album
|
||||
|
||||
err := Album.
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ package postgres
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
model2 "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/dvds/model"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/dvds/table"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/model"
|
||||
|
|
@ -43,7 +43,7 @@ RETURNING link.id AS "link.id",
|
|||
link.description AS "link.description";
|
||||
`, "Gmail", "Outlook")
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []model.Link
|
||||
|
||||
err := deleteStmt.Query(tx, &dest)
|
||||
|
|
@ -67,7 +67,7 @@ func TestDeleteQueryContext(t *testing.T) {
|
|||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
dest := []model.Link{}
|
||||
err := deleteStmt.QueryContext(ctx, tx, &dest)
|
||||
|
||||
|
|
@ -88,7 +88,7 @@ func TestDeleteExecContext(t *testing.T) {
|
|||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
_, err := deleteStmt.ExecContext(ctx, tx)
|
||||
|
||||
require.Error(t, err, "context deadline exceeded")
|
||||
|
|
@ -140,7 +140,7 @@ RETURNING rental.rental_id AS "rental.rental_id",
|
|||
store.last_update AS "store.last_update";
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []struct {
|
||||
Rental model2.Rental
|
||||
Store model2.Store
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ package postgres
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/model"
|
||||
. "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/table"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -34,7 +34,7 @@ RETURNING link.id AS "link.id",
|
|||
101, "http://www.google.com", "Google",
|
||||
102, "http://www.yahoo.com", "Yahoo", nil)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var insertedLinks []model.Link
|
||||
|
||||
err := insertQuery.Query(tx, &insertedLinks)
|
||||
|
|
@ -335,7 +335,7 @@ RETURNING link.id AS "link.id",
|
|||
link.description AS "link.description";
|
||||
`, int64(0))
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []model.Link
|
||||
|
||||
err := query.Query(tx, &dest)
|
||||
|
|
@ -362,7 +362,7 @@ func TestInsertWithQueryContext(t *testing.T) {
|
|||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
dest := []model.Link{}
|
||||
err := stmt.QueryContext(ctx, tx, &dest)
|
||||
|
||||
|
|
@ -379,7 +379,7 @@ func TestInsertWithExecContext(t *testing.T) {
|
|||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
testutils.AssertExecContextErr(ctx, t, stmt, tx, "context deadline exceeded")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,34 +32,14 @@ LOCK TABLE dvds.address IN`
|
|||
query := Address.LOCK().IN(lockMode)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+string(lockMode)+" MODE;\n")
|
||||
|
||||
tx, _ := db.Begin()
|
||||
|
||||
_, err := query.Exec(tx)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
err = tx.Rollback()
|
||||
|
||||
require.NoError(t, err)
|
||||
requireLogged(t, query)
|
||||
testutils.AssertExecAndRollback(t, query, db)
|
||||
}
|
||||
|
||||
for _, lockMode := range testData {
|
||||
query := Address.LOCK().IN(lockMode).NOWAIT()
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+string(lockMode)+" MODE NOWAIT;\n")
|
||||
|
||||
tx, _ := db.Begin()
|
||||
|
||||
_, err := query.Exec(tx)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
err = tx.Rollback()
|
||||
|
||||
require.NoError(t, err)
|
||||
requireLogged(t, query)
|
||||
testutils.AssertExecAndRollback(t, query, db)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ import (
|
|||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
var db *postgres.DB
|
||||
var testRoot string
|
||||
|
||||
var source string
|
||||
|
|
@ -60,18 +60,25 @@ func TestMain(m *testing.M) {
|
|||
connectionString = dbconfig.CockroachConnectString
|
||||
}
|
||||
|
||||
var err error
|
||||
db, err = sql.Open(driverName, connectionString)
|
||||
sqlDB, err := sql.Open(driverName, connectionString)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
panic("Failed to connect to test db")
|
||||
}
|
||||
db = postgres.NewDB(sqlDB).WithStatementsCaching(true)
|
||||
defer db.Close()
|
||||
|
||||
ret := m.Run()
|
||||
for i := 0; i < 2; i++ {
|
||||
ret := m.Run()
|
||||
if ret != 0 {
|
||||
os.Exit(ret)
|
||||
}
|
||||
}
|
||||
|
||||
if ret != 0 {
|
||||
os.Exit(ret)
|
||||
err = db.Clear()
|
||||
|
||||
if err != nil {
|
||||
os.Exit(-2)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package postgres
|
|||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/northwind/model"
|
||||
. "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/northwind/table"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -10,30 +11,34 @@ import (
|
|||
|
||||
func TestNorthwindJoinEverything(t *testing.T) {
|
||||
|
||||
stmt := Customers.
|
||||
LEFT_JOIN(CustomerCustomerDemo, Customers.CustomerID.EQ(CustomerCustomerDemo.CustomerID)).
|
||||
LEFT_JOIN(CustomerDemographics, CustomerCustomerDemo.CustomerTypeID.EQ(CustomerDemographics.CustomerTypeID)).
|
||||
LEFT_JOIN(Orders, Orders.CustomerID.EQ(Customers.CustomerID)).
|
||||
LEFT_JOIN(Shippers, Orders.ShipVia.EQ(Shippers.ShipperID)).
|
||||
LEFT_JOIN(OrderDetails, Orders.OrderID.EQ(OrderDetails.OrderID)).
|
||||
LEFT_JOIN(Products, OrderDetails.ProductID.EQ(Products.ProductID)).
|
||||
LEFT_JOIN(Categories, Products.CategoryID.EQ(Categories.CategoryID)).
|
||||
LEFT_JOIN(Suppliers, Products.SupplierID.EQ(Suppliers.SupplierID)).
|
||||
LEFT_JOIN(Employees, Orders.EmployeeID.EQ(Employees.EmployeeID)).
|
||||
LEFT_JOIN(EmployeeTerritories, EmployeeTerritories.EmployeeID.EQ(Employees.EmployeeID)).
|
||||
LEFT_JOIN(Territories, EmployeeTerritories.TerritoryID.EQ(Territories.TerritoryID)).
|
||||
LEFT_JOIN(Region, Territories.RegionID.EQ(Region.RegionID)).
|
||||
SELECT(
|
||||
Customers.AllColumns,
|
||||
CustomerDemographics.AllColumns,
|
||||
Orders.AllColumns,
|
||||
Shippers.AllColumns,
|
||||
OrderDetails.AllColumns,
|
||||
Products.AllColumns,
|
||||
Categories.AllColumns,
|
||||
Suppliers.AllColumns,
|
||||
).
|
||||
ORDER_BY(Customers.CustomerID, Orders.OrderID, Products.ProductID)
|
||||
stmt := SELECT(
|
||||
Customers.AllColumns,
|
||||
CustomerDemographics.AllColumns,
|
||||
Orders.AllColumns,
|
||||
Shippers.AllColumns,
|
||||
OrderDetails.AllColumns,
|
||||
Products.AllColumns,
|
||||
Categories.AllColumns,
|
||||
Suppliers.AllColumns,
|
||||
).FROM(
|
||||
Customers.
|
||||
LEFT_JOIN(CustomerCustomerDemo, Customers.CustomerID.EQ(CustomerCustomerDemo.CustomerID)).
|
||||
LEFT_JOIN(CustomerDemographics, CustomerCustomerDemo.CustomerTypeID.EQ(CustomerDemographics.CustomerTypeID)).
|
||||
LEFT_JOIN(Orders, Orders.CustomerID.EQ(Customers.CustomerID)).
|
||||
LEFT_JOIN(Shippers, Orders.ShipVia.EQ(Shippers.ShipperID)).
|
||||
LEFT_JOIN(OrderDetails, Orders.OrderID.EQ(OrderDetails.OrderID)).
|
||||
LEFT_JOIN(Products, OrderDetails.ProductID.EQ(Products.ProductID)).
|
||||
LEFT_JOIN(Categories, Products.CategoryID.EQ(Categories.CategoryID)).
|
||||
LEFT_JOIN(Suppliers, Products.SupplierID.EQ(Suppliers.SupplierID)).
|
||||
LEFT_JOIN(Employees, Orders.EmployeeID.EQ(Employees.EmployeeID)).
|
||||
LEFT_JOIN(EmployeeTerritories, EmployeeTerritories.EmployeeID.EQ(Employees.EmployeeID)).
|
||||
LEFT_JOIN(Territories, EmployeeTerritories.TerritoryID.EQ(Territories.TerritoryID)).
|
||||
LEFT_JOIN(Region, Territories.RegionID.EQ(Region.RegionID)),
|
||||
).ORDER_BY(
|
||||
Customers.CustomerID,
|
||||
Orders.OrderID,
|
||||
Products.ProductID,
|
||||
)
|
||||
|
||||
var dest []struct {
|
||||
model.Customers
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/jackc/pgtype"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -294,7 +294,7 @@ RETURNING sample_ranges.date_range AS "sample_ranges.date_range",
|
|||
`
|
||||
testutils.AssertDebugStatementSql(t, insertQuery, expectedQuery)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []model.SampleRanges
|
||||
err := insertQuery.Query(tx, &dest)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -324,7 +324,7 @@ RETURNING sample_ranges.date_range AS "sample_ranges.date_range",
|
|||
sample_ranges.num_range AS "sample_ranges.num_range";
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []model.SampleRanges
|
||||
|
||||
err := stmt.Query(tx, &dest)
|
||||
|
|
@ -351,7 +351,7 @@ SET int4_range = int4range(-12::integer, 78::integer),
|
|||
WHERE LOWER(sample_ranges.timestampz_range) > NOW();
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
testutils.AssertExec(t, stmt, tx, 1)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ package postgres
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -128,7 +128,7 @@ RETURNING link.id AS "link.id",
|
|||
link.description AS "link.description";
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var links []model2.Link
|
||||
err := stmt.Query(tx, &links)
|
||||
require.NoError(t, err)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"github.com/google/uuid"
|
||||
"testing"
|
||||
|
||||
|
|
@ -512,7 +512,7 @@ func TestMutableColumnsExcludeGeneratedColumn(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("should insert without generated columns", func(t *testing.T) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
insertQuery := People.INSERT(
|
||||
People.MutableColumns,
|
||||
).MODEL(
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ ORDER BY rental.staff_id ASC, rental.customer_id ASC, rental.rental_id ASC;
|
|||
`)
|
||||
}
|
||||
|
||||
func TestClassicSelect(t *testing.T) {
|
||||
func TestSelectClassic(t *testing.T) {
|
||||
expectedSQL := `
|
||||
SELECT payment.payment_id AS "payment.payment_id",
|
||||
payment.customer_id AS "payment.customer_id",
|
||||
|
|
@ -145,7 +145,7 @@ LIMIT 30;
|
|||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(30))
|
||||
|
||||
dest := []model.Payment{}
|
||||
var dest []model.Payment
|
||||
|
||||
err := query.Query(db, &dest)
|
||||
|
||||
|
|
@ -231,12 +231,12 @@ LIMIT 12;
|
|||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(1), int64(1), int64(10), int64(1), int64(2), int64(1), int64(12))
|
||||
|
||||
dest := []struct{}{}
|
||||
var dest []struct{}
|
||||
err := query.Query(db, &dest)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestFetchFirst(t *testing.T) {
|
||||
func TestSelectFetchFirst(t *testing.T) {
|
||||
|
||||
t.Run("rows only", func(t *testing.T) {
|
||||
stmt := SELECT(Actor.AllColumns).
|
||||
|
|
@ -320,7 +320,7 @@ FETCH FIRST (
|
|||
})
|
||||
}
|
||||
|
||||
func TestOffsetExpression(t *testing.T) {
|
||||
func TestSelectOffsetExpression(t *testing.T) {
|
||||
|
||||
stmt := SELECT(Actor.AllColumns).
|
||||
FROM(Actor).
|
||||
|
|
@ -352,7 +352,7 @@ OFFSET (
|
|||
require.Equal(t, dest[0].ActorID, int32(3))
|
||||
}
|
||||
|
||||
func TestJoinQueryStruct(t *testing.T) {
|
||||
func TestSelectJoinQueryStruct(t *testing.T) {
|
||||
|
||||
expectedSQL := `
|
||||
SELECT film_actor.actor_id AS "film_actor.actor_id",
|
||||
|
|
@ -445,7 +445,7 @@ LIMIT 1000;
|
|||
}
|
||||
}
|
||||
|
||||
func TestJoinQuerySlice(t *testing.T) {
|
||||
func TestSelectJoinQuerySlice(t *testing.T) {
|
||||
expectedSQL := `
|
||||
SELECT language.language_id AS "language.language_id",
|
||||
language.name AS "language.name",
|
||||
|
|
@ -474,7 +474,7 @@ LIMIT 15;
|
|||
Film []model.Film
|
||||
}
|
||||
|
||||
filmsPerLanguage := []FilmsPerLanguage{}
|
||||
var filmsPerLanguage []FilmsPerLanguage
|
||||
limit := 15
|
||||
|
||||
query := Film.
|
||||
|
|
@ -504,7 +504,7 @@ LIMIT 15;
|
|||
}
|
||||
|
||||
// https://github.com/go-jet/jet/issues/226
|
||||
func TestDuplicateSlicesInDestination(t *testing.T) {
|
||||
func TestSelectDuplicateSlicesInDestination(t *testing.T) {
|
||||
|
||||
type Staffs struct {
|
||||
StaffList []model.Staff
|
||||
|
|
@ -645,7 +645,7 @@ func TestDuplicateSlicesInDestination(t *testing.T) {
|
|||
`)
|
||||
}
|
||||
|
||||
func TestExecution1(t *testing.T) {
|
||||
func TestSelectExecution1(t *testing.T) {
|
||||
stmt := City.
|
||||
INNER_JOIN(Address, Address.CityID.EQ(City.CityID)).
|
||||
INNER_JOIN(Customer, Customer.AddressID.EQ(Address.AddressID)).
|
||||
|
|
@ -706,7 +706,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|||
|
||||
}
|
||||
|
||||
func TestExecution2(t *testing.T) {
|
||||
func TestSelectExecution2(t *testing.T) {
|
||||
|
||||
type MyAddress struct {
|
||||
ID int32 `sql:"primary_key"`
|
||||
|
|
@ -769,7 +769,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|||
require.Equal(t, *dest[0].Customers[1].LastName, "Vines")
|
||||
}
|
||||
|
||||
func TestExecution3(t *testing.T) {
|
||||
func TestSelectExecution3(t *testing.T) {
|
||||
|
||||
var dest []struct {
|
||||
CityID int32 `sql:"primary_key"`
|
||||
|
|
@ -826,7 +826,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|||
require.Equal(t, *dest[0].Customers[1].LastName, "Vines")
|
||||
}
|
||||
|
||||
func TestExecution4(t *testing.T) {
|
||||
func TestSelectExecution4(t *testing.T) {
|
||||
|
||||
var dest []struct {
|
||||
CityID int32 `sql:"primary_key" alias:"city.city_id"`
|
||||
|
|
@ -918,7 +918,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|||
}
|
||||
|
||||
// Test join with custom primary keys (sql.NullInt64)
|
||||
func TestExecutionCustomPKTypes1(t *testing.T) {
|
||||
func TestSelectExecutionCustomPKTypes1(t *testing.T) {
|
||||
|
||||
var dest []struct {
|
||||
CityID sql.NullInt64 `sql:"primary_key" alias:"city.city_id"`
|
||||
|
|
@ -1039,7 +1039,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|||
}
|
||||
|
||||
// Test join with custom primary keys (null.Int)
|
||||
func TestExecutionCustomPKTypes2(t *testing.T) {
|
||||
func TestSelectExecutionCustomPKTypes2(t *testing.T) {
|
||||
|
||||
var dest []struct {
|
||||
CityID null.Int `sql:"primary_key" alias:"city.city_id"`
|
||||
|
|
@ -1135,7 +1135,7 @@ ORDER BY city.city_id, address.address_id, customer.customer_id;
|
|||
`)
|
||||
}
|
||||
|
||||
func TestJoinQuerySliceWithPtrs(t *testing.T) {
|
||||
func TestSelectJoinQuerySliceWithPtrs(t *testing.T) {
|
||||
type FilmsPerLanguage struct {
|
||||
Language model.Language
|
||||
Film *[]*model.Film
|
||||
|
|
@ -1160,7 +1160,7 @@ func TestJoinQuerySliceWithPtrs(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSelect_WithoutUniqueColumnSelected(t *testing.T) {
|
||||
query := Customer.SELECT(Customer.FirstName, Customer.LastName, Customer.Email)
|
||||
query := Customer.SELECT(Customer.FirstName, Customer.LastName, Customer.Email).ORDER_BY(Customer.Email)
|
||||
|
||||
var customers []model.Customer
|
||||
|
||||
|
|
@ -1219,7 +1219,7 @@ func TestSelectOrderByAscDesc(t *testing.T) {
|
|||
testutils.AssertDeepEqual(t, customerAscDesc327, customersAscDesc[327])
|
||||
}
|
||||
|
||||
func TestOrderBy(t *testing.T) {
|
||||
func TestSelectOrderBy(t *testing.T) {
|
||||
|
||||
t.Run("default", func(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
|
|
@ -1650,7 +1650,7 @@ LIMIT 1000;
|
|||
testutils.AssertDeepEqual(t, films[0], thesameLengthFilms{"Alien Center", "Iron Moon", 46})
|
||||
}
|
||||
|
||||
func TestSubQuery(t *testing.T) {
|
||||
func TestSelectSubQuery(t *testing.T) {
|
||||
rRatingFilms :=
|
||||
SELECT(
|
||||
Film.FilmID,
|
||||
|
|
@ -1816,7 +1816,7 @@ ORDER BY film.film_id ASC;
|
|||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL)
|
||||
|
||||
maxRentalRateFilms := []model.Film{}
|
||||
var maxRentalRateFilms []model.Film
|
||||
err := query.Query(db, &maxRentalRateFilms)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
|
@ -1920,7 +1920,7 @@ ORDER BY customer.customer_id, SUM(payment.amount) ASC;
|
|||
testutils.AssertJSONFile(t, dest, "./testdata/results/postgres/customer_payment_sum.json")
|
||||
}
|
||||
|
||||
func TestGroupByGroupingSets(t *testing.T) {
|
||||
func TestSelectGroupByGroupingSets(t *testing.T) {
|
||||
skipForCockroachDB(t)
|
||||
|
||||
stmt := SELECT(
|
||||
|
|
@ -2002,7 +2002,7 @@ ORDER BY inventory.film_id, inventory.store_id;
|
|||
`)
|
||||
}
|
||||
|
||||
func TestGroupByCube(t *testing.T) {
|
||||
func TestSelectGroupByCube(t *testing.T) {
|
||||
skipForCockroachDB(t)
|
||||
|
||||
stmt := SELECT(
|
||||
|
|
@ -2079,7 +2079,7 @@ ORDER BY country.country, city.city;
|
|||
`)
|
||||
}
|
||||
|
||||
func TestGroupByRollup(t *testing.T) {
|
||||
func TestSelectGroupByRollup(t *testing.T) {
|
||||
skipForCockroachDB(t)
|
||||
|
||||
stmt := SELECT(
|
||||
|
|
@ -2166,7 +2166,7 @@ ORDER BY year ASC, EXTRACT(MONTH FROM rental.rental_date) ASC, day ASC;
|
|||
`)
|
||||
}
|
||||
|
||||
func TestAggregateFunctionDistinct(t *testing.T) {
|
||||
func TestSelectAggregateFunctionDistinct(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Payment.CustomerID,
|
||||
|
||||
|
|
@ -2297,7 +2297,7 @@ ORDER BY customer_payment_sum.amount_sum ASC;
|
|||
}
|
||||
|
||||
func TestSelectStaff(t *testing.T) {
|
||||
staffs := []model.Staff{}
|
||||
var staffs []model.Staff
|
||||
|
||||
err := Staff.SELECT(Staff.AllColumns).Query(db, &staffs)
|
||||
|
||||
|
|
@ -2371,7 +2371,7 @@ ORDER BY payment.payment_date ASC;
|
|||
})
|
||||
}
|
||||
|
||||
func TestUnion(t *testing.T) {
|
||||
func TestSelectUnion(t *testing.T) {
|
||||
expectedQuery := `
|
||||
(
|
||||
SELECT payment.payment_id AS "payment.payment_id",
|
||||
|
|
@ -2424,7 +2424,7 @@ OFFSET 20;
|
|||
})
|
||||
}
|
||||
|
||||
func TestUnionOffsetWithExpression(t *testing.T) {
|
||||
func TestSelectUnionOffsetWithExpression(t *testing.T) {
|
||||
stmt := UNION(
|
||||
SELECT(Rental.AllColumns).
|
||||
FROM(Rental).
|
||||
|
|
@ -2474,7 +2474,7 @@ OFFSET (
|
|||
require.Len(t, dest, 10)
|
||||
}
|
||||
|
||||
func TestAllSetOperators(t *testing.T) {
|
||||
func TestSelectAllSetOperators(t *testing.T) {
|
||||
var select1 = Payment.SELECT(Payment.AllColumns).WHERE(Payment.PaymentID.GT_EQ(Int(17600)).AND(Payment.PaymentID.LT(Int(17610))))
|
||||
var select2 = Payment.SELECT(Payment.AllColumns).WHERE(Payment.PaymentID.GT_EQ(Int(17620)).AND(Payment.PaymentID.LT(Int(17630))))
|
||||
|
||||
|
|
@ -2579,46 +2579,30 @@ func getRowLockTestData() map[RowLock]string {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRowLock(t *testing.T) {
|
||||
func TestSelectRowLock(t *testing.T) {
|
||||
|
||||
query := SELECT(STAR).
|
||||
FROM(Address).
|
||||
WHERE(Address.AddressID.LT(Int(10)))
|
||||
|
||||
expectedSQL := `
|
||||
SELECT *
|
||||
FROM dvds.address
|
||||
LIMIT 3
|
||||
WHERE address.address_id < 10
|
||||
FOR`
|
||||
query := Address.
|
||||
SELECT(STAR).
|
||||
LIMIT(3)
|
||||
|
||||
for lockType, lockTypeStr := range getRowLockTestData() {
|
||||
query.FOR(lockType)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+";\n", int64(3))
|
||||
|
||||
tx, _ := db.Begin()
|
||||
|
||||
res, err := query.Exec(tx)
|
||||
require.NoError(t, err)
|
||||
rowsAffected, _ := res.RowsAffected()
|
||||
require.Equal(t, rowsAffected, int64(3))
|
||||
|
||||
err = tx.Rollback()
|
||||
require.NoError(t, err)
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+";\n", int64(10))
|
||||
testutils.AssertExecAndRollback(t, query, db, 9)
|
||||
}
|
||||
|
||||
for lockType, lockTypeStr := range getRowLockTestData() {
|
||||
query.FOR(lockType.NOWAIT())
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+" NOWAIT;\n", int64(3))
|
||||
|
||||
tx, _ := db.Begin()
|
||||
|
||||
res, err := query.Exec(tx)
|
||||
require.NoError(t, err)
|
||||
rowsAffected, _ := res.RowsAffected()
|
||||
require.Equal(t, rowsAffected, int64(3))
|
||||
|
||||
err = tx.Rollback()
|
||||
require.NoError(t, err)
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+" NOWAIT;\n", int64(10))
|
||||
testutils.AssertExecAndRollback(t, query, db, 9)
|
||||
}
|
||||
|
||||
if sourceIsCockroachDB() {
|
||||
|
|
@ -2628,21 +2612,13 @@ FOR`
|
|||
for lockType, lockTypeStr := range getRowLockTestData() {
|
||||
query.FOR(lockType.SKIP_LOCKED())
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+" SKIP LOCKED;\n", int64(3))
|
||||
|
||||
tx, _ := db.Begin()
|
||||
|
||||
res, err := query.Exec(tx)
|
||||
require.NoError(t, err)
|
||||
rowsAffected, _ := res.RowsAffected()
|
||||
require.Equal(t, rowsAffected, int64(3))
|
||||
|
||||
err = tx.Rollback()
|
||||
require.NoError(t, err)
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL+" "+lockTypeStr+" SKIP LOCKED;\n", int64(10))
|
||||
testutils.AssertExecAndRollback(t, query, db, 9)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRowLockWithUpdateOf(t *testing.T) {
|
||||
func TestSelectRowLockWithUpdateOf(t *testing.T) {
|
||||
|
||||
stmt := SELECT(
|
||||
Film.FilmID,
|
||||
Film.Title,
|
||||
|
|
@ -2672,9 +2648,10 @@ LIMIT 1
|
|||
FOR UPDATE OF film, actor NOWAIT;
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []struct {
|
||||
model.Film
|
||||
|
||||
CategoryID int
|
||||
Actor []model.Actor
|
||||
}
|
||||
|
|
@ -2685,7 +2662,7 @@ FOR UPDATE OF film, actor NOWAIT;
|
|||
})
|
||||
}
|
||||
|
||||
func TestRowLockWithUpdateOfAliasedTable(t *testing.T) {
|
||||
func TestSelectRowLockWithUpdateOfAliasedTable(t *testing.T) {
|
||||
|
||||
myFilm := Film.AS("myFilm")
|
||||
|
||||
|
|
@ -2708,18 +2685,18 @@ LIMIT 1
|
|||
FOR UPDATE OF "myFilm";
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []struct {
|
||||
model.Film `alias:"myFilm.*"`
|
||||
}
|
||||
|
||||
err := stmt.Query(db, &dest)
|
||||
err := stmt.Query(tx, &dest)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, dest, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestQuickStart(t *testing.T) {
|
||||
func TestSelectQuickStart(t *testing.T) {
|
||||
|
||||
var expectedSQL = `
|
||||
SELECT actor.actor_id AS "actor.actor_id",
|
||||
|
|
@ -2810,7 +2787,7 @@ ORDER BY actor.actor_id ASC, film.film_id ASC;
|
|||
testutils.AssertJSONFile(t, dest2, "./testdata/results/postgres/quick-start-dest2.json")
|
||||
}
|
||||
|
||||
func TestQuickStartWithSubQueries(t *testing.T) {
|
||||
func TestSelectQuickStartWithSubQueries(t *testing.T) {
|
||||
|
||||
filmLogerThan180 := Film.
|
||||
SELECT(Film.AllColumns).
|
||||
|
|
@ -2875,7 +2852,7 @@ func TestQuickStartWithSubQueries(t *testing.T) {
|
|||
testutils.AssertJSONFile(t, dest2, "./testdata/results/postgres/quick-start-dest2.json")
|
||||
}
|
||||
|
||||
func TestExpressionWrappers(t *testing.T) {
|
||||
func TestSelectExpressionWrappers(t *testing.T) {
|
||||
query := SELECT(
|
||||
BoolExp(Raw("true")),
|
||||
IntExp(Raw("11")),
|
||||
|
|
@ -2905,7 +2882,7 @@ SELECT true,
|
|||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestWindowFunction(t *testing.T) {
|
||||
func TestSelectWindowFunction(t *testing.T) {
|
||||
var expectedSQL = `
|
||||
SELECT AVG(payment.amount) OVER (),
|
||||
AVG(payment.amount) OVER (PARTITION BY payment.customer_id),
|
||||
|
|
@ -2977,7 +2954,7 @@ GROUP BY payment.amount, payment.customer_id, payment.payment_date;
|
|||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestWindowClause(t *testing.T) {
|
||||
func TestSelectWindowClause(t *testing.T) {
|
||||
var expectedSQL = `
|
||||
SELECT AVG(payment.amount) OVER (),
|
||||
AVG(payment.amount) OVER (w1),
|
||||
|
|
@ -3014,7 +2991,7 @@ ORDER BY payment.customer_id;
|
|||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestSimpleView(t *testing.T) {
|
||||
func TestSelectSimpleView(t *testing.T) {
|
||||
|
||||
query := SELECT(
|
||||
view.ActorInfo.AllColumns,
|
||||
|
|
@ -3053,7 +3030,7 @@ func TestSimpleView(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestJoinViewWithTable(t *testing.T) {
|
||||
func TestSelectJoinViewWithTable(t *testing.T) {
|
||||
query := SELECT(
|
||||
view.CustomerList.AllColumns,
|
||||
Rental.AllColumns,
|
||||
|
|
@ -3079,7 +3056,7 @@ func TestJoinViewWithTable(t *testing.T) {
|
|||
require.Equal(t, len(dest[1].Rentals), 27)
|
||||
}
|
||||
|
||||
func TestDynamicProjectionList(t *testing.T) {
|
||||
func TestSelectDynamicProjectionList(t *testing.T) {
|
||||
|
||||
var request struct {
|
||||
ColumnsToSelect []string
|
||||
|
|
@ -3126,7 +3103,7 @@ LIMIT 3;
|
|||
require.Equal(t, len(dest), 3)
|
||||
}
|
||||
|
||||
func TestDynamicCondition(t *testing.T) {
|
||||
func TestSelectDynamicCondition(t *testing.T) {
|
||||
var request struct {
|
||||
CustomerID *int64
|
||||
Email *string
|
||||
|
|
@ -3176,7 +3153,7 @@ WHERE ($1::boolean AND (customer.customer_id = $2)) AND (customer.activebool = $
|
|||
testutils.AssertDeepEqual(t, dest[0], customer0)
|
||||
}
|
||||
|
||||
func TestLateral(t *testing.T) {
|
||||
func TestSelectLateral(t *testing.T) {
|
||||
|
||||
languages := LATERAL(
|
||||
SELECT(
|
||||
|
|
@ -3348,7 +3325,7 @@ type ActorWrap struct {
|
|||
Films []FilmWrap
|
||||
}
|
||||
|
||||
func TestRecursionScanNxM(t *testing.T) {
|
||||
func TestSelectRecursionScanNxM(t *testing.T) {
|
||||
|
||||
stmt := SELECT(
|
||||
Actor.AllColumns,
|
||||
|
|
@ -3491,7 +3468,7 @@ type StaffWrap struct {
|
|||
Store StoreWrap
|
||||
}
|
||||
|
||||
func TestRecursionScanNx1(t *testing.T) {
|
||||
func TestSelectRecursionScanNx1(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
Store.AllColumns,
|
||||
Staff.AllColumns,
|
||||
|
|
@ -3638,7 +3615,7 @@ type ManagerInfo struct {
|
|||
Store *StoreInfo
|
||||
}
|
||||
|
||||
func TestRecursionScan1x1(t *testing.T) {
|
||||
func TestSelectRecursionScan1x1(t *testing.T) {
|
||||
|
||||
stmt := SELECT(
|
||||
Store.AllColumns,
|
||||
|
|
@ -3703,7 +3680,7 @@ func TestRecursionScan1x1(t *testing.T) {
|
|||
// In parameterized statements integer literals, like Int(num), are replaced with a placeholders. For some expressions,
|
||||
// postgres interpreter will not have enough information to deduce the type. If this is the case postgres returns an error.
|
||||
// Int8, Int16, .... functions will add automatic type cast over placeholder, so type deduction is always possible.
|
||||
func TestLiteralTypeDeduction(t *testing.T) {
|
||||
func TestSelectLiteralTypeDeduction(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
SUM(
|
||||
CASE().WHEN(Staff.Active.IS_TRUE()).
|
||||
|
|
@ -3725,7 +3702,7 @@ func GET_FILM_COUNT(lenFrom, lenTo IntegerExpression) IntegerExpression {
|
|||
return IntExp(Func("dvds.get_film_count", lenFrom, lenTo))
|
||||
}
|
||||
|
||||
func TestCustomFunctionCall(t *testing.T) {
|
||||
func TestSelectCustomFunctionCall(t *testing.T) {
|
||||
skipForCockroachDB(t)
|
||||
|
||||
stmt := SELECT(
|
||||
|
|
@ -3761,7 +3738,7 @@ SELECT dvds.get_film_count(100, 120) AS "film_count";
|
|||
require.Equal(t, dest.FilmCount, 165)
|
||||
}
|
||||
|
||||
func TestScanUsingConn(t *testing.T) {
|
||||
func TestSelectScanUsingConn(t *testing.T) {
|
||||
conn, err := db.Conn(context.Background())
|
||||
require.NoError(t, err)
|
||||
defer conn.Close()
|
||||
|
|
@ -3803,7 +3780,7 @@ func TestScanUsingConn(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestConditionalFunctions(t *testing.T) {
|
||||
func TestSelectConditionalFunctions(t *testing.T) {
|
||||
stmt := SELECT(
|
||||
EXISTS(
|
||||
Film.SELECT(Film.FilmID).WHERE(Film.RentalDuration.GT(Int(100))),
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ package postgres
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
model2 "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/dvds/model"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/dvds/table"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/model"
|
||||
|
|
@ -27,7 +27,7 @@ SET (name, url) = ('Bong', 'http://bong.com')
|
|||
WHERE link.name = 'Bing'::text;
|
||||
`, "Bong", "http://bong.com", "Bing")
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
|
||||
testutils.AssertExec(t, query, tx, 1)
|
||||
requireLogged(t, query)
|
||||
|
|
@ -142,7 +142,7 @@ RETURNING link.id AS "link.id",
|
|||
link.description AS "link.description";
|
||||
`, "DuckDuckGo", "http://www.duckduckgo.com", "Ask")
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
links := []model.Link{}
|
||||
|
||||
err := stmt.Query(tx, &links)
|
||||
|
|
@ -325,8 +325,8 @@ func TestUpdateQueryContext(t *testing.T) {
|
|||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
dest := []model.Link{}
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []model.Link
|
||||
err := updateStmt.QueryContext(ctx, tx, &dest)
|
||||
|
||||
require.Error(t, err, "context deadline exceeded")
|
||||
|
|
@ -344,7 +344,10 @@ func TestUpdateExecContext(t *testing.T) {
|
|||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
testutils.AssertExecContextErr(ctx, t, updateStmt, db, "context deadline exceeded")
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
_, err := updateStmt.ExecContext(ctx, db)
|
||||
require.Error(t, err, "context deadline exceeded")
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateFrom(t *testing.T) {
|
||||
|
|
@ -385,7 +388,7 @@ RETURNING rental.rental_id AS "rental.rental_id",
|
|||
store.address_id AS "store.address_id";
|
||||
`)
|
||||
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
||||
var dest []struct {
|
||||
Rental model2.Rental
|
||||
Store model2.Store
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue