feature: Add support for LIMIT query in UPDATE for sql
Signed-off-by: Rohan Hasabe <rohanhasabe8@gmail.com>
This commit is contained in:
parent
00b8155f74
commit
d733f9688e
3 changed files with 101 additions and 5 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,21 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestUpdateWithLimit(t *testing.T) {
|
||||
expectedSQL := `
|
||||
UPDATE db.table1
|
||||
SET col_int = ?
|
||||
WHERE table1.col_int >= ?
|
||||
LIMIT ?;
|
||||
`
|
||||
stmt := table1.UPDATE(table1ColInt).
|
||||
SET(1).
|
||||
WHERE(table1ColInt.GT_EQ(Int(33))).
|
||||
LIMIT(5)
|
||||
|
||||
assertStatementSql(t, stmt, expectedSQL, 1, int64(33), int64(5))
|
||||
}
|
||||
|
||||
func TestUpdateWithOneValue(t *testing.T) {
|
||||
expectedSQL := `
|
||||
UPDATE db.table1
|
||||
|
|
@ -69,11 +84,26 @@ func TestUpdateReservedWorldColumn(t *testing.T) {
|
|||
Load: "foo",
|
||||
},
|
||||
).
|
||||
WHERE(loadColumn.EQ(String("bar"))), strings.Replace(`
|
||||
WHERE(loadColumn.EQ(String("bar"))).
|
||||
LIMIT(0),
|
||||
strings.Replace(`
|
||||
UPDATE db.table1
|
||||
SET ''Load'' = ?
|
||||
WHERE ''Load'' = ?;
|
||||
`, "''", "`", -1), "foo", "bar")
|
||||
WHERE ''Load'' = ?
|
||||
LIMIT ?;
|
||||
`, "''", "`", -1), "foo", "bar", int64(0))
|
||||
}
|
||||
|
||||
func LimitPanicStatement() {
|
||||
joinedTable := table1.INNER_JOIN(table2, table1Col1.EQ(table2Col3))
|
||||
joinedTable.UPDATE(table1ColInt).
|
||||
SET(1).
|
||||
WHERE(table1ColInt.GT_EQ(Int(33))).
|
||||
LIMIT(5)
|
||||
}
|
||||
|
||||
func TestUpdateWithMultiTableAndLimit(t *testing.T) {
|
||||
assertPanicErr(t, func() { LimitPanicStatement() }, "jet: MySQL does not support LIMIT with multi-table UPDATE statements")
|
||||
}
|
||||
|
||||
func TestInvalidInputs(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue