2021-08-25 13:34:33 +00:00
|
|
|
package bun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2021-09-08 19:05:26 +00:00
|
|
|
"database/sql/driver"
|
2021-08-25 13:34:33 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-10-24 11:14:37 +00:00
|
|
|
"time"
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
"github.com/uptrace/bun/dialect/feature"
|
|
|
|
"github.com/uptrace/bun/internal"
|
|
|
|
"github.com/uptrace/bun/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-13 11:29:08 +00:00
|
|
|
forceDeleteFlag internal.Flag = 1 << iota
|
2021-08-25 13:34:33 +00:00
|
|
|
deletedFlag
|
|
|
|
allWithDeletedFlag
|
|
|
|
)
|
|
|
|
|
|
|
|
type withQuery struct {
|
2022-11-28 10:19:39 +00:00
|
|
|
name string
|
|
|
|
query schema.QueryAppender
|
|
|
|
recursive bool
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IConn is a common interface for *sql.DB, *sql.Conn, and *sql.Tx.
|
|
|
|
type IConn interface {
|
|
|
|
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
|
|
|
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
|
|
|
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ IConn = (*sql.DB)(nil)
|
|
|
|
_ IConn = (*sql.Conn)(nil)
|
|
|
|
_ IConn = (*sql.Tx)(nil)
|
|
|
|
_ IConn = (*DB)(nil)
|
|
|
|
_ IConn = (*Conn)(nil)
|
|
|
|
_ IConn = (*Tx)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// IDB is a common interface for *bun.DB, bun.Conn, and bun.Tx.
|
|
|
|
type IDB interface {
|
|
|
|
IConn
|
2022-03-07 10:08:26 +00:00
|
|
|
Dialect() schema.Dialect
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
NewValues(model interface{}) *ValuesQuery
|
|
|
|
NewSelect() *SelectQuery
|
|
|
|
NewInsert() *InsertQuery
|
|
|
|
NewUpdate() *UpdateQuery
|
|
|
|
NewDelete() *DeleteQuery
|
2022-09-28 17:30:40 +00:00
|
|
|
NewRaw(query string, args ...interface{}) *RawQuery
|
2021-08-25 13:34:33 +00:00
|
|
|
NewCreateTable() *CreateTableQuery
|
|
|
|
NewDropTable() *DropTableQuery
|
|
|
|
NewCreateIndex() *CreateIndexQuery
|
|
|
|
NewDropIndex() *DropIndexQuery
|
|
|
|
NewTruncateTable() *TruncateTableQuery
|
|
|
|
NewAddColumn() *AddColumnQuery
|
|
|
|
NewDropColumn() *DropColumnQuery
|
2022-08-15 10:35:05 +00:00
|
|
|
|
|
|
|
BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
|
|
|
|
RunInTx(ctx context.Context, opts *sql.TxOptions, f func(ctx context.Context, tx Tx) error) error
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-03-07 10:08:26 +00:00
|
|
|
_ IDB = (*DB)(nil)
|
|
|
|
_ IDB = (*Conn)(nil)
|
|
|
|
_ IDB = (*Tx)(nil)
|
2021-08-25 13:34:33 +00:00
|
|
|
)
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
// QueryBuilder is used for common query methods
|
|
|
|
type QueryBuilder interface {
|
|
|
|
Query
|
|
|
|
Where(query string, args ...interface{}) QueryBuilder
|
|
|
|
WhereGroup(sep string, fn func(QueryBuilder) QueryBuilder) QueryBuilder
|
|
|
|
WhereOr(query string, args ...interface{}) QueryBuilder
|
|
|
|
WhereDeleted() QueryBuilder
|
|
|
|
WhereAllWithDeleted() QueryBuilder
|
|
|
|
WherePK(cols ...string) QueryBuilder
|
|
|
|
Unwrap() interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ QueryBuilder = (*selectQueryBuilder)(nil)
|
|
|
|
_ QueryBuilder = (*updateQueryBuilder)(nil)
|
|
|
|
_ QueryBuilder = (*deleteQueryBuilder)(nil)
|
|
|
|
)
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
type baseQuery struct {
|
|
|
|
db *DB
|
|
|
|
conn IConn
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
model Model
|
2021-08-25 13:34:33 +00:00
|
|
|
err error
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
tableModel TableModel
|
2021-08-25 13:34:33 +00:00
|
|
|
table *schema.Table
|
|
|
|
|
2022-03-07 10:08:26 +00:00
|
|
|
with []withQuery
|
|
|
|
modelTableName schema.QueryWithArgs
|
|
|
|
tables []schema.QueryWithArgs
|
|
|
|
columns []schema.QueryWithArgs
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
flags internal.Flag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) DB() *DB {
|
|
|
|
return q.db
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
func (q *baseQuery) GetConn() IConn {
|
|
|
|
return q.conn
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (q *baseQuery) GetModel() Model {
|
|
|
|
return q.model
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (q *baseQuery) GetTableName() string {
|
|
|
|
if q.table != nil {
|
|
|
|
return q.table.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, wq := range q.with {
|
2022-03-07 10:08:26 +00:00
|
|
|
if v, ok := wq.query.(Query); ok {
|
2021-09-29 13:09:45 +00:00
|
|
|
if model := v.GetModel(); model != nil {
|
|
|
|
return v.GetTableName()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:08:26 +00:00
|
|
|
if q.modelTableName.Query != "" {
|
2022-04-24 10:26:22 +00:00
|
|
|
return q.modelTableName.Query
|
2021-09-29 13:09:45 +00:00
|
|
|
}
|
2022-04-24 10:26:22 +00:00
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
if len(q.tables) > 0 {
|
2022-04-24 10:26:22 +00:00
|
|
|
b, _ := q.tables[0].AppendQuery(q.db.fmter, nil)
|
|
|
|
if len(b) < 64 {
|
|
|
|
return string(b)
|
|
|
|
}
|
2021-09-29 13:09:45 +00:00
|
|
|
}
|
2022-04-24 10:26:22 +00:00
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (q *baseQuery) setConn(db IConn) {
|
|
|
|
// Unwrap Bun wrappers to not call query hooks twice.
|
|
|
|
switch db := db.(type) {
|
|
|
|
case *DB:
|
|
|
|
q.conn = db.DB
|
|
|
|
case Conn:
|
|
|
|
q.conn = db.Conn
|
|
|
|
case Tx:
|
|
|
|
q.conn = db.Tx
|
|
|
|
default:
|
|
|
|
q.conn = db
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 10:19:39 +00:00
|
|
|
func (q *baseQuery) setModel(modeli interface{}) {
|
2021-08-25 13:34:33 +00:00
|
|
|
model, err := newSingleModel(q.db, modeli)
|
|
|
|
if err != nil {
|
|
|
|
q.setErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q.model = model
|
2021-09-29 13:09:45 +00:00
|
|
|
if tm, ok := model.(TableModel); ok {
|
2021-08-25 13:34:33 +00:00
|
|
|
q.tableModel = tm
|
|
|
|
q.table = tm.Table()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) setErr(err error) {
|
|
|
|
if q.err == nil {
|
|
|
|
q.err = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:09:45 +00:00
|
|
|
func (q *baseQuery) getModel(dest []interface{}) (Model, error) {
|
2021-08-25 13:34:33 +00:00
|
|
|
if len(dest) == 0 {
|
|
|
|
if q.model != nil {
|
|
|
|
return q.model, nil
|
|
|
|
}
|
|
|
|
return nil, errNilModel
|
|
|
|
}
|
|
|
|
return newModel(q.db, dest)
|
|
|
|
}
|
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
func (q *baseQuery) beforeAppendModel(ctx context.Context, query Query) error {
|
|
|
|
if q.tableModel != nil {
|
|
|
|
return q.tableModel.BeforeAppendModel(ctx, query)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
func (q *baseQuery) hasFeature(feature feature.Feature) bool {
|
|
|
|
return q.db.features.Has(feature)
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (q *baseQuery) checkSoftDelete() error {
|
|
|
|
if q.table == nil {
|
|
|
|
return errors.New("bun: can't use soft deletes without a table")
|
|
|
|
}
|
|
|
|
if q.table.SoftDeleteField == nil {
|
|
|
|
return fmt.Errorf("%s does not have a soft delete field", q.table)
|
|
|
|
}
|
|
|
|
if q.tableModel == nil {
|
|
|
|
return errors.New("bun: can't use soft deletes without a table model")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deleted adds `WHERE deleted_at IS NOT NULL` clause for soft deleted models.
|
|
|
|
func (q *baseQuery) whereDeleted() {
|
|
|
|
if err := q.checkSoftDelete(); err != nil {
|
|
|
|
q.setErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q.flags = q.flags.Set(deletedFlag)
|
|
|
|
q.flags = q.flags.Remove(allWithDeletedFlag)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllWithDeleted changes query to return all rows including soft deleted ones.
|
|
|
|
func (q *baseQuery) whereAllWithDeleted() {
|
|
|
|
if err := q.checkSoftDelete(); err != nil {
|
|
|
|
q.setErr(err)
|
|
|
|
return
|
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
q.flags = q.flags.Set(allWithDeletedFlag).Remove(deletedFlag)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) isSoftDelete() bool {
|
|
|
|
if q.table != nil {
|
2022-08-15 10:35:05 +00:00
|
|
|
return q.table.SoftDeleteField != nil &&
|
|
|
|
!q.flags.Has(allWithDeletedFlag) &&
|
2022-11-28 10:19:39 +00:00
|
|
|
(!q.flags.Has(forceDeleteFlag) || q.flags.Has(deletedFlag))
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2022-11-28 10:19:39 +00:00
|
|
|
func (q *baseQuery) addWith(name string, query schema.QueryAppender, recursive bool) {
|
2021-08-25 13:34:33 +00:00
|
|
|
q.with = append(q.with, withQuery{
|
2022-11-28 10:19:39 +00:00
|
|
|
name: name,
|
|
|
|
query: query,
|
|
|
|
recursive: recursive,
|
2021-08-25 13:34:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) appendWith(fmter schema.Formatter, b []byte) (_ []byte, err error) {
|
|
|
|
if len(q.with) == 0 {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b = append(b, "WITH "...)
|
|
|
|
for i, with := range q.with {
|
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
|
2022-11-28 10:19:39 +00:00
|
|
|
if with.recursive {
|
|
|
|
b = append(b, "RECURSIVE "...)
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
b, err = q.appendCTE(fmter, b, with)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = append(b, ' ')
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) appendCTE(
|
|
|
|
fmter schema.Formatter, b []byte, cte withQuery,
|
|
|
|
) (_ []byte, err error) {
|
|
|
|
if !fmter.Dialect().Features().Has(feature.WithValues) {
|
|
|
|
if values, ok := cte.query.(*ValuesQuery); ok {
|
|
|
|
return q.appendSelectFromValues(fmter, b, cte, values)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
2022-04-24 10:26:22 +00:00
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
b = fmter.AppendIdent(b, cte.name)
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
if q, ok := cte.query.(schema.ColumnsAppender); ok {
|
|
|
|
b = append(b, " ("...)
|
|
|
|
b, err = q.AppendColumns(fmter, b)
|
2021-08-25 13:34:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-24 10:26:22 +00:00
|
|
|
b = append(b, ")"...)
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
b = append(b, " AS ("...)
|
|
|
|
|
|
|
|
b, err = cte.query.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
2022-04-24 10:26:22 +00:00
|
|
|
|
|
|
|
b = append(b, ")"...)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) appendSelectFromValues(
|
|
|
|
fmter schema.Formatter, b []byte, cte withQuery, values *ValuesQuery,
|
|
|
|
) (_ []byte, err error) {
|
|
|
|
b = fmter.AppendIdent(b, cte.name)
|
|
|
|
b = append(b, " AS (SELECT * FROM ("...)
|
|
|
|
|
|
|
|
b, err = cte.query.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b = append(b, ") AS t"...)
|
|
|
|
if q, ok := cte.query.(schema.ColumnsAppender); ok {
|
|
|
|
b = append(b, " ("...)
|
|
|
|
b, err = q.AppendColumns(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b = append(b, ")"...)
|
|
|
|
}
|
|
|
|
b = append(b, ")"...)
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (q *baseQuery) addTable(table schema.QueryWithArgs) {
|
|
|
|
q.tables = append(q.tables, table)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) addColumn(column schema.QueryWithArgs) {
|
|
|
|
q.columns = append(q.columns, column)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) excludeColumn(columns []string) {
|
2021-10-24 11:14:37 +00:00
|
|
|
if q.table == nil {
|
|
|
|
q.setErr(errNilModel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
if q.columns == nil {
|
|
|
|
for _, f := range q.table.Fields {
|
|
|
|
q.columns = append(q.columns, schema.UnsafeIdent(f.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(columns) == 1 && columns[0] == "*" {
|
|
|
|
q.columns = make([]schema.QueryWithArgs, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, column := range columns {
|
|
|
|
if !q._excludeColumn(column) {
|
|
|
|
q.setErr(fmt.Errorf("bun: can't find column=%q", column))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) _excludeColumn(column string) bool {
|
|
|
|
for i, col := range q.columns {
|
|
|
|
if col.Args == nil && col.Query == column {
|
|
|
|
q.columns = append(q.columns[:i], q.columns[i+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (q *baseQuery) modelHasTableName() bool {
|
2022-03-07 10:08:26 +00:00
|
|
|
if !q.modelTableName.IsZero() {
|
|
|
|
return q.modelTableName.Query != ""
|
2021-09-08 19:05:26 +00:00
|
|
|
}
|
|
|
|
return q.table != nil
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) hasTables() bool {
|
|
|
|
return q.modelHasTableName() || len(q.tables) > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) appendTables(
|
|
|
|
fmter schema.Formatter, b []byte,
|
|
|
|
) (_ []byte, err error) {
|
|
|
|
return q._appendTables(fmter, b, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) appendTablesWithAlias(
|
|
|
|
fmter schema.Formatter, b []byte,
|
|
|
|
) (_ []byte, err error) {
|
|
|
|
return q._appendTables(fmter, b, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) _appendTables(
|
|
|
|
fmter schema.Formatter, b []byte, withAlias bool,
|
|
|
|
) (_ []byte, err error) {
|
|
|
|
startLen := len(b)
|
|
|
|
|
|
|
|
if q.modelHasTableName() {
|
2022-03-07 10:08:26 +00:00
|
|
|
if !q.modelTableName.IsZero() {
|
|
|
|
b, err = q.modelTableName.AppendQuery(fmter, b)
|
2021-08-25 13:34:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b = fmter.AppendQuery(b, string(q.table.SQLNameForSelects))
|
|
|
|
if withAlias && q.table.SQLAlias != q.table.SQLNameForSelects {
|
|
|
|
b = append(b, " AS "...)
|
|
|
|
b = append(b, q.table.SQLAlias...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range q.tables {
|
|
|
|
if len(b) > startLen {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
b, err = table.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) appendFirstTable(fmter schema.Formatter, b []byte) ([]byte, error) {
|
|
|
|
return q._appendFirstTable(fmter, b, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) appendFirstTableWithAlias(
|
|
|
|
fmter schema.Formatter, b []byte,
|
|
|
|
) ([]byte, error) {
|
|
|
|
return q._appendFirstTable(fmter, b, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) _appendFirstTable(
|
|
|
|
fmter schema.Formatter, b []byte, withAlias bool,
|
|
|
|
) ([]byte, error) {
|
2022-03-07 10:08:26 +00:00
|
|
|
if !q.modelTableName.IsZero() {
|
|
|
|
return q.modelTableName.AppendQuery(fmter, b)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if q.table != nil {
|
|
|
|
b = fmter.AppendQuery(b, string(q.table.SQLName))
|
|
|
|
if withAlias {
|
|
|
|
b = append(b, " AS "...)
|
|
|
|
b = append(b, q.table.SQLAlias...)
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.tables) > 0 {
|
|
|
|
return q.tables[0].AppendQuery(fmter, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("bun: query does not have a table")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) hasMultiTables() bool {
|
|
|
|
if q.modelHasTableName() {
|
|
|
|
return len(q.tables) >= 1
|
|
|
|
}
|
|
|
|
return len(q.tables) >= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) appendOtherTables(fmter schema.Formatter, b []byte) (_ []byte, err error) {
|
|
|
|
tables := q.tables
|
|
|
|
if !q.modelHasTableName() {
|
|
|
|
tables = tables[1:]
|
|
|
|
}
|
|
|
|
for i, table := range tables {
|
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
b, err = table.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (q *baseQuery) appendColumns(fmter schema.Formatter, b []byte) (_ []byte, err error) {
|
|
|
|
for i, f := range q.columns {
|
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
b, err = f.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) getFields() ([]*schema.Field, error) {
|
|
|
|
if len(q.columns) == 0 {
|
2022-04-24 10:26:22 +00:00
|
|
|
if q.table == nil {
|
|
|
|
return nil, errNilModel
|
|
|
|
}
|
2021-09-08 19:05:26 +00:00
|
|
|
return q.table.Fields, nil
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
2021-09-08 19:05:26 +00:00
|
|
|
return q._getFields(false)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) getDataFields() ([]*schema.Field, error) {
|
|
|
|
if len(q.columns) == 0 {
|
2022-04-24 10:26:22 +00:00
|
|
|
if q.table == nil {
|
|
|
|
return nil, errNilModel
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
return q.table.DataFields, nil
|
|
|
|
}
|
|
|
|
return q._getFields(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) _getFields(omitPK bool) ([]*schema.Field, error) {
|
|
|
|
fields := make([]*schema.Field, 0, len(q.columns))
|
|
|
|
for _, col := range q.columns {
|
|
|
|
if col.Args != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
field, err := q.table.Field(col.Query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if omitPK && field.IsPK {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields = append(fields, field)
|
|
|
|
}
|
|
|
|
return fields, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) scan(
|
|
|
|
ctx context.Context,
|
2021-10-24 11:14:37 +00:00
|
|
|
iquery Query,
|
2021-08-25 13:34:33 +00:00
|
|
|
query string,
|
2021-09-29 13:09:45 +00:00
|
|
|
model Model,
|
2021-08-25 13:34:33 +00:00
|
|
|
hasDest bool,
|
2021-09-08 19:05:26 +00:00
|
|
|
) (sql.Result, error) {
|
2022-03-07 10:08:26 +00:00
|
|
|
ctx, event := q.db.beforeQuery(ctx, iquery, query, nil, query, q.model)
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
rows, err := q.conn.QueryContext(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
q.db.afterQuery(ctx, event, nil, err)
|
2021-09-08 19:05:26 +00:00
|
|
|
return nil, err
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
numRow, err := model.ScanRows(ctx, rows)
|
2021-08-25 13:34:33 +00:00
|
|
|
if err != nil {
|
|
|
|
q.db.afterQuery(ctx, event, nil, err)
|
2021-09-08 19:05:26 +00:00
|
|
|
return nil, err
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
if numRow == 0 && hasDest && isSingleRowModel(model) {
|
2021-08-25 13:34:33 +00:00
|
|
|
err = sql.ErrNoRows
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
res := driver.RowsAffected(numRow)
|
|
|
|
q.db.afterQuery(ctx, event, res, err)
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) exec(
|
|
|
|
ctx context.Context,
|
2021-10-24 11:14:37 +00:00
|
|
|
iquery Query,
|
2021-08-25 13:34:33 +00:00
|
|
|
query string,
|
2021-09-08 19:05:26 +00:00
|
|
|
) (sql.Result, error) {
|
2022-03-07 10:08:26 +00:00
|
|
|
ctx, event := q.db.beforeQuery(ctx, iquery, query, nil, query, q.model)
|
2021-09-08 19:05:26 +00:00
|
|
|
res, err := q.conn.ExecContext(ctx, query)
|
2022-03-07 10:08:26 +00:00
|
|
|
q.db.afterQuery(ctx, event, nil, err)
|
|
|
|
return res, err
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (q *baseQuery) AppendNamedArg(fmter schema.Formatter, b []byte, name string) ([]byte, bool) {
|
|
|
|
if q.table == nil {
|
|
|
|
return b, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if m, ok := q.tableModel.(*structTableModel); ok {
|
|
|
|
if b, ok := m.AppendNamedArg(fmter, b, name); ok {
|
|
|
|
return b, ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "TableName":
|
|
|
|
b = fmter.AppendQuery(b, string(q.table.SQLName))
|
|
|
|
return b, true
|
|
|
|
case "TableAlias":
|
|
|
|
b = fmter.AppendQuery(b, string(q.table.SQLAlias))
|
|
|
|
return b, true
|
|
|
|
case "PKs":
|
|
|
|
b = appendColumns(b, "", q.table.PKs)
|
|
|
|
return b, true
|
|
|
|
case "TablePKs":
|
|
|
|
b = appendColumns(b, q.table.SQLAlias, q.table.PKs)
|
|
|
|
return b, true
|
|
|
|
case "Columns":
|
|
|
|
b = appendColumns(b, "", q.table.Fields)
|
|
|
|
return b, true
|
|
|
|
case "TableColumns":
|
|
|
|
b = appendColumns(b, q.table.SQLAlias, q.table.Fields)
|
|
|
|
return b, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, false
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (q *baseQuery) Dialect() schema.Dialect {
|
|
|
|
return q.db.Dialect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewValues(model interface{}) *ValuesQuery {
|
|
|
|
return NewValuesQuery(q.db, model).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewSelect() *SelectQuery {
|
|
|
|
return NewSelectQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewInsert() *InsertQuery {
|
|
|
|
return NewInsertQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewUpdate() *UpdateQuery {
|
|
|
|
return NewUpdateQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewDelete() *DeleteQuery {
|
|
|
|
return NewDeleteQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
2022-09-28 17:30:40 +00:00
|
|
|
func (q *baseQuery) NewRaw(query string, args ...interface{}) *RawQuery {
|
|
|
|
return NewRawQuery(q.db, query, args...).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
func (q *baseQuery) NewCreateTable() *CreateTableQuery {
|
|
|
|
return NewCreateTableQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewDropTable() *DropTableQuery {
|
|
|
|
return NewDropTableQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewCreateIndex() *CreateIndexQuery {
|
|
|
|
return NewCreateIndexQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewDropIndex() *DropIndexQuery {
|
|
|
|
return NewDropIndexQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewTruncateTable() *TruncateTableQuery {
|
|
|
|
return NewTruncateTableQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewAddColumn() *AddColumnQuery {
|
|
|
|
return NewAddColumnQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *baseQuery) NewDropColumn() *DropColumnQuery {
|
|
|
|
return NewDropColumnQuery(q.db).Conn(q.conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func appendColumns(b []byte, table schema.Safe, fields []*schema.Field) []byte {
|
|
|
|
for i, f := range fields {
|
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(table) > 0 {
|
|
|
|
b = append(b, table...)
|
|
|
|
b = append(b, '.')
|
|
|
|
}
|
|
|
|
b = append(b, f.SQLName...)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatterWithModel(fmter schema.Formatter, model schema.NamedArgAppender) schema.Formatter {
|
|
|
|
if fmter.IsNop() {
|
|
|
|
return fmter
|
|
|
|
}
|
|
|
|
return fmter.WithArg(model)
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type whereBaseQuery struct {
|
|
|
|
baseQuery
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
where []schema.QueryWithSep
|
|
|
|
whereFields []*schema.Field
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *whereBaseQuery) addWhere(where schema.QueryWithSep) {
|
|
|
|
q.where = append(q.where, where)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *whereBaseQuery) addWhereGroup(sep string, where []schema.QueryWithSep) {
|
|
|
|
if len(where) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
q.addWhere(schema.SafeQueryWithSep("", nil, sep))
|
|
|
|
q.addWhere(schema.SafeQueryWithSep("", nil, "("))
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
where[0].Sep = ""
|
2021-08-25 13:34:33 +00:00
|
|
|
q.where = append(q.where, where...)
|
2021-09-08 19:05:26 +00:00
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
q.addWhere(schema.SafeQueryWithSep("", nil, ")"))
|
|
|
|
}
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
func (q *whereBaseQuery) addWhereCols(cols []string) {
|
|
|
|
if q.table == nil {
|
|
|
|
err := fmt.Errorf("bun: got %T, but WherePK requires a struct or slice-based model", q.model)
|
|
|
|
q.setErr(err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-07 10:08:26 +00:00
|
|
|
if q.whereFields != nil {
|
|
|
|
err := errors.New("bun: WherePK can only be called once")
|
|
|
|
q.setErr(err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
|
|
|
|
if cols == nil {
|
|
|
|
if err := q.table.CheckPKs(); err != nil {
|
|
|
|
q.setErr(err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-07 10:08:26 +00:00
|
|
|
q.whereFields = q.table.PKs
|
2021-11-13 11:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:08:26 +00:00
|
|
|
q.whereFields = make([]*schema.Field, len(cols))
|
|
|
|
for i, col := range cols {
|
|
|
|
field, err := q.table.Field(col)
|
|
|
|
if err != nil {
|
|
|
|
q.setErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q.whereFields[i] = field
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
func (q *whereBaseQuery) mustAppendWhere(
|
|
|
|
fmter schema.Formatter, b []byte, withAlias bool,
|
|
|
|
) ([]byte, error) {
|
2022-11-28 10:19:39 +00:00
|
|
|
if len(q.where) == 0 && q.whereFields == nil && !q.flags.Has(deletedFlag) {
|
2021-08-25 13:34:33 +00:00
|
|
|
err := errors.New("bun: Update and Delete queries require at least one Where")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return q.appendWhere(fmter, b, withAlias)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *whereBaseQuery) appendWhere(
|
|
|
|
fmter schema.Formatter, b []byte, withAlias bool,
|
|
|
|
) (_ []byte, err error) {
|
2021-11-13 11:29:08 +00:00
|
|
|
if len(q.where) == 0 && q.whereFields == nil && !q.isSoftDelete() {
|
2021-08-25 13:34:33 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b = append(b, " WHERE "...)
|
|
|
|
startLen := len(b)
|
|
|
|
|
|
|
|
if len(q.where) > 0 {
|
|
|
|
b, err = appendWhere(fmter, b, q.where)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.isSoftDelete() {
|
|
|
|
if len(b) > startLen {
|
|
|
|
b = append(b, " AND "...)
|
|
|
|
}
|
2022-04-24 10:26:22 +00:00
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
if withAlias {
|
|
|
|
b = append(b, q.tableModel.Table().SQLAlias...)
|
2022-04-24 10:26:22 +00:00
|
|
|
} else {
|
|
|
|
b = append(b, q.tableModel.Table().SQLName...)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
2022-04-24 10:26:22 +00:00
|
|
|
b = append(b, '.')
|
2021-10-24 11:14:37 +00:00
|
|
|
|
|
|
|
field := q.tableModel.Table().SoftDeleteField
|
|
|
|
b = append(b, field.SQLName...)
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
if field.IsPtr || field.NullZero {
|
2021-10-24 11:14:37 +00:00
|
|
|
if q.flags.Has(deletedFlag) {
|
|
|
|
b = append(b, " IS NOT NULL"...)
|
|
|
|
} else {
|
|
|
|
b = append(b, " IS NULL"...)
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
} else {
|
2021-10-24 11:14:37 +00:00
|
|
|
if q.flags.Has(deletedFlag) {
|
|
|
|
b = append(b, " != "...)
|
|
|
|
} else {
|
|
|
|
b = append(b, " = "...)
|
|
|
|
}
|
|
|
|
b = fmter.Dialect().AppendTime(b, time.Time{})
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
if q.whereFields != nil {
|
2021-08-25 13:34:33 +00:00
|
|
|
if len(b) > startLen {
|
|
|
|
b = append(b, " AND "...)
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
b, err = q.appendWhereFields(fmter, b, q.whereFields, withAlias)
|
2021-08-25 13:34:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendWhere(
|
|
|
|
fmter schema.Formatter, b []byte, where []schema.QueryWithSep,
|
|
|
|
) (_ []byte, err error) {
|
|
|
|
for i, where := range where {
|
2021-09-08 19:05:26 +00:00
|
|
|
if i > 0 {
|
2021-08-25 13:34:33 +00:00
|
|
|
b = append(b, where.Sep...)
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
if where.Query == "" {
|
2021-08-25 13:34:33 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
b = append(b, '(')
|
|
|
|
b, err = where.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b = append(b, ')')
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
func (q *whereBaseQuery) appendWhereFields(
|
|
|
|
fmter schema.Formatter, b []byte, fields []*schema.Field, withAlias bool,
|
2021-08-25 13:34:33 +00:00
|
|
|
) (_ []byte, err error) {
|
|
|
|
if q.table == nil {
|
2021-11-13 11:29:08 +00:00
|
|
|
err := fmt.Errorf("bun: got %T, but WherePK requires struct or slice-based model", q.model)
|
2021-08-25 13:34:33 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch model := q.tableModel.(type) {
|
|
|
|
case *structTableModel:
|
2021-11-13 11:29:08 +00:00
|
|
|
return q.appendWhereStructFields(fmter, b, model, fields, withAlias)
|
2021-08-25 13:34:33 +00:00
|
|
|
case *sliceTableModel:
|
2021-11-13 11:29:08 +00:00
|
|
|
return q.appendWhereSliceFields(fmter, b, model, fields, withAlias)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("bun: WhereColumn does not support %T", q.tableModel)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
func (q *whereBaseQuery) appendWhereStructFields(
|
|
|
|
fmter schema.Formatter,
|
|
|
|
b []byte,
|
|
|
|
model *structTableModel,
|
|
|
|
fields []*schema.Field,
|
|
|
|
withAlias bool,
|
2021-08-25 13:34:33 +00:00
|
|
|
) (_ []byte, err error) {
|
|
|
|
if !model.strct.IsValid() {
|
|
|
|
return nil, errNilModel
|
|
|
|
}
|
|
|
|
|
|
|
|
isTemplate := fmter.IsNop()
|
|
|
|
b = append(b, '(')
|
2021-11-13 11:29:08 +00:00
|
|
|
for i, f := range fields {
|
2021-08-25 13:34:33 +00:00
|
|
|
if i > 0 {
|
|
|
|
b = append(b, " AND "...)
|
|
|
|
}
|
|
|
|
if withAlias {
|
|
|
|
b = append(b, q.table.SQLAlias...)
|
|
|
|
b = append(b, '.')
|
|
|
|
}
|
|
|
|
b = append(b, f.SQLName...)
|
|
|
|
b = append(b, " = "...)
|
|
|
|
if isTemplate {
|
|
|
|
b = append(b, '?')
|
|
|
|
} else {
|
|
|
|
b = f.AppendValue(fmter, b, model.strct)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = append(b, ')')
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
func (q *whereBaseQuery) appendWhereSliceFields(
|
|
|
|
fmter schema.Formatter,
|
|
|
|
b []byte,
|
|
|
|
model *sliceTableModel,
|
|
|
|
fields []*schema.Field,
|
|
|
|
withAlias bool,
|
2021-08-25 13:34:33 +00:00
|
|
|
) (_ []byte, err error) {
|
2021-11-13 11:29:08 +00:00
|
|
|
if len(fields) > 1 {
|
2021-08-25 13:34:33 +00:00
|
|
|
b = append(b, '(')
|
|
|
|
}
|
|
|
|
if withAlias {
|
2021-11-13 11:29:08 +00:00
|
|
|
b = appendColumns(b, q.table.SQLAlias, fields)
|
2021-08-25 13:34:33 +00:00
|
|
|
} else {
|
2021-11-13 11:29:08 +00:00
|
|
|
b = appendColumns(b, "", fields)
|
2021-08-25 13:34:33 +00:00
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
if len(fields) > 1 {
|
2021-08-25 13:34:33 +00:00
|
|
|
b = append(b, ')')
|
|
|
|
}
|
|
|
|
|
|
|
|
b = append(b, " IN ("...)
|
|
|
|
|
|
|
|
isTemplate := fmter.IsNop()
|
|
|
|
slice := model.slice
|
|
|
|
sliceLen := slice.Len()
|
|
|
|
for i := 0; i < sliceLen; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
if isTemplate {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
|
|
|
|
el := indirect(slice.Index(i))
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
if len(fields) > 1 {
|
2021-08-25 13:34:33 +00:00
|
|
|
b = append(b, '(')
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
for i, f := range fields {
|
2021-08-25 13:34:33 +00:00
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
if isTemplate {
|
|
|
|
b = append(b, '?')
|
|
|
|
} else {
|
|
|
|
b = f.AppendValue(fmter, b, el)
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 11:29:08 +00:00
|
|
|
if len(fields) > 1 {
|
2021-08-25 13:34:33 +00:00
|
|
|
b = append(b, ')')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b = append(b, ')')
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type returningQuery struct {
|
|
|
|
returning []schema.QueryWithArgs
|
|
|
|
returningFields []*schema.Field
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *returningQuery) addReturning(ret schema.QueryWithArgs) {
|
|
|
|
q.returning = append(q.returning, ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *returningQuery) addReturningField(field *schema.Field) {
|
|
|
|
if len(q.returning) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, f := range q.returningFields {
|
|
|
|
if f == field {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
q.returningFields = append(q.returningFields, field)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *returningQuery) appendReturning(
|
|
|
|
fmter schema.Formatter, b []byte,
|
|
|
|
) (_ []byte, err error) {
|
2022-04-24 10:26:22 +00:00
|
|
|
return q._appendReturning(fmter, b, "")
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
func (q *returningQuery) appendOutput(
|
|
|
|
fmter schema.Formatter, b []byte,
|
|
|
|
) (_ []byte, err error) {
|
|
|
|
return q._appendReturning(fmter, b, "INSERTED")
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
func (q *returningQuery) _appendReturning(
|
|
|
|
fmter schema.Formatter, b []byte, table string,
|
|
|
|
) (_ []byte, err error) {
|
2021-08-25 13:34:33 +00:00
|
|
|
for i, f := range q.returning {
|
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
b, err = f.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(q.returning) > 0 {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
b = appendColumns(b, schema.Safe(table), q.returningFields)
|
2021-08-25 13:34:33 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:26:22 +00:00
|
|
|
func (q *returningQuery) hasReturning() bool {
|
|
|
|
if len(q.returning) == 1 {
|
|
|
|
if ret := q.returning[0]; len(ret.Args) == 0 {
|
|
|
|
switch ret.Query {
|
|
|
|
case "", "null", "NULL":
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(q.returning) > 0 || len(q.returningFields) > 0
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:34:33 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type columnValue struct {
|
|
|
|
column string
|
|
|
|
value schema.QueryWithArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
type customValueQuery struct {
|
|
|
|
modelValues map[string]schema.QueryWithArgs
|
|
|
|
extraValues []columnValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *customValueQuery) addValue(
|
|
|
|
table *schema.Table, column string, value string, args []interface{},
|
|
|
|
) {
|
|
|
|
if _, ok := table.FieldMap[column]; ok {
|
|
|
|
if q.modelValues == nil {
|
|
|
|
q.modelValues = make(map[string]schema.QueryWithArgs)
|
|
|
|
}
|
|
|
|
q.modelValues[column] = schema.SafeQuery(value, args)
|
|
|
|
} else {
|
|
|
|
q.extraValues = append(q.extraValues, columnValue{
|
|
|
|
column: column,
|
|
|
|
value: schema.SafeQuery(value, args),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type setQuery struct {
|
|
|
|
set []schema.QueryWithArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *setQuery) addSet(set schema.QueryWithArgs) {
|
|
|
|
q.set = append(q.set, set)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q setQuery) appendSet(fmter schema.Formatter, b []byte) (_ []byte, err error) {
|
|
|
|
for i, f := range q.set {
|
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
b, err = f.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type cascadeQuery struct {
|
2022-03-07 10:08:26 +00:00
|
|
|
cascade bool
|
2021-08-25 13:34:33 +00:00
|
|
|
restrict bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q cascadeQuery) appendCascade(fmter schema.Formatter, b []byte) []byte {
|
|
|
|
if !fmter.HasFeature(feature.TableCascade) {
|
|
|
|
return b
|
|
|
|
}
|
2022-03-07 10:08:26 +00:00
|
|
|
if q.cascade {
|
|
|
|
b = append(b, " CASCADE"...)
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
if q.restrict {
|
|
|
|
b = append(b, " RESTRICT"...)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2022-08-15 10:35:05 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type idxHintsQuery struct {
|
|
|
|
use *indexHints
|
|
|
|
ignore *indexHints
|
|
|
|
force *indexHints
|
|
|
|
}
|
|
|
|
|
|
|
|
type indexHints struct {
|
|
|
|
names []schema.QueryWithArgs
|
|
|
|
forJoin []schema.QueryWithArgs
|
|
|
|
forOrderBy []schema.QueryWithArgs
|
|
|
|
forGroupBy []schema.QueryWithArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) lazyUse() *indexHints {
|
|
|
|
if ih.use == nil {
|
|
|
|
ih.use = new(indexHints)
|
|
|
|
}
|
|
|
|
return ih.use
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) lazyIgnore() *indexHints {
|
|
|
|
if ih.ignore == nil {
|
|
|
|
ih.ignore = new(indexHints)
|
|
|
|
}
|
|
|
|
return ih.ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) lazyForce() *indexHints {
|
|
|
|
if ih.force == nil {
|
|
|
|
ih.force = new(indexHints)
|
|
|
|
}
|
|
|
|
return ih.force
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) appendIndexes(hints []schema.QueryWithArgs, indexes ...string) []schema.QueryWithArgs {
|
|
|
|
for _, idx := range indexes {
|
|
|
|
hints = append(hints, schema.UnsafeIdent(idx))
|
|
|
|
}
|
|
|
|
return hints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addUseIndex(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyUse().names = ih.appendIndexes(ih.use.names, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addUseIndexForJoin(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyUse().forJoin = ih.appendIndexes(ih.use.forJoin, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addUseIndexForOrderBy(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyUse().forOrderBy = ih.appendIndexes(ih.use.forOrderBy, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addUseIndexForGroupBy(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyUse().forGroupBy = ih.appendIndexes(ih.use.forGroupBy, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addIgnoreIndex(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyIgnore().names = ih.appendIndexes(ih.ignore.names, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addIgnoreIndexForJoin(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyIgnore().forJoin = ih.appendIndexes(ih.ignore.forJoin, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addIgnoreIndexForOrderBy(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyIgnore().forOrderBy = ih.appendIndexes(ih.ignore.forOrderBy, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addIgnoreIndexForGroupBy(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyIgnore().forGroupBy = ih.appendIndexes(ih.ignore.forGroupBy, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addForceIndex(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyForce().names = ih.appendIndexes(ih.force.names, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addForceIndexForJoin(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyForce().forJoin = ih.appendIndexes(ih.force.forJoin, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addForceIndexForOrderBy(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyForce().forOrderBy = ih.appendIndexes(ih.force.forOrderBy, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) addForceIndexForGroupBy(indexes ...string) {
|
|
|
|
if len(indexes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ih.lazyForce().forGroupBy = ih.appendIndexes(ih.force.forGroupBy, indexes...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) appendIndexHints(
|
|
|
|
fmter schema.Formatter, b []byte,
|
|
|
|
) ([]byte, error) {
|
|
|
|
type IdxHint struct {
|
|
|
|
Name string
|
|
|
|
Values []schema.QueryWithArgs
|
|
|
|
}
|
|
|
|
|
|
|
|
var hints []IdxHint
|
|
|
|
if ih.use != nil {
|
|
|
|
hints = append(hints, []IdxHint{
|
|
|
|
{
|
|
|
|
Name: "USE INDEX",
|
|
|
|
Values: ih.use.names,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "USE INDEX FOR JOIN",
|
|
|
|
Values: ih.use.forJoin,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "USE INDEX FOR ORDER BY",
|
|
|
|
Values: ih.use.forOrderBy,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "USE INDEX FOR GROUP BY",
|
|
|
|
Values: ih.use.forGroupBy,
|
|
|
|
},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ih.ignore != nil {
|
|
|
|
hints = append(hints, []IdxHint{
|
|
|
|
{
|
|
|
|
Name: "IGNORE INDEX",
|
|
|
|
Values: ih.ignore.names,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "IGNORE INDEX FOR JOIN",
|
|
|
|
Values: ih.ignore.forJoin,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "IGNORE INDEX FOR ORDER BY",
|
|
|
|
Values: ih.ignore.forOrderBy,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "IGNORE INDEX FOR GROUP BY",
|
|
|
|
Values: ih.ignore.forGroupBy,
|
|
|
|
},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ih.force != nil {
|
|
|
|
hints = append(hints, []IdxHint{
|
|
|
|
{
|
|
|
|
Name: "FORCE INDEX",
|
|
|
|
Values: ih.force.names,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FORCE INDEX FOR JOIN",
|
|
|
|
Values: ih.force.forJoin,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FORCE INDEX FOR ORDER BY",
|
|
|
|
Values: ih.force.forOrderBy,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FORCE INDEX FOR GROUP BY",
|
|
|
|
Values: ih.force.forGroupBy,
|
|
|
|
},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, h := range hints {
|
|
|
|
b, err = ih.bufIndexHint(h.Name, h.Values, fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ih *idxHintsQuery) bufIndexHint(
|
|
|
|
name string,
|
|
|
|
hints []schema.QueryWithArgs,
|
|
|
|
fmter schema.Formatter, b []byte,
|
|
|
|
) ([]byte, error) {
|
|
|
|
var err error
|
|
|
|
if len(hints) == 0 {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
b = append(b, fmt.Sprintf(" %s (", name)...)
|
|
|
|
for i, f := range hints {
|
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
b, err = f.AppendQuery(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = append(b, ")"...)
|
|
|
|
return b, nil
|
|
|
|
}
|