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-04-14 17:55:10 +02:00
|
|
|
)
|
|
|
|
|
|
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-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
|
|
|
|
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-06-14 14:35:50 +02:00
|
|
|
if isNil(u.table) {
|
|
|
|
|
return "", nil, errors.New("table to update is nil")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
if err = u.table.serialize(update_statement, 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 {
|
|
|
|
|
return "", nil, errors.New("no columns selected")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(u.row) == 0 {
|
|
|
|
|
return "", nil, errors.New("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-06-14 14:35:50 +02:00
|
|
|
err = serializeClauseList(update_statement, 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-06-14 14:35:50 +02:00
|
|
|
return "", nil, errors.New("WHERE clause not set")
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
if err = out.writeWhere(update_statement, u.where); err != nil {
|
2019-04-14 17:55:10 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 17:16:00 +02:00
|
|
|
if err = out.writeReturning(update_statement, u.returning); err != nil {
|
|
|
|
|
return
|
2019-04-14 17:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-30 17:16:00 +02:00
|
|
|
//if len(u.returning) > 0 {
|
|
|
|
|
// out.newLine()
|
|
|
|
|
// out.writeString("RETURNING")
|
|
|
|
|
// out.increaseIdent()
|
|
|
|
|
// out.increaseIdent()
|
|
|
|
|
//
|
|
|
|
|
// err = serializeProjectionList(update_statement, u.returning, out)
|
|
|
|
|
//
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// return
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
sql, args = out.finalize()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *updateStatementImpl) DebugSql() (query string, err error) {
|
2019-06-29 16:58:41 +02:00
|
|
|
return debugSql(u)
|
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-06-23 18:55:57 +02:00
|
|
|
func (u *updateStatementImpl) QueryContext(db execution.DB, context context.Context, destination interface{}) error {
|
2019-06-29 16:58:41 +02:00
|
|
|
return queryContext(u, db, context, 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-06-23 18:55:57 +02:00
|
|
|
func (u *updateStatementImpl) ExecContext(db execution.DB, context context.Context) (res sql.Result, err error) {
|
2019-06-29 16:58:41 +02:00
|
|
|
return execContext(u, db, context)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|