summaryrefslogtreecommitdiff
path: root/vendor/github.com/uptrace/bun/schema
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/uptrace/bun/schema')
-rw-r--r--vendor/github.com/uptrace/bun/schema/field.go5
-rw-r--r--vendor/github.com/uptrace/bun/schema/relation.go3
-rw-r--r--vendor/github.com/uptrace/bun/schema/scan.go5
-rw-r--r--vendor/github.com/uptrace/bun/schema/table.go69
4 files changed, 81 insertions, 1 deletions
diff --git a/vendor/github.com/uptrace/bun/schema/field.go b/vendor/github.com/uptrace/bun/schema/field.go
index ac6359da4..283a3b992 100644
--- a/vendor/github.com/uptrace/bun/schema/field.go
+++ b/vendor/github.com/uptrace/bun/schema/field.go
@@ -32,6 +32,7 @@ type Field struct {
NotNull bool
NullZero bool
AutoIncrement bool
+ Identity bool
Append AppenderFunc
Scan ScannerFunc
@@ -120,6 +121,10 @@ func (f *Field) ScanValue(strct reflect.Value, src interface{}) error {
return f.ScanWithCheck(fv, src)
}
+func (f *Field) SkipUpdate() bool {
+ return f.Tag.HasOption("skipupdate")
+}
+
func indexEqual(ind1, ind2 []int) bool {
if len(ind1) != len(ind2) {
return false
diff --git a/vendor/github.com/uptrace/bun/schema/relation.go b/vendor/github.com/uptrace/bun/schema/relation.go
index 8d1baeb3f..6636e26a6 100644
--- a/vendor/github.com/uptrace/bun/schema/relation.go
+++ b/vendor/github.com/uptrace/bun/schema/relation.go
@@ -18,6 +18,9 @@ type Relation struct {
JoinTable *Table
BaseFields []*Field
JoinFields []*Field
+ OnUpdate string
+ OnDelete string
+ Condition []string
PolymorphicField *Field
PolymorphicValue string
diff --git a/vendor/github.com/uptrace/bun/schema/scan.go b/vendor/github.com/uptrace/bun/schema/scan.go
index 069b14e44..96b31caf3 100644
--- a/vendor/github.com/uptrace/bun/schema/scan.go
+++ b/vendor/github.com/uptrace/bun/schema/scan.go
@@ -449,6 +449,11 @@ func PtrScanner(fn ScannerFunc) ScannerFunc {
if dest.IsNil() {
dest.Set(reflect.New(dest.Type().Elem()))
}
+
+ if dest.Kind() == reflect.Map {
+ return fn(dest, src)
+ }
+
return fn(dest.Elem(), src)
}
}
diff --git a/vendor/github.com/uptrace/bun/schema/table.go b/vendor/github.com/uptrace/bun/schema/table.go
index 1a8393fc7..9791f8ff1 100644
--- a/vendor/github.com/uptrace/bun/schema/table.go
+++ b/vendor/github.com/uptrace/bun/schema/table.go
@@ -353,6 +353,9 @@ func (t *Table) newField(f reflect.StructField, prefix string, index []int) *Fie
field.AutoIncrement = true
field.NullZero = true
}
+ if tag.HasOption("identity") {
+ field.Identity = true
+ }
if v, ok := tag.Options["unique"]; ok {
var names []string
@@ -374,6 +377,7 @@ func (t *Table) newField(f reflect.StructField, prefix string, index []int) *Fie
}
if s, ok := tag.Option("default"); ok {
field.SQLDefault = s
+ field.NullZero = true
}
if s, ok := field.Tag.Option("type"); ok {
field.UserSQLType = s
@@ -478,6 +482,39 @@ func (t *Table) belongsToRelation(field *Field) *Relation {
JoinTable: joinTable,
}
+ 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
+ }
+
if join, ok := field.Tag.Options["join"]; ok {
baseColumns, joinColumns := parseRelationJoin(join)
for i, baseColumn := range baseColumns {
@@ -539,6 +576,10 @@ func (t *Table) hasOneRelation(field *Field) *Relation {
JoinTable: joinTable,
}
+ if field.Tag.HasOption("join_on") {
+ rel.Condition = field.Tag.Options["join_on"]
+ }
+
if join, ok := field.Tag.Options["join"]; ok {
baseColumns, joinColumns := parseRelationJoin(join)
for i, baseColumn := range baseColumns {
@@ -605,6 +646,11 @@ func (t *Table) hasManyRelation(field *Field) *Relation {
Field: field,
JoinTable: joinTable,
}
+
+ if field.Tag.HasOption("join_on") {
+ rel.Condition = field.Tag.Options["join_on"]
+ }
+
var polymorphicColumn string
if join, ok := field.Tag.Options["join"]; ok {
@@ -715,6 +761,11 @@ func (t *Table) m2mRelation(field *Field) *Relation {
JoinTable: joinTable,
M2MTable: m2mTable,
}
+
+ if field.Tag.HasOption("join_on") {
+ rel.Condition = field.Tag.Options["join_on"]
+ }
+
var leftColumn, rightColumn string
if join, ok := field.Tag.Options["join"]; ok {
@@ -853,13 +904,29 @@ func isKnownFieldOption(name string) bool {
"unique",
"soft_delete",
"scanonly",
+ "skipupdate",
"pk",
"autoincrement",
"rel",
"join",
+ "join_on",
+ "on_update",
+ "on_delete",
"m2m",
- "polymorphic":
+ "polymorphic",
+ "identity":
+ return true
+ }
+ return false
+}
+
+func isKnownFKRule(name string) bool {
+ switch name {
+ case "CASCADE",
+ "RESTRICT",
+ "SET NULL",
+ "SET DEFAULT":
return true
}
return false