From 607f7156347522cfd3aa6ff8ff093c52e3faf0a0 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 28 Oct 2023 03:30:11 +0000 Subject: [PATCH] Move convert internal (#2355) Reviewed-on: https://gitea.com/xorm/xorm/pulls/2355 --- session_cols.go => columns.go | 0 session_cond.go => conditions.go | 0 session_delete.go => delete.go | 0 dialects/dameng.go | 2 +- error.go | 6 +-- session_find.go => find.go | 2 +- session_get.go => get.go | 29 ++++++++++- session_insert.go => insert.go | 5 +- {convert => internal/convert}/bool.go | 0 {convert => internal/convert}/conversion.go | 0 {convert => internal/convert}/float.go | 0 {convert => internal/convert}/int.go | 0 {convert => internal/convert}/interface.go | 0 internal/convert/kind.go | 49 +++++++++++++++++ {convert => internal/convert}/scanner.go | 0 {convert => internal/convert}/string.go | 0 {convert => internal/convert}/time.go | 0 {convert => internal/convert}/time_test.go | 0 internal/statements/statement.go | 2 +- internal/statements/update.go | 2 +- internal/statements/values.go | 2 +- session_iterate.go => iterate.go | 0 session_raw.go => raw.go | 0 scan.go | 2 +- session_schema.go => schema.go | 0 session.go | 58 +-------------------- session_exist.go | 32 ------------ session_stats.go => stats.go | 0 tags/parser.go | 2 +- tests/processors_test.go | 5 +- tests/session_get_test.go | 2 +- tests/session_raw_test.go | 2 +- tests/time_test.go | 2 +- tests/types_test.go | 2 +- session_tx.go => tx.go | 21 ++++++++ session_update.go => update.go | 0 36 files changed, 121 insertions(+), 106 deletions(-) rename session_cols.go => columns.go (100%) rename session_cond.go => conditions.go (100%) rename session_delete.go => delete.go (100%) rename session_find.go => find.go (99%) rename session_get.go => get.go (91%) rename session_insert.go => insert.go (99%) rename {convert => internal/convert}/bool.go (100%) rename {convert => internal/convert}/conversion.go (100%) rename {convert => internal/convert}/float.go (100%) rename {convert => internal/convert}/int.go (100%) rename {convert => internal/convert}/interface.go (100%) create mode 100644 internal/convert/kind.go rename {convert => internal/convert}/scanner.go (100%) rename {convert => internal/convert}/string.go (100%) rename {convert => internal/convert}/time.go (100%) rename {convert => internal/convert}/time_test.go (100%) rename session_iterate.go => iterate.go (100%) rename session_raw.go => raw.go (100%) rename session_schema.go => schema.go (100%) delete mode 100644 session_exist.go rename session_stats.go => stats.go (100%) rename session_tx.go => tx.go (84%) rename session_update.go => update.go (100%) diff --git a/session_cols.go b/columns.go similarity index 100% rename from session_cols.go rename to columns.go diff --git a/session_cond.go b/conditions.go similarity index 100% rename from session_cond.go rename to conditions.go diff --git a/session_delete.go b/delete.go similarity index 100% rename from session_delete.go rename to delete.go diff --git a/dialects/dameng.go b/dialects/dameng.go index b950059f..9003ef5d 100644 --- a/dialects/dameng.go +++ b/dialects/dameng.go @@ -13,7 +13,7 @@ import ( "strconv" "strings" - "xorm.io/xorm/v2/convert" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/core" "xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/schemas" diff --git a/error.go b/error.go index 93837f49..d171c94a 100644 --- a/error.go +++ b/error.go @@ -9,16 +9,14 @@ import ( ) var ( - // ErrPtrSliceType represents a type error - ErrPtrSliceType = errors.New("A point to a slice is needed") + // ErrParamsType params error ErrParamsType = errors.New("Params type error") // ErrTableNotFound table not found error ErrTableNotFound = errors.New("Table not found") // ErrUnSupportedType unsupported error ErrUnSupportedType = errors.New("Unsupported type error") - // ErrNotExist record does not exist error - ErrNotExist = errors.New("Record does not exist") + // ErrConditionType condition type unsupported ErrConditionType = errors.New("Unsupported condition type") ) diff --git a/session_find.go b/find.go similarity index 99% rename from session_find.go rename to find.go index e1f61426..6355dcfe 100644 --- a/session_find.go +++ b/find.go @@ -12,7 +12,7 @@ import ( "xorm.io/builder" - "xorm.io/xorm/v2/convert" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/schemas" ) diff --git a/session_get.go b/get.go similarity index 91% rename from session_get.go rename to get.go index f16d5f8b..0ec155aa 100644 --- a/session_get.go +++ b/get.go @@ -12,7 +12,7 @@ import ( "reflect" "time" - "xorm.io/xorm/v2/convert" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/core" "xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/schemas" @@ -257,3 +257,30 @@ func (session *Session) getMap(rows *core.Rows, types []*sql.ColumnType, fields return fmt.Errorf("unspoorted map type: %t", t) } } + +// Exist returns true if the record exist otherwise return false +func (session *Session) Exist(bean ...interface{}) (bool, error) { + if session.isAutoClose { + defer session.Close() + } + + if session.statement.LastError != nil { + return false, session.statement.LastError + } + + sqlStr, args, err := session.statement.GenExistSQL(bean...) + if err != nil { + return false, err + } + + rows, err := session.queryRows(sqlStr, args...) + if err != nil { + return false, err + } + defer rows.Close() + + if rows.Next() { + return true, nil + } + return false, rows.Err() +} diff --git a/session_insert.go b/insert.go similarity index 99% rename from session_insert.go rename to insert.go index 3a3b12bf..ce5d81c7 100644 --- a/session_insert.go +++ b/insert.go @@ -13,8 +13,8 @@ import ( "time" "xorm.io/builder" - "xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/dialects" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/schemas" ) @@ -22,6 +22,9 @@ import ( // ErrNoElementsOnSlice represents an error there is no element when insert var ErrNoElementsOnSlice = errors.New("no element on slice when insert") +// ErrPtrSliceType represents a type error +var ErrPtrSliceType = errors.New("A point to a slice is needed") + // Insert insert one or more beans func (session *Session) Insert(beans ...interface{}) (int64, error) { var affected int64 diff --git a/convert/bool.go b/internal/convert/bool.go similarity index 100% rename from convert/bool.go rename to internal/convert/bool.go diff --git a/convert/conversion.go b/internal/convert/conversion.go similarity index 100% rename from convert/conversion.go rename to internal/convert/conversion.go diff --git a/convert/float.go b/internal/convert/float.go similarity index 100% rename from convert/float.go rename to internal/convert/float.go diff --git a/convert/int.go b/internal/convert/int.go similarity index 100% rename from convert/int.go rename to internal/convert/int.go diff --git a/convert/interface.go b/internal/convert/interface.go similarity index 100% rename from convert/interface.go rename to internal/convert/interface.go diff --git a/internal/convert/kind.go b/internal/convert/kind.go new file mode 100644 index 00000000..f870c4e4 --- /dev/null +++ b/internal/convert/kind.go @@ -0,0 +1,49 @@ +// Copyright 2023 The Xorm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package convert + +import ( + "fmt" + "reflect" + "strconv" +) + +func AsKind(vv reflect.Value, tp reflect.Type) (interface{}, error) { + switch tp.Kind() { + case reflect.Ptr: + return AsKind(vv.Elem(), tp.Elem()) + case reflect.Int64: + return vv.Int(), nil + case reflect.Int: + return int(vv.Int()), nil + case reflect.Int32: + return int32(vv.Int()), nil + case reflect.Int16: + return int16(vv.Int()), nil + case reflect.Int8: + return int8(vv.Int()), nil + case reflect.Uint64: + return vv.Uint(), nil + case reflect.Uint: + return uint(vv.Uint()), nil + case reflect.Uint32: + return uint32(vv.Uint()), nil + case reflect.Uint16: + return uint16(vv.Uint()), nil + case reflect.Uint8: + return uint8(vv.Uint()), nil + case reflect.String: + return vv.String(), nil + case reflect.Slice: + if tp.Elem().Kind() == reflect.Uint8 { + v, err := strconv.ParseInt(string(vv.Interface().([]byte)), 10, 64) + if err != nil { + return nil, err + } + return v, nil + } + } + return nil, fmt.Errorf("unsupported primary key type: %v, %v", tp, vv) +} diff --git a/convert/scanner.go b/internal/convert/scanner.go similarity index 100% rename from convert/scanner.go rename to internal/convert/scanner.go diff --git a/convert/string.go b/internal/convert/string.go similarity index 100% rename from convert/string.go rename to internal/convert/string.go diff --git a/convert/time.go b/internal/convert/time.go similarity index 100% rename from convert/time.go rename to internal/convert/time.go diff --git a/convert/time_test.go b/internal/convert/time_test.go similarity index 100% rename from convert/time_test.go rename to internal/convert/time_test.go diff --git a/internal/statements/statement.go b/internal/statements/statement.go index 7bb8f342..f20c503e 100644 --- a/internal/statements/statement.go +++ b/internal/statements/statement.go @@ -15,8 +15,8 @@ import ( "xorm.io/builder" "xorm.io/xorm/v2/contexts" - "xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/dialects" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/json" "xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/schemas" diff --git a/internal/statements/update.go b/internal/statements/update.go index ef9a495d..40ae1067 100644 --- a/internal/statements/update.go +++ b/internal/statements/update.go @@ -12,8 +12,8 @@ import ( "time" "xorm.io/builder" - "xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/dialects" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/json" "xorm.io/xorm/v2/internal/utils" "xorm.io/xorm/v2/schemas" diff --git a/internal/statements/values.go b/internal/statements/values.go index 5f364c6d..150f2454 100644 --- a/internal/statements/values.go +++ b/internal/statements/values.go @@ -12,8 +12,8 @@ import ( "reflect" "time" - "xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/dialects" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/json" "xorm.io/xorm/v2/schemas" ) diff --git a/session_iterate.go b/iterate.go similarity index 100% rename from session_iterate.go rename to iterate.go diff --git a/session_raw.go b/raw.go similarity index 100% rename from session_raw.go rename to raw.go diff --git a/scan.go b/scan.go index 288abc60..d2bac84b 100644 --- a/scan.go +++ b/scan.go @@ -11,8 +11,8 @@ import ( "reflect" "time" - "xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/dialects" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/core" "xorm.io/xorm/v2/schemas" ) diff --git a/session_schema.go b/schema.go similarity index 100% rename from session_schema.go rename to schema.go diff --git a/session.go b/session.go index f9c6abac..29a373bb 100644 --- a/session.go +++ b/session.go @@ -15,10 +15,9 @@ import ( "hash/crc32" "io" "reflect" - "strconv" "xorm.io/xorm/v2/contexts" - "xorm.io/xorm/v2/convert" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/core" "xorm.io/xorm/v2/internal/json" "xorm.io/xorm/v2/internal/statements" @@ -354,21 +353,6 @@ func (session *Session) doPrepare(db *core.DB, sqlStr string) (stmt *core.Stmt, return } -func (session *Session) doPrepareTx(sqlStr string) (stmt *core.Stmt, err error) { - crc := crc32.ChecksumIEEE([]byte(sqlStr)) - // TODO try hash(sqlStr+len(sqlStr)) - var has bool - stmt, has = session.txStmtCache[crc] - if !has { - stmt, err = session.tx.PrepareContext(session.ctx, sqlStr) - if err != nil { - return nil, err - } - session.txStmtCache[crc] = stmt - } - return -} - func getField(dataStruct *reflect.Value, table *schemas.Table, field *QueryedField) (*schemas.Column, *reflect.Value, error) { col := field.ColumnSchema if col == nil { @@ -470,44 +454,6 @@ func setJSON(fieldValue *reflect.Value, fieldType reflect.Type, scanResult inter return nil } -func asKind(vv reflect.Value, tp reflect.Type) (interface{}, error) { - switch tp.Kind() { - case reflect.Ptr: - return asKind(vv.Elem(), tp.Elem()) - case reflect.Int64: - return vv.Int(), nil - case reflect.Int: - return int(vv.Int()), nil - case reflect.Int32: - return int32(vv.Int()), nil - case reflect.Int16: - return int16(vv.Int()), nil - case reflect.Int8: - return int8(vv.Int()), nil - case reflect.Uint64: - return vv.Uint(), nil - case reflect.Uint: - return uint(vv.Uint()), nil - case reflect.Uint32: - return uint32(vv.Uint()), nil - case reflect.Uint16: - return uint16(vv.Uint()), nil - case reflect.Uint8: - return uint8(vv.Uint()), nil - case reflect.String: - return vv.String(), nil - case reflect.Slice: - if tp.Elem().Kind() == reflect.Uint8 { - v, err := strconv.ParseInt(string(vv.Interface().([]byte)), 10, 64) - if err != nil { - return nil, err - } - return v, nil - } - } - return nil, fmt.Errorf("unsupported primary key type: %v, %v", tp, vv) -} - var uint8ZeroValue = reflect.ValueOf(uint8(0)) func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflect.Value, @@ -652,7 +598,7 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec return errors.New("unsupported non or composited primary key cascade") } pk := make(schemas.PK, len(table.PrimaryKeys)) - pk[0], err = asKind(vv, reflect.TypeOf(scanResult)) + pk[0], err = convert.AsKind(vv, reflect.TypeOf(scanResult)) if err != nil { return err } diff --git a/session_exist.go b/session_exist.go deleted file mode 100644 index b5e4a655..00000000 --- a/session_exist.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2017 The Xorm Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package xorm - -// Exist returns true if the record exist otherwise return false -func (session *Session) Exist(bean ...interface{}) (bool, error) { - if session.isAutoClose { - defer session.Close() - } - - if session.statement.LastError != nil { - return false, session.statement.LastError - } - - sqlStr, args, err := session.statement.GenExistSQL(bean...) - if err != nil { - return false, err - } - - rows, err := session.queryRows(sqlStr, args...) - if err != nil { - return false, err - } - defer rows.Close() - - if rows.Next() { - return true, nil - } - return false, rows.Err() -} diff --git a/session_stats.go b/stats.go similarity index 100% rename from session_stats.go rename to stats.go diff --git a/tags/parser.go b/tags/parser.go index 1e787c00..70f255a6 100644 --- a/tags/parser.go +++ b/tags/parser.go @@ -12,8 +12,8 @@ import ( "sync" "unicode" - "xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/dialects" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/names" "xorm.io/xorm/v2/schemas" ) diff --git a/tests/processors_test.go b/tests/processors_test.go index 4f780270..96db2b7d 100644 --- a/tests/processors_test.go +++ b/tests/processors_test.go @@ -832,6 +832,9 @@ type AfterLoadStructB struct { Err error `xorm:"-"` } +// ErrNotExist record does not exist error +var ErrNotExist = errors.New("Record does not exist") + func (s *AfterLoadStructB) AfterLoad(session *xorm.Session) { has, err := session.ID(s.AId).NoAutoCondition().Get(&s.A) if err != nil { @@ -839,7 +842,7 @@ func (s *AfterLoadStructB) AfterLoad(session *xorm.Session) { return } if !has { - s.Err = xorm.ErrNotExist + s.Err = ErrNotExist } } diff --git a/tests/session_get_test.go b/tests/session_get_test.go index 7c521288..d75d95f9 100644 --- a/tests/session_get_test.go +++ b/tests/session_get_test.go @@ -14,8 +14,8 @@ import ( "xorm.io/xorm/v2" "xorm.io/xorm/v2/contexts" - "xorm.io/xorm/v2/convert" "xorm.io/xorm/v2/dialects" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/schemas" "github.com/shopspring/decimal" diff --git a/tests/session_raw_test.go b/tests/session_raw_test.go index f74a4be4..18ea6d59 100644 --- a/tests/session_raw_test.go +++ b/tests/session_raw_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "xorm.io/xorm/v2/convert" + "xorm.io/xorm/v2/internal/convert" "github.com/stretchr/testify/assert" ) diff --git a/tests/time_test.go b/tests/time_test.go index 7e288861..fab79491 100644 --- a/tests/time_test.go +++ b/tests/time_test.go @@ -11,7 +11,7 @@ import ( "testing" "time" - "xorm.io/xorm/v2/convert" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/utils" "github.com/stretchr/testify/assert" diff --git a/tests/types_test.go b/tests/types_test.go index 6b0a9027..17cc9cc4 100644 --- a/tests/types_test.go +++ b/tests/types_test.go @@ -13,7 +13,7 @@ import ( "testing" "xorm.io/xorm/v2" - "xorm.io/xorm/v2/convert" + "xorm.io/xorm/v2/internal/convert" "xorm.io/xorm/v2/internal/json" "xorm.io/xorm/v2/schemas" diff --git a/session_tx.go b/tx.go similarity index 84% rename from session_tx.go rename to tx.go index 91b706d7..ec5cbffd 100644 --- a/session_tx.go +++ b/tx.go @@ -4,6 +4,27 @@ package xorm +import ( + "hash/crc32" + + "xorm.io/xorm/v2/internal/core" +) + +func (session *Session) doPrepareTx(sqlStr string) (stmt *core.Stmt, err error) { + crc := crc32.ChecksumIEEE([]byte(sqlStr)) + // TODO try hash(sqlStr+len(sqlStr)) + var has bool + stmt, has = session.txStmtCache[crc] + if !has { + stmt, err = session.tx.PrepareContext(session.ctx, sqlStr) + if err != nil { + return nil, err + } + session.txStmtCache[crc] = stmt + } + return +} + // Begin a transaction func (session *Session) Begin() error { if session.isAutoCommit { diff --git a/session_update.go b/update.go similarity index 100% rename from session_update.go rename to update.go