Merge pull request #530 from cgroschupp/feat/mariadb-returning
feat: add returning to mysql
This commit is contained in:
commit
adef2f9b1a
5 changed files with 42 additions and 6 deletions
|
|
@ -12,6 +12,7 @@ type DeleteStatement interface {
|
|||
WHERE(expression BoolExpression) DeleteStatement
|
||||
ORDER_BY(orderByClauses ...OrderByClause) DeleteStatement
|
||||
LIMIT(limit int64) DeleteStatement
|
||||
RETURNING(projections ...jet.Projection) DeleteStatement
|
||||
}
|
||||
|
||||
type deleteStatementImpl struct {
|
||||
|
|
@ -22,6 +23,7 @@ type deleteStatementImpl struct {
|
|||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue