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

@ -12,6 +12,7 @@ type UpdateStatement interface {
MODEL(data interface{}) UpdateStatement
WHERE(expression BoolExpression) UpdateStatement
LIMIT(limit int64) UpdateStatement
}
type updateStatementImpl struct {
@ -21,6 +22,7 @@ type updateStatementImpl struct {
Set jet.SetClause
SetNew jet.SetClauseNew
Where jet.ClauseWhere
Limit jet.ClauseLimit
}
func newUpdateStatement(table Table, columns []jet.Column) UpdateStatement {
@ -29,11 +31,13 @@ func newUpdateStatement(table Table, columns []jet.Column) UpdateStatement {
&update.Update,
&update.Set,
&update.SetNew,
&update.Where)
&update.Where,
&update.Limit)
update.Update.Table = table
update.Set.Columns = columns
update.Where.Mandatory = true
update.Limit.Count = -1 // Initialize to -1 to indicate no LIMIT
return update
}
@ -67,3 +71,11 @@ func (u *updateStatementImpl) WHERE(expression BoolExpression) UpdateStatement {
u.Where.Condition = expression
return u
}
func (u *updateStatementImpl) LIMIT(limit int64) UpdateStatement {
if _, isJoinTable := u.Update.Table.(*joinTable); isJoinTable {
panic("jet: MySQL does not support LIMIT with multi-table UPDATE statements")
}
u.Limit.Count = limit
return u
}