2019-08-11 09:52:02 +02:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
|
|
import "github.com/go-jet/jet/internal/jet"
|
|
|
|
|
|
|
|
|
|
// InsertStatement is interface for SQL INSERT statements
|
|
|
|
|
type InsertStatement interface {
|
2019-08-17 10:43:16 +02:00
|
|
|
Statement
|
2019-08-11 09:52:02 +02:00
|
|
|
|
|
|
|
|
// Insert row of values
|
|
|
|
|
VALUES(value interface{}, values ...interface{}) InsertStatement
|
|
|
|
|
// 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
|
|
|
|
|
QUERY(selectStatement SelectStatement) InsertStatement
|
|
|
|
|
|
2020-04-12 18:53:57 +02:00
|
|
|
ON_CONFLICT(indexExpressions ...jet.ColumnExpression) onConflict
|
|
|
|
|
|
|
|
|
|
RETURNING(projections ...Projection) InsertStatement
|
2019-08-11 09:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-11 14:29:03 +02:00
|
|
|
func newInsertStatement(table WritableTable, columns []jet.Column) InsertStatement {
|
2019-08-11 09:52:02 +02:00
|
|
|
newInsert := &insertStatementImpl{}
|
2019-08-17 18:32:01 +02:00
|
|
|
newInsert.SerializerStatement = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
|
2020-04-12 18:53:57 +02:00
|
|
|
&newInsert.Insert, &newInsert.ValuesQuery, &newInsert.OnConflict, &newInsert.Returning)
|
2019-08-11 09:52:02 +02:00
|
|
|
|
|
|
|
|
newInsert.Insert.Table = table
|
|
|
|
|
newInsert.Insert.Columns = columns
|
|
|
|
|
|
|
|
|
|
return newInsert
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type insertStatementImpl struct {
|
2019-08-17 18:32:01 +02:00
|
|
|
jet.SerializerStatement
|
2019-08-11 09:52:02 +02:00
|
|
|
|
2019-08-12 12:27:33 +02:00
|
|
|
Insert jet.ClauseInsert
|
|
|
|
|
ValuesQuery jet.ClauseValuesQuery
|
2019-08-17 14:49:35 +02:00
|
|
|
Returning clauseReturning
|
2020-04-12 18:53:57 +02:00
|
|
|
OnConflict onConflictClause
|
2019-08-11 09:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *insertStatementImpl) VALUES(value interface{}, values ...interface{}) InsertStatement {
|
2019-08-12 12:27:33 +02:00
|
|
|
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowFromValues(value, values))
|
2019-08-11 09:52:02 +02:00
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *insertStatementImpl) MODEL(data interface{}) InsertStatement {
|
2019-08-12 12:27:33 +02:00
|
|
|
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowFromModel(i.Insert.GetColumns(), data))
|
2019-08-11 09:52:02 +02:00
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *insertStatementImpl) MODELS(data interface{}) InsertStatement {
|
2019-08-12 12:27:33 +02:00
|
|
|
i.ValuesQuery.Rows = append(i.ValuesQuery.Rows, jet.UnwindRowsFromModels(i.Insert.GetColumns(), data)...)
|
2019-08-11 09:52:02 +02:00
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *insertStatementImpl) RETURNING(projections ...jet.Projection) InsertStatement {
|
|
|
|
|
i.Returning.Projections = projections
|
|
|
|
|
return i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
2019-08-12 12:27:33 +02:00
|
|
|
i.ValuesQuery.Query = selectStatement
|
2019-08-11 09:52:02 +02:00
|
|
|
return i
|
|
|
|
|
}
|
2020-04-12 18:53:57 +02:00
|
|
|
|
|
|
|
|
func (i *insertStatementImpl) ON_CONFLICT(indexExpressions ...jet.ColumnExpression) onConflict {
|
|
|
|
|
i.OnConflict = onConflictClause{
|
|
|
|
|
insertStatement: i,
|
|
|
|
|
indexExpressions: indexExpressions,
|
|
|
|
|
}
|
|
|
|
|
return &i.OnConflict
|
|
|
|
|
}
|