feature: Add support for LIMIT query in UPDATE for sql

Signed-off-by: Rohan Hasabe <rohanhasabe8@gmail.com>
This commit is contained in:
Rohan Hasabe 2025-02-08 17:25:02 -05:00
parent 00b8155f74
commit d733f9688e
No known key found for this signature in database
GPG key ID: BA28FE62BF71733A
3 changed files with 101 additions and 5 deletions

View file

@ -2,10 +2,11 @@ package mysql
import (
"context"
"github.com/go-jet/jet/v2/qrm"
"testing"
"time"
"github.com/go-jet/jet/v2/qrm"
"github.com/stretchr/testify/require"
"github.com/go-jet/jet/v2/internal/testutils"
@ -285,3 +286,56 @@ WHERE link.name = 'Bing';
require.NoError(t, err)
})
}
func TestUpdateWithLimit(t *testing.T) {
t.Run("single table update with limit", func(t *testing.T) {
stmt := Link.
UPDATE(Link.Name).
SET(String("Updated Link")).
WHERE(Link.Name.NOT_EQ(String(""))).
LIMIT(2)
testutils.AssertDebugStatementSql(t, stmt, `
UPDATE test_sample.link
SET name = 'Updated Link'
WHERE link.name != ''
LIMIT 2;
`)
testutils.ExecuteInTxAndRollback(t, db, func(tx qrm.DB) {
// Execute update
testutils.AssertExec(t, stmt, tx)
requireLogged(t, stmt)
// Verify only 2 rows were updated
var updatedLinks []model.Link
err := Link.
SELECT(Link.AllColumns).
WHERE(Link.Name.EQ(String("Updated Link"))).
Query(tx, &updatedLinks)
require.NoError(t, err)
require.Equal(t, 2, len(updatedLinks))
})
})
t.Run("multi-table update with limit should panic", func(t *testing.T) {
defer func() {
r := recover()
require.NotNil(t, r)
require.Equal(t, "jet: MySQL does not support LIMIT with multi-table UPDATE statements", r)
}()
joinedTable := Link.
INNER_JOIN(Link2, Link.Name.EQ(Link2.Name))
stmt := joinedTable.
UPDATE(Link.Name).
SET(String("Updated Link")).
WHERE(Link.Name.NOT_EQ(String(""))).
LIMIT(2)
// Statement construction itself should panic
_ = stmt
})
}