From 126c9331d6d8789563fae5d5bac2196d63fee0e8 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 5 Jan 2021 14:28:51 +0800 Subject: [PATCH] upgrade xorm to v1.0.6 (#14246) --- go.mod | 2 +- go.sum | 4 ++-- vendor/modules.txt | 2 +- vendor/xorm.io/xorm/CHANGELOG.md | 14 +++++++++++ vendor/xorm.io/xorm/dialects/dialect.go | 2 +- vendor/xorm.io/xorm/dialects/mssql.go | 5 ++++ vendor/xorm.io/xorm/dialects/sqlite3.go | 2 +- vendor/xorm.io/xorm/engine.go | 21 +++++++++++++--- .../xorm/internal/statements/statement.go | 4 ++-- .../xorm/internal/statements/update.go | 2 +- .../xorm/internal/statements/values.go | 2 +- vendor/xorm.io/xorm/schemas/column.go | 1 + vendor/xorm.io/xorm/schemas/table.go | 7 +----- vendor/xorm.io/xorm/session.go | 4 ++-- vendor/xorm.io/xorm/session_convert.go | 24 ++++++++++++++++++- vendor/xorm.io/xorm/session_find.go | 3 +++ vendor/xorm.io/xorm/session_update.go | 9 ++++++- vendor/xorm.io/xorm/tags/parser.go | 4 +++- vendor/xorm.io/xorm/tags/tag.go | 3 +++ 19 files changed, 91 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 6556f54ed..c28c8d470 100644 --- a/go.mod +++ b/go.mod @@ -122,7 +122,7 @@ require ( mvdan.cc/xurls/v2 v2.2.0 strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 xorm.io/builder v0.3.7 - xorm.io/xorm v1.0.5 + xorm.io/xorm v1.0.6 ) replace github.com/hashicorp/go-version => github.com/6543/go-version v1.2.4 diff --git a/go.sum b/go.sum index 0291b7766..66b8083e0 100644 --- a/go.sum +++ b/go.sum @@ -1612,5 +1612,5 @@ xorm.io/builder v0.3.7/go.mod h1:aUW0S9eb9VCaPohFCH3j7czOx1PMW3i1HrSzbLYGBSE= xorm.io/core v0.7.2 h1:mEO22A2Z7a3fPaZMk6gKL/jMD80iiyNwRrX5HOv3XLw= xorm.io/core v0.7.2/go.mod h1:jJfd0UAEzZ4t87nbQYtVjmqpIODugN6PD2D9E+dJvdM= xorm.io/xorm v0.8.0/go.mod h1:ZkJLEYLoVyg7amJK/5r779bHyzs2AU8f8VMiP6BM7uY= -xorm.io/xorm v1.0.5 h1:LRr5PfOUb4ODPR63YwbowkNDwcolT2LnkwP/TUaMaB0= -xorm.io/xorm v1.0.5/go.mod h1:uF9EtbhODq5kNWxMbnBEj8hRRZnlcNSz2t2N7HW/+A4= +xorm.io/xorm v1.0.6 h1:7eco1c8QUpGz+3dztpLDj9gU1bTiQdFC/KtmPaLxUJk= +xorm.io/xorm v1.0.6/go.mod h1:uF9EtbhODq5kNWxMbnBEj8hRRZnlcNSz2t2N7HW/+A4= diff --git a/vendor/modules.txt b/vendor/modules.txt index 346d0a749..ee584932d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -982,7 +982,7 @@ strk.kbt.io/projects/go/libravatar # xorm.io/builder v0.3.7 ## explicit xorm.io/builder -# xorm.io/xorm v1.0.5 +# xorm.io/xorm v1.0.6 ## explicit xorm.io/xorm xorm.io/xorm/caches diff --git a/vendor/xorm.io/xorm/CHANGELOG.md b/vendor/xorm.io/xorm/CHANGELOG.md index 3cead87d0..32403a1a8 100644 --- a/vendor/xorm.io/xorm/CHANGELOG.md +++ b/vendor/xorm.io/xorm/CHANGELOG.md @@ -3,6 +3,20 @@ This changelog goes through all the changes that have been made in each release without substantial changes to our git log. +## [1.0.6](https://gitea.com/xorm/xorm/pulls?q=&type=all&state=closed&milestone=1308) - 2021-01-05 + +* BUGFIXES + * Fix bug when modify column on mssql (#1849) + * Fix find and count bug with cols (#1826) + * Fix update bug (#1823) + * Fix json tag with other type (#1822) +* ENHANCEMENTS + * prevent panic when struct with unexport field (#1839) + * Automatically convert datetime to int64 (#1715) +* MISC + * Fix index (#1841) + * Performance improvement for columnsbyName (#1788) + ## [1.0.5](https://gitea.com/xorm/xorm/pulls?q=&type=all&state=closed&milestone=1299) - 2020-09-08 * BUGFIXES diff --git a/vendor/xorm.io/xorm/dialects/dialect.go b/vendor/xorm.io/xorm/dialects/dialect.go index dc96f73ae..18b781a15 100644 --- a/vendor/xorm.io/xorm/dialects/dialect.go +++ b/vendor/xorm.io/xorm/dialects/dialect.go @@ -163,7 +163,7 @@ func (db *Base) DropIndexSQL(tableName string, index *schemas.Index) string { func (db *Base) ModifyColumnSQL(tableName string, col *schemas.Column) string { s, _ := ColumnString(db.dialect, col, false) - return fmt.Sprintf("alter table %s MODIFY COLUMN %s", tableName, s) + return fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s", tableName, s) } func (b *Base) ForUpdateSQL(query string) string { diff --git a/vendor/xorm.io/xorm/dialects/mssql.go b/vendor/xorm.io/xorm/dialects/mssql.go index 8e76e5385..5340455dd 100644 --- a/vendor/xorm.io/xorm/dialects/mssql.go +++ b/vendor/xorm.io/xorm/dialects/mssql.go @@ -368,6 +368,11 @@ func (db *mssql) DropTableSQL(tableName string) (string, bool) { "DROP TABLE \"%s\"", tableName, tableName), true } +func (db *mssql) ModifyColumnSQL(tableName string, col *schemas.Column) string { + s, _ := ColumnString(db.dialect, col, false) + return fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s", tableName, s) +} + func (db *mssql) IndexCheckSQL(tableName, idxName string) (string, []interface{}) { args := []interface{}{idxName} sql := "select name from sysindexes where id=object_id('" + tableName + "') and name=?" diff --git a/vendor/xorm.io/xorm/dialects/sqlite3.go b/vendor/xorm.io/xorm/dialects/sqlite3.go index 73f98beb9..62a383976 100644 --- a/vendor/xorm.io/xorm/dialects/sqlite3.go +++ b/vendor/xorm.io/xorm/dialects/sqlite3.go @@ -483,7 +483,7 @@ func (db *sqlite3) GetIndexes(queryer core.Queryer, ctx context.Context, tableNa continue } - indexName := strings.Trim(sql[nNStart+6:nNEnd], "` []'\"") + indexName := strings.Trim(strings.TrimSpace(sql[nNStart+6:nNEnd]), "`[]'\"") var isRegular bool if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) { index.Name = indexName[5+len(tableName):] diff --git a/vendor/xorm.io/xorm/engine.go b/vendor/xorm.io/xorm/engine.go index 4159a7b24..873fcdc1b 100644 --- a/vendor/xorm.io/xorm/engine.go +++ b/vendor/xorm.io/xorm/engine.go @@ -61,6 +61,10 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) { return nil, err } + return newEngine(driverName, dataSourceName, dialect, db) +} + +func newEngine(driverName, dataSourceName string, dialect dialects.Dialect, db *core.DB) (*Engine, error) { cacherMgr := caches.NewManager() mapper := names.NewCacheMapper(new(names.SnakeMapper)) tagParser := tags.NewParser("xorm", dialect, mapper, mapper, cacherMgr) @@ -88,7 +92,7 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) { engine.SetLogger(log.NewLoggerAdapter(logger)) runtime.SetFinalizer(engine, func(engine *Engine) { - engine.Close() + _ = engine.Close() }) return engine, nil @@ -101,6 +105,14 @@ func NewEngineWithParams(driverName string, dataSourceName string, params map[st return engine, err } +// NewEngineWithDialectAndDB new a db manager according to the parameter. +// If you do not want to use your own dialect or db, please use NewEngine. +// For creating dialect, you can call dialects.OpenDialect. And, for creating db, +// you can call core.Open or core.FromDB. +func NewEngineWithDialectAndDB(driverName, dataSourceName string, dialect dialects.Dialect, db *core.DB) (*Engine, error) { + return newEngine(driverName, dataSourceName, dialect, db) +} + // EnableSessionID if enable session id func (engine *Engine) EnableSessionID(enable bool) { engine.logSessionID = enable @@ -347,13 +359,16 @@ func (engine *Engine) loadTableInfo(table *schemas.Table) error { var seq int for _, index := range indexes { for _, name := range index.Cols { - parts := strings.Split(name, " ") + parts := strings.Split(strings.TrimSpace(name), " ") if len(parts) > 1 { if parts[1] == "DESC" { seq = 1 + } else if parts[1] == "ASC" { + seq = 0 } } - if col := table.GetColumn(parts[0]); col != nil { + var colName = strings.Trim(parts[0], `"`) + if col := table.GetColumn(colName); col != nil { col.Indexes[index.Name] = index.Type } else { return fmt.Errorf("Unknown col %s seq %d, in index %v of table %v, columns %v", name, seq, index.Name, table.Name, table.ColumnsSeq()) diff --git a/vendor/xorm.io/xorm/internal/statements/statement.go b/vendor/xorm.io/xorm/internal/statements/statement.go index ed7bdaeb6..a4294bec6 100644 --- a/vendor/xorm.io/xorm/internal/statements/statement.go +++ b/vendor/xorm.io/xorm/internal/statements/statement.go @@ -704,7 +704,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{}, col.SQLType.IsBlob() || col.SQLType.Name == schemas.TimeStampz) { continue } - if col.SQLType.IsJson() { + if col.IsJSON { continue } @@ -813,7 +813,7 @@ func (statement *Statement) buildConds2(table *schemas.Table, bean interface{}, continue } } else { - if col.SQLType.IsJson() { + if col.IsJSON { if col.SQLType.IsText() { bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface()) if err != nil { diff --git a/vendor/xorm.io/xorm/internal/statements/update.go b/vendor/xorm.io/xorm/internal/statements/update.go index ff5f82b73..251880b23 100644 --- a/vendor/xorm.io/xorm/internal/statements/update.go +++ b/vendor/xorm.io/xorm/internal/statements/update.go @@ -204,7 +204,7 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value, continue } } else { - if !col.SQLType.IsJson() { + if !col.IsJSON { table, err := statement.tagParser.ParseWithCache(fieldValue) if err != nil { val = fieldValue.Interface() diff --git a/vendor/xorm.io/xorm/internal/statements/values.go b/vendor/xorm.io/xorm/internal/statements/values.go index 99eaa7669..71327c556 100644 --- a/vendor/xorm.io/xorm/internal/statements/values.go +++ b/vendor/xorm.io/xorm/internal/statements/values.go @@ -86,7 +86,7 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl return t.Float64, nil } - if !col.SQLType.IsJson() { + if !col.IsJSON { // !! 增加支持driver.Valuer接口的结构,如sql.NullString if v, ok := fieldValue.Interface().(driver.Valuer); ok { return v.Value() diff --git a/vendor/xorm.io/xorm/schemas/column.go b/vendor/xorm.io/xorm/schemas/column.go index db66a3a67..4f32afab0 100644 --- a/vendor/xorm.io/xorm/schemas/column.go +++ b/vendor/xorm.io/xorm/schemas/column.go @@ -51,6 +51,7 @@ type Column struct { func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column { return &Column{ Name: name, + IsJSON: sqlType.IsJson(), TableName: "", FieldName: fieldName, SQLType: sqlType, diff --git a/vendor/xorm.io/xorm/schemas/table.go b/vendor/xorm.io/xorm/schemas/table.go index 6c57a7e38..7ca9531f7 100644 --- a/vendor/xorm.io/xorm/schemas/table.go +++ b/vendor/xorm.io/xorm/schemas/table.go @@ -58,12 +58,7 @@ func (table *Table) ColumnsSeq() []string { } func (table *Table) columnsByName(name string) []*Column { - for k, cols := range table.columnsMap { - if strings.EqualFold(k, name) { - return cols - } - } - return nil + return table.columnsMap[strings.ToLower(name)] } // GetColumn returns column according column name, if column not found, return nil diff --git a/vendor/xorm.io/xorm/session.go b/vendor/xorm.io/xorm/session.go index b6ab3eb5b..17abd453a 100644 --- a/vendor/xorm.io/xorm/session.go +++ b/vendor/xorm.io/xorm/session.go @@ -503,7 +503,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b fieldType := fieldValue.Type() hasAssigned := false - if col.SQLType.IsJson() { + if col.IsJSON { var bs []byte if rawValueType.Kind() == reflect.String { bs = []byte(vv.String()) @@ -683,7 +683,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b session.engine.logger.Errorf("sql.Sanner error: %v", err) hasAssigned = false } - } else if col.SQLType.IsJson() { + } else if col.IsJSON { if rawValueType.Kind() == reflect.String { hasAssigned = true x := reflect.New(fieldType) diff --git a/vendor/xorm.io/xorm/session_convert.go b/vendor/xorm.io/xorm/session_convert.go index a68399473..4a4dd8be0 100644 --- a/vendor/xorm.io/xorm/session_convert.go +++ b/vendor/xorm.io/xorm/session_convert.go @@ -168,7 +168,29 @@ func (session *Session) bytes2Value(col *schemas.Column, fieldValue *reflect.Val } else if strings.EqualFold(sdata, "false") { x = 0 } else { - x, err = strconv.ParseInt(sdata, 10, 64) + if col.SQLType.Name == schemas.DateTime { + if len(sdata) == 20 { + t, err := time.Parse("2006-01-02T15:04:05Z", sdata) + if err != nil { + return fmt.Errorf("arg %v as int: %s", key, err.Error()) + } + x = t.Unix() + } else if len(sdata) == 19 { + var parseFormat = "2006-01-02 15:04:05" + if sdata[10] == 'T' { + parseFormat = "2006-01-02T15:04:05" + } + t, err := time.Parse(parseFormat, sdata) + if err != nil { + return fmt.Errorf("arg %v as int: %s", key, err.Error()) + } + x = t.Unix() + } else { + x, err = strconv.ParseInt(sdata, 10, 64) + } + } else { + x, err = strconv.ParseInt(sdata, 10, 64) + } } if err != nil { return fmt.Errorf("arg %v as int: %s", key, err.Error()) diff --git a/vendor/xorm.io/xorm/session_find.go b/vendor/xorm.io/xorm/session_find.go index 642093f22..0daea005d 100644 --- a/vendor/xorm.io/xorm/session_find.go +++ b/vendor/xorm.io/xorm/session_find.go @@ -57,6 +57,9 @@ func (session *Session) FindAndCount(rowsSlicePtr interface{}, condiBean ...inte if session.statement.SelectStr != "" { session.statement.SelectStr = "" } + if len(session.statement.ColumnMap) > 0 { + session.statement.ColumnMap = []string{} + } if session.statement.OrderStr != "" { session.statement.OrderStr = "" } diff --git a/vendor/xorm.io/xorm/session_update.go b/vendor/xorm.io/xorm/session_update.go index 7df8c7525..0adac25e4 100644 --- a/vendor/xorm.io/xorm/session_update.go +++ b/vendor/xorm.io/xorm/session_update.go @@ -273,8 +273,15 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6 k = ct.Elem().Kind() } if k == reflect.Struct { + var refTable = session.statement.RefTable + if refTable == nil { + refTable, err = session.engine.TableInfo(condiBean[0]) + if err != nil { + return 0, err + } + } var err error - autoCond, err = session.statement.BuildConds(session.statement.RefTable, condiBean[0], true, true, false, true, false) + autoCond, err = session.statement.BuildConds(refTable, condiBean[0], true, true, false, true, false) if err != nil { return 0, err } diff --git a/vendor/xorm.io/xorm/tags/parser.go b/vendor/xorm.io/xorm/tags/parser.go index add30a134..a301d1249 100644 --- a/vendor/xorm.io/xorm/tags/parser.go +++ b/vendor/xorm.io/xorm/tags/parser.go @@ -253,7 +253,7 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) { addIndex(indexName, table, col, indexType) } } - } else { + } else if fieldValue.CanSet() { var sqlType schemas.SQLType if fieldValue.CanAddr() { if _, ok := fieldValue.Addr().Interface().(convert.Conversion); ok { @@ -272,6 +272,8 @@ func (parser *Parser) Parse(v reflect.Value) (*schemas.Table, error) { if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) { idFieldColName = col.Name } + } else { + continue } if col.IsAutoIncrement { col.Nullable = false diff --git a/vendor/xorm.io/xorm/tags/tag.go b/vendor/xorm.io/xorm/tags/tag.go index ee3f1e824..bb5b58388 100644 --- a/vendor/xorm.io/xorm/tags/tag.go +++ b/vendor/xorm.io/xorm/tags/tag.go @@ -226,6 +226,9 @@ func CommentTagHandler(ctx *Context) error { // SQLTypeTagHandler describes SQL Type tag handler func SQLTypeTagHandler(ctx *Context) error { ctx.col.SQLType = schemas.SQLType{Name: ctx.tagName} + if strings.EqualFold(ctx.tagName, "JSON") { + ctx.col.IsJSON = true + } if len(ctx.params) > 0 { if ctx.tagName == schemas.Enum { ctx.col.EnumOptions = make(map[string]int)