2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
import (
|
2019-06-20 12:22:19 +02:00
|
|
|
"context"
|
2019-04-14 17:55:10 +02:00
|
|
|
"database/sql"
|
2019-06-05 17:15:20 +02:00
|
|
|
"errors"
|
2019-06-21 13:56:57 +02:00
|
|
|
"github.com/go-jet/jet/execution"
|
2019-07-20 17:44:43 +02:00
|
|
|
"github.com/go-jet/jet/internal/utils"
|
2019-04-14 17:55:10 +02:00
|
|
|
)
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
// UpdateStatement is interface of SQL UPDATE statement
|
2019-06-04 12:10:23 +02:00
|
|
|
type UpdateStatement interface {
|
2019-05-12 18:15:23 +02:00
|
|
|
Statement
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
SET(value interface{}, values ...interface{}) UpdateStatement
|
2019-06-30 13:42:01 +02:00
|
|
|
MODEL(data interface{}) UpdateStatement
|
2019-06-14 14:35:50 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
WHERE(expression BoolExpression) UpdateStatement
|
|
|
|
|
RETURNING(projections ...projection) UpdateStatement
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func newUpdateStatement(table WritableTable, columns []column) UpdateStatement {
|
2019-04-14 17:55:10 +02:00
|
|
|
return &updateStatementImpl{
|
|
|
|
|
table: table,
|
|
|
|
|
columns: columns,
|
2019-06-14 14:35:50 +02:00
|
|
|
row: make([]clause, 0, len(columns)),
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type updateStatementImpl struct {
|
2019-06-14 14:35:50 +02:00
|
|
|
table WritableTable
|
|
|
|
|
columns []column
|
|
|
|
|
row []clause
|
|
|
|
|
where BoolExpression
|
|
|
|
|
returning []projection
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func (u *updateStatementImpl) SET(value interface{}, values ...interface{}) UpdateStatement {
|
|
|
|
|
u.row = unwindRowFromValues(value, values)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 13:42:01 +02:00
|
|
|
func (u *updateStatementImpl) MODEL(data interface{}) UpdateStatement {
|
|
|
|
|
u.row = unwindRowFromModel(u.columns, data)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (u *updateStatementImpl) WHERE(expression BoolExpression) UpdateStatement {
|
2019-04-14 17:55:10 +02:00
|
|
|
u.where = expression
|
|
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (u *updateStatementImpl) RETURNING(projections ...projection) UpdateStatement {
|
2019-06-09 11:06:08 +02:00
|
|
|
u.returning = projections
|
2019-04-14 17:55:10 +02:00
|
|
|
return u
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 14:57:02 +02:00
|
|
|
func (u *updateStatementImpl) accept(visitor visitor) {
|
|
|
|
|
visitor.visit(u)
|
|
|
|
|
u.table.accept(visitor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *updateStatementImpl) Sql(dialect ...Dialect) (query string, args []interface{}, err error) {
|
|
|
|
|
out := &sqlBuilder{
|
|
|
|
|
dialect: detectDialect(u, dialect...),
|
|
|
|
|
}
|
2019-05-03 12:51:57 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
out.newLine()
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("UPDATE")
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-07-20 17:44:43 +02:00
|
|
|
if utils.IsNil(u.table) {
|
2019-07-08 13:00:44 +02:00
|
|
|
return "", nil, errors.New("jet: table to update is nil")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
if err = u.table.serialize(updateStatement, out); err != nil {
|
2019-04-14 17:55:10 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
if len(u.columns) == 0 {
|
2019-07-08 13:00:44 +02:00
|
|
|
return "", nil, errors.New("jet: no columns selected")
|
2019-06-14 14:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(u.row) == 0 {
|
2019-07-08 13:00:44 +02:00
|
|
|
return "", nil, errors.New("jet: no values to updated")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
out.newLine()
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("SET")
|
2019-04-14 17:55:10 +02:00
|
|
|
|
|
|
|
|
if len(u.columns) > 1 {
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("(")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
err = serializeColumnNames(u.columns, out)
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
2019-06-14 14:35:50 +02:00
|
|
|
return
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(u.columns) > 1 {
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString(")")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("=")
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
if len(u.row) > 1 {
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString("(")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
err = serializeClauseList(updateStatement, u.row, out)
|
2019-04-14 17:55:10 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
if len(u.row) > 1 {
|
2019-05-12 18:15:23 +02:00
|
|
|
out.writeString(")")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if u.where == nil {
|
2019-07-08 13:00:44 +02:00
|
|
|
return "", nil, errors.New("jet: WHERE clause not set")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
if err = out.writeWhere(updateStatement, u.where); err != nil {
|
2019-04-14 17:55:10 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
if err = out.writeReturning(updateStatement, u.returning); err != nil {
|
2019-06-30 17:16:00 +02:00
|
|
|
return
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-28 14:57:02 +02:00
|
|
|
query, args = out.finalize()
|
2019-05-12 18:15:23 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 14:57:02 +02:00
|
|
|
func (u *updateStatementImpl) DebugSql(dialect ...Dialect) (query string, err error) {
|
|
|
|
|
return debugSql(u, dialect...)
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
2019-05-03 12:51:57 +02:00
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
func (u *updateStatementImpl) Query(db execution.DB, destination interface{}) error {
|
2019-06-29 16:58:41 +02:00
|
|
|
return query(u, db, destination)
|
2019-05-03 12:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-19 10:40:30 +02:00
|
|
|
func (u *updateStatementImpl) QueryContext(context context.Context, db execution.DB, destination interface{}) error {
|
2019-07-18 17:43:11 +02:00
|
|
|
return queryContext(context, u, db, destination)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
func (u *updateStatementImpl) Exec(db execution.DB) (res sql.Result, err error) {
|
2019-06-29 16:58:41 +02:00
|
|
|
return exec(u, db)
|
2019-05-03 12:51:57 +02:00
|
|
|
}
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2019-07-19 10:40:30 +02:00
|
|
|
func (u *updateStatementImpl) ExecContext(context context.Context, db execution.DB) (res sql.Result, err error) {
|
2019-07-19 10:46:41 +02:00
|
|
|
return execContext(context, u, db)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|