update dependencies (#296)

This commit is contained in:
tobi 2021-11-13 12:29:08 +01:00 committed by GitHub
commit 829a934d23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
124 changed files with 2453 additions and 1588 deletions

View file

@ -31,7 +31,7 @@ func Append(fmter Formatter, b []byte, v interface{}) []byte {
case float64:
return dialect.AppendFloat64(b, v)
case string:
return dialect.AppendString(b, v)
return fmter.Dialect().AppendString(b, v)
case time.Time:
return fmter.Dialect().AppendTime(b, v)
case []byte:

View file

@ -194,7 +194,7 @@ func appendArrayBytesValue(fmter Formatter, b []byte, v reflect.Value) []byte {
}
func AppendStringValue(fmter Formatter, b []byte, v reflect.Value) []byte {
return dialect.AppendString(b, v.String())
return fmter.Dialect().AppendString(b, v.String())
}
func AppendJSONValue(fmter Formatter, b []byte, v reflect.Value) []byte {
@ -217,12 +217,12 @@ func appendTimeValue(fmter Formatter, b []byte, v reflect.Value) []byte {
func appendIPValue(fmter Formatter, b []byte, v reflect.Value) []byte {
ip := v.Interface().(net.IP)
return dialect.AppendString(b, ip.String())
return fmter.Dialect().AppendString(b, ip.String())
}
func appendIPNetValue(fmter Formatter, b []byte, v reflect.Value) []byte {
ipnet := v.Interface().(net.IPNet)
return dialect.AppendString(b, ipnet.String())
return fmter.Dialect().AppendString(b, ipnet.String())
}
func appendJSONRawMessageValue(fmter Formatter, b []byte, v reflect.Value) []byte {
@ -230,7 +230,7 @@ func appendJSONRawMessageValue(fmter Formatter, b []byte, v reflect.Value) []byt
if bytes == nil {
return dialect.AppendNull(b)
}
return dialect.AppendString(b, internal.String(bytes))
return fmter.Dialect().AppendString(b, internal.String(bytes))
}
func appendQueryAppenderValue(fmter Formatter, b []byte, v reflect.Value) []byte {

View file

@ -2,8 +2,10 @@ package schema
import (
"database/sql"
"encoding/hex"
"strconv"
"time"
"unicode/utf8"
"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/feature"
@ -24,6 +26,7 @@ type Dialect interface {
AppendUint32(b []byte, n uint32) []byte
AppendUint64(b []byte, n uint64) []byte
AppendTime(b []byte, tm time.Time) []byte
AppendString(b []byte, s string) []byte
AppendBytes(b []byte, bs []byte) []byte
AppendJSON(b, jsonb []byte) []byte
}
@ -47,8 +50,48 @@ func (BaseDialect) AppendTime(b []byte, tm time.Time) []byte {
return b
}
func (BaseDialect) AppendString(b []byte, s string) []byte {
b = append(b, '\'')
for _, r := range s {
if r == '\000' {
continue
}
if r == '\'' {
b = append(b, '\'', '\'')
continue
}
if r < utf8.RuneSelf {
b = append(b, byte(r))
continue
}
l := len(b)
if cap(b)-l < utf8.UTFMax {
b = append(b, make([]byte, utf8.UTFMax)...)
}
n := utf8.EncodeRune(b[l:l+utf8.UTFMax], r)
b = b[:l+n]
}
b = append(b, '\'')
return b
}
func (BaseDialect) AppendBytes(b, bs []byte) []byte {
return dialect.AppendBytes(b, bs)
if bs == nil {
return dialect.AppendNull(b)
}
b = append(b, `'\x`...)
s := len(b)
b = append(b, make([]byte, hex.EncodedLen(len(bs)))...)
hex.Encode(b[s:], bs)
b = append(b, '\'')
return b
}
func (BaseDialect) AppendJSON(b, jsonb []byte) []byte {

View file

@ -300,11 +300,11 @@ func (t *Table) processBaseModelField(f reflect.StructField) {
t.setName(tag.Name)
}
if s, ok := tag.Options["select"]; ok {
if s, ok := tag.Option("select"); ok {
t.SQLNameForSelects = t.quoteTableName(s)
}
if s, ok := tag.Options["alias"]; ok {
if s, ok := tag.Option("alias"); ok {
t.Alias = s
t.SQLAlias = t.quoteIdent(s)
}
@ -315,17 +315,16 @@ func (t *Table) newField(f reflect.StructField, index []int) *Field {
tag := tagparser.Parse(f.Tag.Get("bun"))
sqlName := internal.Underscore(f.Name)
if tag.Name != "" {
if tag.Name != "" && tag.Name != sqlName {
if isKnownFieldOption(tag.Name) {
internal.Warn.Printf(
"%s.%s tag name %q is also an option name; is it a mistake?",
t.TypeName, f.Name, tag.Name,
)
}
sqlName = tag.Name
}
if tag.Name != sqlName && isKnownFieldOption(tag.Name) {
internal.Warn.Printf(
"%s.%s tag name %q is also an option name; is it a mistake?",
t.TypeName, f.Name, tag.Name,
)
}
for name := range tag.Options {
if !isKnownFieldOption(name) {
internal.Warn.Printf("%s.%s has unknown tag option: %q", t.TypeName, f.Name, name)
@ -360,20 +359,27 @@ func (t *Table) newField(f reflect.StructField, index []int) *Field {
}
if v, ok := tag.Options["unique"]; ok {
// 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.
for _, uniqueName := range strings.Split(v, ",") {
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 {
if t.Unique == nil {
t.Unique = make(map[string][]*Field)
}
t.Unique[uniqueName] = append(t.Unique[uniqueName], field)
}
}
if s, ok := tag.Options["default"]; ok {
if s, ok := tag.Option("default"); ok {
field.SQLDefault = s
}
if s, ok := field.Tag.Options["type"]; ok {
if s, ok := field.Tag.Option("type"); ok {
field.UserSQLType = s
}
field.DiscoveredSQLType = DiscoverSQLType(field.IndirectType)
@ -381,7 +387,7 @@ func (t *Table) newField(f reflect.StructField, index []int) *Field {
field.Scan = FieldScanner(t.dialect, field)
field.IsZero = zeroChecker(field.StructField.Type)
if v, ok := tag.Options["alt"]; ok {
if v, ok := tag.Option("alt"); ok {
t.FieldMap[v] = field
}
@ -433,7 +439,7 @@ func (t *Table) initRelations() {
}
func (t *Table) tryRelation(field *Field) bool {
if rel, ok := field.Tag.Options["rel"]; ok {
if rel, ok := field.Tag.Option("rel"); ok {
t.initRelation(field, rel)
return true
}
@ -444,7 +450,7 @@ func (t *Table) tryRelation(field *Field) bool {
if field.Tag.HasOption("join") {
internal.Warn.Printf(
`%s.%s option "join" requires a relation type`,
`%s.%s "join" option must come together with "rel" option`,
t.TypeName, field.GoName,
)
}
@ -609,7 +615,7 @@ func (t *Table) hasManyRelation(field *Field) *Relation {
}
joinTable := t.dialect.Tables().Ref(indirectType(field.IndirectType.Elem()))
polymorphicValue, isPolymorphic := field.Tag.Options["polymorphic"]
polymorphicValue, isPolymorphic := field.Tag.Option("polymorphic")
rel := &Relation{
Type: HasManyRelation,
Field: field,
@ -706,7 +712,7 @@ func (t *Table) m2mRelation(field *Field) *Relation {
panic(err)
}
m2mTableName, ok := field.Tag.Options["m2m"]
m2mTableName, ok := field.Tag.Option("m2m")
if !ok {
panic(fmt.Errorf("bun: %s must have m2m tag option", field.GoName))
}
@ -891,8 +897,14 @@ func removeField(fields []*Field, field *Field) []*Field {
return fields
}
func parseRelationJoin(join string) ([]string, []string) {
ss := strings.Split(join, ",")
func parseRelationJoin(join []string) ([]string, []string) {
var ss []string
if len(join) == 1 {
ss = strings.Split(join[0], ",")
} else {
ss = join
}
baseColumns := make([]string, len(ss))
joinColumns := make([]string, len(ss))
for i, s := range ss {

View file

@ -101,13 +101,15 @@ func (t *Tables) table(typ reflect.Type, allowInProgress bool) *Table {
return table
}
if inProgress.init2() {
t.mu.Lock()
delete(t.inProgress, typ)
t.tables.Store(typ, table)
t.mu.Unlock()
if !inProgress.init2() {
return table
}
t.mu.Lock()
delete(t.inProgress, typ)
t.tables.Store(typ, table)
t.mu.Unlock()
t.dialect.OnTable(table)
for _, field := range table.FieldMap {