Move RETURNING clause to internal/jet so it can be reused for sqlite implementation.

This commit is contained in:
go-jet 2021-10-21 13:31:54 +02:00
parent 6080ae134f
commit 22b2901336
7 changed files with 56 additions and 43 deletions

View file

@ -4,33 +4,10 @@ import (
"github.com/go-jet/jet/v2/internal/jet"
)
type clauseReturning struct {
ProjectionList []jet.Projection
}
func (r *clauseReturning) Serialize(statementType jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
if len(r.ProjectionList) == 0 {
return
}
out.NewLine()
out.WriteString("RETURNING")
out.IncreaseIdent()
out.WriteProjections(statementType, r.ProjectionList)
out.DecreaseIdent()
}
func (r clauseReturning) Projections() ProjectionList {
return r.ProjectionList
}
// ========================================== //
type onConflict interface {
ON_CONSTRAINT(name string) conflictTarget
WHERE(indexPredicate BoolExpression) conflictTarget
DO_NOTHING() InsertStatement
DO_UPDATE(action conflictAction) InsertStatement
conflictTarget
}
type conflictTarget interface {

View file

@ -16,7 +16,7 @@ type deleteStatementImpl struct {
Delete jet.ClauseStatementBegin
Where jet.ClauseWhere
Returning clauseReturning
Returning jet.ClauseReturning
}
func newDeleteStatement(table WritableTable) DeleteStatement {

View file

@ -22,7 +22,11 @@ type InsertStatement interface {
func newInsertStatement(table WritableTable, columns []jet.Column) InsertStatement {
newInsert := &insertStatementImpl{}
newInsert.SerializerStatement = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
&newInsert.Insert, &newInsert.ValuesQuery, &newInsert.OnConflict, &newInsert.Returning)
&newInsert.Insert,
&newInsert.ValuesQuery,
&newInsert.OnConflict,
&newInsert.Returning,
)
newInsert.Insert.Table = table
newInsert.Insert.Columns = columns
@ -35,7 +39,7 @@ type insertStatementImpl struct {
Insert jet.ClauseInsert
ValuesQuery jet.ClauseValuesQuery
Returning clauseReturning
Returning jet.ClauseReturning
OnConflict onConflictClause
}

View file

@ -8,7 +8,6 @@ import (
)
func TestInvalidInsert(t *testing.T) {
assertStatementSqlErr(t, table1.INSERT(table1Col1), "jet: VALUES or QUERY has to be specified for INSERT statement")
assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
}

View file

@ -116,7 +116,7 @@ func INTERVAL(quantityAndUnit ...quantityAndUnit) IntervalExpression {
panic("jet: invalid number of quantity and unit fields")
}
fields := []string{}
var fields []string
for i := 0; i < len(quantityAndUnit); i += 2 {
quantity := strconv.FormatFloat(quantityAndUnit[i], 'f', -1, 64)

View file

@ -22,7 +22,7 @@ type updateStatementImpl struct {
Set clauseSet
SetNew jet.SetClauseNew
Where jet.ClauseWhere
Returning clauseReturning
Returning jet.ClauseReturning
}
func newUpdateStatement(table WritableTable, columns []jet.Column) UpdateStatement {