2021-08-25 15:34:33 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/inflection"
|
|
|
|
|
|
|
|
"github.com/uptrace/bun/internal"
|
|
|
|
"github.com/uptrace/bun/internal/tagparser"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-24 13:14:37 +02:00
|
|
|
beforeAppendModelHookFlag internal.Flag = 1 << iota
|
|
|
|
beforeScanHookFlag
|
2021-08-25 15:34:33 +02:00
|
|
|
afterScanHookFlag
|
2021-10-24 13:14:37 +02:00
|
|
|
beforeScanRowHookFlag
|
|
|
|
afterScanRowHookFlag
|
2021-08-25 15:34:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
baseModelType = reflect.TypeOf((*BaseModel)(nil)).Elem()
|
|
|
|
tableNameInflector = inflection.Plural
|
|
|
|
)
|
|
|
|
|
|
|
|
type BaseModel struct{}
|
|
|
|
|
|
|
|
// SetTableNameInflector overrides the default func that pluralizes
|
|
|
|
// model name to get table name, e.g. my_article becomes my_articles.
|
|
|
|
func SetTableNameInflector(fn func(string) string) {
|
|
|
|
tableNameInflector = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Table represents a SQL table created from Go struct.
|
|
|
|
type Table struct {
|
|
|
|
dialect Dialect
|
|
|
|
|
|
|
|
Type reflect.Type
|
|
|
|
ZeroValue reflect.Value // reflect.Struct
|
|
|
|
ZeroIface interface{} // struct pointer
|
|
|
|
|
|
|
|
TypeName string
|
|
|
|
ModelName string
|
|
|
|
|
|
|
|
Name string
|
|
|
|
SQLName Safe
|
|
|
|
SQLNameForSelects Safe
|
|
|
|
Alias string
|
|
|
|
SQLAlias Safe
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
allFields []*Field // all fields including scanonly
|
2021-08-25 15:34:33 +02:00
|
|
|
Fields []*Field // PKs + DataFields
|
|
|
|
PKs []*Field
|
|
|
|
DataFields []*Field
|
2024-04-15 12:01:20 +02:00
|
|
|
relFields []*Field
|
2021-08-25 15:34:33 +02:00
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
FieldMap map[string]*Field
|
|
|
|
StructMap map[string]*structField
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
Relations map[string]*Relation
|
|
|
|
Unique map[string][]*Field
|
|
|
|
|
|
|
|
SoftDeleteField *Field
|
2021-09-08 21:05:26 +02:00
|
|
|
UpdateSoftDeleteField func(fv reflect.Value, tm time.Time) error
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
flags internal.Flag
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
type structField struct {
|
|
|
|
Index []int
|
|
|
|
Table *Table
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTable(
|
|
|
|
dialect Dialect, typ reflect.Type, seen map[reflect.Type]*Table, canAddr bool,
|
|
|
|
) *Table {
|
|
|
|
if table, ok := seen[typ]; ok {
|
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
|
|
|
table := new(Table)
|
|
|
|
seen[typ] = table
|
|
|
|
|
|
|
|
table.dialect = dialect
|
|
|
|
table.Type = typ
|
|
|
|
table.ZeroValue = reflect.New(table.Type).Elem()
|
|
|
|
table.ZeroIface = reflect.New(table.Type).Interface()
|
|
|
|
table.TypeName = internal.ToExported(table.Type.Name())
|
|
|
|
table.ModelName = internal.Underscore(table.Type.Name())
|
|
|
|
tableName := tableNameInflector(table.ModelName)
|
|
|
|
table.setName(tableName)
|
|
|
|
table.Alias = table.ModelName
|
|
|
|
table.SQLAlias = table.quoteIdent(table.ModelName)
|
|
|
|
|
|
|
|
table.Fields = make([]*Field, 0, typ.NumField())
|
|
|
|
table.FieldMap = make(map[string]*Field, typ.NumField())
|
|
|
|
table.processFields(typ, seen, canAddr)
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
hooks := []struct {
|
|
|
|
typ reflect.Type
|
|
|
|
flag internal.Flag
|
|
|
|
}{
|
2021-10-24 13:14:37 +02:00
|
|
|
{beforeAppendModelHookType, beforeAppendModelHookFlag},
|
|
|
|
|
|
|
|
{beforeScanRowHookType, beforeScanRowHookFlag},
|
|
|
|
{afterScanRowHookType, afterScanRowHookFlag},
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
typ = reflect.PtrTo(table.Type)
|
2021-08-25 15:34:33 +02:00
|
|
|
for _, hook := range hooks {
|
|
|
|
if typ.Implements(hook.typ) {
|
2024-04-15 12:01:20 +02:00
|
|
|
table.flags = table.flags.Set(hook.flag)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
return table
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) init() {
|
|
|
|
for _, field := range t.relFields {
|
|
|
|
t.processRelation(field)
|
2021-10-24 13:14:37 +02:00
|
|
|
}
|
2024-04-15 12:01:20 +02:00
|
|
|
t.relFields = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) processFields(
|
|
|
|
typ reflect.Type,
|
|
|
|
seen map[reflect.Type]*Table,
|
|
|
|
canAddr bool,
|
|
|
|
) {
|
|
|
|
type embeddedField struct {
|
|
|
|
prefix string
|
|
|
|
index []int
|
|
|
|
unexported bool
|
|
|
|
subtable *Table
|
|
|
|
subfield *Field
|
|
|
|
}
|
|
|
|
|
|
|
|
names := make(map[string]struct{})
|
|
|
|
embedded := make([]embeddedField, 0, 10)
|
|
|
|
|
|
|
|
for i, n := 0, typ.NumField(); i < n; i++ {
|
|
|
|
sf := typ.Field(i)
|
|
|
|
unexported := sf.PkgPath != ""
|
|
|
|
|
|
|
|
tagstr := sf.Tag.Get("bun")
|
|
|
|
if tagstr == "-" {
|
|
|
|
names[sf.Name] = struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tag := tagparser.Parse(tagstr)
|
|
|
|
|
|
|
|
if unexported && !sf.Anonymous { // unexported
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if sf.Anonymous {
|
|
|
|
if sf.Name == "BaseModel" && sf.Type == baseModelType {
|
|
|
|
t.processBaseModelField(sf)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sfType := sf.Type
|
|
|
|
if sfType.Kind() == reflect.Ptr {
|
|
|
|
sfType = sfType.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if sfType.Kind() != reflect.Struct { // ignore unexported non-struct types
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
subtable := newTable(t.dialect, sfType, seen, canAddr)
|
|
|
|
|
|
|
|
for _, subfield := range subtable.allFields {
|
|
|
|
embedded = append(embedded, embeddedField{
|
|
|
|
index: sf.Index,
|
|
|
|
unexported: unexported,
|
|
|
|
subtable: subtable,
|
|
|
|
subfield: subfield,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagstr != "" {
|
|
|
|
tag := tagparser.Parse(tagstr)
|
|
|
|
if tag.HasOption("inherit") || tag.HasOption("extend") {
|
|
|
|
t.Name = subtable.Name
|
|
|
|
t.TypeName = subtable.TypeName
|
|
|
|
t.SQLName = subtable.SQLName
|
|
|
|
t.SQLNameForSelects = subtable.SQLNameForSelects
|
|
|
|
t.Alias = subtable.Alias
|
|
|
|
t.SQLAlias = subtable.SQLAlias
|
|
|
|
t.ModelName = subtable.ModelName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if prefix, ok := tag.Option("embed"); ok {
|
|
|
|
fieldType := indirectType(sf.Type)
|
|
|
|
if fieldType.Kind() != reflect.Struct {
|
|
|
|
panic(fmt.Errorf("bun: embed %s.%s: got %s, wanted reflect.Struct",
|
|
|
|
t.TypeName, sf.Name, fieldType.Kind()))
|
|
|
|
}
|
|
|
|
|
|
|
|
subtable := newTable(t.dialect, fieldType, seen, canAddr)
|
|
|
|
for _, subfield := range subtable.allFields {
|
|
|
|
embedded = append(embedded, embeddedField{
|
|
|
|
prefix: prefix,
|
|
|
|
index: sf.Index,
|
|
|
|
unexported: unexported,
|
|
|
|
subtable: subtable,
|
|
|
|
subfield: subfield,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
field := t.newField(sf, tag)
|
|
|
|
t.addField(field)
|
|
|
|
names[field.Name] = struct{}{}
|
|
|
|
|
|
|
|
if field.IndirectType.Kind() == reflect.Struct {
|
|
|
|
if t.StructMap == nil {
|
|
|
|
t.StructMap = make(map[string]*structField)
|
|
|
|
}
|
|
|
|
t.StructMap[field.Name] = &structField{
|
|
|
|
Index: field.Index,
|
|
|
|
Table: newTable(t.dialect, field.IndirectType, seen, canAddr),
|
|
|
|
}
|
2021-10-24 13:14:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
// Only unambiguous embedded fields must be serialized.
|
|
|
|
ambiguousNames := make(map[string]int)
|
|
|
|
ambiguousTags := make(map[string]int)
|
2021-08-25 15:34:33 +02:00
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
// Embedded types can never override a field that was already present at
|
|
|
|
// the top-level.
|
|
|
|
for name := range names {
|
|
|
|
ambiguousNames[name]++
|
|
|
|
ambiguousTags[name]++
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range embedded {
|
|
|
|
ambiguousNames[f.prefix+f.subfield.Name]++
|
|
|
|
if !f.subfield.Tag.IsZero() {
|
|
|
|
ambiguousTags[f.prefix+f.subfield.Name]++
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
for _, embfield := range embedded {
|
|
|
|
subfield := embfield.subfield.Clone()
|
|
|
|
|
|
|
|
if ambiguousNames[subfield.Name] > 1 &&
|
|
|
|
!(!subfield.Tag.IsZero() && ambiguousTags[subfield.Name] == 1) {
|
|
|
|
continue // ambiguous embedded field
|
|
|
|
}
|
|
|
|
|
|
|
|
subfield.Index = makeIndex(embfield.index, subfield.Index)
|
|
|
|
if embfield.prefix != "" {
|
|
|
|
subfield.Name = embfield.prefix + subfield.Name
|
|
|
|
subfield.SQLName = t.quoteIdent(subfield.Name)
|
|
|
|
}
|
|
|
|
t.addField(subfield)
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) setName(name string) {
|
|
|
|
t.Name = name
|
|
|
|
t.SQLName = t.quoteIdent(name)
|
|
|
|
t.SQLNameForSelects = t.quoteIdent(name)
|
|
|
|
if t.SQLAlias == "" {
|
|
|
|
t.Alias = name
|
|
|
|
t.SQLAlias = t.quoteIdent(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) String() string {
|
|
|
|
return "model=" + t.TypeName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) CheckPKs() error {
|
|
|
|
if len(t.PKs) == 0 {
|
|
|
|
return fmt.Errorf("bun: %s does not have primary keys", t)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) addField(field *Field) {
|
2024-04-15 12:01:20 +02:00
|
|
|
t.allFields = append(t.allFields, field)
|
|
|
|
|
|
|
|
if field.Tag.HasOption("rel") || field.Tag.HasOption("m2m") {
|
|
|
|
t.relFields = append(t.relFields, field)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Tag.HasOption("join") {
|
|
|
|
internal.Warn.Printf(
|
|
|
|
`%s.%s "join" option must come together with "rel" option`,
|
|
|
|
t.TypeName, field.GoName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.FieldMap[field.Name] = field
|
|
|
|
if altName, ok := field.Tag.Option("alt"); ok {
|
|
|
|
t.FieldMap[altName] = field
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Tag.HasOption("scanonly") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := field.Tag.Options["soft_delete"]; ok {
|
|
|
|
t.SoftDeleteField = field
|
|
|
|
t.UpdateSoftDeleteField = softDeleteFieldUpdater(field)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
t.Fields = append(t.Fields, field)
|
|
|
|
if field.IsPK {
|
|
|
|
t.PKs = append(t.PKs, field)
|
|
|
|
} else {
|
|
|
|
t.DataFields = append(t.DataFields, field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
func (t *Table) LookupField(name string) *Field {
|
|
|
|
if field, ok := t.FieldMap[name]; ok {
|
|
|
|
return field
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
table := t
|
|
|
|
var index []int
|
|
|
|
for {
|
|
|
|
structName, columnName, ok := strings.Cut(name, "__")
|
|
|
|
if !ok {
|
|
|
|
field, ok := table.FieldMap[name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return field.WithIndex(index)
|
|
|
|
}
|
|
|
|
name = columnName
|
|
|
|
|
|
|
|
strct := table.StructMap[structName]
|
|
|
|
if strct == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
table = strct.Table
|
|
|
|
index = append(index, strct.Index...)
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) HasField(name string) bool {
|
|
|
|
_, ok := t.FieldMap[name]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) Field(name string) (*Field, error) {
|
|
|
|
field, ok := t.FieldMap[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("bun: %s does not have column=%s", t, name)
|
|
|
|
}
|
|
|
|
return field, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) fieldByGoName(name string) *Field {
|
|
|
|
for _, f := range t.allFields {
|
|
|
|
if f.GoName == name {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) processBaseModelField(f reflect.StructField) {
|
|
|
|
tag := tagparser.Parse(f.Tag.Get("bun"))
|
|
|
|
|
|
|
|
if isKnownTableOption(tag.Name) {
|
|
|
|
internal.Warn.Printf(
|
2021-12-12 15:47:51 +01:00
|
|
|
"%s.%s tag name %q is also an option name, is it a mistake? Try table:%s.",
|
|
|
|
t.TypeName, f.Name, tag.Name, tag.Name,
|
2021-08-25 15:34:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name := range tag.Options {
|
|
|
|
if !isKnownTableOption(name) {
|
|
|
|
internal.Warn.Printf("%s.%s has unknown tag option: %q", t.TypeName, f.Name, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tag.Name != "" {
|
|
|
|
t.setName(tag.Name)
|
|
|
|
}
|
|
|
|
|
2021-12-12 15:47:51 +01:00
|
|
|
if s, ok := tag.Option("table"); ok {
|
|
|
|
t.setName(s)
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:29:08 +01:00
|
|
|
if s, ok := tag.Option("select"); ok {
|
2021-08-25 15:34:33 +02:00
|
|
|
t.SQLNameForSelects = t.quoteTableName(s)
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:29:08 +01:00
|
|
|
if s, ok := tag.Option("alias"); ok {
|
2021-08-25 15:34:33 +02:00
|
|
|
t.Alias = s
|
|
|
|
t.SQLAlias = t.quoteIdent(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:30:40 +02:00
|
|
|
// nolint
|
2024-04-15 12:01:20 +02:00
|
|
|
func (t *Table) newField(sf reflect.StructField, tag tagparser.Tag) *Field {
|
|
|
|
sqlName := internal.Underscore(sf.Name)
|
2021-11-13 12:29:08 +01:00
|
|
|
if tag.Name != "" && tag.Name != sqlName {
|
|
|
|
if isKnownFieldOption(tag.Name) {
|
|
|
|
internal.Warn.Printf(
|
2021-12-12 15:47:51 +01:00
|
|
|
"%s.%s tag name %q is also an option name, is it a mistake? Try column:%s.",
|
2024-04-15 12:01:20 +02:00
|
|
|
t.TypeName, sf.Name, tag.Name, tag.Name,
|
2021-11-13 12:29:08 +01:00
|
|
|
)
|
|
|
|
}
|
2021-09-08 21:05:26 +02:00
|
|
|
sqlName = tag.Name
|
|
|
|
}
|
2021-12-12 15:47:51 +01:00
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
for name := range tag.Options {
|
|
|
|
if !isKnownFieldOption(name) {
|
2024-04-15 12:01:20 +02:00
|
|
|
internal.Warn.Printf("%s.%s has unknown tag option: %q", t.TypeName, sf.Name, name)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
field := &Field{
|
2024-04-15 12:01:20 +02:00
|
|
|
StructField: sf,
|
|
|
|
IsPtr: sf.Type.Kind() == reflect.Ptr,
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
Tag: tag,
|
2024-04-15 12:01:20 +02:00
|
|
|
IndirectType: indirectType(sf.Type),
|
|
|
|
Index: sf.Index,
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
Name: sqlName,
|
2024-04-15 12:01:20 +02:00
|
|
|
GoName: sf.Name,
|
2021-08-25 15:34:33 +02:00
|
|
|
SQLName: t.quoteIdent(sqlName),
|
|
|
|
}
|
|
|
|
|
|
|
|
field.NotNull = tag.HasOption("notnull")
|
|
|
|
field.NullZero = tag.HasOption("nullzero")
|
|
|
|
if tag.HasOption("pk") {
|
2022-04-24 12:26:22 +02:00
|
|
|
field.IsPK = true
|
|
|
|
field.NotNull = true
|
|
|
|
}
|
|
|
|
if tag.HasOption("autoincrement") {
|
|
|
|
field.AutoIncrement = true
|
|
|
|
field.NullZero = true
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
2022-08-15 12:35:05 +02:00
|
|
|
if tag.HasOption("identity") {
|
|
|
|
field.Identity = true
|
|
|
|
}
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
if v, ok := tag.Options["unique"]; ok {
|
2021-11-13 12:29:08 +01:00
|
|
|
var names []string
|
|
|
|
if len(v) == 1 {
|
|
|
|
// Split the value by comma, this will allow multiple names to be specified.
|
|
|
|
// We can use this to create multiple named unique constraints where a single column
|
|
|
|
// might be included in multiple constraints.
|
|
|
|
names = strings.Split(v[0], ",")
|
|
|
|
} else {
|
|
|
|
names = v
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, uniqueName := range names {
|
2021-08-25 15:34:33 +02:00
|
|
|
if t.Unique == nil {
|
|
|
|
t.Unique = make(map[string][]*Field)
|
|
|
|
}
|
|
|
|
t.Unique[uniqueName] = append(t.Unique[uniqueName], field)
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 12:29:08 +01:00
|
|
|
if s, ok := tag.Option("default"); ok {
|
2021-08-25 15:34:33 +02:00
|
|
|
field.SQLDefault = s
|
|
|
|
}
|
2021-11-13 12:29:08 +01:00
|
|
|
if s, ok := field.Tag.Option("type"); ok {
|
2021-08-25 15:34:33 +02:00
|
|
|
field.UserSQLType = s
|
|
|
|
}
|
|
|
|
field.DiscoveredSQLType = DiscoverSQLType(field.IndirectType)
|
2021-10-24 13:14:37 +02:00
|
|
|
field.Append = FieldAppender(t.dialect, field)
|
2021-08-25 15:34:33 +02:00
|
|
|
field.Scan = FieldScanner(t.dialect, field)
|
2021-09-11 13:53:44 +02:00
|
|
|
field.IsZero = zeroChecker(field.StructField.Type)
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
func (t *Table) processRelation(field *Field) {
|
2021-11-13 12:29:08 +01:00
|
|
|
if rel, ok := field.Tag.Option("rel"); ok {
|
2021-08-25 15:34:33 +02:00
|
|
|
t.initRelation(field, rel)
|
2024-04-15 12:01:20 +02:00
|
|
|
return
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
if field.Tag.HasOption("m2m") {
|
|
|
|
t.addRelation(t.m2mRelation(field))
|
2024-04-15 12:01:20 +02:00
|
|
|
return
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
2024-04-15 12:01:20 +02:00
|
|
|
panic("not reached")
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) initRelation(field *Field, rel string) {
|
|
|
|
switch rel {
|
|
|
|
case "belongs-to":
|
|
|
|
t.addRelation(t.belongsToRelation(field))
|
|
|
|
case "has-one":
|
|
|
|
t.addRelation(t.hasOneRelation(field))
|
|
|
|
case "has-many":
|
|
|
|
t.addRelation(t.hasManyRelation(field))
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("bun: unknown relation=%s on field=%s", rel, field.GoName))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) addRelation(rel *Relation) {
|
|
|
|
if t.Relations == nil {
|
|
|
|
t.Relations = make(map[string]*Relation)
|
|
|
|
}
|
|
|
|
_, ok := t.Relations[rel.Field.GoName]
|
|
|
|
if ok {
|
|
|
|
panic(fmt.Errorf("%s already has %s", t, rel))
|
|
|
|
}
|
|
|
|
t.Relations[rel.Field.GoName] = rel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) belongsToRelation(field *Field) *Relation {
|
2024-04-15 12:01:20 +02:00
|
|
|
joinTable := t.dialect.Tables().InProgress(field.IndirectType)
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := joinTable.CheckPKs(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rel := &Relation{
|
2024-01-15 14:08:07 +01:00
|
|
|
Type: BelongsToRelation,
|
2021-08-25 15:34:33 +02:00
|
|
|
Field: field,
|
|
|
|
JoinTable: joinTable,
|
|
|
|
}
|
|
|
|
|
2022-08-15 12:35:05 +02:00
|
|
|
if field.Tag.HasOption("join_on") {
|
|
|
|
rel.Condition = field.Tag.Options["join_on"]
|
|
|
|
}
|
|
|
|
|
|
|
|
rel.OnUpdate = "ON UPDATE NO ACTION"
|
|
|
|
if onUpdate, ok := field.Tag.Options["on_update"]; ok {
|
|
|
|
if len(onUpdate) > 1 {
|
|
|
|
panic(fmt.Errorf("bun: %s belongs-to %s: on_update option must be a single field", t.TypeName, field.GoName))
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := strings.ToUpper(onUpdate[0])
|
|
|
|
if !isKnownFKRule(rule) {
|
|
|
|
internal.Warn.Printf("bun: %s belongs-to %s: unknown on_update rule %s", t.TypeName, field.GoName, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := fmt.Sprintf("ON UPDATE %s", rule)
|
|
|
|
rel.OnUpdate = s
|
|
|
|
}
|
|
|
|
|
|
|
|
rel.OnDelete = "ON DELETE NO ACTION"
|
|
|
|
if onDelete, ok := field.Tag.Options["on_delete"]; ok {
|
|
|
|
if len(onDelete) > 1 {
|
|
|
|
panic(fmt.Errorf("bun: %s belongs-to %s: on_delete option must be a single field", t.TypeName, field.GoName))
|
|
|
|
}
|
|
|
|
|
|
|
|
rule := strings.ToUpper(onDelete[0])
|
|
|
|
if !isKnownFKRule(rule) {
|
|
|
|
internal.Warn.Printf("bun: %s belongs-to %s: unknown on_delete rule %s", t.TypeName, field.GoName, rule)
|
|
|
|
}
|
|
|
|
s := fmt.Sprintf("ON DELETE %s", rule)
|
|
|
|
rel.OnDelete = s
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if join, ok := field.Tag.Options["join"]; ok {
|
|
|
|
baseColumns, joinColumns := parseRelationJoin(join)
|
|
|
|
for i, baseColumn := range baseColumns {
|
|
|
|
joinColumn := joinColumns[i]
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
if f := t.FieldMap[baseColumn]; f != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.BaseFields = append(rel.BaseFields, f)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s belongs-to %s: %s must have column %s",
|
|
|
|
t.TypeName, field.GoName, t.TypeName, baseColumn,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
if f := joinTable.FieldMap[joinColumn]; f != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.JoinFields = append(rel.JoinFields, f)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s belongs-to %s: %s must have column %s",
|
2023-05-12 14:33:40 +02:00
|
|
|
t.TypeName, field.GoName, joinTable.TypeName, joinColumn,
|
2021-08-25 15:34:33 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
rel.JoinFields = joinTable.PKs
|
|
|
|
fkPrefix := internal.Underscore(field.GoName) + "_"
|
|
|
|
for _, joinPK := range joinTable.PKs {
|
|
|
|
fkName := fkPrefix + joinPK.Name
|
2024-04-15 12:01:20 +02:00
|
|
|
if fk := t.FieldMap[fkName]; fk != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.BaseFields = append(rel.BaseFields, fk)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
if fk := t.FieldMap[joinPK.Name]; fk != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.BaseFields = append(rel.BaseFields, fk)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s belongs-to %s: %s must have column %s "+
|
|
|
|
"(to override, use join:base_column=join_column tag on %s field)",
|
|
|
|
t.TypeName, field.GoName, t.TypeName, fkName, field.GoName,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) hasOneRelation(field *Field) *Relation {
|
|
|
|
if err := t.CheckPKs(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
joinTable := t.dialect.Tables().InProgress(field.IndirectType)
|
2021-08-25 15:34:33 +02:00
|
|
|
rel := &Relation{
|
2024-01-15 14:08:07 +01:00
|
|
|
Type: HasOneRelation,
|
2021-08-25 15:34:33 +02:00
|
|
|
Field: field,
|
|
|
|
JoinTable: joinTable,
|
|
|
|
}
|
|
|
|
|
2022-08-15 12:35:05 +02:00
|
|
|
if field.Tag.HasOption("join_on") {
|
|
|
|
rel.Condition = field.Tag.Options["join_on"]
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if join, ok := field.Tag.Options["join"]; ok {
|
|
|
|
baseColumns, joinColumns := parseRelationJoin(join)
|
|
|
|
for i, baseColumn := range baseColumns {
|
2024-04-15 12:01:20 +02:00
|
|
|
if f := t.FieldMap[baseColumn]; f != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.BaseFields = append(rel.BaseFields, f)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s has-one %s: %s must have column %s",
|
2023-05-12 14:33:40 +02:00
|
|
|
field.GoName, t.TypeName, t.TypeName, baseColumn,
|
2021-08-25 15:34:33 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
joinColumn := joinColumns[i]
|
2024-04-15 12:01:20 +02:00
|
|
|
if f := joinTable.FieldMap[joinColumn]; f != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.JoinFields = append(rel.JoinFields, f)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s has-one %s: %s must have column %s",
|
2023-05-12 14:33:40 +02:00
|
|
|
field.GoName, t.TypeName, joinTable.TypeName, joinColumn,
|
2021-08-25 15:34:33 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
rel.BaseFields = t.PKs
|
|
|
|
fkPrefix := internal.Underscore(t.ModelName) + "_"
|
|
|
|
for _, pk := range t.PKs {
|
|
|
|
fkName := fkPrefix + pk.Name
|
2024-04-15 12:01:20 +02:00
|
|
|
if f := joinTable.FieldMap[fkName]; f != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.JoinFields = append(rel.JoinFields, f)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
if f := joinTable.FieldMap[pk.Name]; f != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.JoinFields = append(rel.JoinFields, f)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s has-one %s: %s must have column %s "+
|
|
|
|
"(to override, use join:base_column=join_column tag on %s field)",
|
|
|
|
field.GoName, t.TypeName, joinTable.TypeName, fkName, field.GoName,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) hasManyRelation(field *Field) *Relation {
|
|
|
|
if err := t.CheckPKs(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if field.IndirectType.Kind() != reflect.Slice {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s.%s has-many relation requires slice, got %q",
|
|
|
|
t.TypeName, field.GoName, field.IndirectType.Kind(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
joinTable := t.dialect.Tables().InProgress(indirectType(field.IndirectType.Elem()))
|
2021-11-13 12:29:08 +01:00
|
|
|
polymorphicValue, isPolymorphic := field.Tag.Option("polymorphic")
|
2021-08-25 15:34:33 +02:00
|
|
|
rel := &Relation{
|
|
|
|
Type: HasManyRelation,
|
|
|
|
Field: field,
|
|
|
|
JoinTable: joinTable,
|
|
|
|
}
|
2022-08-15 12:35:05 +02:00
|
|
|
|
|
|
|
if field.Tag.HasOption("join_on") {
|
|
|
|
rel.Condition = field.Tag.Options["join_on"]
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
var polymorphicColumn string
|
|
|
|
|
|
|
|
if join, ok := field.Tag.Options["join"]; ok {
|
|
|
|
baseColumns, joinColumns := parseRelationJoin(join)
|
|
|
|
for i, baseColumn := range baseColumns {
|
|
|
|
joinColumn := joinColumns[i]
|
|
|
|
|
|
|
|
if isPolymorphic && baseColumn == "type" {
|
|
|
|
polymorphicColumn = joinColumn
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
if f := t.FieldMap[baseColumn]; f != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.BaseFields = append(rel.BaseFields, f)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf(
|
2022-04-24 12:26:22 +02:00
|
|
|
"bun: %s has-many %s: %s must have column %s",
|
2021-08-25 15:34:33 +02:00
|
|
|
t.TypeName, field.GoName, t.TypeName, baseColumn,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
if f := joinTable.FieldMap[joinColumn]; f != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.JoinFields = append(rel.JoinFields, f)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf(
|
2022-04-24 12:26:22 +02:00
|
|
|
"bun: %s has-many %s: %s must have column %s",
|
2023-05-12 14:33:40 +02:00
|
|
|
t.TypeName, field.GoName, joinTable.TypeName, joinColumn,
|
2021-08-25 15:34:33 +02:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rel.BaseFields = t.PKs
|
|
|
|
fkPrefix := internal.Underscore(t.ModelName) + "_"
|
|
|
|
if isPolymorphic {
|
|
|
|
polymorphicColumn = fkPrefix + "type"
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pk := range t.PKs {
|
|
|
|
joinColumn := fkPrefix + pk.Name
|
2024-04-15 12:01:20 +02:00
|
|
|
if fk := joinTable.FieldMap[joinColumn]; fk != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.JoinFields = append(rel.JoinFields, fk)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
if fk := joinTable.FieldMap[pk.Name]; fk != nil {
|
2021-08-25 15:34:33 +02:00
|
|
|
rel.JoinFields = append(rel.JoinFields, fk)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s has-many %s: %s must have column %s "+
|
|
|
|
"(to override, use join:base_column=join_column tag on the field %s)",
|
|
|
|
t.TypeName, field.GoName, joinTable.TypeName, joinColumn, field.GoName,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isPolymorphic {
|
2024-04-15 12:01:20 +02:00
|
|
|
rel.PolymorphicField = joinTable.FieldMap[polymorphicColumn]
|
2021-08-25 15:34:33 +02:00
|
|
|
if rel.PolymorphicField == nil {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s has-many %s: %s must have polymorphic column %s",
|
|
|
|
t.TypeName, field.GoName, joinTable.TypeName, polymorphicColumn,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
if polymorphicValue == "" {
|
|
|
|
polymorphicValue = t.ModelName
|
|
|
|
}
|
|
|
|
rel.PolymorphicValue = polymorphicValue
|
|
|
|
}
|
|
|
|
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) m2mRelation(field *Field) *Relation {
|
|
|
|
if field.IndirectType.Kind() != reflect.Slice {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s.%s m2m relation requires slice, got %q",
|
|
|
|
t.TypeName, field.GoName, field.IndirectType.Kind(),
|
|
|
|
))
|
|
|
|
}
|
2024-04-15 12:01:20 +02:00
|
|
|
joinTable := t.dialect.Tables().InProgress(indirectType(field.IndirectType.Elem()))
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
if err := t.CheckPKs(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := joinTable.CheckPKs(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:29:08 +01:00
|
|
|
m2mTableName, ok := field.Tag.Option("m2m")
|
2021-08-25 15:34:33 +02:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("bun: %s must have m2m tag option", field.GoName))
|
|
|
|
}
|
|
|
|
|
|
|
|
m2mTable := t.dialect.Tables().ByName(m2mTableName)
|
|
|
|
if m2mTable == nil {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: can't find m2m %s table (use db.RegisterModel)",
|
|
|
|
m2mTableName,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
rel := &Relation{
|
|
|
|
Type: ManyToManyRelation,
|
|
|
|
Field: field,
|
|
|
|
JoinTable: joinTable,
|
|
|
|
M2MTable: m2mTable,
|
|
|
|
}
|
2022-08-15 12:35:05 +02:00
|
|
|
|
|
|
|
if field.Tag.HasOption("join_on") {
|
|
|
|
rel.Condition = field.Tag.Options["join_on"]
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
var leftColumn, rightColumn string
|
|
|
|
|
|
|
|
if join, ok := field.Tag.Options["join"]; ok {
|
|
|
|
left, right := parseRelationJoin(join)
|
|
|
|
leftColumn = left[0]
|
|
|
|
rightColumn = right[0]
|
|
|
|
} else {
|
|
|
|
leftColumn = t.TypeName
|
|
|
|
rightColumn = joinTable.TypeName
|
|
|
|
}
|
|
|
|
|
|
|
|
leftField := m2mTable.fieldByGoName(leftColumn)
|
|
|
|
if leftField == nil {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s many-to-many %s: %s must have field %s "+
|
|
|
|
"(to override, use tag join:LeftField=RightField on field %s.%s",
|
|
|
|
t.TypeName, field.GoName, m2mTable.TypeName, leftColumn, t.TypeName, field.GoName,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
rightField := m2mTable.fieldByGoName(rightColumn)
|
|
|
|
if rightField == nil {
|
|
|
|
panic(fmt.Errorf(
|
|
|
|
"bun: %s many-to-many %s: %s must have field %s "+
|
|
|
|
"(to override, use tag join:LeftField=RightField on field %s.%s",
|
|
|
|
t.TypeName, field.GoName, m2mTable.TypeName, rightColumn, t.TypeName, field.GoName,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
leftRel := m2mTable.belongsToRelation(leftField)
|
|
|
|
rel.BaseFields = leftRel.JoinFields
|
|
|
|
rel.M2MBaseFields = leftRel.BaseFields
|
|
|
|
|
|
|
|
rightRel := m2mTable.belongsToRelation(rightField)
|
|
|
|
rel.JoinFields = rightRel.JoinFields
|
|
|
|
rel.M2MJoinFields = rightRel.BaseFields
|
|
|
|
|
|
|
|
return rel
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2021-10-24 13:14:37 +02:00
|
|
|
func (t *Table) Dialect() Dialect { return t.dialect }
|
|
|
|
|
|
|
|
func (t *Table) HasBeforeAppendModelHook() bool { return t.flags.Has(beforeAppendModelHookFlag) }
|
|
|
|
|
|
|
|
// DEPRECATED. Use HasBeforeScanRowHook.
|
2021-08-25 15:34:33 +02:00
|
|
|
func (t *Table) HasBeforeScanHook() bool { return t.flags.Has(beforeScanHookFlag) }
|
2021-10-24 13:14:37 +02:00
|
|
|
|
|
|
|
// DEPRECATED. Use HasAfterScanRowHook.
|
|
|
|
func (t *Table) HasAfterScanHook() bool { return t.flags.Has(afterScanHookFlag) }
|
|
|
|
|
|
|
|
func (t *Table) HasBeforeScanRowHook() bool { return t.flags.Has(beforeScanRowHookFlag) }
|
|
|
|
func (t *Table) HasAfterScanRowHook() bool { return t.flags.Has(afterScanRowHookFlag) }
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (t *Table) AppendNamedArg(
|
|
|
|
fmter Formatter, b []byte, name string, strct reflect.Value,
|
|
|
|
) ([]byte, bool) {
|
|
|
|
if field, ok := t.FieldMap[name]; ok {
|
2022-03-07 11:08:26 +01:00
|
|
|
return field.AppendValue(fmter, b, strct), true
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
return b, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) quoteTableName(s string) Safe {
|
|
|
|
// Don't quote if table name contains placeholder (?) or parentheses.
|
|
|
|
if strings.IndexByte(s, '?') >= 0 ||
|
|
|
|
strings.IndexByte(s, '(') >= 0 ||
|
|
|
|
strings.IndexByte(s, ')') >= 0 {
|
|
|
|
return Safe(s)
|
|
|
|
}
|
|
|
|
return t.quoteIdent(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) quoteIdent(s string) Safe {
|
|
|
|
return Safe(NewFormatter(t.dialect).AppendIdent(nil, s))
|
|
|
|
}
|
|
|
|
|
|
|
|
func isKnownTableOption(name string) bool {
|
|
|
|
switch name {
|
2021-12-12 15:47:51 +01:00
|
|
|
case "table", "alias", "select":
|
2021-08-25 15:34:33 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isKnownFieldOption(name string) bool {
|
|
|
|
switch name {
|
2021-12-12 15:47:51 +01:00
|
|
|
case "column",
|
2024-04-15 12:01:20 +02:00
|
|
|
"alt",
|
2021-08-25 15:34:33 +02:00
|
|
|
"type",
|
|
|
|
"array",
|
|
|
|
"hstore",
|
|
|
|
"composite",
|
|
|
|
"json_use_number",
|
|
|
|
"msgpack",
|
|
|
|
"notnull",
|
|
|
|
"nullzero",
|
|
|
|
"default",
|
|
|
|
"unique",
|
|
|
|
"soft_delete",
|
2021-09-08 21:05:26 +02:00
|
|
|
"scanonly",
|
2022-08-15 12:35:05 +02:00
|
|
|
"skipupdate",
|
2021-08-25 15:34:33 +02:00
|
|
|
|
|
|
|
"pk",
|
|
|
|
"autoincrement",
|
|
|
|
"rel",
|
|
|
|
"join",
|
2022-08-15 12:35:05 +02:00
|
|
|
"join_on",
|
|
|
|
"on_update",
|
|
|
|
"on_delete",
|
2021-08-25 15:34:33 +02:00
|
|
|
"m2m",
|
2022-08-15 12:35:05 +02:00
|
|
|
"polymorphic",
|
|
|
|
"identity":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isKnownFKRule(name string) bool {
|
|
|
|
switch name {
|
|
|
|
case "CASCADE",
|
|
|
|
"RESTRICT",
|
|
|
|
"SET NULL",
|
|
|
|
"SET DEFAULT":
|
2021-08-25 15:34:33 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:29:08 +01:00
|
|
|
func parseRelationJoin(join []string) ([]string, []string) {
|
|
|
|
var ss []string
|
|
|
|
if len(join) == 1 {
|
|
|
|
ss = strings.Split(join[0], ",")
|
|
|
|
} else {
|
|
|
|
ss = join
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
baseColumns := make([]string, len(ss))
|
|
|
|
joinColumns := make([]string, len(ss))
|
|
|
|
for i, s := range ss {
|
|
|
|
ss := strings.Split(strings.TrimSpace(s), "=")
|
|
|
|
if len(ss) != 2 {
|
|
|
|
panic(fmt.Errorf("can't parse relation join: %q", join))
|
|
|
|
}
|
|
|
|
baseColumns[i] = ss[0]
|
|
|
|
joinColumns[i] = ss[1]
|
|
|
|
}
|
|
|
|
return baseColumns, joinColumns
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2021-09-08 21:05:26 +02:00
|
|
|
func softDeleteFieldUpdater(field *Field) func(fv reflect.Value, tm time.Time) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
typ := field.StructField.Type
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case timeType:
|
2021-09-08 21:05:26 +02:00
|
|
|
return func(fv reflect.Value, tm time.Time) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
ptr := fv.Addr().Interface().(*time.Time)
|
2021-09-08 21:05:26 +02:00
|
|
|
*ptr = tm
|
2021-08-25 15:34:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case nullTimeType:
|
2021-09-08 21:05:26 +02:00
|
|
|
return func(fv reflect.Value, tm time.Time) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
ptr := fv.Addr().Interface().(*sql.NullTime)
|
2021-09-08 21:05:26 +02:00
|
|
|
*ptr = sql.NullTime{Time: tm}
|
2021-08-25 15:34:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case nullIntType:
|
2021-09-08 21:05:26 +02:00
|
|
|
return func(fv reflect.Value, tm time.Time) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
ptr := fv.Addr().Interface().(*sql.NullInt64)
|
2021-09-08 21:05:26 +02:00
|
|
|
*ptr = sql.NullInt64{Int64: tm.UnixNano()}
|
2021-08-25 15:34:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch field.IndirectType.Kind() {
|
|
|
|
case reflect.Int64:
|
2021-09-08 21:05:26 +02:00
|
|
|
return func(fv reflect.Value, tm time.Time) error {
|
2021-08-25 15:34:33 +02:00
|
|
|
ptr := fv.Addr().Interface().(*int64)
|
2021-09-08 21:05:26 +02:00
|
|
|
*ptr = tm.UnixNano()
|
2021-08-25 15:34:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
typ = typ.Elem()
|
|
|
|
default:
|
|
|
|
return softDeleteFieldUpdaterFallback(field)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ { //nolint:gocritic
|
|
|
|
case timeType:
|
2021-09-08 21:05:26 +02:00
|
|
|
return func(fv reflect.Value, tm time.Time) error {
|
|
|
|
fv.Set(reflect.ValueOf(&tm))
|
2021-08-25 15:34:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ.Kind() { //nolint:gocritic
|
|
|
|
case reflect.Int64:
|
2021-09-08 21:05:26 +02:00
|
|
|
return func(fv reflect.Value, tm time.Time) error {
|
|
|
|
utime := tm.UnixNano()
|
2021-08-25 15:34:33 +02:00
|
|
|
fv.Set(reflect.ValueOf(&utime))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return softDeleteFieldUpdaterFallback(field)
|
|
|
|
}
|
|
|
|
|
2021-09-08 21:05:26 +02:00
|
|
|
func softDeleteFieldUpdaterFallback(field *Field) func(fv reflect.Value, tm time.Time) error {
|
|
|
|
return func(fv reflect.Value, tm time.Time) error {
|
|
|
|
return field.ScanWithCheck(fv, tm)
|
2021-08-25 15:34:33 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-07 11:08:26 +01:00
|
|
|
|
2024-04-15 12:01:20 +02:00
|
|
|
func makeIndex(a, b []int) []int {
|
2022-03-07 11:08:26 +01:00
|
|
|
dest := make([]int, 0, len(a)+len(b))
|
|
|
|
dest = append(dest, a...)
|
|
|
|
dest = append(dest, b...)
|
|
|
|
return dest
|
|
|
|
}
|