Add USING clause support for DELETE statements
This commit is contained in:
parent
72e8d7d584
commit
60ffd004c5
6 changed files with 134 additions and 11 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
. "github.com/go-jet/jet/v2/mysql"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/mysql/dvds/table"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/mysql/test_sample/model"
|
||||
. "github.com/go-jet/jet/v2/tests/.gentestdata/mysql/test_sample/table"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -91,3 +92,29 @@ func initForDeleteTest(t *testing.T) {
|
|||
|
||||
testutils.AssertExec(t, stmt, db, 2)
|
||||
}
|
||||
|
||||
func TestDeleteWithUsing(t *testing.T) {
|
||||
tx := beginTx(t)
|
||||
defer tx.Rollback()
|
||||
|
||||
stmt := table.Rental.DELETE().
|
||||
USING(
|
||||
table.Rental.
|
||||
INNER_JOIN(table.Staff, table.Rental.StaffID.EQ(table.Staff.StaffID)),
|
||||
table.Actor,
|
||||
).WHERE(
|
||||
table.Staff.StaffID.EQ(Int(2)).
|
||||
AND(table.Rental.RentalID.LT(Int(10))),
|
||||
)
|
||||
|
||||
testutils.AssertStatementSql(t, stmt, `
|
||||
DELETE FROM dvds.rental
|
||||
USING dvds.rental
|
||||
INNER JOIN dvds.staff ON (rental.staff_id = staff.staff_id),
|
||||
dvds.actor
|
||||
WHERE (staff.staff_id = ?) AND (rental.rental_id < ?);
|
||||
`)
|
||||
|
||||
_, err := stmt.Exec(tx)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import (
|
|||
"context"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
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"
|
||||
. "github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/table"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
|
@ -103,3 +105,70 @@ func TestDeleteExecContext(t *testing.T) {
|
|||
require.Error(t, err, "context deadline exceeded")
|
||||
requireLogged(t, deleteStmt)
|
||||
}
|
||||
|
||||
func TestDeleteFrom(t *testing.T) {
|
||||
tx := beginTx(t)
|
||||
defer tx.Rollback()
|
||||
|
||||
stmt := table.Rental.DELETE().
|
||||
USING(
|
||||
table.Staff.
|
||||
INNER_JOIN(table.Store, table.Store.StoreID.EQ(table.Staff.StaffID)),
|
||||
table.Actor,
|
||||
).WHERE(
|
||||
table.Staff.StaffID.EQ(table.Rental.StaffID).
|
||||
AND(table.Staff.StaffID.EQ(Int(2))).
|
||||
AND(table.Rental.RentalID.LT(Int(10))),
|
||||
).RETURNING(
|
||||
table.Rental.AllColumns,
|
||||
table.Store.AllColumns,
|
||||
)
|
||||
|
||||
testutils.AssertStatementSql(t, stmt, `
|
||||
DELETE FROM dvds.rental
|
||||
USING dvds.staff
|
||||
INNER JOIN dvds.store ON (store.store_id = staff.staff_id),
|
||||
dvds.actor
|
||||
WHERE ((staff.staff_id = rental.staff_id) AND (staff.staff_id = $1)) AND (rental.rental_id < $2)
|
||||
RETURNING 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",
|
||||
store.store_id AS "store.store_id",
|
||||
store.manager_staff_id AS "store.manager_staff_id",
|
||||
store.address_id AS "store.address_id",
|
||||
store.last_update AS "store.last_update";
|
||||
`)
|
||||
|
||||
var dest []struct {
|
||||
Rental model2.Rental
|
||||
Store model2.Store
|
||||
}
|
||||
|
||||
err := stmt.Query(tx, &dest)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, dest, 3)
|
||||
testutils.AssertJSON(t, dest[0], `
|
||||
{
|
||||
"Rental": {
|
||||
"RentalID": 4,
|
||||
"RentalDate": "2005-05-24T23:04:41Z",
|
||||
"InventoryID": 2452,
|
||||
"CustomerID": 333,
|
||||
"ReturnDate": "2005-06-03T01:43:41Z",
|
||||
"StaffID": 2,
|
||||
"LastUpdate": "2006-02-16T02:30:53Z"
|
||||
},
|
||||
"Store": {
|
||||
"StoreID": 2,
|
||||
"ManagerStaffID": 2,
|
||||
"AddressID": 2,
|
||||
"LastUpdate": "2006-02-15T09:57:12Z"
|
||||
}
|
||||
}
|
||||
`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue