2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-04-07 09:58:12 +02:00
|
|
|
|
|
|
|
|
import (
|
2019-06-20 12:22:19 +02:00
|
|
|
"context"
|
2019-04-07 09:58:12 +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-07 09:58:12 +02:00
|
|
|
)
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
type InsertStatement interface {
|
2019-05-12 18:15:23 +02:00
|
|
|
Statement
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
// Add a row of values to the insert Statement.
|
2019-06-14 14:35:50 +02:00
|
|
|
VALUES(value interface{}, values ...interface{}) InsertStatement
|
2019-06-11 12:47:35 +02:00
|
|
|
// Model structure mapped to column names
|
2019-06-14 14:35:50 +02:00
|
|
|
USING(data interface{}) InsertStatement
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
QUERY(selectStatement SelectStatement) InsertStatement
|
2019-06-11 12:47:35 +02:00
|
|
|
|
|
|
|
|
RETURNING(projections ...projection) InsertStatement
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func newInsertStatement(t WritableTable, columns []column) InsertStatement {
|
2019-04-07 09:58:12 +02:00
|
|
|
return &insertStatementImpl{
|
2019-04-14 17:55:10 +02:00
|
|
|
table: t,
|
|
|
|
|
columns: columns,
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type insertStatementImpl struct {
|
2019-06-05 17:15:20 +02:00
|
|
|
table WritableTable
|
2019-06-14 14:35:50 +02:00
|
|
|
columns []column
|
2019-05-07 19:06:21 +02:00
|
|
|
rows [][]clause
|
2019-06-04 12:10:23 +02:00
|
|
|
query SelectStatement
|
2019-05-07 19:06:21 +02:00
|
|
|
returning []projection
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func (i *insertStatementImpl) VALUES(value interface{}, values ...interface{}) InsertStatement {
|
|
|
|
|
i.rows = append(i.rows, unwindRowFromValues(value, values))
|
2019-05-12 18:15:23 +02:00
|
|
|
return i
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
func (i *insertStatementImpl) USING(data interface{}) InsertStatement {
|
|
|
|
|
i.rows = append(i.rows, unwindRowFromModel(i.columns, data))
|
2019-04-07 09:58:12 +02:00
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (i *insertStatementImpl) RETURNING(projections ...projection) InsertStatement {
|
2019-06-09 11:06:08 +02:00
|
|
|
i.returning = projections
|
2019-04-07 09:58:12 +02:00
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-04 12:10:23 +02:00
|
|
|
func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
2019-04-07 16:54:06 +02:00
|
|
|
i.query = selectStatement
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
func (i *insertStatementImpl) DebugSql() (query string, err error) {
|
2019-06-29 16:58:41 +02:00
|
|
|
return debugSql(i)
|
2019-05-12 18:15:23 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
func (i *insertStatementImpl) Sql() (sql string, args []interface{}, err error) {
|
2019-04-29 14:39:48 +02:00
|
|
|
queryData := &queryData{}
|
2019-05-12 18:15:23 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
queryData.newLine()
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.writeString("INSERT INTO")
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
if isNil(i.table) {
|
|
|
|
|
return "", nil, errors.New("table is nil")
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
err = i.table.serialize(insert_statement, queryData)
|
2019-05-12 18:15:23 +02:00
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
if err != nil {
|
2019-06-11 12:47:35 +02:00
|
|
|
return
|
2019-04-29 14:39:48 +02:00
|
|
|
}
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
if len(i.columns) > 0 {
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.writeString("(")
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-06-14 14:35:50 +02:00
|
|
|
err = serializeColumnNames(i.columns, queryData)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-04-29 14:39:48 +02:00
|
|
|
if err != nil {
|
2019-06-11 12:47:35 +02:00
|
|
|
return
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.writeString(")")
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
if len(i.rows) == 0 && i.query == nil {
|
2019-06-16 11:20:44 +02:00
|
|
|
return "", nil, errors.New("no row values or query specified")
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
if len(i.rows) > 0 && i.query != nil {
|
|
|
|
|
return "", nil, errors.New("only row values or query has to be specified")
|
2019-04-07 16:54:06 +02:00
|
|
|
}
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
if len(i.rows) > 0 {
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.writeString("VALUES")
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
for row_i, row := range i.rows {
|
2019-04-07 16:54:06 +02:00
|
|
|
if row_i > 0 {
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.writeString(",")
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.increaseIdent()
|
2019-06-14 14:35:50 +02:00
|
|
|
queryData.newLine()
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.writeString("(")
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
if len(row) != len(i.columns) {
|
|
|
|
|
return "", nil, errors.New("number of values does not match number of columns")
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
err = serializeClauseList(insert_statement, row, queryData)
|
2019-04-29 14:39:48 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", nil, err
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
2019-04-29 14:39:48 +02:00
|
|
|
|
2019-05-08 13:47:01 +02:00
|
|
|
queryData.writeByte(')')
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.decreaseIdent()
|
2019-04-07 16:54:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
if i.query != nil {
|
|
|
|
|
err = i.query.serialize(insert_statement, queryData)
|
2019-04-07 16:54:06 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
if len(i.returning) > 0 {
|
2019-06-14 14:35:50 +02:00
|
|
|
queryData.newLine()
|
2019-05-12 18:15:23 +02:00
|
|
|
queryData.writeString("RETURNING")
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-06-11 12:47:35 +02:00
|
|
|
err = queryData.writeProjections(insert_statement, i.returning)
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-04-14 17:55:10 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
sql, args = queryData.finalize()
|
2019-04-07 09:58:12 +02:00
|
|
|
|
2019-05-12 18:15:23 +02:00
|
|
|
return
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
2019-06-14 14:35:50 +02:00
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
func (i *insertStatementImpl) Query(db execution.DB, destination interface{}) error {
|
2019-06-29 16:58:41 +02:00
|
|
|
return query(i, db, destination)
|
2019-06-14 14:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
func (i *insertStatementImpl) QueryContext(db execution.DB, context context.Context, destination interface{}) error {
|
2019-06-29 16:58:41 +02:00
|
|
|
return queryContext(i, db, context, destination)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
func (i *insertStatementImpl) Exec(db execution.DB) (res sql.Result, err error) {
|
2019-06-29 16:58:41 +02:00
|
|
|
return exec(i, db)
|
2019-06-14 14:35:50 +02:00
|
|
|
}
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2019-06-23 18:55:57 +02:00
|
|
|
func (i *insertStatementImpl) ExecContext(db execution.DB, context context.Context) (res sql.Result, err error) {
|
2019-06-29 16:58:41 +02:00
|
|
|
return execContext(i, db, context)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|