2019-08-02 11:29:38 +02:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-05-14 16:26:47 +00:00
|
|
|
"source.gleipnir.technology/Gleipnir/jet/internal/testutils"
|
|
|
|
|
. "source.gleipnir.technology/Gleipnir/jet/mysql"
|
|
|
|
|
"source.gleipnir.technology/Gleipnir/jet/qrm"
|
2026-05-14 16:30:22 +00:00
|
|
|
"source.gleipnir.technology/Gleipnir/jet/v2/tests/.gentestdata/mysql/dvds/table"
|
|
|
|
|
"source.gleipnir.technology/Gleipnir/jet/v2/tests/.gentestdata/mysql/test_sample/model"
|
|
|
|
|
. "source.gleipnir.technology/Gleipnir/jet/v2/tests/.gentestdata/mysql/test_sample/table"
|
2020-05-09 11:00:22 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-02 11:29:38 +02:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDeleteWithWhere(t *testing.T) {
|
|
|
|
|
deleteStmt := Link.
|
|
|
|
|
DELETE().
|
|
|
|
|
WHERE(Link.Name.IN(String("Gmail"), String("Outlook")))
|
|
|
|
|
|
2022-05-05 13:01:42 +02:00
|
|
|
testutils.AssertDebugStatementSql(t, deleteStmt, `
|
|
|
|
|
DELETE FROM test_sample.link
|
|
|
|
|
WHERE link.name IN ('Gmail', 'Outlook');
|
|
|
|
|
`, "Gmail", "Outlook")
|
|
|
|
|
|
|
|
|
|
testutils.AssertExecAndRollback(t, deleteStmt, db, 2)
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, deleteStmt)
|
2019-08-02 11:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-11 18:23:02 +02:00
|
|
|
func TestDeleteWithWhereOrderByLimit(t *testing.T) {
|
|
|
|
|
var expectedSQL = `
|
|
|
|
|
DELETE FROM test_sample.link
|
|
|
|
|
WHERE link.name IN ('Gmail', 'Outlook')
|
|
|
|
|
ORDER BY link.name
|
|
|
|
|
LIMIT 1;
|
|
|
|
|
`
|
|
|
|
|
deleteStmt := Link.
|
|
|
|
|
DELETE().
|
|
|
|
|
WHERE(Link.Name.IN(String("Gmail"), String("Outlook"))).
|
|
|
|
|
ORDER_BY(Link.Name).
|
|
|
|
|
LIMIT(1)
|
|
|
|
|
|
|
|
|
|
testutils.AssertDebugStatementSql(t, deleteStmt, expectedSQL, "Gmail", "Outlook", int64(1))
|
2022-05-05 13:01:42 +02:00
|
|
|
testutils.AssertExecAndRollback(t, deleteStmt, db, 1)
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, deleteStmt)
|
2019-08-11 18:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-02 11:29:38 +02:00
|
|
|
func TestDeleteQueryContext(t *testing.T) {
|
|
|
|
|
deleteStmt := Link.
|
|
|
|
|
DELETE().
|
|
|
|
|
WHERE(Link.Name.IN(String("Gmail"), String("Outlook")))
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
2022-05-05 13:01:42 +02:00
|
|
|
var dest []model.Link
|
2019-08-02 11:29:38 +02:00
|
|
|
err := deleteStmt.QueryContext(ctx, db, &dest)
|
|
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Error(t, err, "context deadline exceeded")
|
2020-05-10 11:41:07 +02:00
|
|
|
requireLogged(t, deleteStmt)
|
2019-08-02 11:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDeleteExecContext(t *testing.T) {
|
|
|
|
|
deleteStmt := Link.
|
|
|
|
|
DELETE().
|
|
|
|
|
WHERE(Link.Name.IN(String("Gmail"), String("Outlook")))
|
|
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Microsecond)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
|
|
_, err := deleteStmt.ExecContext(ctx, db)
|
|
|
|
|
|
2020-05-09 11:00:22 +02:00
|
|
|
require.Error(t, err, "context deadline exceeded")
|
2019-08-02 11:29:38 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-08 18:14:57 +01:00
|
|
|
func TestDeleteWithUsing(t *testing.T) {
|
|
|
|
|
stmt := table.Rental.DELETE().
|
|
|
|
|
USING(
|
|
|
|
|
table.Rental.
|
|
|
|
|
INNER_JOIN(table.Staff, table.Rental.StaffID.EQ(table.Staff.StaffID)),
|
|
|
|
|
table.Actor,
|
2022-01-20 16:54:18 +01:00
|
|
|
).
|
|
|
|
|
WHERE(
|
2022-01-20 17:06:03 +01:00
|
|
|
table.Staff.StaffID.NOT_EQ(Int(2)).
|
|
|
|
|
AND(table.Rental.RentalID.LT(Int(100))),
|
2022-01-20 16:54:18 +01:00
|
|
|
)
|
2021-12-08 18:14:57 +01:00
|
|
|
|
|
|
|
|
testutils.AssertStatementSql(t, stmt, `
|
|
|
|
|
DELETE FROM dvds.rental
|
|
|
|
|
USING dvds.rental
|
|
|
|
|
INNER JOIN dvds.staff ON (rental.staff_id = staff.staff_id),
|
|
|
|
|
dvds.actor
|
2022-01-20 17:06:03 +01:00
|
|
|
WHERE (staff.staff_id != ?) AND (rental.rental_id < ?);
|
2021-12-08 18:14:57 +01:00
|
|
|
`)
|
|
|
|
|
|
2022-05-05 13:01:42 +02:00
|
|
|
testutils.AssertExecAndRollback(t, stmt, db)
|
2021-12-08 18:14:57 +01:00
|
|
|
}
|
2022-09-29 13:33:00 +02:00
|
|
|
|
|
|
|
|
func TestDeleteOptimizerHints(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
stmt := Link.DELETE().
|
|
|
|
|
OPTIMIZER_HINTS(QB_NAME("deleteIns"), "MRR(link)").
|
|
|
|
|
WHERE(
|
|
|
|
|
Link.Name.IN(String("Gmail"), String("Outlook")),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
testutils.AssertDebugStatementSql(t, stmt, `
|
|
|
|
|
DELETE /*+ QB_NAME(deleteIns) MRR(link) */ FROM test_sample.link
|
|
|
|
|
WHERE link.name IN ('Gmail', 'Outlook');
|
|
|
|
|
`)
|
|
|
|
|
|
2024-03-07 18:01:31 +01:00
|
|
|
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
|
2022-09-29 13:33:00 +02:00
|
|
|
_, err := stmt.Exec(tx)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
}
|