Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions mysql/delete_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,6 +34,7 @@ func newDeleteStatement(table Table) DeleteStatement {
&newDelete.Where,
&newDelete.OrderBy,
&newDelete.Limit,
&newDelete.Returning,
)

newDelete.Delete.Table = table
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions mysql/delete_statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 2 additions & 0 deletions mysql/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"encoding/hex"
"fmt"

"github.com/go-jet/jet/v2/internal/jet"
)

Expand Down Expand Up @@ -437,6 +438,7 @@ var reservedWords = []string{
"RESIGNAL",
"RESTRICT",
"RETURN",
"RETURNING",
"REVOKE",
"RIGHT",
"RLIKE",
Expand Down
9 changes: 9 additions & 0 deletions mysql/insert_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -27,6 +29,7 @@ func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
&newInsert.Insert,
&newInsert.ValuesQuery,
&newInsert.OnDuplicateKey,
&newInsert.Returning,
)

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

Insert jet.ClauseInsert
ValuesQuery jet.ClauseValuesQuery
Returning jet.ClauseReturning
OnDuplicateKey onDuplicateKeyUpdateClause
}

Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion mysql/insert_statement_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package mysql

import (
"github.com/stretchr/testify/require"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestInvalidInsert(t *testing.T) {
Expand All @@ -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}

Expand Down
Loading