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,6 +12,7 @@ type DeleteStatement interface {
WHERE(expression BoolExpression) DeleteStatement WHERE(expression BoolExpression) DeleteStatement
ORDER_BY(orderByClauses ...OrderByClause) DeleteStatement ORDER_BY(orderByClauses ...OrderByClause) DeleteStatement
LIMIT(limit int64) DeleteStatement LIMIT(limit int64) DeleteStatement
RETURNING(projections ...jet.Projection) DeleteStatement
} }
type deleteStatementImpl struct { type deleteStatementImpl struct {
@ -22,6 +23,7 @@ type deleteStatementImpl struct {
Where jet.ClauseWhere Where jet.ClauseWhere
OrderBy jet.ClauseOrderBy OrderBy jet.ClauseOrderBy
Limit jet.ClauseLimit Limit jet.ClauseLimit
Returning jet.ClauseReturning
} }
func newDeleteStatement(table Table) DeleteStatement { func newDeleteStatement(table Table) DeleteStatement {
@ -32,6 +34,7 @@ func newDeleteStatement(table Table) DeleteStatement {
&newDelete.Where, &newDelete.Where,
&newDelete.OrderBy, &newDelete.OrderBy,
&newDelete.Limit, &newDelete.Limit,
&newDelete.Returning,
) )
newDelete.Delete.Table = table newDelete.Delete.Table = table
@ -66,3 +69,8 @@ func (d *deleteStatementImpl) LIMIT(limit int64) DeleteStatement {
d.Limit.Count = limit d.Limit.Count = limit
return d 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 ?; LIMIT ?;
`, int64(1), int64(1)) `, 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 ( import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/go-jet/jet/v2/internal/jet" "github.com/go-jet/jet/v2/internal/jet"
) )
@ -437,6 +438,7 @@ var reservedWords = []string{
"RESIGNAL", "RESIGNAL",
"RESTRICT", "RESTRICT",
"RETURN", "RETURN",
"RETURNING",
"REVOKE", "REVOKE",
"RIGHT", "RIGHT",
"RLIKE", "RLIKE",

View file

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

View file

@ -1,9 +1,10 @@
package mysql package mysql
import ( import (
"github.com/stretchr/testify/require"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require"
) )
func TestInvalidInsert(t *testing.T) { func TestInvalidInsert(t *testing.T) {
@ -24,6 +25,14 @@ VALUES (?);
`, int(1)) `, 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) { func TestInsertWithColumnList(t *testing.T) {
columnList := ColumnList{table3ColInt} columnList := ColumnList{table3ColInt}