feat: add returning to mysql

This commit is contained in:
Christian Groschupp 2025-11-13 16:57:58 +01:00
parent 0585cd1949
commit b2b1a59a45
5 changed files with 42 additions and 6 deletions

View file

@ -12,16 +12,18 @@ type DeleteStatement interface {
WHERE(expression BoolExpression) DeleteStatement
ORDER_BY(orderByClauses ...OrderByClause) DeleteStatement
LIMIT(limit int64) DeleteStatement
RETURNING(projections ...jet.Projection) DeleteStatement
}
type deleteStatementImpl struct {
jet.SerializerStatement
Delete jet.ClauseDelete
Using jet.ClauseFrom
Where jet.ClauseWhere
OrderBy jet.ClauseOrderBy
Limit jet.ClauseLimit
Delete jet.ClauseDelete
Using jet.ClauseFrom
Where jet.ClauseWhere
OrderBy jet.ClauseOrderBy
Limit jet.ClauseLimit
Returning jet.ClauseReturning
}
func newDeleteStatement(table Table) DeleteStatement {
@ -32,6 +34,7 @@ func newDeleteStatement(table Table) DeleteStatement {
&newDelete.Where,
&newDelete.OrderBy,
&newDelete.Limit,
&newDelete.Returning,
)
newDelete.Delete.Table = table
@ -66,3 +69,8 @@ func (d *deleteStatementImpl) LIMIT(limit int64) DeleteStatement {
d.Limit.Count = limit
return d
}
func (d *deleteStatementImpl) RETURNING(projections ...jet.Projection) DeleteStatement {
d.Returning.ProjectionList = projections
return d
}

View file

@ -24,3 +24,11 @@ ORDER BY table1.col1
LIMIT ?;
`, int64(1), int64(1))
}
func TestDeleteWithWhereAndReturning(t *testing.T) {
assertStatementSql(t, table1.DELETE().WHERE(table1Col1.EQ(Int(1))).RETURNING(table1Col1), `
DELETE FROM db.table1
WHERE table1.col1 = ?
RETURNING table1.col1 AS "table1.col1";
`, int64(1))
}

View file

@ -3,6 +3,7 @@ package mysql
import (
"encoding/hex"
"fmt"
"github.com/go-jet/jet/v2/internal/jet"
)
@ -437,6 +438,7 @@ var reservedWords = []string{
"RESIGNAL",
"RESTRICT",
"RETURN",
"RETURNING",
"REVOKE",
"RIGHT",
"RLIKE",

View file

@ -19,6 +19,8 @@ type InsertStatement interface {
ON_DUPLICATE_KEY_UPDATE(assigments ...ColumnAssigment) InsertStatement
QUERY(selectStatement SelectStatement) InsertStatement
RETURNING(projections ...Projection) InsertStatement
}
func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
@ -27,6 +29,7 @@ func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
&newInsert.Insert,
&newInsert.ValuesQuery,
&newInsert.OnDuplicateKey,
&newInsert.Returning,
)
newInsert.Insert.Table = table
@ -40,6 +43,7 @@ type insertStatementImpl struct {
Insert jet.ClauseInsert
ValuesQuery jet.ClauseValuesQuery
Returning jet.ClauseReturning
OnDuplicateKey onDuplicateKeyUpdateClause
}
@ -63,6 +67,11 @@ func (is *insertStatementImpl) MODELS(data interface{}) InsertStatement {
return is
}
func (i *insertStatementImpl) RETURNING(projections ...jet.Projection) InsertStatement {
i.Returning.ProjectionList = projections
return i
}
func (is *insertStatementImpl) AS_NEW() InsertStatement {
is.ValuesQuery.As = "new"
return is

View file

@ -1,9 +1,10 @@
package mysql
import (
"github.com/stretchr/testify/require"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestInvalidInsert(t *testing.T) {
@ -24,6 +25,14 @@ VALUES (?);
`, int(1))
}
func TestInsertWithReturing(t *testing.T) {
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(1).RETURNING(table1Col1), `
INSERT INTO db.table1 (col1)
VALUES (?)
RETURNING table1.col1 AS "table1.col1";
`, int(1))
}
func TestInsertWithColumnList(t *testing.T) {
columnList := ColumnList{table3ColInt}