jet/postgres/update_statement.go
go-jet 486e45db5c SQL Builder panics on invalid SQL.
Query execution panics on invalid destination.
2019-08-13 13:57:26 +02:00

93 lines
2.1 KiB
Go

package postgres
import (
"github.com/go-jet/jet/internal/jet"
)
// UpdateStatement is interface of SQL UPDATE statement
type UpdateStatement interface {
jet.Statement
SET(value interface{}, values ...interface{}) UpdateStatement
MODEL(data interface{}) UpdateStatement
WHERE(expression BoolExpression) UpdateStatement
RETURNING(projections ...jet.Projection) UpdateStatement
}
type updateStatementImpl struct {
jet.StatementImpl
Update jet.ClauseUpdate
Set ClauseSet
Where jet.ClauseWhere
Returning ClauseReturning
}
func newUpdateStatement(table WritableTable, columns []jet.Column) UpdateStatement {
update := &updateStatementImpl{}
update.StatementImpl = jet.NewStatementImpl(Dialect, jet.UpdateStatementType, update, &update.Update,
&update.Set, &update.Where, &update.Returning)
update.Update.Table = table
update.Set.Columns = columns
update.Where.Mandatory = true
return update
}
func (u *updateStatementImpl) SET(value interface{}, values ...interface{}) UpdateStatement {
u.Set.Values = jet.UnwindRowFromValues(value, values)
return u
}
func (u *updateStatementImpl) MODEL(data interface{}) UpdateStatement {
u.Set.Values = jet.UnwindRowFromModel(u.Set.Columns, data)
return u
}
func (u *updateStatementImpl) WHERE(expression BoolExpression) UpdateStatement {
u.Where.Condition = expression
return u
}
func (u *updateStatementImpl) RETURNING(projections ...jet.Projection) UpdateStatement {
u.Returning.Projections = projections
return u
}
type ClauseSet struct {
Columns []jet.Column
Values []jet.Serializer
}
func (s *ClauseSet) Serialize(statementType jet.StatementType, out *jet.SqlBuilder) {
out.NewLine()
out.WriteString("SET")
if len(s.Columns) == 0 {
panic("jet: no columns selected")
}
if len(s.Columns) > 1 {
out.WriteString("(")
}
jet.SerializeColumnNames(s.Columns, out)
if len(s.Columns) > 1 {
out.WriteString(")")
}
out.WriteString("=")
if len(s.Values) > 1 {
out.WriteString("(")
}
jet.SerializeClauseList(statementType, s.Values, out)
if len(s.Values) > 1 {
out.WriteString(")")
}
}