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-07-18 17:43:11 +02:00
|
|
|
// InsertStatement is interface for SQL INSERT statements
|
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-06-30 11:53:35 +02:00
|
|
|
// Insert row of values
|
2019-06-14 14:35:50 +02:00
|
|
|
VALUES(value interface{}, values ...interface{}) InsertStatement
|
2019-06-30 11:53:35 +02:00
|
|
|
// Insert row of values, where value for each column is extracted from filed of structure data.
|
|
|
|
|
// If data is not struct or there is no field for every column selected, this method will panic.
|
|
|
|
|
MODEL(data interface{}) InsertStatement
|
|
|
|
|
|
|
|
|
|
MODELS(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-30 11:53:35 +02:00
|
|
|
func (i *insertStatementImpl) MODEL(data interface{}) InsertStatement {
|
|
|
|
|
i.rows = append(i.rows, unwindRowFromModel(i.getColumns(), data))
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *insertStatementImpl) MODELS(data interface{}) InsertStatement {
|
|
|
|
|
i.rows = append(i.rows, unwindRowsFromModels(i.getColumns(), 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-06-30 11:53:35 +02:00
|
|
|
func (i *insertStatementImpl) getColumns() []column {
|
|
|
|
|
if len(i.columns) > 0 {
|
|
|
|
|
return i.columns
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return i.table.columns()
|
|
|
|
|
}
|
|
|
|
|
|
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-07-08 10:48:03 +02:00
|
|
|
queryData := &sqlBuilder{}
|
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) {
|
2019-07-08 13:00:44 +02:00
|
|
|
return "", nil, errors.New("jet: table is nil")
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
err = i.table.serialize(insertStatement, 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-07-08 13:00:44 +02:00
|
|
|
return "", nil, errors.New("jet: 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 {
|
2019-07-08 13:00:44 +02:00
|
|
|
return "", nil, errors.New("jet: 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-07-18 17:43:11 +02:00
|
|
|
for rowIndex, row := range i.rows {
|
|
|
|
|
if rowIndex > 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-07-18 17:43:11 +02:00
|
|
|
err = serializeClauseList(insertStatement, 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 {
|
2019-07-18 17:43:11 +02:00
|
|
|
err = i.query.serialize(insertStatement, queryData)
|
2019-04-07 16:54:06 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2019-04-07 09:58:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-18 17:43:11 +02:00
|
|
|
if err = queryData.writeReturning(insertStatement, i.returning); err != nil {
|
2019-06-30 17:16:00 +02:00
|
|
|
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-07-18 17:43:11 +02:00
|
|
|
return queryContext(context, i, db, 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
|
|
|
}
|