2019-04-14 17:55:10 +02:00
|
|
|
package sqlbuilder
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"github.com/dropbox/godropbox/errors"
|
|
|
|
|
"github.com/sub0zero/go-sqlbuilder/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UpdateStatement interface {
|
|
|
|
|
Statement
|
|
|
|
|
|
|
|
|
|
SET(values ...interface{}) UpdateStatement
|
|
|
|
|
WHERE(expression BoolExpression) UpdateStatement
|
|
|
|
|
RETURNING(projections ...Projection) UpdateStatement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newUpdateStatement(table WritableTable, columns []Column) UpdateStatement {
|
|
|
|
|
return &updateStatementImpl{
|
|
|
|
|
table: table,
|
|
|
|
|
columns: columns,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type updateStatementImpl struct {
|
|
|
|
|
table WritableTable
|
|
|
|
|
columns []Column
|
|
|
|
|
updateValues []Clause
|
|
|
|
|
where BoolExpression
|
|
|
|
|
returning []Projection
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *updateStatementImpl) SET(values ...interface{}) UpdateStatement {
|
|
|
|
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
|
if clause, ok := value.(Clause); ok {
|
|
|
|
|
u.updateValues = append(u.updateValues, clause)
|
|
|
|
|
} else {
|
|
|
|
|
u.updateValues = append(u.updateValues, Literal(value))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *updateStatementImpl) WHERE(expression BoolExpression) UpdateStatement {
|
|
|
|
|
u.where = expression
|
|
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *updateStatementImpl) RETURNING(projections ...Projection) UpdateStatement {
|
2019-05-03 12:51:57 +02:00
|
|
|
u.returning = defaultProjectionAliasing(projections)
|
2019-04-14 17:55:10 +02:00
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
func (u *updateStatementImpl) Sql() (sql string, args []interface{}, err error) {
|
|
|
|
|
out := &queryData{}
|
2019-05-03 12:51:57 +02:00
|
|
|
out.statementType = update_statement
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString("UPDATE ")
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
if u.table == nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return "", nil, errors.New("nil tableName.")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
if err = u.table.SerializeSql(out); err != nil {
|
2019-04-14 17:55:10 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(u.updateValues) == 0 {
|
2019-04-29 14:39:48 +02:00
|
|
|
return "", nil, errors.New("No column updated.")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" SET")
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
if len(u.columns) > 1 {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" ( ")
|
2019-04-14 17:55:10 +02:00
|
|
|
} else {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" ")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
err = serializeColumnList(u.columns, out)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", nil, err
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(u.columns) > 1 {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" )")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" =")
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
if len(u.updateValues) > 1 {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" (")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, value := range u.updateValues {
|
|
|
|
|
if i > 0 {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(", ")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
err = value.Serialize(out)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(u.updateValues) > 1 {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" )")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if u.where == nil {
|
2019-04-29 14:39:48 +02:00
|
|
|
return "", nil, errors.New("Updating without a WHERE clause.")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-03 12:51:57 +02:00
|
|
|
if err = out.WriteWhere(u.where); err != nil {
|
2019-04-14 17:55:10 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(u.returning) > 0 {
|
2019-04-29 14:39:48 +02:00
|
|
|
out.WriteString(" RETURNING ")
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
err = serializeProjectionList(u.returning, out)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-01 14:42:46 +02:00
|
|
|
return out.buff.String(), out.args, nil
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
2019-05-03 12:51:57 +02:00
|
|
|
|
|
|
|
|
func (u *updateStatementImpl) Query(db types.Db, destination interface{}) error {
|
|
|
|
|
return Query(u, db, destination)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *updateStatementImpl) Execute(db types.Db) (res sql.Result, err error) {
|
|
|
|
|
return Execute(u, db)
|
|
|
|
|
}
|