Skip to content

Commit adef2f9

Browse files
authored
Merge pull request #530 from cgroschupp/feat/mariadb-returning
feat: add returning to mysql
2 parents 0585cd1 + b2b1a59 commit adef2f9

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

mysql/delete_statement.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ type DeleteStatement interface {
1212
WHERE(expression BoolExpression) DeleteStatement
1313
ORDER_BY(orderByClauses ...OrderByClause) DeleteStatement
1414
LIMIT(limit int64) DeleteStatement
15+
RETURNING(projections ...jet.Projection) DeleteStatement
1516
}
1617

1718
type deleteStatementImpl struct {
1819
jet.SerializerStatement
1920

20-
Delete jet.ClauseDelete
21-
Using jet.ClauseFrom
22-
Where jet.ClauseWhere
23-
OrderBy jet.ClauseOrderBy
24-
Limit jet.ClauseLimit
21+
Delete jet.ClauseDelete
22+
Using jet.ClauseFrom
23+
Where jet.ClauseWhere
24+
OrderBy jet.ClauseOrderBy
25+
Limit jet.ClauseLimit
26+
Returning jet.ClauseReturning
2527
}
2628

2729
func newDeleteStatement(table Table) DeleteStatement {
@@ -32,6 +34,7 @@ func newDeleteStatement(table Table) DeleteStatement {
3234
&newDelete.Where,
3335
&newDelete.OrderBy,
3436
&newDelete.Limit,
37+
&newDelete.Returning,
3538
)
3639

3740
newDelete.Delete.Table = table
@@ -66,3 +69,8 @@ func (d *deleteStatementImpl) LIMIT(limit int64) DeleteStatement {
6669
d.Limit.Count = limit
6770
return d
6871
}
72+
73+
func (d *deleteStatementImpl) RETURNING(projections ...jet.Projection) DeleteStatement {
74+
d.Returning.ProjectionList = projections
75+
return d
76+
}

mysql/delete_statement_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ ORDER BY table1.col1
2424
LIMIT ?;
2525
`, int64(1), int64(1))
2626
}
27+
28+
func TestDeleteWithWhereAndReturning(t *testing.T) {
29+
assertStatementSql(t, table1.DELETE().WHERE(table1Col1.EQ(Int(1))).RETURNING(table1Col1), `
30+
DELETE FROM db.table1
31+
WHERE table1.col1 = ?
32+
RETURNING table1.col1 AS "table1.col1";
33+
`, int64(1))
34+
}

mysql/dialect.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mysql
33
import (
44
"encoding/hex"
55
"fmt"
6+
67
"github.com/go-jet/jet/v2/internal/jet"
78
)
89

@@ -437,6 +438,7 @@ var reservedWords = []string{
437438
"RESIGNAL",
438439
"RESTRICT",
439440
"RETURN",
441+
"RETURNING",
440442
"REVOKE",
441443
"RIGHT",
442444
"RLIKE",

mysql/insert_statement.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type InsertStatement interface {
1919
ON_DUPLICATE_KEY_UPDATE(assigments ...ColumnAssigment) InsertStatement
2020

2121
QUERY(selectStatement SelectStatement) InsertStatement
22+
23+
RETURNING(projections ...Projection) InsertStatement
2224
}
2325

2426
func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
@@ -27,6 +29,7 @@ func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
2729
&newInsert.Insert,
2830
&newInsert.ValuesQuery,
2931
&newInsert.OnDuplicateKey,
32+
&newInsert.Returning,
3033
)
3134

3235
newInsert.Insert.Table = table
@@ -40,6 +43,7 @@ type insertStatementImpl struct {
4043

4144
Insert jet.ClauseInsert
4245
ValuesQuery jet.ClauseValuesQuery
46+
Returning jet.ClauseReturning
4347
OnDuplicateKey onDuplicateKeyUpdateClause
4448
}
4549

@@ -63,6 +67,11 @@ func (is *insertStatementImpl) MODELS(data interface{}) InsertStatement {
6367
return is
6468
}
6569

70+
func (i *insertStatementImpl) RETURNING(projections ...jet.Projection) InsertStatement {
71+
i.Returning.ProjectionList = projections
72+
return i
73+
}
74+
6675
func (is *insertStatementImpl) AS_NEW() InsertStatement {
6776
is.ValuesQuery.As = "new"
6877
return is

mysql/insert_statement_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package mysql
22

33
import (
4-
"github.com/stretchr/testify/require"
54
"testing"
65
"time"
6+
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestInvalidInsert(t *testing.T) {
@@ -24,6 +25,14 @@ VALUES (?);
2425
`, int(1))
2526
}
2627

28+
func TestInsertWithReturing(t *testing.T) {
29+
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(1).RETURNING(table1Col1), `
30+
INSERT INTO db.table1 (col1)
31+
VALUES (?)
32+
RETURNING table1.col1 AS "table1.col1";
33+
`, int(1))
34+
}
35+
2736
func TestInsertWithColumnList(t *testing.T) {
2837
columnList := ColumnList{table3ColInt}
2938

0 commit comments

Comments
 (0)