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/append.go21
-rw-r--r--vendor/github.com/uptrace/bun/schema/field.go5
-rw-r--r--vendor/github.com/uptrace/bun/schema/formatter.go4
-rw-r--r--vendor/github.com/uptrace/bun/schema/reflect.go24
-rw-r--r--vendor/github.com/uptrace/bun/schema/sqlfmt.go14
-rw-r--r--vendor/github.com/uptrace/bun/schema/table.go37
-rw-r--r--vendor/github.com/uptrace/bun/schema/zerochecker.go39
7 files changed, 109 insertions, 35 deletions
diff --git a/vendor/github.com/uptrace/bun/schema/append.go b/vendor/github.com/uptrace/bun/schema/append.go
index 6f633b101..0cfc1180b 100644
--- a/vendor/github.com/uptrace/bun/schema/append.go
+++ b/vendor/github.com/uptrace/bun/schema/append.go
@@ -81,7 +81,7 @@ func appendIn(fmter Formatter, b []byte, slice reflect.Value) []byte {
sliceLen := slice.Len()
if sliceLen == 0 {
- return append(b, "NULL"...)
+ return dialect.AppendNull(b)
}
for i := 0; i < sliceLen; i++ {
@@ -104,3 +104,22 @@ func appendIn(fmter Formatter, b []byte, slice reflect.Value) []byte {
}
return b
}
+
+//------------------------------------------------------------------------------
+
+func NullZero(value interface{}) QueryAppender {
+ return nullZero{
+ value: value,
+ }
+}
+
+type nullZero struct {
+ value interface{}
+}
+
+func (nz nullZero) AppendQuery(fmter Formatter, b []byte) (_ []byte, err error) {
+ if isZero(nz.value) {
+ return dialect.AppendNull(b), nil
+ }
+ return fmter.AppendValue(b, reflect.ValueOf(nz.value)), nil
+}
diff --git a/vendor/github.com/uptrace/bun/schema/field.go b/vendor/github.com/uptrace/bun/schema/field.go
index 283a3b992..a3b086054 100644
--- a/vendor/github.com/uptrace/bun/schema/field.go
+++ b/vendor/github.com/uptrace/bun/schema/field.go
@@ -5,6 +5,7 @@ import (
"reflect"
"github.com/uptrace/bun/dialect"
+ "github.com/uptrace/bun/internal"
"github.com/uptrace/bun/internal/tagparser"
)
@@ -50,7 +51,7 @@ func (f *Field) Clone() *Field {
}
func (f *Field) Value(strct reflect.Value) reflect.Value {
- return fieldByIndexAlloc(strct, f.Index)
+ return internal.FieldByIndexAlloc(strct, f.Index)
}
func (f *Field) HasNilValue(v reflect.Value) bool {
@@ -117,7 +118,7 @@ func (f *Field) ScanValue(strct reflect.Value, src interface{}) error {
return nil
}
- fv := fieldByIndexAlloc(strct, f.Index)
+ fv := internal.FieldByIndexAlloc(strct, f.Index)
return f.ScanWithCheck(fv, src)
}
diff --git a/vendor/github.com/uptrace/bun/schema/formatter.go b/vendor/github.com/uptrace/bun/schema/formatter.go
index 1fba1b59e..1d8d9a9ee 100644
--- a/vendor/github.com/uptrace/bun/schema/formatter.go
+++ b/vendor/github.com/uptrace/bun/schema/formatter.go
@@ -42,6 +42,10 @@ func (f Formatter) IdentQuote() byte {
return f.dialect.IdentQuote()
}
+func (f Formatter) AppendName(b []byte, name string) []byte {
+ return dialect.AppendName(b, name, f.IdentQuote())
+}
+
func (f Formatter) AppendIdent(b []byte, ident string) []byte {
return dialect.AppendIdent(b, ident, f.IdentQuote())
}
diff --git a/vendor/github.com/uptrace/bun/schema/reflect.go b/vendor/github.com/uptrace/bun/schema/reflect.go
index f13826a6c..89be8eeb6 100644
--- a/vendor/github.com/uptrace/bun/schema/reflect.go
+++ b/vendor/github.com/uptrace/bun/schema/reflect.go
@@ -46,27 +46,3 @@ func fieldByIndex(v reflect.Value, index []int) (_ reflect.Value, ok bool) {
}
return v, true
}
-
-func fieldByIndexAlloc(v reflect.Value, index []int) reflect.Value {
- if len(index) == 1 {
- return v.Field(index[0])
- }
-
- for i, idx := range index {
- if i > 0 {
- v = indirectNil(v)
- }
- v = v.Field(idx)
- }
- return v
-}
-
-func indirectNil(v reflect.Value) reflect.Value {
- if v.Kind() == reflect.Ptr {
- if v.IsNil() {
- v.Set(reflect.New(v.Type().Elem()))
- }
- v = v.Elem()
- }
- return v
-}
diff --git a/vendor/github.com/uptrace/bun/schema/sqlfmt.go b/vendor/github.com/uptrace/bun/schema/sqlfmt.go
index a4ed24af6..7b4a9493f 100644
--- a/vendor/github.com/uptrace/bun/schema/sqlfmt.go
+++ b/vendor/github.com/uptrace/bun/schema/sqlfmt.go
@@ -27,7 +27,19 @@ func (s Safe) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
//------------------------------------------------------------------------------
-// Ident represents a SQL identifier, for example, table or column name.
+// Name represents a single SQL name, for example, a column name.
+type Name string
+
+var _ QueryAppender = (*Name)(nil)
+
+func (s Name) AppendQuery(fmter Formatter, b []byte) ([]byte, error) {
+ return fmter.AppendName(b, string(s)), nil
+}
+
+//------------------------------------------------------------------------------
+
+// Ident represents a SQL identifier, for example,
+// a fully qualified column name such as `table_name.col_name`.
type Ident string
var _ QueryAppender = (*Ident)(nil)
diff --git a/vendor/github.com/uptrace/bun/schema/table.go b/vendor/github.com/uptrace/bun/schema/table.go
index 9eb7d1bfe..cd0ff20b2 100644
--- a/vendor/github.com/uptrace/bun/schema/table.go
+++ b/vendor/github.com/uptrace/bun/schema/table.go
@@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"reflect"
+ "strconv"
"strings"
"sync"
"time"
@@ -806,18 +807,38 @@ func (t *Table) m2mRelation(field *Field) *Relation {
return rel
}
-func (t *Table) inlineFields(field *Field, seen map[reflect.Type]struct{}) {
- if seen == nil {
- seen = map[reflect.Type]struct{}{t.Type: {}}
+type seenKey struct {
+ Table reflect.Type
+ FieldIndex string
+}
+
+type seenMap map[seenKey]struct{}
+
+func NewSeenKey(table reflect.Type, fieldIndex []int) (key seenKey) {
+ key.Table = table
+ for _, index := range fieldIndex {
+ key.FieldIndex += strconv.Itoa(index) + "-"
}
+ return key
+}
- if _, ok := seen[field.IndirectType]; ok {
- return
+func (s seenMap) Clone() seenMap {
+ t := make(seenMap)
+ for k, v := range s {
+ t[k] = v
+ }
+ return t
+}
+
+func (t *Table) inlineFields(field *Field, seen seenMap) {
+ if seen == nil {
+ seen = make(seenMap)
}
- seen[field.IndirectType] = struct{}{}
joinTable := t.dialect.Tables().Ref(field.IndirectType)
for _, f := range joinTable.allFields {
+ key := NewSeenKey(joinTable.Type, f.Index)
+
f = f.Clone()
f.GoName = field.GoName + "_" + f.GoName
f.Name = field.Name + "__" + f.Name
@@ -834,7 +855,9 @@ func (t *Table) inlineFields(field *Field, seen map[reflect.Type]struct{}) {
continue
}
- if _, ok := seen[f.IndirectType]; !ok {
+ if _, ok := seen[key]; !ok {
+ seen = seen.Clone()
+ seen[key] = struct{}{}
t.inlineFields(f, seen)
}
}
diff --git a/vendor/github.com/uptrace/bun/schema/zerochecker.go b/vendor/github.com/uptrace/bun/schema/zerochecker.go
index f088b8c2c..f24e51d30 100644
--- a/vendor/github.com/uptrace/bun/schema/zerochecker.go
+++ b/vendor/github.com/uptrace/bun/schema/zerochecker.go
@@ -11,6 +11,45 @@ type isZeroer interface {
IsZero() bool
}
+func isZero(v interface{}) bool {
+ switch v := v.(type) {
+ case isZeroer:
+ return v.IsZero()
+ case string:
+ return v == ""
+ case []byte:
+ return v == nil
+ case int:
+ return v == 0
+ case int64:
+ return v == 0
+ case uint:
+ return v == 0
+ case uint64:
+ return v == 0
+ case float32:
+ return v == 0
+ case float64:
+ return v == 0
+ case int8:
+ return v == 0
+ case int16:
+ return v == 0
+ case int32:
+ return v == 0
+ case uint8:
+ return v == 0
+ case uint16:
+ return v == 0
+ case uint32:
+ return v == 0
+ default:
+ rv := reflect.ValueOf(v)
+ fn := zeroChecker(rv.Type())
+ return fn(rv)
+ }
+}
+
type IsZeroerFunc func(reflect.Value) bool
func zeroChecker(typ reflect.Type) IsZeroerFunc {