2021-08-25 13:34:33 +00:00
|
|
|
package bun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
"github.com/uptrace/bun/internal"
|
2021-08-25 13:34:33 +00:00
|
|
|
"github.com/uptrace/bun/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Safe = schema.Safe
|
|
|
|
Ident = schema.Ident
|
|
|
|
|
2021-09-08 19:05:26 +00:00
|
|
|
NullTime = schema.NullTime
|
|
|
|
BaseModel = schema.BaseModel
|
2021-10-24 11:14:37 +00:00
|
|
|
Query = schema.Query
|
2021-08-25 13:34:33 +00:00
|
|
|
|
2021-10-24 11:14:37 +00:00
|
|
|
BeforeAppendModelHook = schema.BeforeAppendModelHook
|
|
|
|
|
|
|
|
BeforeScanRowHook = schema.BeforeScanRowHook
|
|
|
|
AfterScanRowHook = schema.AfterScanRowHook
|
|
|
|
|
|
|
|
// DEPRECATED. Use BeforeScanRowHook instead.
|
2021-08-25 13:34:33 +00:00
|
|
|
BeforeScanHook = schema.BeforeScanHook
|
2021-10-24 11:14:37 +00:00
|
|
|
// DEPRECATED. Use AfterScanRowHook instead.
|
|
|
|
AfterScanHook = schema.AfterScanHook
|
2021-08-25 13:34:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BeforeSelectHook interface {
|
|
|
|
BeforeSelect(ctx context.Context, query *SelectQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type AfterSelectHook interface {
|
|
|
|
AfterSelect(ctx context.Context, query *SelectQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BeforeInsertHook interface {
|
|
|
|
BeforeInsert(ctx context.Context, query *InsertQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type AfterInsertHook interface {
|
|
|
|
AfterInsert(ctx context.Context, query *InsertQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BeforeUpdateHook interface {
|
|
|
|
BeforeUpdate(ctx context.Context, query *UpdateQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type AfterUpdateHook interface {
|
|
|
|
AfterUpdate(ctx context.Context, query *UpdateQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BeforeDeleteHook interface {
|
|
|
|
BeforeDelete(ctx context.Context, query *DeleteQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type AfterDeleteHook interface {
|
|
|
|
AfterDelete(ctx context.Context, query *DeleteQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BeforeCreateTableHook interface {
|
|
|
|
BeforeCreateTable(ctx context.Context, query *CreateTableQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type AfterCreateTableHook interface {
|
|
|
|
AfterCreateTable(ctx context.Context, query *CreateTableQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type BeforeDropTableHook interface {
|
|
|
|
BeforeDropTable(ctx context.Context, query *DropTableQuery) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type AfterDropTableHook interface {
|
|
|
|
AfterDropTable(ctx context.Context, query *DropTableQuery) error
|
|
|
|
}
|
2021-09-08 19:05:26 +00:00
|
|
|
|
|
|
|
// SetLogger overwriters default Bun logger.
|
|
|
|
func SetLogger(logger internal.Logging) {
|
|
|
|
internal.Logger = logger
|
|
|
|
}
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
type InValues struct {
|
|
|
|
slice reflect.Value
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ schema.QueryAppender = InValues{}
|
|
|
|
|
|
|
|
func In(slice interface{}) InValues {
|
|
|
|
v := reflect.ValueOf(slice)
|
|
|
|
if v.Kind() != reflect.Slice {
|
|
|
|
return InValues{
|
|
|
|
err: fmt.Errorf("bun: In(non-slice %T)", slice),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return InValues{
|
|
|
|
slice: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (in InValues) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
|
|
|
|
if in.err != nil {
|
|
|
|
return nil, in.err
|
|
|
|
}
|
|
|
|
return appendIn(fmter, b, in.slice), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendIn(fmter schema.Formatter, b []byte, slice reflect.Value) []byte {
|
|
|
|
sliceLen := slice.Len()
|
|
|
|
for i := 0; i < sliceLen; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
b = append(b, ", "...)
|
|
|
|
}
|
|
|
|
|
|
|
|
elem := slice.Index(i)
|
|
|
|
if elem.Kind() == reflect.Interface {
|
|
|
|
elem = elem.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if elem.Kind() == reflect.Slice {
|
|
|
|
b = append(b, '(')
|
|
|
|
b = appendIn(fmter, b, elem)
|
|
|
|
b = append(b, ')')
|
|
|
|
} else {
|
|
|
|
b = fmter.AppendValue(b, elem)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|