Move convert internal (#2355)
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2355
This commit is contained in:
parent
af9edecff1
commit
607f715634
|
@ -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"
|
||||
|
|
6
error.go
6
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")
|
||||
)
|
||||
|
|
|
@ -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"
|
||||
)
|
|
@ -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()
|
||||
}
|
|
@ -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
|
|
@ -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)
|
||||
}
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
2
scan.go
2
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"
|
||||
)
|
||||
|
|
58
session.go
58
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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm/v2/convert"
|
||||
"xorm.io/xorm/v2/internal/convert"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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 {
|
Loading…
Reference in New Issue