mirror of
https://github.com/tangpanqing/aorm.git
synced 2025-10-07 08:50:55 +08:00
修改部分转换问题
This commit is contained in:
61
crud.go
61
crud.go
@@ -4,7 +4,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gopkg.in/guregu/null.v4"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -35,11 +34,11 @@ type WhereItem struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IntStruct struct {
|
type IntStruct struct {
|
||||||
C null.Int
|
C Int
|
||||||
}
|
}
|
||||||
|
|
||||||
type FloatStruct struct {
|
type FloatStruct struct {
|
||||||
C null.Float
|
C Float
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert 增加记录
|
// Insert 增加记录
|
||||||
@@ -49,8 +48,7 @@ func (db *Executor) Insert(dest interface{}) (int64, error) {
|
|||||||
|
|
||||||
//如果没有设置表名
|
//如果没有设置表名
|
||||||
if db.TableName == "" {
|
if db.TableName == "" {
|
||||||
arr := strings.Split(typeOf.String(), ".")
|
db.TableName = reflectTableName(typeOf, valueOf)
|
||||||
db.TableName = UnderLine(arr[len(arr)-1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var keys []string
|
var keys []string
|
||||||
@@ -143,22 +141,22 @@ func (db *Executor) GetMapArr() []map[string]interface{} {
|
|||||||
|
|
||||||
func transToNullType(v interface{}, filedType string) reflect.Value {
|
func transToNullType(v interface{}, filedType string) reflect.Value {
|
||||||
x := reflect.ValueOf("")
|
x := reflect.ValueOf("")
|
||||||
if "null.String" == filedType {
|
if "aorm.String" == filedType {
|
||||||
if nil == v {
|
if nil == v {
|
||||||
x = reflect.ValueOf(null.String{})
|
x = reflect.ValueOf(String{})
|
||||||
} else {
|
} else {
|
||||||
x = reflect.ValueOf(null.StringFrom(fmt.Sprintf("%v", v)))
|
x = reflect.ValueOf(StringFrom(fmt.Sprintf("%v", v)))
|
||||||
}
|
}
|
||||||
} else if "null.Int" == filedType {
|
} else if "aorm.Int" == filedType {
|
||||||
if nil == v {
|
if nil == v {
|
||||||
x = reflect.ValueOf(null.Int{})
|
x = reflect.ValueOf(Int{})
|
||||||
} else {
|
} else {
|
||||||
int64Val, _ := strconv.ParseInt(fmt.Sprintf("%v", v), 10, 64)
|
int64Val, _ := strconv.ParseInt(fmt.Sprintf("%v", v), 10, 64)
|
||||||
x = reflect.ValueOf(null.IntFrom(int64Val))
|
x = reflect.ValueOf(IntFrom(int64Val))
|
||||||
}
|
}
|
||||||
} else if "null.Time" == filedType {
|
} else if "aorm.Time" == filedType {
|
||||||
if nil == v {
|
if nil == v {
|
||||||
x = reflect.ValueOf(null.Time{})
|
x = reflect.ValueOf(Time{})
|
||||||
} else {
|
} else {
|
||||||
timeStr := fmt.Sprintf("%v", v)
|
timeStr := fmt.Sprintf("%v", v)
|
||||||
timeArr := strings.Split(timeStr, " ")
|
timeArr := strings.Split(timeStr, " ")
|
||||||
@@ -173,21 +171,21 @@ func transToNullType(v interface{}, filedType string) reflect.Value {
|
|||||||
0,
|
0,
|
||||||
time.Local,
|
time.Local,
|
||||||
)
|
)
|
||||||
x = reflect.ValueOf(null.TimeFrom(a))
|
x = reflect.ValueOf(TimeFrom(a))
|
||||||
}
|
}
|
||||||
} else if "null.Bool" == filedType {
|
} else if "aorm.Bool" == filedType {
|
||||||
if nil == v {
|
if nil == v {
|
||||||
x = reflect.ValueOf(null.Bool{})
|
x = reflect.ValueOf(Bool{})
|
||||||
} else {
|
} else {
|
||||||
boolVal, _ := strconv.ParseBool(fmt.Sprintf("%v", v))
|
boolVal, _ := strconv.ParseBool(fmt.Sprintf("%v", v))
|
||||||
x = reflect.ValueOf(null.BoolFrom(boolVal))
|
x = reflect.ValueOf(BoolFrom(boolVal))
|
||||||
}
|
}
|
||||||
} else if "null.Float" == filedType {
|
} else if "aorm.Float" == filedType {
|
||||||
if nil == v {
|
if nil == v {
|
||||||
x = reflect.ValueOf(null.Float{})
|
x = reflect.ValueOf(Float{})
|
||||||
} else {
|
} else {
|
||||||
float64Val, _ := strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
|
float64Val, _ := strconv.ParseFloat(fmt.Sprintf("%v", v), 64)
|
||||||
x = reflect.ValueOf(null.FloatFrom(float64Val))
|
x = reflect.ValueOf(FloatFrom(float64Val))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
panic("不受支持的类型转换" + filedType)
|
panic("不受支持的类型转换" + filedType)
|
||||||
@@ -470,8 +468,7 @@ func (db *Executor) Where(dest interface{}) *Executor {
|
|||||||
|
|
||||||
//如果没有设置表名
|
//如果没有设置表名
|
||||||
if db.TableName == "" {
|
if db.TableName == "" {
|
||||||
arr := strings.Split(typeOf.String(), ".")
|
db.TableName = reflectTableName(typeOf, valueOf)
|
||||||
db.TableName = UnderLine(arr[len(arr)-1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < typeOf.Elem().NumField(); i++ {
|
for i := 0; i < typeOf.Elem().NumField(); i++ {
|
||||||
@@ -505,8 +502,7 @@ func (db *Executor) Having(dest interface{}) *Executor {
|
|||||||
|
|
||||||
//如果没有设置表名
|
//如果没有设置表名
|
||||||
if db.TableName == "" {
|
if db.TableName == "" {
|
||||||
arr := strings.Split(typeOf.String(), ".")
|
db.TableName = reflectTableName(typeOf, valueOf)
|
||||||
db.TableName = UnderLine(arr[len(arr)-1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < typeOf.Elem().NumField(); i++ {
|
for i := 0; i < typeOf.Elem().NumField(); i++ {
|
||||||
@@ -580,8 +576,7 @@ func (db *Executor) handleSet(dest interface{}, paramList []any) (string, []any)
|
|||||||
|
|
||||||
//如果没有设置表名
|
//如果没有设置表名
|
||||||
if db.TableName == "" {
|
if db.TableName == "" {
|
||||||
arr := strings.Split(typeOf.String(), ".")
|
db.TableName = reflectTableName(typeOf, valueOf)
|
||||||
db.TableName = UnderLine(arr[len(arr)-1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var keys []string
|
var keys []string
|
||||||
@@ -759,3 +754,17 @@ func str2Int64(str string) int64 {
|
|||||||
}
|
}
|
||||||
return dataNew
|
return dataNew
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//反射表名,优先从方法获取,没有方法则从名字获取
|
||||||
|
func reflectTableName(typeOf reflect.Type, valueOf reflect.Value) string {
|
||||||
|
method, isSet := typeOf.MethodByName("TableName")
|
||||||
|
if isSet {
|
||||||
|
var paramList []reflect.Value
|
||||||
|
paramList = append(paramList, valueOf)
|
||||||
|
res := method.Func.Call(paramList)
|
||||||
|
return res[0].String()
|
||||||
|
} else {
|
||||||
|
arr := strings.Split(typeOf.String(), ".")
|
||||||
|
return UnderLine(arr[len(arr)-1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
2
go.mod
2
go.mod
@@ -4,4 +4,4 @@ go 1.18
|
|||||||
|
|
||||||
require gopkg.in/guregu/null.v4 v4.0.0
|
require gopkg.in/guregu/null.v4 v4.0.0
|
||||||
|
|
||||||
require github.com/go-sql-driver/mysql v1.7.0 // indirect
|
require github.com/go-sql-driver/mysql v1.7.0
|
||||||
|
250
null.go
Normal file
250
null.go
Normal file
@@ -0,0 +1,250 @@
|
|||||||
|
package aorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Int 整数
|
||||||
|
type Int struct {
|
||||||
|
sql.NullInt64
|
||||||
|
}
|
||||||
|
|
||||||
|
// IntFrom 创建整数
|
||||||
|
func IntFrom(i int64) Int {
|
||||||
|
return Int{
|
||||||
|
NullInt64: sql.NullInt64{
|
||||||
|
Int64: i,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// String 字符串
|
||||||
|
type String struct {
|
||||||
|
sql.NullString
|
||||||
|
}
|
||||||
|
|
||||||
|
// StringFrom 创建字符串
|
||||||
|
func StringFrom(s string) String {
|
||||||
|
return String{
|
||||||
|
NullString: sql.NullString{
|
||||||
|
String: s,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float 浮点数
|
||||||
|
type Float struct {
|
||||||
|
sql.NullFloat64
|
||||||
|
}
|
||||||
|
|
||||||
|
// FloatFrom 创建浮点数
|
||||||
|
func FloatFrom(f float64) Float {
|
||||||
|
return Float{
|
||||||
|
NullFloat64: sql.NullFloat64{
|
||||||
|
Float64: f,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bool 布尔值
|
||||||
|
type Bool struct {
|
||||||
|
sql.NullBool
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoolFrom 创建布尔值
|
||||||
|
func BoolFrom(b bool) Bool {
|
||||||
|
return Bool{
|
||||||
|
NullBool: sql.NullBool{
|
||||||
|
Bool: b,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time 时间
|
||||||
|
type Time struct {
|
||||||
|
sql.NullTime
|
||||||
|
}
|
||||||
|
|
||||||
|
// TimeFrom 创建时间
|
||||||
|
func TimeFrom(t time.Time) Time {
|
||||||
|
return Time{
|
||||||
|
NullTime: sql.NullTime{
|
||||||
|
Time: t,
|
||||||
|
Valid: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var nullBytes = []byte("null")
|
||||||
|
|
||||||
|
// UnmarshalJSON 反序列化浮点数
|
||||||
|
func (f *Float) UnmarshalJSON(data []byte) error {
|
||||||
|
if bytes.Equal(data, nullBytes) {
|
||||||
|
f.Valid = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &f.Float64); err != nil {
|
||||||
|
var typeError *json.UnmarshalTypeError
|
||||||
|
if errors.As(err, &typeError) {
|
||||||
|
// special case: accept string input
|
||||||
|
if typeError.Value != "string" {
|
||||||
|
return fmt.Errorf("null: JSON input is invalid type (need float or string): %w", err)
|
||||||
|
}
|
||||||
|
var str string
|
||||||
|
if err := json.Unmarshal(data, &str); err != nil {
|
||||||
|
return fmt.Errorf("null: couldn't unmarshal number string: %w", err)
|
||||||
|
}
|
||||||
|
n, err := strconv.ParseFloat(str, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("null: couldn't convert string to float: %w", err)
|
||||||
|
}
|
||||||
|
f.Float64 = n
|
||||||
|
f.Valid = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("null: couldn't unmarshal JSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Valid = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON 序列化浮点数
|
||||||
|
func (f Float) MarshalJSON() ([]byte, error) {
|
||||||
|
if !f.Valid {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
if math.IsInf(f.Float64, 0) || math.IsNaN(f.Float64) {
|
||||||
|
return nil, &json.UnsupportedValueError{
|
||||||
|
Value: reflect.ValueOf(f.Float64),
|
||||||
|
Str: strconv.FormatFloat(f.Float64, 'g', -1, 64),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []byte(strconv.FormatFloat(f.Float64, 'f', -1, 64)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON 反序列化布尔值
|
||||||
|
func (b *Bool) UnmarshalJSON(data []byte) error {
|
||||||
|
if bytes.Equal(data, nullBytes) {
|
||||||
|
b.Valid = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &b.Bool); err != nil {
|
||||||
|
return fmt.Errorf("null: couldn't unmarshal JSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Valid = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON 序列化布尔值
|
||||||
|
func (b Bool) MarshalJSON() ([]byte, error) {
|
||||||
|
if !b.Valid {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
if !b.Bool {
|
||||||
|
return []byte("false"), nil
|
||||||
|
}
|
||||||
|
return []byte("true"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON 反序列化时间
|
||||||
|
func (t *Time) UnmarshalJSON(data []byte) error {
|
||||||
|
if bytes.Equal(data, nullBytes) {
|
||||||
|
t.Valid = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &t.Time); err != nil {
|
||||||
|
return fmt.Errorf("null: couldn't unmarshal JSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Valid = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON 序列化时间
|
||||||
|
func (t Time) MarshalJSON() ([]byte, error) {
|
||||||
|
if !t.Valid {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
return t.Time.MarshalJSON()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON 反序列化整数
|
||||||
|
func (i *Int) UnmarshalJSON(data []byte) error {
|
||||||
|
if bytes.Equal(data, nullBytes) {
|
||||||
|
i.Valid = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &i.Int64); err != nil {
|
||||||
|
var typeError *json.UnmarshalTypeError
|
||||||
|
if errors.As(err, &typeError) {
|
||||||
|
// special case: accept string input
|
||||||
|
if typeError.Value != "string" {
|
||||||
|
return fmt.Errorf("null: JSON input is invalid type (need int or string): %w", err)
|
||||||
|
}
|
||||||
|
var str string
|
||||||
|
if err := json.Unmarshal(data, &str); err != nil {
|
||||||
|
return fmt.Errorf("null: couldn't unmarshal number string: %w", err)
|
||||||
|
}
|
||||||
|
n, err := strconv.ParseInt(str, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("null: couldn't convert string to int: %w", err)
|
||||||
|
}
|
||||||
|
i.Int64 = n
|
||||||
|
i.Valid = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("null: couldn't unmarshal JSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
i.Valid = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON 序列化整数
|
||||||
|
func (i Int) MarshalJSON() ([]byte, error) {
|
||||||
|
if !i.Valid {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
return []byte(strconv.FormatInt(i.Int64, 10)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON 反序列化字符串
|
||||||
|
func (s *String) UnmarshalJSON(data []byte) error {
|
||||||
|
if bytes.Equal(data, nullBytes) {
|
||||||
|
s.Valid = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(data, &s.String); err != nil {
|
||||||
|
return fmt.Errorf("null: couldn't unmarshal JSON: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Valid = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON 序列化字符串
|
||||||
|
func (s String) MarshalJSON() ([]byte, error) {
|
||||||
|
if !s.Valid {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
return json.Marshal(s.String)
|
||||||
|
}
|
@@ -5,43 +5,43 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/tangpanqing/aorm"
|
"github.com/tangpanqing/aorm"
|
||||||
"gopkg.in/guregu/null.v4"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Article struct {
|
type Article struct {
|
||||||
Id null.Int `aorm:"primary;auto_increment;type:bigint" json:"id"`
|
Id aorm.Int `aorm:"primary;auto_increment;type:bigint" json:"id"`
|
||||||
Type null.Int `aorm:"index;comment:类型" json:"type"`
|
Type aorm.Int `aorm:"index;comment:类型" json:"type"`
|
||||||
PersonId null.Int `aorm:"comment:人员Id" json:"personId"`
|
PersonId aorm.Int `aorm:"comment:人员Id" json:"personId"`
|
||||||
ArticleBody null.String `aorm:"type:text;comment:文章内容" json:"articleBody"`
|
ArticleBody aorm.String `aorm:"type:text;comment:文章内容" json:"articleBody"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ArticleVO struct {
|
type ArticleVO struct {
|
||||||
Id null.Int `aorm:"primary;auto_increment;type:bigint" json:"id"`
|
Id aorm.Int `aorm:"primary;auto_increment;type:bigint" json:"id"`
|
||||||
Type null.Int `aorm:"index;comment:类型" json:"type"`
|
Type aorm.Int `aorm:"index;comment:类型" json:"type"`
|
||||||
PersonId null.Int `aorm:"comment:人员Id" json:"personId"`
|
PersonId aorm.Int `aorm:"comment:人员Id" json:"personId"`
|
||||||
PersonName null.Int `aorm:"comment:人员名称" json:"personName"`
|
PersonName aorm.Int `aorm:"comment:人员名称" json:"personName"`
|
||||||
ArticleBody null.String `aorm:"type:text;comment:文章内容" json:"articleBody"`
|
ArticleBody aorm.String `aorm:"type:text;comment:文章内容" json:"articleBody"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Id null.Int `aorm:"primary;auto_increment;type:bigint" json:"id"`
|
Id aorm.Int `aorm:"primary;auto_increment;type:bigint" json:"id"`
|
||||||
Name null.String `aorm:"size:100;not null;comment:名字" json:"name"`
|
Name aorm.String `aorm:"size:100;not null;comment:名字" json:"name"`
|
||||||
Sex null.Bool `aorm:"index;comment:性别" json:"sex"`
|
Sex aorm.Bool `aorm:"index;comment:性别" json:"sex"`
|
||||||
Age null.Int `aorm:"index;comment:年龄" json:"age"`
|
Age aorm.Int `aorm:"index;comment:年龄" json:"age"`
|
||||||
Type null.Int `aorm:"index;comment:类型" json:"type"`
|
Type aorm.Int `aorm:"index;comment:类型" json:"type"`
|
||||||
CreateTime null.Time `aorm:"comment:创建时间" json:"createTime"`
|
CreateTime aorm.Time `aorm:"comment:创建时间" json:"createTime"`
|
||||||
Money null.Float `aorm:"comment:金额" json:"money"`
|
Money aorm.Float `aorm:"comment:金额" json:"money"`
|
||||||
Test null.Float `aorm:"type:double;comment:测试" json:"test"`
|
Test aorm.Float `aorm:"type:double;comment:测试" json:"test"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PersonAge struct {
|
type PersonAge struct {
|
||||||
Age null.Int
|
Age aorm.Int
|
||||||
AgeCount null.Int
|
AgeCount aorm.Int
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAll(t *testing.T) {
|
func TestAll(t *testing.T) {
|
||||||
|
|
||||||
db := testConnect()
|
db := testConnect()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
@@ -49,6 +49,9 @@ func TestAll(t *testing.T) {
|
|||||||
testShowCreateTable(db)
|
testShowCreateTable(db)
|
||||||
|
|
||||||
id := testInsert(db)
|
id := testInsert(db)
|
||||||
|
|
||||||
|
fmt.Println(id)
|
||||||
|
return
|
||||||
testGetOne(db, id)
|
testGetOne(db, id)
|
||||||
testGetMany(db)
|
testGetMany(db)
|
||||||
testUpdate(db, id)
|
testUpdate(db, id)
|
||||||
@@ -83,7 +86,6 @@ func TestAll(t *testing.T) {
|
|||||||
testExec(db)
|
testExec(db)
|
||||||
|
|
||||||
testTransaction(db)
|
testTransaction(db)
|
||||||
|
|
||||||
testTruncate(db)
|
testTruncate(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,21 +119,21 @@ func testMigrate(db *sql.DB) {
|
|||||||
func testShowCreateTable(db *sql.DB) {
|
func testShowCreateTable(db *sql.DB) {
|
||||||
fmt.Println("--- testShowCreateTable ---")
|
fmt.Println("--- testShowCreateTable ---")
|
||||||
|
|
||||||
showCreate := aorm.Use(db).ShowCreateTable("person")
|
//showCreate := aorm.Use(db).ShowCreateTable("person")
|
||||||
fmt.Println(showCreate)
|
//fmt.Println(showCreate)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testInsert(db *sql.DB) int64 {
|
func testInsert(db *sql.DB) int64 {
|
||||||
fmt.Println("--- testInsert ---")
|
fmt.Println("--- testInsert ---")
|
||||||
|
|
||||||
id, errInsert := aorm.Use(db).Debug(true).Insert(&Person{
|
id, errInsert := aorm.Use(db).Debug(true).Insert(&Person{
|
||||||
Name: null.StringFrom("Alice"),
|
Name: aorm.StringFrom("Alice"),
|
||||||
Sex: null.BoolFrom(false),
|
Sex: aorm.BoolFrom(false),
|
||||||
Age: null.IntFrom(18),
|
Age: aorm.IntFrom(18),
|
||||||
Type: null.IntFrom(0),
|
Type: aorm.IntFrom(0),
|
||||||
CreateTime: null.TimeFrom(time.Now()),
|
CreateTime: aorm.TimeFrom(time.Now()),
|
||||||
Money: null.FloatFrom(100.15987654321),
|
Money: aorm.FloatFrom(100.15987654321),
|
||||||
Test: null.FloatFrom(200.15987654321987654321),
|
Test: aorm.FloatFrom(200.15987654321987654321),
|
||||||
})
|
})
|
||||||
if errInsert != nil {
|
if errInsert != nil {
|
||||||
fmt.Println(errInsert)
|
fmt.Println(errInsert)
|
||||||
@@ -145,7 +147,7 @@ func testGetOne(db *sql.DB, id int64) {
|
|||||||
fmt.Println("--- testGetOne ---")
|
fmt.Println("--- testGetOne ---")
|
||||||
|
|
||||||
var person Person
|
var person Person
|
||||||
errFind := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).GetOne(&person)
|
errFind := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).GetOne(&person)
|
||||||
if errFind != nil {
|
if errFind != nil {
|
||||||
fmt.Println(errFind)
|
fmt.Println(errFind)
|
||||||
}
|
}
|
||||||
@@ -156,7 +158,7 @@ func testGetMany(db *sql.DB) {
|
|||||||
fmt.Println("--- testGetMany ---")
|
fmt.Println("--- testGetMany ---")
|
||||||
|
|
||||||
var list []Person
|
var list []Person
|
||||||
errSelect := aorm.Use(db).Debug(true).Where(&Person{Type: null.IntFrom(0)}).GetMany(&list)
|
errSelect := aorm.Use(db).Debug(true).Where(&Person{Type: aorm.IntFrom(0)}).GetMany(&list)
|
||||||
if errSelect != nil {
|
if errSelect != nil {
|
||||||
fmt.Println(errSelect)
|
fmt.Println(errSelect)
|
||||||
}
|
}
|
||||||
@@ -168,7 +170,7 @@ func testGetMany(db *sql.DB) {
|
|||||||
func testUpdate(db *sql.DB, id int64) {
|
func testUpdate(db *sql.DB, id int64) {
|
||||||
fmt.Println("--- testUpdate ---")
|
fmt.Println("--- testUpdate ---")
|
||||||
|
|
||||||
countUpdate, errUpdate := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).Update(&Person{Name: null.StringFrom("Bob")})
|
countUpdate, errUpdate := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).Update(&Person{Name: aorm.StringFrom("Bob")})
|
||||||
if errUpdate != nil {
|
if errUpdate != nil {
|
||||||
fmt.Println(errUpdate)
|
fmt.Println(errUpdate)
|
||||||
}
|
}
|
||||||
@@ -178,7 +180,7 @@ func testUpdate(db *sql.DB, id int64) {
|
|||||||
func testDelete(db *sql.DB, id int64) {
|
func testDelete(db *sql.DB, id int64) {
|
||||||
fmt.Println("--- testDelete ---")
|
fmt.Println("--- testDelete ---")
|
||||||
|
|
||||||
countDelete, errDelete := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).Delete()
|
countDelete, errDelete := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).Delete()
|
||||||
if errDelete != nil {
|
if errDelete != nil {
|
||||||
fmt.Println(errDelete)
|
fmt.Println(errDelete)
|
||||||
}
|
}
|
||||||
@@ -188,14 +190,14 @@ func testDelete(db *sql.DB, id int64) {
|
|||||||
func testTable(db *sql.DB) {
|
func testTable(db *sql.DB) {
|
||||||
fmt.Println("--- testTable ---")
|
fmt.Println("--- testTable ---")
|
||||||
|
|
||||||
aorm.Use(db).Debug(true).Table("person_1").Insert(&Person{Name: null.StringFrom("Cherry")})
|
aorm.Use(db).Debug(true).Table("person_1").Insert(&Person{Name: aorm.StringFrom("Cherry")})
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSelect(db *sql.DB) {
|
func testSelect(db *sql.DB) {
|
||||||
fmt.Println("--- testSelect ---")
|
fmt.Println("--- testSelect ---")
|
||||||
|
|
||||||
var listByFiled []Person
|
var listByFiled []Person
|
||||||
aorm.Use(db).Debug(true).Select("name,age").Where(&Person{Age: null.IntFrom(18)}).GetMany(&listByFiled)
|
aorm.Use(db).Debug(true).Select("name,age").Where(&Person{Age: aorm.IntFrom(18)}).GetMany(&listByFiled)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testWhere(db *sql.DB) {
|
func testWhere(db *sql.DB) {
|
||||||
@@ -211,7 +213,7 @@ func testWhere(db *sql.DB) {
|
|||||||
where1 = append(where1, aorm.WhereItem{Field: "money", Opt: aorm.Eq, Val: 100.15})
|
where1 = append(where1, aorm.WhereItem{Field: "money", Opt: aorm.Eq, Val: 100.15})
|
||||||
where1 = append(where1, aorm.WhereItem{Field: "name", Opt: aorm.Like, Val: []string{"%", "li", "%"}})
|
where1 = append(where1, aorm.WhereItem{Field: "name", Opt: aorm.Like, Val: []string{"%", "li", "%"}})
|
||||||
|
|
||||||
aorm.Use(db).Debug(true).Table("person").WhereArr(where1).GetMany(&listByWhere)
|
aorm.Use(db).Debug(true).Table("person").Where(where1).GetMany(&listByWhere)
|
||||||
for i := 0; i < len(listByWhere); i++ {
|
for i := 0; i < len(listByWhere); i++ {
|
||||||
fmt.Println(listByWhere[i])
|
fmt.Println(listByWhere[i])
|
||||||
}
|
}
|
||||||
@@ -325,7 +327,7 @@ func testLock(db *sql.DB, id int64) {
|
|||||||
fmt.Println("--- testLock ---")
|
fmt.Println("--- testLock ---")
|
||||||
|
|
||||||
var itemByLock Person
|
var itemByLock Person
|
||||||
err := aorm.Use(db).Debug(true).LockForUpdate(true).Where(&Person{Id: null.IntFrom(id)}).GetOne(&itemByLock)
|
err := aorm.Use(db).Debug(true).LockForUpdate(true).Where(&Person{Id: aorm.IntFrom(id)}).GetOne(&itemByLock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -335,7 +337,7 @@ func testLock(db *sql.DB, id int64) {
|
|||||||
func testIncrement(db *sql.DB, id int64) {
|
func testIncrement(db *sql.DB, id int64) {
|
||||||
fmt.Println("--- testIncrement ---")
|
fmt.Println("--- testIncrement ---")
|
||||||
|
|
||||||
count, err := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).Increment("age", 1)
|
count, err := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).Increment("age", 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -345,7 +347,7 @@ func testIncrement(db *sql.DB, id int64) {
|
|||||||
func testDecrement(db *sql.DB, id int64) {
|
func testDecrement(db *sql.DB, id int64) {
|
||||||
fmt.Println("--- testDecrement ---")
|
fmt.Println("--- testDecrement ---")
|
||||||
|
|
||||||
count, err := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).Decrement("age", 2)
|
count, err := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).Decrement("age", 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -355,7 +357,7 @@ func testDecrement(db *sql.DB, id int64) {
|
|||||||
func testValue(db *sql.DB, id int64) {
|
func testValue(db *sql.DB, id int64) {
|
||||||
fmt.Println("--- testValue ---")
|
fmt.Println("--- testValue ---")
|
||||||
|
|
||||||
name, err := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).Value("name")
|
name, err := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).Value("name")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -365,7 +367,7 @@ func testValue(db *sql.DB, id int64) {
|
|||||||
func testValueInt64(db *sql.DB, id int64) {
|
func testValueInt64(db *sql.DB, id int64) {
|
||||||
fmt.Println("--- testValueInt64 ---")
|
fmt.Println("--- testValueInt64 ---")
|
||||||
|
|
||||||
age, err := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).ValueInt64("age")
|
age, err := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).ValueInt64("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -375,7 +377,7 @@ func testValueInt64(db *sql.DB, id int64) {
|
|||||||
func testValueFloat32(db *sql.DB, id int64) {
|
func testValueFloat32(db *sql.DB, id int64) {
|
||||||
fmt.Println("--- testValueFloat32 ---")
|
fmt.Println("--- testValueFloat32 ---")
|
||||||
|
|
||||||
money, err := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).ValueFloat32("money")
|
money, err := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).ValueFloat32("money")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -385,7 +387,7 @@ func testValueFloat32(db *sql.DB, id int64) {
|
|||||||
func testValueFloat64(db *sql.DB, id int64) {
|
func testValueFloat64(db *sql.DB, id int64) {
|
||||||
fmt.Println("--- testValueFloat64 ---")
|
fmt.Println("--- testValueFloat64 ---")
|
||||||
|
|
||||||
money, err := aorm.Use(db).Debug(true).Where(&Person{Id: null.IntFrom(id)}).ValueFloat64("test")
|
money, err := aorm.Use(db).Debug(true).Where(&Person{Id: aorm.IntFrom(id)}).ValueFloat64("test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -395,7 +397,7 @@ func testValueFloat64(db *sql.DB, id int64) {
|
|||||||
func testCount(db *sql.DB) {
|
func testCount(db *sql.DB) {
|
||||||
fmt.Println("--- testCount ---")
|
fmt.Println("--- testCount ---")
|
||||||
|
|
||||||
count, err := aorm.Use(db).Debug(true).Where(&Person{Age: null.IntFrom(18)}).Count("*")
|
count, err := aorm.Use(db).Debug(true).Where(&Person{Age: aorm.IntFrom(18)}).Count("*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -405,7 +407,7 @@ func testCount(db *sql.DB) {
|
|||||||
func testSum(db *sql.DB) {
|
func testSum(db *sql.DB) {
|
||||||
fmt.Println("--- testSum ---")
|
fmt.Println("--- testSum ---")
|
||||||
|
|
||||||
sum, err := aorm.Use(db).Debug(true).Where(&Person{Age: null.IntFrom(18)}).Sum("age")
|
sum, err := aorm.Use(db).Debug(true).Where(&Person{Age: aorm.IntFrom(18)}).Sum("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -415,7 +417,7 @@ func testSum(db *sql.DB) {
|
|||||||
func testAvg(db *sql.DB) {
|
func testAvg(db *sql.DB) {
|
||||||
fmt.Println("--- testAvg ---")
|
fmt.Println("--- testAvg ---")
|
||||||
|
|
||||||
avg, err := aorm.Use(db).Debug(true).Where(&Person{Age: null.IntFrom(18)}).Avg("age")
|
avg, err := aorm.Use(db).Debug(true).Where(&Person{Age: aorm.IntFrom(18)}).Avg("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -425,7 +427,7 @@ func testAvg(db *sql.DB) {
|
|||||||
func testMin(db *sql.DB) {
|
func testMin(db *sql.DB) {
|
||||||
fmt.Println("--- testMin ---")
|
fmt.Println("--- testMin ---")
|
||||||
|
|
||||||
min, err := aorm.Use(db).Debug(true).Where(&Person{Age: null.IntFrom(18)}).Min("age")
|
min, err := aorm.Use(db).Debug(true).Where(&Person{Age: aorm.IntFrom(18)}).Min("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -435,7 +437,7 @@ func testMin(db *sql.DB) {
|
|||||||
func testMax(db *sql.DB) {
|
func testMax(db *sql.DB) {
|
||||||
fmt.Println("--- testMax ---")
|
fmt.Println("--- testMax ---")
|
||||||
|
|
||||||
max, err := aorm.Use(db).Debug(true).Where(&Person{Age: null.IntFrom(18)}).Max("age")
|
max, err := aorm.Use(db).Debug(true).Where(&Person{Age: aorm.IntFrom(18)}).Max("age")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -445,7 +447,7 @@ func testMax(db *sql.DB) {
|
|||||||
func testQuery(db *sql.DB) {
|
func testQuery(db *sql.DB) {
|
||||||
fmt.Println("--- testQuery ---")
|
fmt.Println("--- testQuery ---")
|
||||||
|
|
||||||
resQuery, err := aorm.Use(db).Query("SELECT * FROM person WHERE id=? AND type=?", 1, 3)
|
resQuery, err := aorm.Use(db).Debug(true).Query("SELECT * FROM person WHERE id=? AND type=?", 1, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -456,7 +458,7 @@ func testQuery(db *sql.DB) {
|
|||||||
func testExec(db *sql.DB) {
|
func testExec(db *sql.DB) {
|
||||||
fmt.Println("--- testExec ---")
|
fmt.Println("--- testExec ---")
|
||||||
|
|
||||||
resExec, err := aorm.Use(db).Exec("UPDATE person SET name = ? WHERE id=?", "Bob", 3)
|
resExec, err := aorm.Use(db).Debug(true).Exec("UPDATE person SET name = ? WHERE id=?", "Bob", 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -469,7 +471,7 @@ func testTransaction(db *sql.DB) {
|
|||||||
tx, _ := db.Begin()
|
tx, _ := db.Begin()
|
||||||
|
|
||||||
id, errInsert := aorm.Use(tx).Insert(&Person{
|
id, errInsert := aorm.Use(tx).Insert(&Person{
|
||||||
Name: null.StringFrom("Alice"),
|
Name: aorm.StringFrom("Alice"),
|
||||||
})
|
})
|
||||||
|
|
||||||
if errInsert != nil {
|
if errInsert != nil {
|
||||||
@@ -479,9 +481,9 @@ func testTransaction(db *sql.DB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
countUpdate, errUpdate := aorm.Use(tx).Where(&Person{
|
countUpdate, errUpdate := aorm.Use(tx).Where(&Person{
|
||||||
Id: null.IntFrom(id),
|
Id: aorm.IntFrom(id),
|
||||||
}).Update(&Person{
|
}).Update(&Person{
|
||||||
Name: null.StringFrom("Bob"),
|
Name: aorm.StringFrom("Bob"),
|
||||||
})
|
})
|
||||||
|
|
||||||
if errUpdate != nil {
|
if errUpdate != nil {
|
||||||
|
Reference in New Issue
Block a user