2021-01-04 19:46:46 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-07-17 23:24:27 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2022-05-10 15:38:08 +08:00
|
|
|
package mysql_test
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
import (
|
2021-09-28 22:02:08 +08:00
|
|
|
"bytes"
|
2021-05-19 21:11:51 +08:00
|
|
|
"context"
|
2019-07-17 23:24:27 +08:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2021-09-28 22:02:08 +08:00
|
|
|
"os"
|
2024-09-28 12:06:22 +08:00
|
|
|
"strings"
|
2021-05-19 21:11:51 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/garray"
|
|
|
|
"github.com/gogf/gf/v2/container/gmap"
|
2022-05-10 15:38:08 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
2022-07-07 21:16:26 +08:00
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2022-03-31 21:40:28 +08:00
|
|
|
"github.com/gogf/gf/v2/util/guid"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gutil"
|
2019-07-17 23:24:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Model_Insert(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2022-01-01 15:47:17 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
user := db.Model(table)
|
2021-05-11 20:14:06 +08:00
|
|
|
result, err := user.Data(g.Map{
|
2019-07-17 23:24:27 +08:00
|
|
|
"id": 1,
|
|
|
|
"uid": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
}).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.LastInsertId()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-05-11 20:14:06 +08:00
|
|
|
result, err = db.Model(table).Data(g.Map{
|
2019-07-17 23:24:27 +08:00
|
|
|
"id": "2",
|
|
|
|
"uid": "2",
|
|
|
|
"passport": "t2",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_2",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
}).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ = result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
type User struct {
|
2020-08-31 00:39:12 +08:00
|
|
|
Id int `gconv:"id"`
|
|
|
|
Uid int `gconv:"uid"`
|
|
|
|
Passport string `json:"passport"`
|
|
|
|
Password string `gconv:"password"`
|
|
|
|
Nickname string `gconv:"nickname"`
|
|
|
|
CreateTime *gtime.Time `json:"create_time"`
|
2019-07-17 23:24:27 +08:00
|
|
|
}
|
2020-08-31 00:39:12 +08:00
|
|
|
// Model inserting.
|
2021-05-11 20:14:06 +08:00
|
|
|
result, err = db.Model(table).Data(User{
|
2020-08-31 00:39:12 +08:00
|
|
|
Id: 3,
|
|
|
|
Uid: 3,
|
|
|
|
Passport: "t3",
|
|
|
|
Password: "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
Nickname: "name_3",
|
2019-07-17 23:24:27 +08:00
|
|
|
}).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ = result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err := db.Model(table).Fields("passport").Where("id=3").Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(value.String(), "t3")
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-05-11 20:14:06 +08:00
|
|
|
result, err = db.Model(table).Data(&User{
|
2019-07-17 23:24:27 +08:00
|
|
|
Id: 4,
|
|
|
|
Uid: 4,
|
|
|
|
Passport: "t4",
|
|
|
|
Password: "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
Nickname: "T4",
|
2020-08-31 00:39:12 +08:00
|
|
|
CreateTime: gtime.Now(),
|
2019-07-17 23:24:27 +08:00
|
|
|
}).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ = result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err = db.Model(table).Fields("passport").Where("id=4").Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(value.String(), "t4")
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err = db.Model(table).Where("id>?", 1).Delete()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ = result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-09-13 11:21:10 +08:00
|
|
|
}
|
|
|
|
|
2020-10-17 18:16:13 +08:00
|
|
|
// Fix issue: https://github.com/gogf/gf/issues/819
|
2020-10-10 17:29:38 +08:00
|
|
|
func Test_Model_Insert_WithStructAndSliceAttribute(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type Password struct {
|
|
|
|
Salt string `json:"salt"`
|
|
|
|
Pass string `json:"pass"`
|
|
|
|
}
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": &Password{"123", "456"},
|
|
|
|
"nickname": []string{"A", "B", "C"},
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
_, err := db.Model(table).Data(data).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-10 17:29:38 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).One("id", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-10 17:29:38 +08:00
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["create_time"], data["create_time"])
|
2021-09-27 22:47:39 +08:00
|
|
|
t.Assert(one["nickname"], gjson.New(data["nickname"]).MustToJson())
|
2020-10-10 17:29:38 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:16:13 +08:00
|
|
|
func Test_Model_Insert_KeyFieldNameMapping(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
data := User{
|
|
|
|
Id: 1,
|
|
|
|
Passport: "user_1",
|
|
|
|
Password: "pass_1",
|
|
|
|
Nickname: "name_1",
|
|
|
|
CreateTime: "2020-10-10 12:00:01",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).Data(data).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 18:16:13 +08:00
|
|
|
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 18:16:13 +08:00
|
|
|
t.Assert(one["passport"], data.Passport)
|
|
|
|
t.Assert(one["create_time"], data.CreateTime)
|
|
|
|
t.Assert(one["nickname"], data.Nickname)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Update_KeyFieldNameMapping(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
CreateTime string
|
|
|
|
}
|
|
|
|
data := User{
|
|
|
|
Id: 1,
|
|
|
|
Passport: "user_10",
|
|
|
|
Password: "pass_10",
|
|
|
|
Nickname: "name_10",
|
|
|
|
CreateTime: "2020-10-10 12:00:01",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).Data(data).WherePri(1).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 18:16:13 +08:00
|
|
|
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-17 18:16:13 +08:00
|
|
|
t.Assert(one["passport"], data.Passport)
|
|
|
|
t.Assert(one["create_time"], data.CreateTime)
|
|
|
|
t.Assert(one["nickname"], data.Nickname)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-11 22:21:03 +08:00
|
|
|
func Test_Model_Insert_Time(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "p1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2020-10-10 20:09:18.334",
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
_, err := db.Model(table).Data(data).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-11 22:21:03 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).One("id", 1)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-10-11 22:21:03 +08:00
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["create_time"], "2020-10-10 20:09:18")
|
|
|
|
t.Assert(one["nickname"], data["nickname"])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:21:10 +08:00
|
|
|
func Test_Model_BatchInsertWithArrayStruct(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
user := db.Model(table)
|
2020-09-13 11:21:10 +08:00
|
|
|
array := garray.New()
|
2021-02-27 23:58:36 +08:00
|
|
|
for i := 1; i <= TableSize; i++ {
|
2020-09-13 11:21:10 +08:00
|
|
|
array.Append(g.Map{
|
|
|
|
"id": i,
|
|
|
|
"uid": i,
|
|
|
|
"passport": fmt.Sprintf("t%d", i),
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": fmt.Sprintf("name_%d", i),
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
})
|
|
|
|
}
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-05-11 20:14:06 +08:00
|
|
|
result, err := user.Data(array).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-09-13 11:21:10 +08:00
|
|
|
n, _ := result.LastInsertId()
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(n, TableSize)
|
2020-09-13 11:21:10 +08:00
|
|
|
})
|
2019-07-17 23:24:27 +08:00
|
|
|
}
|
2020-02-08 23:46:10 +08:00
|
|
|
|
|
|
|
func Test_Model_InsertIgnore(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-05-11 20:14:06 +08:00
|
|
|
_, err := db.Model(table).Data(g.Map{
|
2020-02-08 23:46:10 +08:00
|
|
|
"id": 1,
|
|
|
|
"uid": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
}).Insert()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2020-02-08 23:46:10 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-05-11 20:14:06 +08:00
|
|
|
_, err := db.Model(table).Data(g.Map{
|
2020-02-08 23:46:10 +08:00
|
|
|
"id": 1,
|
|
|
|
"uid": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
}).InsertIgnore()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-02-08 23:46:10 +08:00
|
|
|
})
|
|
|
|
}
|
2019-07-17 23:24:27 +08:00
|
|
|
|
|
|
|
func Test_Model_Batch(t *testing.T) {
|
2020-12-27 00:11:26 +08:00
|
|
|
// batch insert
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-05-11 20:14:06 +08:00
|
|
|
result, err := db.Model(table).Data(g.List{
|
2019-07-17 23:24:27 +08:00
|
|
|
{
|
|
|
|
"id": 2,
|
|
|
|
"uid": 2,
|
|
|
|
"passport": "t2",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_2",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 3,
|
|
|
|
"uid": 3,
|
|
|
|
"passport": "t3",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_3",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
},
|
|
|
|
}).Batch(1).Insert()
|
|
|
|
if err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-12-27 00:11:26 +08:00
|
|
|
// batch insert, retrieving last insert auto-increment id.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data(g.List{
|
2020-12-27 00:11:26 +08:00
|
|
|
{"passport": "t1"},
|
|
|
|
{"passport": "t2"},
|
|
|
|
{"passport": "t3"},
|
|
|
|
{"passport": "t4"},
|
|
|
|
{"passport": "t5"},
|
|
|
|
}).Batch(2).Insert()
|
|
|
|
if err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 5)
|
|
|
|
})
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
// batch save
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(result), TableSize)
|
2019-07-17 23:24:27 +08:00
|
|
|
for _, v := range result {
|
|
|
|
v["nickname"].Set(v["nickname"].String() + v["id"].String())
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
r, e := db.Model(table).Data(result).Save()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, e := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(n, TableSize*2)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// batch replace
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(result), TableSize)
|
2019-07-17 23:24:27 +08:00
|
|
|
for _, v := range result {
|
|
|
|
v["nickname"].Set(v["nickname"].String() + v["id"].String())
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
r, e := db.Model(table).Data(result).Replace()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, e := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(e, nil)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(n, TableSize*2)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Replace(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data(g.Map{
|
2019-07-17 23:24:27 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": "t11",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "T11",
|
|
|
|
"create_time": "2018-10-24 10:00:00",
|
|
|
|
}).Replace()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Save(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data(g.Map{
|
2019-07-17 23:24:27 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": "t111",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "T111",
|
|
|
|
"create_time": "2018-10-24 10:00:00",
|
|
|
|
}).Save()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Update(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
// UPDATE...LIMIT
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data("nickname", "T100").Where(1).Order("id desc").Limit(2).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v1, err := db.Model(table).Fields("nickname").Where("id", 10).Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v1.String(), "T100")
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v2, err := db.Model(table).Fields("nickname").Where("id", 8).Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v2.String(), "name_8")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data("passport", "user_22").Where("passport=?", "user_2").Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data("passport", "user_2").Where("passport='user_22'").Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2019-10-15 17:44:47 +08:00
|
|
|
|
|
|
|
// Update + Data(string)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data("passport='user_33'").Where("passport='user_3'").Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-15 17:44:47 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-10-15 17:44:47 +08:00
|
|
|
})
|
2021-01-04 19:46:46 +08:00
|
|
|
// Update + Fields(string)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields("passport").Data(g.Map{
|
2021-01-04 19:46:46 +08:00
|
|
|
"passport": "user_44",
|
|
|
|
"none": "none",
|
|
|
|
}).Where("passport='user_4'").Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-01-04 19:46:46 +08:00
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
|
|
|
})
|
2019-07-17 23:24:27 +08:00
|
|
|
}
|
|
|
|
|
2022-04-08 17:22:07 +08:00
|
|
|
func Test_Model_UpdateAndGetAffected(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
n, err := db.Model(table).Data("nickname", "T100").
|
|
|
|
Where(1).Order("id desc").Limit(2).
|
|
|
|
UpdateAndGetAffected()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(n, 2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_Clone(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-12-14 18:13:32 +08:00
|
|
|
md := db.Model(table).Safe(true).Where("id IN(?)", g.Slice{1, 3})
|
2019-07-17 23:24:27 +08:00
|
|
|
count, err := md.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2022-01-07 21:06:49 +08:00
|
|
|
record, err := md.Safe(true).Order("id DESC").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2022-01-07 21:06:49 +08:00
|
|
|
result, err := md.Safe(true).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(record["id"].Int(), 3)
|
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
|
|
t.Assert(result[1]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Safe(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
md := db.Model(table).Safe(false).Where("id IN(?)", g.Slice{1, 3})
|
2019-07-17 23:24:27 +08:00
|
|
|
count, err := md.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-09-27 22:47:39 +08:00
|
|
|
md.Where("id = ?", 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
count, err = md.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(1))
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
md := db.Model(table).Safe(true).Where("id IN(?)", g.Slice{1, 3})
|
2019-07-17 23:24:27 +08:00
|
|
|
count, err := md.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-09-27 22:47:39 +08:00
|
|
|
md.Where("id = ?", 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
count, err = md.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
md := db.Model(table).Safe().Where("id IN(?)", g.Slice{1, 3})
|
2019-08-12 16:50:57 +08:00
|
|
|
count, err := md.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2019-08-12 16:50:57 +08:00
|
|
|
|
2021-09-27 22:47:39 +08:00
|
|
|
md.Where("id = ?", 1)
|
2019-08-12 16:50:57 +08:00
|
|
|
count, err = md.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2019-08-12 16:50:57 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
md1 := db.Model(table).Safe()
|
2019-11-19 21:50:17 +08:00
|
|
|
md2 := md1.Where("id in (?)", g.Slice{1, 3})
|
|
|
|
count, err := md2.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2019-11-19 21:50:17 +08:00
|
|
|
|
|
|
|
all, err := md2.All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(all), 2)
|
2019-11-19 21:50:17 +08:00
|
|
|
|
2020-04-15 09:37:46 +08:00
|
|
|
all, err = md2.Page(1, 10).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(all), 2)
|
2019-11-19 21:50:17 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-11-20 12:09:26 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
md1 := db.Model(table).Where("id>", 0).Safe()
|
2019-11-19 21:50:17 +08:00
|
|
|
md2 := md1.Where("id in (?)", g.Slice{1, 3})
|
|
|
|
md3 := md1.Where("id in (?)", g.Slice{4, 5, 6})
|
2019-11-20 18:45:09 +08:00
|
|
|
|
2019-11-19 21:50:17 +08:00
|
|
|
// 1,3
|
|
|
|
count, err := md2.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2019-11-19 21:50:17 +08:00
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
all, err := md2.Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(all), 2)
|
|
|
|
t.Assert(all[0]["id"].Int(), 1)
|
|
|
|
t.Assert(all[1]["id"].Int(), 3)
|
2019-11-19 21:50:17 +08:00
|
|
|
|
2020-04-15 09:37:46 +08:00
|
|
|
all, err = md2.Page(1, 10).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(all), 2)
|
2019-11-19 21:50:17 +08:00
|
|
|
|
|
|
|
// 4,5,6
|
|
|
|
count, err = md3.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(3))
|
2019-11-19 21:50:17 +08:00
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
all, err = md3.Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(all), 3)
|
|
|
|
t.Assert(all[0]["id"].Int(), 4)
|
|
|
|
t.Assert(all[1]["id"].Int(), 5)
|
|
|
|
t.Assert(all[2]["id"].Int(), 6)
|
2019-11-19 21:50:17 +08:00
|
|
|
|
2020-04-15 09:37:46 +08:00
|
|
|
all, err = md3.Page(1, 10).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(all), 3)
|
2019-11-19 21:50:17 +08:00
|
|
|
})
|
2019-07-17 23:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_All(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(result), TableSize)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id<0").All()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result, nil)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-19 21:08:01 +08:00
|
|
|
func Test_Model_Fields(t *testing.T) {
|
|
|
|
tableName1 := createInitTable()
|
|
|
|
defer dropTable(tableName1)
|
|
|
|
|
|
|
|
tableName2 := "user_" + gtime.Now().TimestampNanoStr()
|
2021-09-29 20:39:02 +08:00
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
2021-01-19 21:08:01 +08:00
|
|
|
CREATE TABLE %s (
|
|
|
|
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
|
|
name varchar(45) NULL,
|
|
|
|
age int(10) unsigned,
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
`, tableName2,
|
|
|
|
)); err != nil {
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2021-01-19 21:08:01 +08:00
|
|
|
}
|
|
|
|
defer dropTable(tableName2)
|
|
|
|
|
2021-09-29 20:39:02 +08:00
|
|
|
r, err := db.Insert(ctx, tableName2, g.Map{
|
2021-01-19 21:08:01 +08:00
|
|
|
"id": 1,
|
|
|
|
"name": "table2_1",
|
|
|
|
"age": 18,
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2021-01-19 21:08:01 +08:00
|
|
|
n, _ := r.RowsAffected()
|
|
|
|
gtest.Assert(n, 1)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(tableName1).As("u").Fields("u.passport,u.id").Where("u.id<2").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-01-19 21:08:01 +08:00
|
|
|
t.Assert(len(all), 1)
|
|
|
|
t.Assert(len(all[0]), 2)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(tableName1).As("u1").
|
2021-01-19 21:08:01 +08:00
|
|
|
LeftJoin(tableName1, "u2", "u2.id=u1.id").
|
|
|
|
Fields("u1.passport,u1.id,u2.id AS u2id").
|
|
|
|
Where("u1.id<2").
|
|
|
|
All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-01-19 21:08:01 +08:00
|
|
|
t.Assert(len(all), 1)
|
|
|
|
t.Assert(len(all[0]), 3)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(tableName1).As("u1").
|
2021-01-19 21:08:01 +08:00
|
|
|
LeftJoin(tableName2, "u2", "u2.id=u1.id").
|
|
|
|
Fields("u1.passport,u1.id,u2.name,u2.age").
|
|
|
|
Where("u1.id<2").
|
|
|
|
All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-01-19 21:08:01 +08:00
|
|
|
t.Assert(len(all), 1)
|
|
|
|
t.Assert(len(all[0]), 4)
|
|
|
|
t.Assert(all[0]["id"], 1)
|
|
|
|
t.Assert(all[0]["age"], 18)
|
|
|
|
t.Assert(all[0]["name"], "table2_1")
|
|
|
|
t.Assert(all[0]["passport"], "user_1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_One(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
record, err := db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(record["nickname"].String(), "name_1")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2019-12-04 16:21:35 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
record, err := db.Model(table).Where("id", 0).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(record, nil)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Value(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2019-12-19 15:14:05 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err := db.Model(table).Fields("nickname").Where("id", 1).Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(value.String(), "name_1")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2019-12-04 16:21:35 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err := db.Model(table).Fields("nickname").Where("id", 0).Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(value, nil)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-11 23:17:41 +08:00
|
|
|
func Test_Model_Array(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id", g.Slice{1, 2, 3}).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(all.Array("id"), g.Slice{1, 2, 3})
|
|
|
|
t.Assert(all.Array("nickname"), g.Slice{"name_1", "name_2", "name_3"})
|
2020-03-11 23:24:19 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
array, err := db.Model(table).Fields("nickname").Where("id", g.Slice{1, 2, 3}).Array()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array, g.Slice{"name_1", "name_2", "name_3"})
|
2020-03-11 23:17:41 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
array, err := db.Model(table).Array("nickname", "id", g.Slice{1, 2, 3})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(array, g.Slice{"name_1", "name_2", "name_3"})
|
2020-03-11 23:17:41 +08:00
|
|
|
})
|
2019-12-19 15:14:05 +08:00
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_Count(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
count, err := db.Model(table).Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2022-03-31 21:40:28 +08:00
|
|
|
// Count with cache, check internal ctx data feature.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
count, err := db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Name: guid.S(),
|
|
|
|
Force: false,
|
|
|
|
}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2022-03-31 21:40:28 +08:00
|
|
|
}
|
|
|
|
})
|
2020-11-04 19:53:50 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
count, err := db.Model(table).FieldsEx("id").Where("id>8").Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2020-11-04 19:53:50 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
count, err := db.Model(table).Fields("distinct id,nickname").Where("id>8").Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(2))
|
2020-11-04 19:53:50 +08:00
|
|
|
})
|
2020-11-10 10:37:42 +08:00
|
|
|
// COUNT...LIMIT...
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
count, err := db.Model(table).Page(1, 2).Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2020-11-10 10:37:42 +08:00
|
|
|
})
|
2022-11-16 10:04:49 +08:00
|
|
|
}
|
|
|
|
|
2024-10-24 15:29:03 +08:00
|
|
|
func Test_Model_Exist(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
exist, err := db.Model(table).Exist()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(exist, TableSize > 0)
|
|
|
|
exist, err = db.Model(table).Where("id", -1).Exist()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(exist, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:04:49 +08:00
|
|
|
func Test_Model_Value_WithCache(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
value, err := db.Model(table).Where("id", 1).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
|
|
|
}).Value()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(value.Int(), 0)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Data(g.MapStrAny{
|
|
|
|
"id": 1,
|
|
|
|
"passport": fmt.Sprintf(`passport_%d`, 1),
|
|
|
|
"password": fmt.Sprintf(`password_%d`, 1),
|
|
|
|
"nickname": fmt.Sprintf(`nickname_%d`, 1),
|
|
|
|
}).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
value, err := db.Model(table).Where("id", 1).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
2024-10-08 20:42:06 +08:00
|
|
|
}).Value("id")
|
2022-11-16 10:04:49 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(value.Int(), 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Count_WithCache(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", 1).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
|
|
|
}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(0))
|
2022-11-16 10:04:49 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Data(g.MapStrAny{
|
|
|
|
"id": 1,
|
|
|
|
"passport": fmt.Sprintf(`passport_%d`, 1),
|
|
|
|
"password": fmt.Sprintf(`password_%d`, 1),
|
|
|
|
"nickname": fmt.Sprintf(`nickname_%d`, 1),
|
|
|
|
}).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", 1).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
|
|
|
}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(1))
|
2022-11-16 10:04:49 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Count_All_WithCache(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
|
|
|
}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(0))
|
2022-11-16 10:04:49 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Data(g.MapStrAny{
|
|
|
|
"id": 1,
|
|
|
|
"passport": fmt.Sprintf(`passport_%d`, 1),
|
|
|
|
"password": fmt.Sprintf(`password_%d`, 1),
|
|
|
|
"nickname": fmt.Sprintf(`nickname_%d`, 1),
|
|
|
|
}).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
|
|
|
}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(1))
|
2022-11-16 10:04:49 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Data(g.MapStrAny{
|
|
|
|
"id": 2,
|
|
|
|
"passport": fmt.Sprintf(`passport_%d`, 2),
|
|
|
|
"password": fmt.Sprintf(`password_%d`, 2),
|
|
|
|
"nickname": fmt.Sprintf(`nickname_%d`, 2),
|
|
|
|
}).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
|
|
|
}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(1))
|
2022-11-16 10:04:49 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_CountColumn_WithCache(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", 1).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
|
|
|
}).CountColumn("id")
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(0))
|
2022-11-16 10:04:49 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Data(g.MapStrAny{
|
|
|
|
"id": 1,
|
|
|
|
"passport": fmt.Sprintf(`passport_%d`, 1),
|
|
|
|
"password": fmt.Sprintf(`password_%d`, 1),
|
|
|
|
"nickname": fmt.Sprintf(`nickname_%d`, 1),
|
|
|
|
}).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", 1).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second * 10,
|
|
|
|
Force: false,
|
|
|
|
}).CountColumn("id")
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(1))
|
2022-11-16 10:04:49 +08:00
|
|
|
})
|
2019-07-17 23:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Select(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2021-11-23 15:28:40 +08:00
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-23 15:28:40 +08:00
|
|
|
var users []User
|
|
|
|
err := db.Model(table).Scan(&users)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-11-23 15:28:40 +08:00
|
|
|
t.Assert(len(users), TableSize)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Struct(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id=1").Scan(user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_1")
|
|
|
|
t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id=1").Scan(user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_1")
|
|
|
|
t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// Auto creating struct object.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
user := (*User)(nil)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id=1").Scan(&user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_1")
|
|
|
|
t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// Just using Scan.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
user := (*User)(nil)
|
2021-02-27 23:58:36 +08:00
|
|
|
err := db.Model(table).Where("id=1").Scan(&user)
|
2019-07-17 23:24:27 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_1")
|
|
|
|
t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// sql.ErrNoRows
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id=-1").Scan(user)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, sql.ErrNoRows)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2021-01-09 21:05:47 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
var user *User
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id=-1").Scan(&user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-01-09 21:05:47 +08:00
|
|
|
})
|
2019-07-17 23:24:27 +08:00
|
|
|
}
|
|
|
|
|
2020-03-06 23:22:08 +08:00
|
|
|
func Test_Model_Struct_CustomType(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type MyInt int
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-06 23:22:08 +08:00
|
|
|
type User struct {
|
|
|
|
Id MyInt
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id=1").Scan(user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_1")
|
|
|
|
t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00")
|
2020-03-06 23:22:08 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_Structs(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
var users []User
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Order("id asc").Scan(&users)
|
2019-07-17 23:24:27 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
t.Assert(users[1].Id, 2)
|
|
|
|
t.Assert(users[2].Id, 3)
|
|
|
|
t.Assert(users[0].NickName, "name_1")
|
|
|
|
t.Assert(users[1].NickName, "name_2")
|
|
|
|
t.Assert(users[2].NickName, "name_3")
|
|
|
|
t.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// Auto create struct slice.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
var users []*User
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Order("id asc").Scan(&users)
|
2019-07-17 23:24:27 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
t.Assert(users[1].Id, 2)
|
|
|
|
t.Assert(users[2].Id, 3)
|
|
|
|
t.Assert(users[0].NickName, "name_1")
|
|
|
|
t.Assert(users[1].NickName, "name_2")
|
|
|
|
t.Assert(users[2].NickName, "name_3")
|
|
|
|
t.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// Just using Scan.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
var users []*User
|
2021-02-27 23:58:36 +08:00
|
|
|
err := db.Model(table).Order("id asc").Scan(&users)
|
2019-07-17 23:24:27 +08:00
|
|
|
if err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
t.Assert(users[1].Id, 2)
|
|
|
|
t.Assert(users[2].Id, 3)
|
|
|
|
t.Assert(users[0].NickName, "name_1")
|
|
|
|
t.Assert(users[1].NickName, "name_2")
|
|
|
|
t.Assert(users[2].NickName, "name_3")
|
|
|
|
t.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// sql.ErrNoRows
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
var users []*User
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Where("id<0").Scan(&users)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-28 22:02:08 +08:00
|
|
|
func Test_Model_StructsWithOrmTag(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
dbInvalid.SetDebug(true)
|
|
|
|
defer dbInvalid.SetDebug(false)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Uid int `orm:"id"`
|
|
|
|
Passport string
|
|
|
|
Password string `orm:"password"`
|
|
|
|
Name string `orm:"nick_name"`
|
|
|
|
Time gtime.Time `orm:"create_time"`
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
users []User
|
|
|
|
buffer = bytes.NewBuffer(nil)
|
|
|
|
)
|
2022-07-07 21:16:26 +08:00
|
|
|
dbInvalid.GetLogger().(*glog.Logger).SetWriter(buffer)
|
|
|
|
defer dbInvalid.GetLogger().(*glog.Logger).SetWriter(os.Stdout)
|
2021-09-28 22:02:08 +08:00
|
|
|
dbInvalid.Model(table).Order("id asc").Scan(&users)
|
2022-11-17 19:47:17 +08:00
|
|
|
// fmt.Println(buffer.String())
|
2021-09-28 22:02:08 +08:00
|
|
|
t.Assert(
|
2022-09-26 22:11:13 +08:00
|
|
|
gstr.Contains(
|
|
|
|
buffer.String(),
|
|
|
|
"SELECT `id`,`Passport`,`password`,`nick_name`,`create_time` FROM `user",
|
|
|
|
),
|
2021-09-28 22:02:08 +08:00
|
|
|
true,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
|
|
|
// defer db.SetDebug(false)
|
2021-09-28 22:02:08 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type A struct {
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
type B struct {
|
|
|
|
A
|
|
|
|
NickName string
|
|
|
|
}
|
|
|
|
one, err := db.Model(table).Fields(&B{}).Where("id", 2).One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(one), 3)
|
|
|
|
t.Assert(one["nickname"], "name_2")
|
|
|
|
t.Assert(one["passport"], "user_2")
|
|
|
|
t.Assert(one["password"], "pass_2")
|
|
|
|
})
|
|
|
|
}
|
2020-06-16 11:37:00 +08:00
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_Scan(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
2021-02-27 23:58:36 +08:00
|
|
|
err := db.Model(table).Where("id=1").Scan(user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_1")
|
|
|
|
t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
user := new(User)
|
2021-02-27 23:58:36 +08:00
|
|
|
err := db.Model(table).Where("id=1").Scan(user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(user.NickName, "name_1")
|
|
|
|
t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
var users []User
|
2021-02-27 23:58:36 +08:00
|
|
|
err := db.Model(table).Order("id asc").Scan(&users)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
t.Assert(users[1].Id, 2)
|
|
|
|
t.Assert(users[2].Id, 3)
|
|
|
|
t.Assert(users[0].NickName, "name_1")
|
|
|
|
t.Assert(users[1].NickName, "name_2")
|
|
|
|
t.Assert(users[2].NickName, "name_3")
|
|
|
|
t.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
var users []*User
|
2021-02-27 23:58:36 +08:00
|
|
|
err := db.Model(table).Order("id asc").Scan(&users)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(users), TableSize)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
t.Assert(users[1].Id, 2)
|
|
|
|
t.Assert(users[2].Id, 3)
|
|
|
|
t.Assert(users[0].NickName, "name_1")
|
|
|
|
t.Assert(users[1].NickName, "name_2")
|
|
|
|
t.Assert(users[2].NickName, "name_3")
|
|
|
|
t.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// sql.ErrNoRows
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
2020-04-28 21:03:25 +08:00
|
|
|
var (
|
|
|
|
user = new(User)
|
|
|
|
users = new([]*User)
|
|
|
|
)
|
2021-02-27 23:58:36 +08:00
|
|
|
err1 := db.Model(table).Where("id < 0").Scan(user)
|
|
|
|
err2 := db.Model(table).Where("id < 0").Scan(users)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err1, sql.ErrNoRows)
|
2020-04-28 21:03:25 +08:00
|
|
|
t.Assert(err2, nil)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-08 14:43:39 +08:00
|
|
|
func Test_Model_Scan_NilSliceAttrWhenNoRecordsFound(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
CreateTime gtime.Time
|
|
|
|
}
|
|
|
|
type Response struct {
|
|
|
|
Users []User `json:"users"`
|
|
|
|
}
|
|
|
|
var res Response
|
|
|
|
err := db.Model(table).Scan(&res.Users)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(res.Users, nil)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_OrderBy(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).Order("id DESC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
t.Assert(result[0]["nickname"].String(), fmt.Sprintf("name_%d", TableSize))
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2021-11-26 10:38:50 +08:00
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2024-11-21 15:08:29 +08:00
|
|
|
result, err := db.Model(table).Order(gdb.Raw("NULL")).All()
|
2021-11-26 10:38:50 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
t.Assert(result[0]["nickname"].String(), "name_1")
|
|
|
|
})
|
2021-12-22 20:51:03 +08:00
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Order(gdb.Raw("field(id, 10,1,2,3,4,5,6,7,8,9)")).All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
t.Assert(result[0]["nickname"].String(), "name_10")
|
|
|
|
t.Assert(result[1]["nickname"].String(), "name_1")
|
|
|
|
t.Assert(result[2]["nickname"].String(), "name_2")
|
|
|
|
})
|
2021-11-26 10:38:50 +08:00
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_GroupBy(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-19 23:13:53 +08:00
|
|
|
result, err := db.Model(table).Group("id").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(result), TableSize)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result[0]["nickname"].String(), "name_1")
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:21:30 +08:00
|
|
|
func Test_Model_Data(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-24 20:58:11 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data("nickname=?", "test").Where("id=?", 3).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-13 17:21:30 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2020-03-13 17:21:30 +08:00
|
|
|
})
|
2020-03-24 20:58:11 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
users := make([]g.MapStrAny, 0)
|
|
|
|
for i := 1; i <= 10; i++ {
|
|
|
|
users = append(users, g.MapStrAny{
|
|
|
|
"id": i,
|
|
|
|
"passport": fmt.Sprintf(`passport_%d`, i),
|
|
|
|
"password": fmt.Sprintf(`password_%d`, i),
|
|
|
|
"nickname": fmt.Sprintf(`nickname_%d`, i),
|
|
|
|
})
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data(users).Batch(2).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-24 20:58:11 +08:00
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 10)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
users := garray.New()
|
|
|
|
for i := 1; i <= 10; i++ {
|
|
|
|
users.Append(g.MapStrAny{
|
|
|
|
"id": i,
|
|
|
|
"passport": fmt.Sprintf(`passport_%d`, i),
|
|
|
|
"password": fmt.Sprintf(`password_%d`, i),
|
|
|
|
"nickname": fmt.Sprintf(`nickname_%d`, i),
|
|
|
|
})
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data(users).Batch(2).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-24 20:58:11 +08:00
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 10)
|
|
|
|
})
|
2020-03-13 17:21:30 +08:00
|
|
|
}
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
func Test_Model_Where(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
// string
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id=? and nickname=?", 3, "name_3").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-11-10 13:43:12 +08:00
|
|
|
|
|
|
|
// slice
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(g.Slice{"id", 3}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-10 13:43:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(g.Slice{"id", 3, "nickname", "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-10 13:43:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
|
|
|
})
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
// slice parameter
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id=? and nickname=?", g.Slice{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-24 21:21:08 +08:00
|
|
|
})
|
2019-10-13 00:37:25 +08:00
|
|
|
// map like
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(g.Map{
|
2019-10-13 00:37:25 +08:00
|
|
|
"passport like": "user_1%",
|
2019-12-19 15:14:05 +08:00
|
|
|
}).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0].GMap().Get("id"), 1)
|
|
|
|
t.Assert(result[1].GMap().Get("id"), 10)
|
2019-10-13 00:37:25 +08:00
|
|
|
})
|
2019-08-24 23:08:17 +08:00
|
|
|
// map + slice parameter
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(g.Map{
|
2019-08-24 23:08:17 +08:00
|
|
|
"id": g.Slice{1, 2, 3},
|
|
|
|
"passport": g.Slice{"user_2", "user_3"},
|
2021-09-27 22:47:39 +08:00
|
|
|
}).Where("id=? and nickname=?", g.Slice{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-08-24 23:08:17 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id=3", g.Slice{}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id=?", g.Slice{3}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id", 3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id", 3).Where("nickname", "name_3").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).Where("id", 3).Where("nickname", "name_3").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).Where("id", 30).WhereOr("nickname", "name_3").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).Where("id", 30).WhereOr("nickname", "name_3").Where("id>?", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).Where("id", 30).WhereOr("nickname", "name_3").Where("id>", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// slice
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id=? AND nickname=?", g.Slice{3, "name_3"}...).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id=? AND nickname=?", g.Slice{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("passport like ? and nickname like ?", g.Slice{"user_3", "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(g.Map{"id": 3, "nickname": "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// map key operator
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(g.Map{"id>": 1, "id<": 3}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2019-10-14 23:27:48 +08:00
|
|
|
|
|
|
|
// gmap.Map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(gmap.NewFrom(g.MapAnyAny{"id": 3, "nickname": "name_3"})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-10-14 23:27:48 +08:00
|
|
|
})
|
|
|
|
// gmap.Map key operator
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(gmap.NewFrom(g.MapAnyAny{"id>": 1, "id<": 3})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-10-14 23:27:48 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// list map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(gmap.NewListMapFrom(g.MapAnyAny{"id": 3, "nickname": "name_3"})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-10-14 23:27:48 +08:00
|
|
|
})
|
|
|
|
// list map key operator
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(gmap.NewListMapFrom(g.MapAnyAny{"id>": 1, "id<": 3})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-10-14 23:27:48 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// tree map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(gmap.NewTreeMapFrom(gutil.ComparatorString, g.MapAnyAny{"id": 3, "nickname": "name_3"})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-10-14 23:27:48 +08:00
|
|
|
})
|
|
|
|
// tree map key operator
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(gmap.NewTreeMapFrom(gutil.ComparatorString, g.MapAnyAny{"id>": 1, "id<": 3})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-10-14 23:27:48 +08:00
|
|
|
})
|
|
|
|
|
2019-07-17 23:24:27 +08:00
|
|
|
// complicated where 1
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2019-07-17 23:24:27 +08:00
|
|
|
conditions := g.Map{
|
|
|
|
"nickname like ?": "%name%",
|
|
|
|
"id between ? and ?": g.Slice{1, 3},
|
|
|
|
"id > 0": nil,
|
|
|
|
"create_time > 0": nil,
|
|
|
|
"id": g.Slice{1, 2, 3},
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(conditions).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// complicated where 2
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2019-07-17 23:24:27 +08:00
|
|
|
conditions := g.Map{
|
|
|
|
"nickname like ?": "%name%",
|
|
|
|
"id between ? and ?": g.Slice{1, 3},
|
|
|
|
"id >= ?": 1,
|
|
|
|
"create_time > ?": 0,
|
|
|
|
"id in(?)": g.Slice{1, 2, 3},
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(conditions).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
2021-06-23 12:04:16 +08:00
|
|
|
// struct, automatic mapping and filtering.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
2021-06-23 12:04:16 +08:00
|
|
|
Id int
|
|
|
|
Nickname string
|
2019-07-17 23:24:27 +08:00
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(User{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err = db.Model(table).Where(&User{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// slice single
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("id IN(?)", g.Slice{1, 3}).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
|
|
t.Assert(result[1]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// slice + string
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("nickname=? AND id IN(?)", "name_3", g.Slice{1, 3}).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// slice + map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(g.Map{
|
2019-07-17 23:24:27 +08:00
|
|
|
"id": g.Slice{1, 3},
|
|
|
|
"nickname": "name_3",
|
2019-12-19 15:14:05 +08:00
|
|
|
}).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
// slice + struct
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-07-17 23:24:27 +08:00
|
|
|
type User struct {
|
|
|
|
Ids []int `json:"id"`
|
|
|
|
Nickname string `gconv:"nickname"`
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(User{
|
2019-07-17 23:24:27 +08:00
|
|
|
Ids: []int{1, 3},
|
|
|
|
Nickname: "name_3",
|
2019-12-19 15:14:05 +08:00
|
|
|
}).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:21:30 +08:00
|
|
|
func Test_Model_Where_ISNULL_1(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data("nickname", nil).Where("id", 2).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-13 17:21:30 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2020-03-13 17:21:30 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("nickname", nil).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one.IsEmpty(), false)
|
|
|
|
t.Assert(one["id"], 2)
|
2020-03-13 17:21:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Where_ISNULL_2(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
// complicated one.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2020-03-13 17:21:30 +08:00
|
|
|
conditions := g.Map{
|
|
|
|
"nickname like ?": "%name%",
|
|
|
|
"id between ? and ?": g.Slice{1, 3},
|
|
|
|
"id > 0": nil,
|
|
|
|
"create_time > 0": nil,
|
|
|
|
"id": g.Slice{1, 2, 3},
|
|
|
|
}
|
2022-02-17 22:42:33 +08:00
|
|
|
result, err := db.Model(table).Where(conditions).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
2020-03-13 17:21:30 +08:00
|
|
|
})
|
|
|
|
}
|
2020-03-29 09:52:37 +08:00
|
|
|
|
2020-06-28 23:03:41 +08:00
|
|
|
func Test_Model_Where_OmitEmpty(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
conditions := g.Map{
|
|
|
|
"id < 4": "",
|
|
|
|
}
|
2022-02-17 22:42:33 +08:00
|
|
|
result, err := db.Model(table).Where(conditions).Order("id desc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-06-28 23:03:41 +08:00
|
|
|
t.Assert(len(result), 3)
|
2022-02-17 22:42:33 +08:00
|
|
|
t.Assert(result[0]["id"].Int(), 3)
|
2020-06-28 23:03:41 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
conditions := g.Map{
|
|
|
|
"id < 4": "",
|
|
|
|
}
|
2022-02-17 22:42:33 +08:00
|
|
|
result, err := db.Model(table).Where(conditions).OmitEmpty().Order("id desc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-02-17 22:42:33 +08:00
|
|
|
t.Assert(len(result), 10)
|
|
|
|
t.Assert(result[0]["id"].Int(), 10)
|
2020-06-28 23:03:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-29 09:52:37 +08:00
|
|
|
func Test_Model_Where_GTime(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("create_time>?", gtime.NewFromStr("2010-09-01")).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-29 09:52:37 +08:00
|
|
|
t.Assert(len(result), 10)
|
|
|
|
})
|
2020-04-15 18:02:32 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where("create_time>?", *gtime.NewFromStr("2010-09-01")).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-15 18:02:32 +08:00
|
|
|
t.Assert(len(result), 10)
|
|
|
|
})
|
2020-03-29 09:52:37 +08:00
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
func Test_Model_WherePri(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
// primary key
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).WherePri(3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(one, nil)
|
|
|
|
t.Assert(one["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).WherePri(g.Slice{3, 9}).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(all), 2)
|
|
|
|
t.Assert(all[0]["id"].Int(), 3)
|
|
|
|
t.Assert(all[1]["id"].Int(), 9)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// string
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id=? and nickname=?", 3, "name_3").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// slice parameter
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id=? and nickname=?", g.Slice{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// map like
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(g.Map{
|
2019-12-19 15:14:05 +08:00
|
|
|
"passport like": "user_1%",
|
|
|
|
}).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0].GMap().Get("id"), 1)
|
|
|
|
t.Assert(result[1].GMap().Get("id"), 10)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// map + slice parameter
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(g.Map{
|
2019-12-19 15:14:05 +08:00
|
|
|
"id": g.Slice{1, 2, 3},
|
|
|
|
"passport": g.Slice{"user_2", "user_3"},
|
2021-09-27 22:47:39 +08:00
|
|
|
}).Where("id=? and nickname=?", g.Slice{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(g.Map{
|
2019-12-19 15:14:05 +08:00
|
|
|
"id": g.Slice{1, 2, 3},
|
|
|
|
"passport": g.Slice{"user_2", "user_3"},
|
2021-09-27 22:47:39 +08:00
|
|
|
}).WhereOr("nickname=?", g.Slice{"name_4"}).Where("id", 3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
2020-06-03 21:36:16 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id=3", g.Slice{}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id=?", g.Slice{3}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id", 3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id", 3).WherePri("nickname", "name_3").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).WherePri("id", 3).Where("nickname", "name_3").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).WherePri("id", 30).WhereOr("nickname", "name_3").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).WherePri("id", 30).WhereOr("nickname", "name_3").Where("id>?", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).WherePri("id", 30).WhereOr("nickname", "name_3").Where("id>", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// slice
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id=? AND nickname=?", g.Slice{3, "name_3"}...).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id=? AND nickname=?", g.Slice{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("passport like ? and nickname like ?", g.Slice{"user_3", "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(g.Map{"id": 3, "nickname": "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// map key operator
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(g.Map{"id>": 1, "id<": 3}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// gmap.Map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(gmap.NewFrom(g.MapAnyAny{"id": 3, "nickname": "name_3"})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// gmap.Map key operator
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(gmap.NewFrom(g.MapAnyAny{"id>": 1, "id<": 3})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// list map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(gmap.NewListMapFrom(g.MapAnyAny{"id": 3, "nickname": "name_3"})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// list map key operator
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(gmap.NewListMapFrom(g.MapAnyAny{"id>": 1, "id<": 3})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// tree map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(gmap.NewTreeMapFrom(gutil.ComparatorString, g.MapAnyAny{"id": 3, "nickname": "name_3"})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// tree map key operator
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(gmap.NewTreeMapFrom(gutil.ComparatorString, g.MapAnyAny{"id>": 1, "id<": 3})).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 2)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// complicated where 1
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2019-12-19 15:14:05 +08:00
|
|
|
conditions := g.Map{
|
|
|
|
"nickname like ?": "%name%",
|
|
|
|
"id between ? and ?": g.Slice{1, 3},
|
|
|
|
"id > 0": nil,
|
|
|
|
"create_time > 0": nil,
|
|
|
|
"id": g.Slice{1, 2, 3},
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(conditions).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// complicated where 2
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2019-12-19 15:14:05 +08:00
|
|
|
conditions := g.Map{
|
|
|
|
"nickname like ?": "%name%",
|
|
|
|
"id between ? and ?": g.Slice{1, 3},
|
|
|
|
"id >= ?": 1,
|
|
|
|
"create_time > ?": 0,
|
|
|
|
"id in(?)": g.Slice{1, 2, 3},
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(conditions).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// struct
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-19 15:14:05 +08:00
|
|
|
type User struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
Nickname string `gconv:"nickname"`
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(User{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err = db.Model(table).WherePri(&User{3, "name_3"}).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(result["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// slice single
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("id IN(?)", g.Slice{1, 3}).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0]["id"].Int(), 1)
|
|
|
|
t.Assert(result[1]["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// slice + string
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri("nickname=? AND id IN(?)", "name_3", g.Slice{1, 3}).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// slice + map
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(g.Map{
|
2019-12-19 15:14:05 +08:00
|
|
|
"id": g.Slice{1, 3},
|
|
|
|
"nickname": "name_3",
|
|
|
|
}).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"].Int(), 3)
|
2019-12-19 15:14:05 +08:00
|
|
|
})
|
|
|
|
// slice + struct
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-19 15:14:05 +08:00
|
|
|
type User struct {
|
|
|
|
Ids []int `json:"id"`
|
|
|
|
Nickname string `gconv:"nickname"`
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).WherePri(User{
|
2019-12-19 15:14:05 +08:00
|
|
|
Ids: []int{1, 3},
|
|
|
|
Nickname: "name_3",
|
|
|
|
}).Order("id ASC").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"].Int(), 3)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Delete(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
// DELETE...LIMIT
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(1).Limit(2).Delete()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(1).Delete()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-07-17 23:24:27 +08:00
|
|
|
n, _ := result.RowsAffected()
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(n, TableSize-2)
|
2019-07-17 23:24:27 +08:00
|
|
|
})
|
|
|
|
}
|
2019-08-12 16:50:57 +08:00
|
|
|
|
|
|
|
func Test_Model_Offset(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-20 08:49:40 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-27 22:47:39 +08:00
|
|
|
result, err := db.Model(table).Limit(2).Offset(5).Order("id").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-20 08:49:40 +08:00
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0]["id"], 6)
|
|
|
|
t.Assert(result[1]["id"], 7)
|
|
|
|
})
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
|
|
|
|
2020-04-15 09:37:46 +08:00
|
|
|
func Test_Model_Page(t *testing.T) {
|
2019-08-12 16:50:57 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2020-03-20 08:49:40 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Page(3, 3).Order("id").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-20 08:49:40 +08:00
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"], 7)
|
|
|
|
t.Assert(result[1]["id"], 8)
|
|
|
|
})
|
2020-04-15 09:37:46 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
model := db.Model(table).Safe().Order("id")
|
2020-04-15 09:37:46 +08:00
|
|
|
all, err := model.Page(3, 3).All()
|
|
|
|
count, err := model.Count()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-15 09:37:46 +08:00
|
|
|
t.Assert(len(all), 3)
|
|
|
|
t.Assert(all[0]["id"], "7")
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2020-04-15 09:37:46 +08:00
|
|
|
})
|
2019-08-12 16:50:57 +08:00
|
|
|
}
|
2019-09-29 14:27:09 +08:00
|
|
|
|
|
|
|
func Test_Model_Option_Map(t *testing.T) {
|
|
|
|
// Insert
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 14:27:09 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Fields("id, passport").Data(g.Map{
|
2019-09-29 14:27:09 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": "1",
|
|
|
|
"password": "1",
|
|
|
|
"nickname": "1",
|
|
|
|
}).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-29 14:27:09 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(one["password"].String(), "1")
|
|
|
|
t.AssertNE(one["nickname"].String(), "1")
|
|
|
|
t.Assert(one["passport"].String(), "1")
|
2019-09-29 14:27:09 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 14:27:09 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-07-30 14:58:23 +08:00
|
|
|
r, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
2019-09-29 14:27:09 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": 0,
|
|
|
|
"password": 0,
|
|
|
|
"nickname": "1",
|
|
|
|
}).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-29 14:27:09 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(one["passport"].String(), "0")
|
|
|
|
t.AssertNE(one["password"].String(), "0")
|
|
|
|
t.Assert(one["nickname"].String(), "1")
|
2019-09-29 14:27:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// Replace
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 14:27:09 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2021-07-30 14:58:23 +08:00
|
|
|
_, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
2019-09-29 14:27:09 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": 0,
|
|
|
|
"password": 0,
|
|
|
|
"nickname": "1",
|
|
|
|
}).Replace()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(one["passport"].String(), "0")
|
|
|
|
t.AssertNE(one["password"].String(), "0")
|
|
|
|
t.Assert(one["nickname"].String(), "1")
|
2019-09-29 14:27:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// Save
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 14:27:09 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Fields("id, passport").Data(g.Map{
|
2019-09-29 14:27:09 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": "1",
|
|
|
|
"password": "1",
|
|
|
|
"nickname": "1",
|
|
|
|
}).Save()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-29 14:27:09 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(one["password"].String(), "1")
|
|
|
|
t.AssertNE(one["nickname"].String(), "1")
|
|
|
|
t.Assert(one["passport"].String(), "1")
|
2019-09-29 14:27:09 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 14:27:09 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-07-30 14:58:23 +08:00
|
|
|
_, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
2019-09-29 14:27:09 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": 0,
|
|
|
|
"password": 0,
|
|
|
|
"nickname": "1",
|
|
|
|
}).Save()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(one["passport"].String(), "0")
|
|
|
|
t.AssertNE(one["password"].String(), "0")
|
|
|
|
t.Assert(one["nickname"].String(), "1")
|
2019-09-29 14:27:09 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
_, err = db.Model(table).Data(g.Map{
|
2019-09-29 14:27:09 +08:00
|
|
|
"id": 1,
|
|
|
|
"passport": 0,
|
|
|
|
"password": 0,
|
|
|
|
"nickname": "1",
|
|
|
|
}).Save()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err = db.Model(table).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["passport"].String(), "0")
|
|
|
|
t.Assert(one["password"].String(), "0")
|
|
|
|
t.Assert(one["nickname"].String(), "1")
|
2019-09-29 14:27:09 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// Update
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-09-29 14:27:09 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Data(g.Map{"nickname": ""}).Where("id", 1).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-29 14:27:09 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-09-29 14:27:09 +08:00
|
|
|
|
2021-07-30 14:58:23 +08:00
|
|
|
_, err = db.Model(table).OmitEmptyData().Data(g.Map{"nickname": ""}).Where("id", 2).Update()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-09-29 14:27:09 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err = db.Model(table).OmitEmpty().Data(g.Map{"nickname": "", "password": "123"}).Where("id", 3).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-29 14:27:09 +08:00
|
|
|
n, _ = r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-09-29 14:27:09 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
_, err = db.Model(table).OmitEmpty().Fields("nickname").Data(g.Map{"nickname": "", "password": "123"}).Where("id", 4).Update()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-09-29 14:27:09 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err = db.Model(table).OmitEmpty().
|
2019-09-29 14:27:09 +08:00
|
|
|
Fields("password").Data(g.Map{
|
|
|
|
"nickname": "",
|
|
|
|
"passport": "123",
|
|
|
|
"password": "456",
|
|
|
|
}).Where("id", 5).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-09-29 14:27:09 +08:00
|
|
|
n, _ = r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-09-29 14:27:09 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 5).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["password"], "456")
|
|
|
|
t.AssertNE(one["passport"].String(), "")
|
|
|
|
t.AssertNE(one["passport"].String(), "123")
|
2019-09-29 14:27:09 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Option_List(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-21 19:13:25 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Fields("id, password").Data(g.List{
|
2019-10-21 19:13:25 +08:00
|
|
|
g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "1",
|
|
|
|
"password": "1",
|
|
|
|
"nickname": "1",
|
|
|
|
},
|
|
|
|
g.Map{
|
|
|
|
"id": 2,
|
|
|
|
"passport": "2",
|
|
|
|
"password": "2",
|
|
|
|
"nickname": "2",
|
|
|
|
},
|
|
|
|
}).Save()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-21 19:13:25 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2021-02-27 23:58:36 +08:00
|
|
|
list, err := db.Model(table).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(list), 2)
|
|
|
|
t.Assert(list[0]["id"].String(), "1")
|
|
|
|
t.Assert(list[0]["nickname"].String(), "")
|
|
|
|
t.Assert(list[0]["passport"].String(), "")
|
|
|
|
t.Assert(list[0]["password"].String(), "1")
|
2019-10-21 19:13:25 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(list[1]["id"].String(), "2")
|
|
|
|
t.Assert(list[1]["nickname"].String(), "")
|
|
|
|
t.Assert(list[1]["passport"].String(), "")
|
|
|
|
t.Assert(list[1]["password"].String(), "2")
|
2019-10-21 19:13:25 +08:00
|
|
|
})
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-21 19:13:25 +08:00
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).OmitEmpty().Fields("id, password").Data(g.List{
|
2019-10-21 19:13:25 +08:00
|
|
|
g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "1",
|
|
|
|
"password": 0,
|
|
|
|
"nickname": "1",
|
|
|
|
},
|
|
|
|
g.Map{
|
|
|
|
"id": 2,
|
|
|
|
"passport": "2",
|
|
|
|
"password": "2",
|
|
|
|
"nickname": "2",
|
|
|
|
},
|
|
|
|
}).Save()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-21 19:13:25 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 2)
|
2021-02-27 23:58:36 +08:00
|
|
|
list, err := db.Model(table).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(list), 2)
|
|
|
|
t.Assert(list[0]["id"].String(), "1")
|
|
|
|
t.Assert(list[0]["nickname"].String(), "")
|
|
|
|
t.Assert(list[0]["passport"].String(), "")
|
|
|
|
t.Assert(list[0]["password"].String(), "0")
|
2019-10-21 19:13:25 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(list[1]["id"].String(), "2")
|
|
|
|
t.Assert(list[1]["nickname"].String(), "")
|
|
|
|
t.Assert(list[1]["passport"].String(), "")
|
|
|
|
t.Assert(list[1]["password"].String(), "2")
|
2019-10-21 19:13:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-11 19:30:22 +08:00
|
|
|
func Test_Model_OmitEmpty(t *testing.T) {
|
2021-08-13 11:28:47 +08:00
|
|
|
table := fmt.Sprintf(`table_%s`, gtime.TimestampNanoStr())
|
2021-09-29 20:39:02 +08:00
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
2021-01-11 19:30:22 +08:00
|
|
|
CREATE TABLE IF NOT EXISTS %s (
|
|
|
|
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
|
|
name varchar(45) NOT NULL,
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
`, table)); err != nil {
|
2021-08-13 11:28:47 +08:00
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
_, err := db.Model(table).OmitEmpty().Data(g.Map{
|
2021-01-11 19:30:22 +08:00
|
|
|
"id": 1,
|
|
|
|
"name": "",
|
|
|
|
}).Save()
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
})
|
2021-08-13 11:28:47 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
_, err := db.Model(table).OmitEmptyData().Data(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"name": "",
|
|
|
|
}).Save()
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
_, err := db.Model(table).OmitEmptyWhere().Data(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"name": "",
|
|
|
|
}).Save()
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2021-08-13 11:28:47 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_OmitNil(t *testing.T) {
|
|
|
|
table := fmt.Sprintf(`table_%s`, gtime.TimestampNanoStr())
|
2021-09-29 20:39:02 +08:00
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
2021-08-13 11:28:47 +08:00
|
|
|
CREATE TABLE IF NOT EXISTS %s (
|
|
|
|
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
|
|
name varchar(45) NOT NULL,
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
`, table)); err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
_, err := db.Model(table).OmitNil().Data(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"name": nil,
|
|
|
|
}).Save()
|
|
|
|
t.AssertNE(err, nil)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
_, err := db.Model(table).OmitNil().Data(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"name": "",
|
|
|
|
}).Save()
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2021-08-13 11:28:47 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
_, err := db.Model(table).OmitNilWhere().Data(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"name": "",
|
|
|
|
}).Save()
|
2022-03-10 11:36:40 +08:00
|
|
|
t.AssertNil(err)
|
2021-08-13 11:28:47 +08:00
|
|
|
})
|
2021-01-11 19:30:22 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 19:13:25 +08:00
|
|
|
func Test_Model_Option_Where(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-21 19:13:25 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2021-09-27 22:47:39 +08:00
|
|
|
r, err := db.Model(table).OmitEmpty().Data("nickname", 1).Where(g.Map{"id": 0, "passport": ""}).Where(1).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-21 19:13:25 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(n, TableSize)
|
2019-10-21 19:13:25 +08:00
|
|
|
})
|
2024-02-06 10:21:44 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-10-21 19:13:25 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).OmitEmpty().Data("nickname", 1).Where(g.Map{"id": 1, "passport": ""}).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-21 19:13:25 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-10-21 19:13:25 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err := db.Model(table).Where("id", 1).Fields("nickname").Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "1")
|
2019-09-29 14:27:09 +08:00
|
|
|
})
|
|
|
|
}
|
2019-10-29 19:45:21 +08:00
|
|
|
|
2020-06-03 21:36:16 +08:00
|
|
|
func Test_Model_Where_MultiSliceArguments(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Where(g.Map{
|
2020-06-03 21:36:16 +08:00
|
|
|
"id": g.Slice{1, 2, 3, 4},
|
|
|
|
"passport": g.Slice{"user_2", "user_3", "user_4"},
|
|
|
|
"nickname": g.Slice{"name_2", "name_4"},
|
|
|
|
"id >= 4": nil,
|
|
|
|
}).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-06-03 21:36:16 +08:00
|
|
|
t.Assert(len(r), 1)
|
|
|
|
t.Assert(r[0]["id"], 4)
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Where(g.Map{
|
2020-06-03 21:36:16 +08:00
|
|
|
"id": g.Slice{1, 2, 3},
|
|
|
|
"passport": g.Slice{"user_2", "user_3"},
|
2021-09-27 22:47:39 +08:00
|
|
|
}).WhereOr("nickname=?", g.Slice{"name_4"}).Where("id", 3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-06-03 21:36:16 +08:00
|
|
|
t.AssertGT(len(result), 0)
|
|
|
|
t.Assert(result["id"].Int(), 2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:45:21 +08:00
|
|
|
func Test_Model_FieldsEx(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
// Select.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).FieldsEx("create_time, id").Where("id in (?)", g.Slice{1, 2}).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(r), 2)
|
2024-09-10 17:51:22 +08:00
|
|
|
t.Assert(len(r[0]), 4)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(r[0]["id"], "")
|
|
|
|
t.Assert(r[0]["passport"], "user_1")
|
|
|
|
t.Assert(r[0]["password"], "pass_1")
|
|
|
|
t.Assert(r[0]["nickname"], "name_1")
|
|
|
|
t.Assert(r[0]["create_time"], "")
|
|
|
|
t.Assert(r[1]["id"], "")
|
|
|
|
t.Assert(r[1]["passport"], "user_2")
|
|
|
|
t.Assert(r[1]["password"], "pass_2")
|
|
|
|
t.Assert(r[1]["nickname"], "name_2")
|
|
|
|
t.Assert(r[1]["create_time"], "")
|
2019-10-29 19:45:21 +08:00
|
|
|
})
|
2019-12-04 22:38:58 +08:00
|
|
|
// Update.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).FieldsEx("password").Data(g.Map{"nickname": "123", "password": "456"}).Where("id", 3).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2019-10-29 19:45:21 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2019-10-29 19:45:21 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Where("id", 3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(one["nickname"], "123")
|
|
|
|
t.AssertNE(one["password"], "456")
|
2019-10-29 19:45:21 +08:00
|
|
|
})
|
|
|
|
}
|
2019-12-10 21:14:15 +08:00
|
|
|
|
2020-11-04 19:53:50 +08:00
|
|
|
func Test_Model_FieldsEx_WithReservedWords(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-12-04 23:07:54 +08:00
|
|
|
var (
|
|
|
|
table = "fieldsex_test_table"
|
2022-03-17 16:58:04 +08:00
|
|
|
sqlTpcPath = gtest.DataPath("reservedwords_table_tpl.sql")
|
2021-12-04 23:07:54 +08:00
|
|
|
sqlContent = gfile.GetContents(sqlTpcPath)
|
|
|
|
)
|
|
|
|
t.AssertNE(sqlContent, "")
|
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(sqlContent, table)); err != nil {
|
|
|
|
t.AssertNil(err)
|
|
|
|
}
|
|
|
|
defer dropTable(table)
|
2021-02-27 23:58:36 +08:00
|
|
|
_, err := db.Model(table).FieldsEx("content").One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-04 19:53:50 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-10 21:14:15 +08:00
|
|
|
func Test_Model_Prefix(t *testing.T) {
|
|
|
|
db := dbPrefix
|
2021-02-27 23:58:36 +08:00
|
|
|
table := fmt.Sprintf(`%s_%d`, TableName, gtime.TimestampNano())
|
|
|
|
createInitTableWithDb(db, TableNamePrefix1+table)
|
|
|
|
defer dropTable(TableNamePrefix1 + table)
|
2019-12-10 21:14:15 +08:00
|
|
|
// Select.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Where("id in (?)", g.Slice{1, 2}).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(r), 2)
|
|
|
|
t.Assert(r[0]["id"], "1")
|
|
|
|
t.Assert(r[1]["id"], "2")
|
2019-12-10 21:14:15 +08:00
|
|
|
})
|
|
|
|
// Select with alias.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table+" as u").Where("u.id in (?)", g.Slice{1, 2}).Order("u.id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(r), 2)
|
|
|
|
t.Assert(r[0]["id"], "1")
|
|
|
|
t.Assert(r[1]["id"], "2")
|
2019-12-10 21:14:15 +08:00
|
|
|
})
|
2021-08-02 21:13:00 +08:00
|
|
|
// Select with alias to struct.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
NickName string
|
|
|
|
}
|
|
|
|
var users []User
|
|
|
|
err := db.Model(table+" u").Where("u.id in (?)", g.Slice{1, 5}).Order("u.id asc").Scan(&users)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(users), 2)
|
|
|
|
t.Assert(users[0].Id, 1)
|
|
|
|
t.Assert(users[1].Id, 5)
|
|
|
|
})
|
2019-12-10 21:14:15 +08:00
|
|
|
// Select with alias and join statement.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table+" as u1").LeftJoin(table+" as u2", "u2.id=u1.id").Where("u1.id in (?)", g.Slice{1, 2}).Order("u1.id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(r), 2)
|
|
|
|
t.Assert(r[0]["id"], "1")
|
|
|
|
t.Assert(r[1]["id"], "2")
|
2019-12-10 21:14:15 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).As("u1").LeftJoin(table+" as u2", "u2.id=u1.id").Where("u1.id in (?)", g.Slice{1, 2}).Order("u1.id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(r), 2)
|
|
|
|
t.Assert(r[0]["id"], "1")
|
|
|
|
t.Assert(r[1]["id"], "2")
|
2020-01-08 21:24:33 +08:00
|
|
|
})
|
2019-12-10 21:14:15 +08:00
|
|
|
}
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2020-01-10 23:48:19 +08:00
|
|
|
func Test_Model_Schema1(t *testing.T) {
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema1)
|
2021-02-27 23:58:36 +08:00
|
|
|
table := fmt.Sprintf(`%s_%s`, TableName, gtime.TimestampNanoStr())
|
2020-01-07 22:14:32 +08:00
|
|
|
createInitTableWithDb(db, table)
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema2)
|
2020-01-07 22:14:32 +08:00
|
|
|
createInitTableWithDb(db, table)
|
|
|
|
defer func() {
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema1)
|
2020-01-07 22:14:32 +08:00
|
|
|
dropTableWithDb(db, table)
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema2)
|
2020-01-07 22:14:32 +08:00
|
|
|
dropTableWithDb(db, table)
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema1)
|
2020-01-07 22:14:32 +08:00
|
|
|
}()
|
|
|
|
// Method.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema1)
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Update(g.Map{"nickname": "name_100"}, "id=1")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-01-07 22:14:32 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err := db.Model(table).Value("nickname", "id=1")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_100")
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema2)
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err = db.Model(table).Value("nickname", "id=1")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_1")
|
2020-01-07 22:14:32 +08:00
|
|
|
})
|
|
|
|
// Model.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err := db.Model(table).Schema(TestSchema1).Value("nickname", "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_2")
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Schema(TestSchema1).Update(g.Map{"nickname": "name_200"}, "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-01-07 22:14:32 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err = db.Model(table).Schema(TestSchema1).Value("nickname", "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_200")
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err = db.Model(table).Schema(TestSchema2).Value("nickname", "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_2")
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err = db.Model(table).Schema(TestSchema1).Value("nickname", "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_200")
|
2020-01-07 22:14:32 +08:00
|
|
|
})
|
|
|
|
// Model.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-07 22:14:32 +08:00
|
|
|
i := 1000
|
2021-05-11 20:14:06 +08:00
|
|
|
_, err := db.Model(table).Schema(TestSchema1).Insert(g.Map{
|
2020-01-07 22:14:32 +08:00
|
|
|
"id": i,
|
|
|
|
"passport": fmt.Sprintf(`user_%d`, i),
|
|
|
|
"password": fmt.Sprintf(`pass_%d`, i),
|
|
|
|
"nickname": fmt.Sprintf(`name_%d`, i),
|
|
|
|
"create_time": gtime.NewFromStr("2018-10-24 10:00:00").String(),
|
|
|
|
"none-exist-field": 1,
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err := db.Model(table).Schema(TestSchema1).Value("nickname", "id=?", i)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_1000")
|
2020-01-07 22:14:32 +08:00
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
v, err = db.Model(table).Schema(TestSchema2).Value("nickname", "id=?", i)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "")
|
2020-01-07 22:14:32 +08:00
|
|
|
})
|
|
|
|
}
|
2020-01-10 23:48:19 +08:00
|
|
|
|
|
|
|
func Test_Model_Schema2(t *testing.T) {
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2020-01-10 23:48:19 +08:00
|
|
|
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema1)
|
2021-02-27 23:58:36 +08:00
|
|
|
table := fmt.Sprintf(`%s_%s`, TableName, gtime.TimestampNanoStr())
|
2020-01-10 23:48:19 +08:00
|
|
|
createInitTableWithDb(db, table)
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema2)
|
2020-01-10 23:48:19 +08:00
|
|
|
createInitTableWithDb(db, table)
|
|
|
|
defer func() {
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema1)
|
2020-01-10 23:48:19 +08:00
|
|
|
dropTableWithDb(db, table)
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema2)
|
2020-01-10 23:48:19 +08:00
|
|
|
dropTableWithDb(db, table)
|
|
|
|
|
2022-02-16 00:26:06 +08:00
|
|
|
db = db.Schema(TestSchema1)
|
2020-01-10 23:48:19 +08:00
|
|
|
}()
|
|
|
|
// Schema.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-10-29 15:12:31 +08:00
|
|
|
v, err := db.Schema(TestSchema1).Model(table).Value("nickname", "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_2")
|
2020-01-10 23:48:19 +08:00
|
|
|
|
2021-10-29 15:12:31 +08:00
|
|
|
r, err := db.Schema(TestSchema1).Model(table).Update(g.Map{"nickname": "name_200"}, "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-01-10 23:48:19 +08:00
|
|
|
n, _ := r.RowsAffected()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(n, 1)
|
2020-01-10 23:48:19 +08:00
|
|
|
|
2021-10-29 15:12:31 +08:00
|
|
|
v, err = db.Schema(TestSchema1).Model(table).Value("nickname", "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_200")
|
2020-01-10 23:48:19 +08:00
|
|
|
|
2021-10-29 15:12:31 +08:00
|
|
|
v, err = db.Schema(TestSchema2).Model(table).Value("nickname", "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_2")
|
2020-01-10 23:48:19 +08:00
|
|
|
|
2021-10-29 15:12:31 +08:00
|
|
|
v, err = db.Schema(TestSchema1).Model(table).Value("nickname", "id=2")
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_200")
|
2020-01-10 23:48:19 +08:00
|
|
|
})
|
|
|
|
// Schema.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-10 23:48:19 +08:00
|
|
|
i := 1000
|
2021-10-29 15:12:31 +08:00
|
|
|
_, err := db.Schema(TestSchema1).Model(table).Insert(g.Map{
|
2020-01-10 23:48:19 +08:00
|
|
|
"id": i,
|
|
|
|
"passport": fmt.Sprintf(`user_%d`, i),
|
|
|
|
"password": fmt.Sprintf(`pass_%d`, i),
|
|
|
|
"nickname": fmt.Sprintf(`name_%d`, i),
|
|
|
|
"create_time": gtime.NewFromStr("2018-10-24 10:00:00").String(),
|
|
|
|
"none-exist-field": 1,
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-01-10 23:48:19 +08:00
|
|
|
|
2021-10-29 15:12:31 +08:00
|
|
|
v, err := db.Schema(TestSchema1).Model(table).Value("nickname", "id=?", i)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "name_1000")
|
2020-01-10 23:48:19 +08:00
|
|
|
|
2021-10-29 15:12:31 +08:00
|
|
|
v, err = db.Schema(TestSchema2).Model(table).Value("nickname", "id=?", i)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(v.String(), "")
|
2020-01-10 23:48:19 +08:00
|
|
|
})
|
|
|
|
}
|
2020-03-24 20:58:11 +08:00
|
|
|
|
|
|
|
func Test_Model_FieldsExStruct(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int `orm:"id" json:"id"`
|
|
|
|
Passport string `orm:"password" json:"pass_port"`
|
|
|
|
Password string `orm:"password" json:"password"`
|
|
|
|
NickName string `orm:"nickname" json:"nick__name"`
|
|
|
|
}
|
|
|
|
user := &User{
|
|
|
|
Id: 1,
|
|
|
|
Passport: "111",
|
|
|
|
Password: "222",
|
|
|
|
NickName: "333",
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).FieldsEx("create_time, password").OmitEmpty().Data(user).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-24 20:58:11 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-24 20:58:11 +08:00
|
|
|
t.Assert(n, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int `orm:"id" json:"id"`
|
|
|
|
Passport string `orm:"password" json:"pass_port"`
|
|
|
|
Password string `orm:"password" json:"password"`
|
|
|
|
NickName string `orm:"nickname" json:"nick__name"`
|
|
|
|
}
|
|
|
|
users := make([]*User, 0)
|
|
|
|
for i := 100; i < 110; i++ {
|
|
|
|
users = append(users, &User{
|
|
|
|
Id: i,
|
|
|
|
Passport: fmt.Sprintf(`passport_%d`, i),
|
|
|
|
Password: fmt.Sprintf(`password_%d`, i),
|
|
|
|
NickName: fmt.Sprintf(`nickname_%d`, i),
|
|
|
|
})
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).FieldsEx("create_time, password").
|
2020-03-24 20:58:11 +08:00
|
|
|
OmitEmpty().
|
|
|
|
Batch(2).
|
|
|
|
Data(users).
|
|
|
|
Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-24 20:58:11 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-24 20:58:11 +08:00
|
|
|
t.Assert(n, 10)
|
|
|
|
})
|
|
|
|
}
|
2020-03-30 20:44:36 +08:00
|
|
|
|
|
|
|
func Test_Model_OmitEmpty_Time(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int `orm:"id" json:"id"`
|
|
|
|
Passport string `orm:"password" json:"pass_port"`
|
|
|
|
Password string `orm:"password" json:"password"`
|
|
|
|
Time time.Time `orm:"create_time" `
|
|
|
|
}
|
|
|
|
user := &User{
|
|
|
|
Id: 1,
|
|
|
|
Passport: "111",
|
|
|
|
Password: "222",
|
|
|
|
Time: time.Time{},
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).OmitEmpty().Data(user).WherePri(1).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-30 20:44:36 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-03-30 20:44:36 +08:00
|
|
|
t.Assert(n, 1)
|
|
|
|
})
|
|
|
|
}
|
2020-04-02 20:52:37 +08:00
|
|
|
|
|
|
|
func Test_Result_Chunk(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Order("id asc").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-02 20:52:37 +08:00
|
|
|
chunks := r.Chunk(3)
|
|
|
|
t.Assert(len(chunks), 4)
|
|
|
|
t.Assert(chunks[0][0]["id"].Int(), 1)
|
|
|
|
t.Assert(chunks[1][0]["id"].Int(), 4)
|
|
|
|
t.Assert(chunks[2][0]["id"].Int(), 7)
|
|
|
|
t.Assert(chunks[3][0]["id"].Int(), 10)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_DryRun(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
db.SetDryRun(true)
|
|
|
|
defer db.SetDryRun(false)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-02 20:52:37 +08:00
|
|
|
t.Assert(one["id"], 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Data("passport", "port_1").WherePri(1).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-02 20:52:37 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-02 20:52:37 +08:00
|
|
|
t.Assert(n, 0)
|
|
|
|
})
|
|
|
|
}
|
2020-04-09 22:00:02 +08:00
|
|
|
|
2020-09-03 21:57:58 +08:00
|
|
|
func Test_Model_Join_SubQuery(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
subQuery := fmt.Sprintf("select * from `%s`", table)
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table, "t1").Fields("t2.id").LeftJoin(subQuery, "t2", "t2.id=t1.id").Array()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(len(r), TableSize)
|
2020-09-03 21:57:58 +08:00
|
|
|
t.Assert(r[0], "1")
|
2021-02-27 23:58:36 +08:00
|
|
|
t.Assert(r[TableSize-1], TableSize)
|
2020-09-03 21:57:58 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-09 22:00:02 +08:00
|
|
|
func Test_Model_Cache(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err := db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test1",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
t.Assert(one["passport"], "user_1")
|
|
|
|
|
2021-02-27 23:58:36 +08:00
|
|
|
r, err := db.Model(table).Data("passport", "user_100").WherePri(1).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
t.Assert(n, 1)
|
|
|
|
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err = db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test1",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
t.Assert(one["passport"], "user_1")
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err = db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test1",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
t.Assert(one["passport"], "user_100")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err := db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test2",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
t.Assert(one["passport"], "user_2")
|
|
|
|
|
2021-11-09 16:06:31 +08:00
|
|
|
r, err := db.Model(table).Data("passport", "user_200").Cache(gdb.CacheOption{
|
|
|
|
Duration: -1,
|
|
|
|
Name: "test2",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(2).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
t.Assert(n, 1)
|
|
|
|
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err = db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test2",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-09 22:00:02 +08:00
|
|
|
t.Assert(one["passport"], "user_200")
|
|
|
|
})
|
2020-04-26 21:31:55 +08:00
|
|
|
// transaction.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
// make cache for id 3
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err := db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test3",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(one["passport"], "user_3")
|
|
|
|
|
2021-11-09 16:06:31 +08:00
|
|
|
r, err := db.Model(table).Data("passport", "user_300").Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test3",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(3).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(n, 1)
|
|
|
|
|
feature/v2.3.0 (#2296)
* up
* rename function names for package gtcp/gudp; add proxy example for gtcp.Server (#2295)
* fix router supported for handler of package ghttp; fix json tag name issue when it contains for package goai
* add proxy example for http server
* rename function names for package gtcp/gudp; add proxy example for gtcp.Server
* move TX from struct to interface for package gdb (#2247)
* move TX from struct to interface for package gdb
* i updates
* up
* up
* fix comment
Co-authored-by: houseme <housemecn@gmail.com>
* move `go-redis` implements `Adapter` from package `gredis` to `contrib/nosql/redis`; add redis string operation functions for package `gredis` (#2240)
* unify configuration pattern of for package gdb
* version updates
* improve implements `internal/rwmutex` and `internal/mutex`; add `TablesFields` cache implements in `gdb.Core` instead of `contrib/drivers`; add `ClearTableFields` and `ClearCache` functions for `gdb.Core` (#2128)
* add ClearTableFiels/ClearCache for Core of package gdb
* improve TableFields for contrib/drivers
* fix UT case for contrib/drivers/clickhouse
* remove unecessary attribute state for internal/rwmutex and internal/mutex
* add ClearTableFieldsAll/ClearCacheAll for gdb.Core
* improve clickhouse driver
* improve clickhouse driver
* fix ut
* feat: improve import
Co-authored-by: daguang <daguang830@gmail.com>
Co-authored-by: houseme <housemecn@gmail.com>
* refract builtin rules management mechanism, add `eq/not-eq/gt/gte/lt/lte/before/before-equal/after/after-equal/array/not-regex` rules for for package `gvalid` (#2133)
* refract builtin rules management for package gvalid
* refract builtin rules management for package gvalid
* refract builtin rules management for package gvalid
* add valiation rules and implements for package gvalid
* UT cases update for package gvalid
* improve error message of fields validation for package gvalid
* up
* add more validation rules for package gvalid
* add validation rule foreach for package gvalid (#2136)
* add ToSQL/CatchSQL funcions for package gdb (#2137)
* add ToSQL/CatchSQL funcions for package gdb
* Update gdb_core_underlying.go
* fix ci
Co-authored-by: houseme <housemecn@gmail.com>
* add redis interface for package gredis
* up
* remove `FilteredLink` function for DB and all driver implements and improve details for package gdb (#2142)
* fix: pgsql DoExec Transaction checks (#2101)
Co-authored-by: John Guo <john@johng.cn>
* improve package gdb
* up
* up
* up
* up
* up
* add DriverWrapper and DriverWarapperDB for package gdb
* add DriverWrapper and DriverWarapperDB for package gdb
* up
Co-authored-by: HaiLaz <739476267@qq.com>
* add new database driver `dm`
* add drivers dm
* upd go version
* add gf ci yaml
Co-authored-by: Xu <zhenghao.xu>
* move go-redis implements from package gredis to contrib/nosql/redis; add redis string operation functions for package gredis
* improve `contrib/drivers/dm` (#2144)
* improve contrib/drivers/dm
* format TODO list info
* 1) add config.Name is required
2) The upper layer no longer needs to specify the schema
3) Adjust unit tests
Co-authored-by: Xu <zhenghao.xu>
Co-authored-by: houseme <housemecn@gmail.com>
* move redis adapter related ut case from package gcache/gsession to package contrib/nosql/redis
* up
* up
* up
* up
* up
* improve comment
* add implements of `gcfg.Adapter` using kubernetes configmap (#2145)
* remove Logger from kubecm.Client
* README updates for package kubecm
* error message update for package gredis
* comment update for package gdb
* Feature/v2.2.0 gredis (#2155)
* improve package gredis (#2162)
* improve package gredis
* Update gredis_redis_group_list.go
* fix
* up
Co-authored-by: houseme <housemecn@gmail.com>
* up
* up
* up
* up
* up
* up
* add func Test_GroupScript_Eval
* ut cases for group string
* UT cases update for group script
* mv redis operation implements to contrib/nosql/redis from package gredis
* test: add redis group list unit test (#2248)
* test: add redis group list unit test
* improve comment
* test: fix redis group list unit test
Co-authored-by: houseme <housemecn@gmail.com>
* up
* add func Test_GroupGeneric_Copy, Test_GroupGeneric_Exists,Test_GroupGeneric_Type,Test_GroupGeneric_Unlink,Test_GroupGeneric_Rename,Test_GroupGeneric_Move,Test_GroupGeneric_Del
* add Redis GroupGeneric UnitTest (#2253)
add func Test_GroupGeneric_RandomKey,Test_GroupGeneric_DBSize,Test_GroupGeneric_Keys,Test_GroupGeneric_FlushDB,Test_GroupGeneric_FlushAll,Test_GroupGeneric_Expire,Test_GroupGeneric_ExpireAt
* hash test case completed (#2260)
Co-authored-by: junler <sunjun@bookan.com>
* add Redis GroupGeneric Unit Test part2 (#2258)
* up
* ci updates
* ci updates
* up
* Feature/contrib redis fsprouts (#2274)
* Feature/contrib redis starck (#2275)
* up
* up
* fix `/*` router supported for handler of package ghttp; fix json tag name issue when it contains `,` for package goai; add proxy example for http server (#2294)
* fix router supported for handler of package ghttp; fix json tag name issue when it contains for package goai
* add proxy example for http server
* fix: update szenius/set-timezone@v1.1 (#2293)
* add Tag* functions to retreive most commonly used tag value from struct field for package gstructs; use description tag as default value if brief is empty for gcmd.Argument (#2299)
* fix cache issue in Count/Value functions for gdb.Model (#2300)
* add Tag* functions to retreive most commonly used tag value from struct field for package gstructs; use description tag as default value if brief is empty for gcmd.Argument
* fix cache issue in Count/Value functions for gdb.Model
* add more ut case for package gdb
* version updates
* add minus of `start` parameter support for `gstr.Substr`, like the `substr` function in `PHP` (#2297)
* Make the substr like the substr in PHP
Make the substr like the substr in PHP
* Update gstr_z_unit_test.go
* Update gstr_z_unit_test.go
* Make the SubStrRune like the mb_substr in PHP
Make the SubStrRune like the mb_substr in PHP
* Update gstr_z_unit_test.go
* Update gstr_z_unit_test.go
* Update gins_z_unit_view_test.go
* Update gview_z_unit_test.go
* add ut cases for package gcode (#2307)
* add ut cases for package gerror (#2304)
* add ut cases for package gerror
* add ut cases for package gerror
* add ut cases for package gtime (#2303)
* add ut cases for package gtime
* add ut cases for package gtime
* add ut cases for package gtime
* add ut cases for package glog (#2302)
* add ut cases for package glog
* add ut cases for package glog
* add ut cases for package glog
* add ut cases for package glog
* add ut cases for package glog
* add ut cases for package glog
* change result data type of function Count from int to int64 for package gdb (#2298)
* feat: modify model count value int64
* fix
* fix:modify int64
* fix
* feat: cmd gf prebuild suport oracle (#2312)
* add ut cases for package g (#2315)
* add ut cases for package gdebug (#2313)
* add ut cases for package gdebug
* add ut cases for package gdebug
* add ut cases for package gdebug
Co-authored-by: houseme <housemecn@gmail.com>
* add zookeeper registry support (#2284)
* add ut cases for package glog part2 (#2317)
* fix invalid UpdatedAt usage in soft deleting feature for package gdb (#2323)
* fix issue in failed installing when there's shortcut between file paths for command install (#2326)
* fix issue in failed installing when has shortcut between file paths for command install
* version updates
* template for command gf updates
* improve lru clearing for package gcache (#2327)
* add ut cases for package ghttp_middleware and ghttp_request (#2344)
* add ut cases for package ghttp_middleware
* add ut cases for package ghttp_request
* add ut cases for package ghttp_request
* add ut cases for package ghttp_response (#2352)
* add ut cases for package ghttp_response
* add ut cases for package ghttp_response
* add ut cases for package ghttp_response
* add ut cases for package ghttp_request (#2351)
* add ut cases for package ghttp_middleware
* add ut cases for package ghttp_request
* add ut cases for package ghttp_request
* add ut cases for package ghttp_request
* add ut cases for package ghttp_request - form
* add ut cases for package ghttp_request - query
* add ut cases for package ghttp_request - request
* add ut cases for package ghttp_request - router
* add ut cases for package gcache (#2341)
* gTcp Example Function:
1.NewConn 2.NewConnTLS 3.NewConnKeyCrt
* gTcp Example Function:
1.Send
* add example function ExampleConn_Recv and ExampleConn_RecvWithTimeout
* add example function
1. ExampleConn_SendWithTimeout
2. ExampleConn_RecvLine
3. ExampleConn_RecvTill
* add example function
1. ExampleConn_SendRecv
2. ExampleConn_SendRecvWithTimeout
3. ExampleConn_SetDeadline
4. ExampleConn_SetReceiveBufferWait
* add gtcp test function
1. Test_Package_Option_HeadSize4
2. Test_Package_Option_Error
* add gtcp example function
1. ExampleGetFreePorts
2. ExampleSend
3. ExampleSendRecv
4. ExampleSendWithTimeout
5. ExampleSendRecvWithTimeout
6. ExampleMustGetFreePort
* add gtcp example function
1. ExampleSendPkg
2. ExampleSendRecvPkg
3. ExampleSendPkgWithTimeout
4. ExampleSendRecvPkgWithTimeout
* add gtcp test function
1. Test_Pool_Send
2. Test_Pool_Recv
3. Test_Pool_RecvLine
4. Test_Pool_RecvTill
5. Test_Pool_RecvWithTimeout
6. Test_Pool_SendWithTimeout
7. Test_Pool_SendRecvWithTimeout
* fix
* add gtcp example function
1. ExampleGetServer
2. ExampleSetAddress
3. ExampleSetHandler
4. ExampleRun_NilHandle
* exec CI
* exec CI
* exec CI
* modify test server address
* modify and exec CI
* modify and exec CI
* modify and exec CI
* modify and exec CI
* modify and exec CI
* modify and exec CI
* add example funcion ExampleConn_Recv_Once and fix
* fix
* add some error case in example function
* add some error case in example function
* 1.add example function ExampleNewServerKeyCrt
2.add function SendRecvPkgWithTimeout unit test
* add function Test_Server_NewServerKeyCrt unit test
* revert
* add function Test_Package_Timeout, Test_Package_Option_HeadSize3, Test_Conn_RecvPkgError unit test
* fix
* add example function
1.ExampleClient_Clone
2.ExampleLoadKeyCrt
* add example function
1.ExampleNewNetConnKeyCrt
* fix
* add example function
1.ExampleClient_DeleteBytes
2.ExampleClient_HeadBytes
3.ExampleClient_PatchBytes
4.ExampleClient_ConnectBytes
5.ExampleClient_OptionsBytes
6.ExampleClient_TraceBytes
7.ExampleClient_PutBytes
* add example function
1.ExampleClient_Prefix
2.ExampleClient_Retry
3.ExampleClient_RedirectLimit
* add example function
1.ExampleClient_SetBrowserMode
2.ExampleClient_SetHeader
3.ExampleClient_SetRedirectLimit
* add example function
1.ExampleClient_SetTLSKeyCrt
2.ExampleClient_SetTLSConfig
modify example funcion
1.ExampleClient_SetProxy
2.ExampleClient_Proxy
* add example function
1.ExampleClient_PutContent
2.ExampleClient_DeleteContent
3.ExampleClient_HeadContent
4.ExampleClient_PatchContent
5.ExampleClient_ConnectContent
6.ExampleClient_OptionsContent
7.ExampleClient_TraceContent
8.ExampleClient_RequestContent
* add example function
1.ExampleClient_RawRequest
* add unit function
1.TestGetFreePorts
2.TestNewConn
3.TestNewConnTLS
4.TestNewConnKeyCrt
5.TestConn_SendWithTimeout
* add unit function
1.TestConn_Send
2.TestConn_SendRecv
3.TestConn_SendRecvWithTimeout
* modify
* modify
* add example function
1.TestConn_SetReceiveBufferWait
2.TestNewNetConnKeyCrt
3.TestSend
* add example function
1.TestSendRecv
2.TestSendWithTimeout
* add unit function
1.TestMustGetFreePort
2.TestSendRecvWithTimeout
3.TestSendPkg
* add client recevied server's response content assert
* modify
* modify
* add example function
1.TestSendRecvPkg
2.TestSendPkgWithTimeout
3.TestSendRecvPkgWithTimeout
* add GetAddress() function
add unit funciton
1.TestNewServer
2.TestGetServer
3.TestServer_SetAddress
4.TestServer_SetHandler
5.TestServer_Run
* modify
* modify
* add unit funciton
1.TestLoadKeyCrt
* modify
* delete function fromHex
* add gclient dump unit test
* add example function
1.ExampleClient_Put
2.ExampleClient_Delete
3.ExampleClient_Head
4.ExampleClient_Patch
5.ExampleClient_Connect
6.ExampleClient_Options
7.ExampleClient_Trace
* add example function
1.TestClient_DoRequest
* add example function
1.ExampleClient_PutVar
2.ExampleClient_DeleteVar
3.ExampleClient_HeadVar
4.ExampleClient_PatchVar
5.ExampleClient_ConnectVar
6.ExampleClient_OptionsVar
7.ExampleClient_TraceVar
* modify
* modify
* add CustomProvider function
* modify
* add unit funciton
1.Test_NewConn
2.Test_GetFreePorts
* add unit funciton
1.Test_Server
* garray_normal_any code converage
* garray_normal_int code converage
* garray_normal_str code converage
* garray_sorted_any code converage
* garray_sorted_int code converage
* garray_sorted_str code converage
* glist code converage
* gmap, gmap_hash_any_any_map code converage
* gmap_hash_int_any_map code converage
* gmap_hash_int_any_map code converage
* gmap_hash_int_int_map code converage
* gmap_hash_int_str_map code converage
* gmap_hash_str_any_map code converage
* gmap_hash_str_int_map code converage
* gmap_hash_str_str_map code converage
* gmap_list_map code converage
* gmap_list_map code converage
* revert gf.yml
* add gtest unit test function
* add ut cases for package gcache
* add ut cases for package gcache
* add ut cases for package gcache
* add ut cases for package gcache
* add ut cases for package gcache
* modify
Co-authored-by: John Guo <john@johng.cn>
* improve ut case for package internal/rwmutex (#2364)
* fix issue when only one file was uploaded in batch receiver attribute (#2365)
* fix fixed An error occurred when only one file was uploaded in batches and add unit testing(#2092)
* fix issue uploading files for ghttp.Server
Co-authored-by: yxh <yxh1103@qq.com>
* fix issue #2334 when accessing static files with cache time (#2366)
* Solve the problem of error when accessing static files with cache time.
Error message:
2022-11-29 19:40:11.090 [ERRO] http: superfluous response.WriteHeader call from github.com/gogf/gf/v2/net/ghttp.(*ResponseWriter).Flush (ghttp_response_writer.go:58)
Stack:
Verification method:
curl 'http://127.0.0.1:8000/' -H 'If-Modified-Since: Thu, 08 Dec 2022 03:13:55 GMT' --compressed
* Solve the problem of error when accessing static files with cache time.
Error message:
2022-11-29 19:40:11.090 [ERRO] http: superfluous response.WriteHeader call from github.com/gogf/gf/v2/net/ghttp.(*ResponseWriter).Flush (ghttp_response_writer.go:58)
Stack:
Verification method:
curl 'http://127.0.0.1:8000/' -H 'If-Modified-Since: Thu, 08 Dec 2022 03:13:55 GMT' --compressed
* Solve the problem of error when accessing static files with cache time.
Error message:
2022-11-29 19:40:11.090 [ERRO] http: superfluous response.WriteHeader call from github.com/gogf/gf/v2/net/ghttp.(*ResponseWriter).Flush (ghttp_response_writer.go:58)
Stack:
Verification method:
curl 'http://127.0.0.1:8000/' -H 'If-Modified-Since: Thu, 08 Dec 2022 03:13:55 GMT' --compressed
* fix issue #2334 when accessing static files with cache time
* up
Co-authored-by: 曾洪亮 <hongliang.zeng@i-soft.com.cn>
Co-authored-by: houseme <housemecn@gmail.com>
* fix issue in cycle dumping for g.Dump (#2367)
* fix issue in cycle dumping for g.Dump
* up
* up
* up
Co-authored-by: houseme <housemecn@gmail.com>
* 由于 clickhouse 的 position的初始值为 1,导致gdb_core_utility.HasField 中对 fieldsArray 初始化出错 (#2346)
* 由于 clickhouse 的 position的初始值为 1,导致gdb_core_utility.HasField 中对 fieldsArray 初始化出错
* 修复单元测试
* 修复单元测试
* 补充单元测试
* 增加CK防御性代码
Co-authored-by: longl <longlei@dealmap.cloud>
Co-authored-by: houseme <housemecn@gmail.com>
* fix: ghttp server static path config (#2335)
Co-authored-by: daguang <daguang830@gmail.com>
Co-authored-by: houseme <housemecn@gmail.com>
Co-authored-by: ftl <1139556759@qq.com>
Co-authored-by: HaiLaz <739476267@qq.com>
Co-authored-by: zhonghuaxunGM <50815786+zhonghuaxunGM@users.noreply.github.com>
Co-authored-by: huangqian <huangqian1985@qq.com>
Co-authored-by: junler <827640651@qq.com>
Co-authored-by: junler <sunjun@bookan.com>
Co-authored-by: Starccck <28645972+starccck@users.noreply.github.com>
Co-authored-by: Jinhongyu <30454170+cnjinhy@users.noreply.github.com>
Co-authored-by: YuanXin Hu <huyuanxin1999@outlook.com>
Co-authored-by: yxh <yxh1103@qq.com>
Co-authored-by: 曾洪亮 <hongliang.zeng@i-soft.com.cn>
Co-authored-by: long <48313408+qq375251855@users.noreply.github.com>
Co-authored-by: longl <longlei@dealmap.cloud>
Co-authored-by: houseme <housemecn@gmail.com>
Co-authored-by: daguang <daguang830@gmail.com>
Co-authored-by: ftl <1139556759@qq.com>
Co-authored-by: HaiLaz <739476267@qq.com>
Co-authored-by: zhonghuaxunGM <50815786+zhonghuaxunGM@users.noreply.github.com>
Co-authored-by: huangqian <huangqian1985@qq.com>
Co-authored-by: junler <827640651@qq.com>
Co-authored-by: junler <sunjun@bookan.com>
Co-authored-by: Starccck <28645972+starccck@users.noreply.github.com>
Co-authored-by: Jinhongyu <30454170+cnjinhy@users.noreply.github.com>
Co-authored-by: YuanXin Hu <huyuanxin1999@outlook.com>
Co-authored-by: yxh <yxh1103@qq.com>
Co-authored-by: 曾洪亮 <hongliang.zeng@i-soft.com.cn>
Co-authored-by: long <48313408+qq375251855@users.noreply.github.com>
Co-authored-by: longl <longlei@dealmap.cloud>
2023-01-09 14:43:10 +08:00
|
|
|
err = db.Transaction(context.TODO(), func(ctx context.Context, tx gdb.TX) error {
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err := tx.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test3",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(one["passport"], "user_300")
|
|
|
|
return nil
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err = db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test3",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(3).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(one["passport"], "user_3")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
// make cache for id 4
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err := db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test4",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(4).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(one["passport"], "user_4")
|
|
|
|
|
2021-11-09 16:06:31 +08:00
|
|
|
r, err := db.Model(table).Data("passport", "user_400").Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test3",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(4).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(n, 1)
|
|
|
|
|
feature/v2.3.0 (#2296)
* up
* rename function names for package gtcp/gudp; add proxy example for gtcp.Server (#2295)
* fix router supported for handler of package ghttp; fix json tag name issue when it contains for package goai
* add proxy example for http server
* rename function names for package gtcp/gudp; add proxy example for gtcp.Server
* move TX from struct to interface for package gdb (#2247)
* move TX from struct to interface for package gdb
* i updates
* up
* up
* fix comment
Co-authored-by: houseme <housemecn@gmail.com>
* move `go-redis` implements `Adapter` from package `gredis` to `contrib/nosql/redis`; add redis string operation functions for package `gredis` (#2240)
* unify configuration pattern of for package gdb
* version updates
* improve implements `internal/rwmutex` and `internal/mutex`; add `TablesFields` cache implements in `gdb.Core` instead of `contrib/drivers`; add `ClearTableFields` and `ClearCache` functions for `gdb.Core` (#2128)
* add ClearTableFiels/ClearCache for Core of package gdb
* improve TableFields for contrib/drivers
* fix UT case for contrib/drivers/clickhouse
* remove unecessary attribute state for internal/rwmutex and internal/mutex
* add ClearTableFieldsAll/ClearCacheAll for gdb.Core
* improve clickhouse driver
* improve clickhouse driver
* fix ut
* feat: improve import
Co-authored-by: daguang <daguang830@gmail.com>
Co-authored-by: houseme <housemecn@gmail.com>
* refract builtin rules management mechanism, add `eq/not-eq/gt/gte/lt/lte/before/before-equal/after/after-equal/array/not-regex` rules for for package `gvalid` (#2133)
* refract builtin rules management for package gvalid
* refract builtin rules management for package gvalid
* refract builtin rules management for package gvalid
* add valiation rules and implements for package gvalid
* UT cases update for package gvalid
* improve error message of fields validation for package gvalid
* up
* add more validation rules for package gvalid
* add validation rule foreach for package gvalid (#2136)
* add ToSQL/CatchSQL funcions for package gdb (#2137)
* add ToSQL/CatchSQL funcions for package gdb
* Update gdb_core_underlying.go
* fix ci
Co-authored-by: houseme <housemecn@gmail.com>
* add redis interface for package gredis
* up
* remove `FilteredLink` function for DB and all driver implements and improve details for package gdb (#2142)
* fix: pgsql DoExec Transaction checks (#2101)
Co-authored-by: John Guo <john@johng.cn>
* improve package gdb
* up
* up
* up
* up
* up
* add DriverWrapper and DriverWarapperDB for package gdb
* add DriverWrapper and DriverWarapperDB for package gdb
* up
Co-authored-by: HaiLaz <739476267@qq.com>
* add new database driver `dm`
* add drivers dm
* upd go version
* add gf ci yaml
Co-authored-by: Xu <zhenghao.xu>
* move go-redis implements from package gredis to contrib/nosql/redis; add redis string operation functions for package gredis
* improve `contrib/drivers/dm` (#2144)
* improve contrib/drivers/dm
* format TODO list info
* 1) add config.Name is required
2) The upper layer no longer needs to specify the schema
3) Adjust unit tests
Co-authored-by: Xu <zhenghao.xu>
Co-authored-by: houseme <housemecn@gmail.com>
* move redis adapter related ut case from package gcache/gsession to package contrib/nosql/redis
* up
* up
* up
* up
* up
* improve comment
* add implements of `gcfg.Adapter` using kubernetes configmap (#2145)
* remove Logger from kubecm.Client
* README updates for package kubecm
* error message update for package gredis
* comment update for package gdb
* Feature/v2.2.0 gredis (#2155)
* improve package gredis (#2162)
* improve package gredis
* Update gredis_redis_group_list.go
* fix
* up
Co-authored-by: houseme <housemecn@gmail.com>
* up
* up
* up
* up
* up
* up
* add func Test_GroupScript_Eval
* ut cases for group string
* UT cases update for group script
* mv redis operation implements to contrib/nosql/redis from package gredis
* test: add redis group list unit test (#2248)
* test: add redis group list unit test
* improve comment
* test: fix redis group list unit test
Co-authored-by: houseme <housemecn@gmail.com>
* up
* add func Test_GroupGeneric_Copy, Test_GroupGeneric_Exists,Test_GroupGeneric_Type,Test_GroupGeneric_Unlink,Test_GroupGeneric_Rename,Test_GroupGeneric_Move,Test_GroupGeneric_Del
* add Redis GroupGeneric UnitTest (#2253)
add func Test_GroupGeneric_RandomKey,Test_GroupGeneric_DBSize,Test_GroupGeneric_Keys,Test_GroupGeneric_FlushDB,Test_GroupGeneric_FlushAll,Test_GroupGeneric_Expire,Test_GroupGeneric_ExpireAt
* hash test case completed (#2260)
Co-authored-by: junler <sunjun@bookan.com>
* add Redis GroupGeneric Unit Test part2 (#2258)
* up
* ci updates
* ci updates
* up
* Feature/contrib redis fsprouts (#2274)
* Feature/contrib redis starck (#2275)
* up
* up
* fix `/*` router supported for handler of package ghttp; fix json tag name issue when it contains `,` for package goai; add proxy example for http server (#2294)
* fix router supported for handler of package ghttp; fix json tag name issue when it contains for package goai
* add proxy example for http server
* fix: update szenius/set-timezone@v1.1 (#2293)
* add Tag* functions to retreive most commonly used tag value from struct field for package gstructs; use description tag as default value if brief is empty for gcmd.Argument (#2299)
* fix cache issue in Count/Value functions for gdb.Model (#2300)
* add Tag* functions to retreive most commonly used tag value from struct field for package gstructs; use description tag as default value if brief is empty for gcmd.Argument
* fix cache issue in Count/Value functions for gdb.Model
* add more ut case for package gdb
* version updates
* add minus of `start` parameter support for `gstr.Substr`, like the `substr` function in `PHP` (#2297)
* Make the substr like the substr in PHP
Make the substr like the substr in PHP
* Update gstr_z_unit_test.go
* Update gstr_z_unit_test.go
* Make the SubStrRune like the mb_substr in PHP
Make the SubStrRune like the mb_substr in PHP
* Update gstr_z_unit_test.go
* Update gstr_z_unit_test.go
* Update gins_z_unit_view_test.go
* Update gview_z_unit_test.go
* add ut cases for package gcode (#2307)
* add ut cases for package gerror (#2304)
* add ut cases for package gerror
* add ut cases for package gerror
* add ut cases for package gtime (#2303)
* add ut cases for package gtime
* add ut cases for package gtime
* add ut cases for package gtime
* add ut cases for package glog (#2302)
* add ut cases for package glog
* add ut cases for package glog
* add ut cases for package glog
* add ut cases for package glog
* add ut cases for package glog
* add ut cases for package glog
* change result data type of function Count from int to int64 for package gdb (#2298)
* feat: modify model count value int64
* fix
* fix:modify int64
* fix
* feat: cmd gf prebuild suport oracle (#2312)
* add ut cases for package g (#2315)
* add ut cases for package gdebug (#2313)
* add ut cases for package gdebug
* add ut cases for package gdebug
* add ut cases for package gdebug
Co-authored-by: houseme <housemecn@gmail.com>
* add zookeeper registry support (#2284)
* add ut cases for package glog part2 (#2317)
* fix invalid UpdatedAt usage in soft deleting feature for package gdb (#2323)
* fix issue in failed installing when there's shortcut between file paths for command install (#2326)
* fix issue in failed installing when has shortcut between file paths for command install
* version updates
* template for command gf updates
* improve lru clearing for package gcache (#2327)
* add ut cases for package ghttp_middleware and ghttp_request (#2344)
* add ut cases for package ghttp_middleware
* add ut cases for package ghttp_request
* add ut cases for package ghttp_request
* add ut cases for package ghttp_response (#2352)
* add ut cases for package ghttp_response
* add ut cases for package ghttp_response
* add ut cases for package ghttp_response
* add ut cases for package ghttp_request (#2351)
* add ut cases for package ghttp_middleware
* add ut cases for package ghttp_request
* add ut cases for package ghttp_request
* add ut cases for package ghttp_request
* add ut cases for package ghttp_request - form
* add ut cases for package ghttp_request - query
* add ut cases for package ghttp_request - request
* add ut cases for package ghttp_request - router
* add ut cases for package gcache (#2341)
* gTcp Example Function:
1.NewConn 2.NewConnTLS 3.NewConnKeyCrt
* gTcp Example Function:
1.Send
* add example function ExampleConn_Recv and ExampleConn_RecvWithTimeout
* add example function
1. ExampleConn_SendWithTimeout
2. ExampleConn_RecvLine
3. ExampleConn_RecvTill
* add example function
1. ExampleConn_SendRecv
2. ExampleConn_SendRecvWithTimeout
3. ExampleConn_SetDeadline
4. ExampleConn_SetReceiveBufferWait
* add gtcp test function
1. Test_Package_Option_HeadSize4
2. Test_Package_Option_Error
* add gtcp example function
1. ExampleGetFreePorts
2. ExampleSend
3. ExampleSendRecv
4. ExampleSendWithTimeout
5. ExampleSendRecvWithTimeout
6. ExampleMustGetFreePort
* add gtcp example function
1. ExampleSendPkg
2. ExampleSendRecvPkg
3. ExampleSendPkgWithTimeout
4. ExampleSendRecvPkgWithTimeout
* add gtcp test function
1. Test_Pool_Send
2. Test_Pool_Recv
3. Test_Pool_RecvLine
4. Test_Pool_RecvTill
5. Test_Pool_RecvWithTimeout
6. Test_Pool_SendWithTimeout
7. Test_Pool_SendRecvWithTimeout
* fix
* add gtcp example function
1. ExampleGetServer
2. ExampleSetAddress
3. ExampleSetHandler
4. ExampleRun_NilHandle
* exec CI
* exec CI
* exec CI
* modify test server address
* modify and exec CI
* modify and exec CI
* modify and exec CI
* modify and exec CI
* modify and exec CI
* modify and exec CI
* add example funcion ExampleConn_Recv_Once and fix
* fix
* add some error case in example function
* add some error case in example function
* 1.add example function ExampleNewServerKeyCrt
2.add function SendRecvPkgWithTimeout unit test
* add function Test_Server_NewServerKeyCrt unit test
* revert
* add function Test_Package_Timeout, Test_Package_Option_HeadSize3, Test_Conn_RecvPkgError unit test
* fix
* add example function
1.ExampleClient_Clone
2.ExampleLoadKeyCrt
* add example function
1.ExampleNewNetConnKeyCrt
* fix
* add example function
1.ExampleClient_DeleteBytes
2.ExampleClient_HeadBytes
3.ExampleClient_PatchBytes
4.ExampleClient_ConnectBytes
5.ExampleClient_OptionsBytes
6.ExampleClient_TraceBytes
7.ExampleClient_PutBytes
* add example function
1.ExampleClient_Prefix
2.ExampleClient_Retry
3.ExampleClient_RedirectLimit
* add example function
1.ExampleClient_SetBrowserMode
2.ExampleClient_SetHeader
3.ExampleClient_SetRedirectLimit
* add example function
1.ExampleClient_SetTLSKeyCrt
2.ExampleClient_SetTLSConfig
modify example funcion
1.ExampleClient_SetProxy
2.ExampleClient_Proxy
* add example function
1.ExampleClient_PutContent
2.ExampleClient_DeleteContent
3.ExampleClient_HeadContent
4.ExampleClient_PatchContent
5.ExampleClient_ConnectContent
6.ExampleClient_OptionsContent
7.ExampleClient_TraceContent
8.ExampleClient_RequestContent
* add example function
1.ExampleClient_RawRequest
* add unit function
1.TestGetFreePorts
2.TestNewConn
3.TestNewConnTLS
4.TestNewConnKeyCrt
5.TestConn_SendWithTimeout
* add unit function
1.TestConn_Send
2.TestConn_SendRecv
3.TestConn_SendRecvWithTimeout
* modify
* modify
* add example function
1.TestConn_SetReceiveBufferWait
2.TestNewNetConnKeyCrt
3.TestSend
* add example function
1.TestSendRecv
2.TestSendWithTimeout
* add unit function
1.TestMustGetFreePort
2.TestSendRecvWithTimeout
3.TestSendPkg
* add client recevied server's response content assert
* modify
* modify
* add example function
1.TestSendRecvPkg
2.TestSendPkgWithTimeout
3.TestSendRecvPkgWithTimeout
* add GetAddress() function
add unit funciton
1.TestNewServer
2.TestGetServer
3.TestServer_SetAddress
4.TestServer_SetHandler
5.TestServer_Run
* modify
* modify
* add unit funciton
1.TestLoadKeyCrt
* modify
* delete function fromHex
* add gclient dump unit test
* add example function
1.ExampleClient_Put
2.ExampleClient_Delete
3.ExampleClient_Head
4.ExampleClient_Patch
5.ExampleClient_Connect
6.ExampleClient_Options
7.ExampleClient_Trace
* add example function
1.TestClient_DoRequest
* add example function
1.ExampleClient_PutVar
2.ExampleClient_DeleteVar
3.ExampleClient_HeadVar
4.ExampleClient_PatchVar
5.ExampleClient_ConnectVar
6.ExampleClient_OptionsVar
7.ExampleClient_TraceVar
* modify
* modify
* add CustomProvider function
* modify
* add unit funciton
1.Test_NewConn
2.Test_GetFreePorts
* add unit funciton
1.Test_Server
* garray_normal_any code converage
* garray_normal_int code converage
* garray_normal_str code converage
* garray_sorted_any code converage
* garray_sorted_int code converage
* garray_sorted_str code converage
* glist code converage
* gmap, gmap_hash_any_any_map code converage
* gmap_hash_int_any_map code converage
* gmap_hash_int_any_map code converage
* gmap_hash_int_int_map code converage
* gmap_hash_int_str_map code converage
* gmap_hash_str_any_map code converage
* gmap_hash_str_int_map code converage
* gmap_hash_str_str_map code converage
* gmap_list_map code converage
* gmap_list_map code converage
* revert gf.yml
* add gtest unit test function
* add ut cases for package gcache
* add ut cases for package gcache
* add ut cases for package gcache
* add ut cases for package gcache
* add ut cases for package gcache
* modify
Co-authored-by: John Guo <john@johng.cn>
* improve ut case for package internal/rwmutex (#2364)
* fix issue when only one file was uploaded in batch receiver attribute (#2365)
* fix fixed An error occurred when only one file was uploaded in batches and add unit testing(#2092)
* fix issue uploading files for ghttp.Server
Co-authored-by: yxh <yxh1103@qq.com>
* fix issue #2334 when accessing static files with cache time (#2366)
* Solve the problem of error when accessing static files with cache time.
Error message:
2022-11-29 19:40:11.090 [ERRO] http: superfluous response.WriteHeader call from github.com/gogf/gf/v2/net/ghttp.(*ResponseWriter).Flush (ghttp_response_writer.go:58)
Stack:
Verification method:
curl 'http://127.0.0.1:8000/' -H 'If-Modified-Since: Thu, 08 Dec 2022 03:13:55 GMT' --compressed
* Solve the problem of error when accessing static files with cache time.
Error message:
2022-11-29 19:40:11.090 [ERRO] http: superfluous response.WriteHeader call from github.com/gogf/gf/v2/net/ghttp.(*ResponseWriter).Flush (ghttp_response_writer.go:58)
Stack:
Verification method:
curl 'http://127.0.0.1:8000/' -H 'If-Modified-Since: Thu, 08 Dec 2022 03:13:55 GMT' --compressed
* Solve the problem of error when accessing static files with cache time.
Error message:
2022-11-29 19:40:11.090 [ERRO] http: superfluous response.WriteHeader call from github.com/gogf/gf/v2/net/ghttp.(*ResponseWriter).Flush (ghttp_response_writer.go:58)
Stack:
Verification method:
curl 'http://127.0.0.1:8000/' -H 'If-Modified-Since: Thu, 08 Dec 2022 03:13:55 GMT' --compressed
* fix issue #2334 when accessing static files with cache time
* up
Co-authored-by: 曾洪亮 <hongliang.zeng@i-soft.com.cn>
Co-authored-by: houseme <housemecn@gmail.com>
* fix issue in cycle dumping for g.Dump (#2367)
* fix issue in cycle dumping for g.Dump
* up
* up
* up
Co-authored-by: houseme <housemecn@gmail.com>
* 由于 clickhouse 的 position的初始值为 1,导致gdb_core_utility.HasField 中对 fieldsArray 初始化出错 (#2346)
* 由于 clickhouse 的 position的初始值为 1,导致gdb_core_utility.HasField 中对 fieldsArray 初始化出错
* 修复单元测试
* 修复单元测试
* 补充单元测试
* 增加CK防御性代码
Co-authored-by: longl <longlei@dealmap.cloud>
Co-authored-by: houseme <housemecn@gmail.com>
* fix: ghttp server static path config (#2335)
Co-authored-by: daguang <daguang830@gmail.com>
Co-authored-by: houseme <housemecn@gmail.com>
Co-authored-by: ftl <1139556759@qq.com>
Co-authored-by: HaiLaz <739476267@qq.com>
Co-authored-by: zhonghuaxunGM <50815786+zhonghuaxunGM@users.noreply.github.com>
Co-authored-by: huangqian <huangqian1985@qq.com>
Co-authored-by: junler <827640651@qq.com>
Co-authored-by: junler <sunjun@bookan.com>
Co-authored-by: Starccck <28645972+starccck@users.noreply.github.com>
Co-authored-by: Jinhongyu <30454170+cnjinhy@users.noreply.github.com>
Co-authored-by: YuanXin Hu <huyuanxin1999@outlook.com>
Co-authored-by: yxh <yxh1103@qq.com>
Co-authored-by: 曾洪亮 <hongliang.zeng@i-soft.com.cn>
Co-authored-by: long <48313408+qq375251855@users.noreply.github.com>
Co-authored-by: longl <longlei@dealmap.cloud>
Co-authored-by: houseme <housemecn@gmail.com>
Co-authored-by: daguang <daguang830@gmail.com>
Co-authored-by: ftl <1139556759@qq.com>
Co-authored-by: HaiLaz <739476267@qq.com>
Co-authored-by: zhonghuaxunGM <50815786+zhonghuaxunGM@users.noreply.github.com>
Co-authored-by: huangqian <huangqian1985@qq.com>
Co-authored-by: junler <827640651@qq.com>
Co-authored-by: junler <sunjun@bookan.com>
Co-authored-by: Starccck <28645972+starccck@users.noreply.github.com>
Co-authored-by: Jinhongyu <30454170+cnjinhy@users.noreply.github.com>
Co-authored-by: YuanXin Hu <huyuanxin1999@outlook.com>
Co-authored-by: yxh <yxh1103@qq.com>
Co-authored-by: 曾洪亮 <hongliang.zeng@i-soft.com.cn>
Co-authored-by: long <48313408+qq375251855@users.noreply.github.com>
Co-authored-by: longl <longlei@dealmap.cloud>
2023-01-09 14:43:10 +08:00
|
|
|
err = db.Transaction(context.TODO(), func(ctx context.Context, tx gdb.TX) error {
|
2020-04-26 21:31:55 +08:00
|
|
|
// Cache feature disabled.
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err := tx.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test4",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(4).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(one["passport"], "user_400")
|
|
|
|
// Update the cache.
|
2021-05-19 21:11:51 +08:00
|
|
|
r, err := tx.Model(table).Data("passport", "user_4000").
|
2021-11-09 16:06:31 +08:00
|
|
|
Cache(gdb.CacheOption{
|
|
|
|
Duration: -1,
|
|
|
|
Name: "test4",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(4).Update()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
n, err := r.RowsAffected()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(n, 1)
|
|
|
|
return nil
|
|
|
|
})
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
// Read from db.
|
2021-11-09 16:06:31 +08:00
|
|
|
one, err = db.Model(table).Cache(gdb.CacheOption{
|
|
|
|
Duration: time.Second,
|
|
|
|
Name: "test4",
|
|
|
|
Force: false,
|
|
|
|
}).WherePri(4).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-26 21:31:55 +08:00
|
|
|
t.Assert(one["passport"], "user_4000")
|
|
|
|
})
|
2020-04-09 22:00:02 +08:00
|
|
|
}
|
2020-04-11 10:11:52 +08:00
|
|
|
|
|
|
|
func Test_Model_Having(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id > 1").Having("id > 8").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-11 10:11:52 +08:00
|
|
|
t.Assert(len(all), 2)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id > 1").Having("id > ?", 8).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-11 10:11:52 +08:00
|
|
|
t.Assert(len(all), 2)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id > ?", 1).Having("id > ?", 8).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-11 10:11:52 +08:00
|
|
|
t.Assert(len(all), 2)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table).Where("id > ?", 1).Having("id", 8).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-11 10:11:52 +08:00
|
|
|
t.Assert(len(all), 1)
|
|
|
|
})
|
|
|
|
}
|
2020-04-27 16:30:53 +08:00
|
|
|
|
|
|
|
func Test_Model_Distinct(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
all, err := db.Model(table, "t").Fields("distinct t.id").Where("id > 1").Having("id > 8").All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-27 16:30:53 +08:00
|
|
|
t.Assert(len(all), 2)
|
|
|
|
})
|
2021-05-02 12:17:06 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id > 1").Distinct().Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(9))
|
2021-05-02 12:17:06 +08:00
|
|
|
})
|
2020-04-27 16:30:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Min_Max(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err := db.Model(table, "t").Fields("min(t.id)").Where("id > 1").Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-27 16:30:53 +08:00
|
|
|
t.Assert(value.Int(), 2)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err := db.Model(table, "t").Fields("max(t.id)").Where("id > 1").Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-27 16:30:53 +08:00
|
|
|
t.Assert(value.Int(), 10)
|
|
|
|
})
|
|
|
|
}
|
2020-04-27 17:56:04 +08:00
|
|
|
|
2020-11-06 00:00:41 +08:00
|
|
|
func Test_Model_Fields_AutoMapping(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err := db.Model(table).Fields("ID").Where("id", 2).Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-06 00:00:41 +08:00
|
|
|
t.Assert(value.Int(), 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
value, err := db.Model(table).Fields("NICK_NAME").Where("id", 2).Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-06 00:00:41 +08:00
|
|
|
t.Assert(value.String(), "name_2")
|
|
|
|
})
|
2020-11-06 20:52:16 +08:00
|
|
|
// Map
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Fields(g.Map{
|
2020-11-06 20:52:16 +08:00
|
|
|
"ID": 1,
|
|
|
|
"NICK_NAME": 1,
|
|
|
|
}).Where("id", 2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-06 20:52:16 +08:00
|
|
|
t.Assert(len(one), 2)
|
|
|
|
t.Assert(one["id"], 2)
|
|
|
|
t.Assert(one["nickname"], "name_2")
|
|
|
|
})
|
|
|
|
// Struct
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
ID int
|
|
|
|
NICKNAME int
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Fields(&T{
|
2020-11-06 20:52:16 +08:00
|
|
|
ID: 0,
|
|
|
|
NICKNAME: 0,
|
|
|
|
}).Where("id", 2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-06 20:52:16 +08:00
|
|
|
t.Assert(len(one), 2)
|
|
|
|
t.Assert(one["id"], 2)
|
|
|
|
t.Assert(one["nickname"], "name_2")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_FieldsEx_AutoMapping(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
// "id": i,
|
|
|
|
// "passport": fmt.Sprintf(`user_%d`, i),
|
|
|
|
// "password": fmt.Sprintf(`pass_%d`, i),
|
|
|
|
// "nickname": fmt.Sprintf(`name_%d`, i),
|
|
|
|
// "create_time": gtime.NewFromStr("2018-10-24 10:00:00").String(),
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2024-10-08 20:42:06 +08:00
|
|
|
value, err := db.Model(table).FieldsEx("create_date, Passport, Password, NickName, CreateTime").Where("id", 2).Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-06 20:52:16 +08:00
|
|
|
t.Assert(value.Int(), 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2024-10-08 20:42:06 +08:00
|
|
|
value, err := db.Model(table).FieldsEx("create_date, ID, Passport, Password, CreateTime").Where("id", 2).Value()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-06 20:52:16 +08:00
|
|
|
t.Assert(value.String(), "name_2")
|
|
|
|
})
|
|
|
|
// Map
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).FieldsEx(g.Map{
|
2020-11-06 20:52:16 +08:00
|
|
|
"Passport": 1,
|
|
|
|
"Password": 1,
|
|
|
|
"CreateTime": 1,
|
|
|
|
}).Where("id", 2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2024-09-10 17:51:22 +08:00
|
|
|
t.Assert(len(one), 3)
|
2020-11-06 20:52:16 +08:00
|
|
|
t.Assert(one["id"], 2)
|
|
|
|
t.Assert(one["nickname"], "name_2")
|
|
|
|
})
|
|
|
|
// Struct
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type T struct {
|
|
|
|
Passport int
|
|
|
|
Password int
|
|
|
|
CreateTime int
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).FieldsEx(&T{
|
2020-11-06 20:52:16 +08:00
|
|
|
Passport: 0,
|
|
|
|
Password: 0,
|
|
|
|
CreateTime: 0,
|
|
|
|
}).Where("id", 2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2024-09-10 17:51:22 +08:00
|
|
|
t.Assert(len(one), 3)
|
2020-11-06 20:52:16 +08:00
|
|
|
t.Assert(one["id"], 2)
|
|
|
|
t.Assert(one["nickname"], "name_2")
|
|
|
|
})
|
2020-11-06 00:00:41 +08:00
|
|
|
}
|
|
|
|
|
2020-11-20 00:53:12 +08:00
|
|
|
func Test_Model_Fields_Struct(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type A struct {
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
type B struct {
|
|
|
|
A
|
|
|
|
NickName string
|
|
|
|
}
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Fields(A{}).Where("id", 2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-20 00:53:12 +08:00
|
|
|
t.Assert(len(one), 2)
|
|
|
|
t.Assert(one["passport"], "user_2")
|
|
|
|
t.Assert(one["password"], "pass_2")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Fields(&A{}).Where("id", 2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-20 00:53:12 +08:00
|
|
|
t.Assert(len(one), 2)
|
|
|
|
t.Assert(one["passport"], "user_2")
|
|
|
|
t.Assert(one["password"], "pass_2")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Fields(B{}).Where("id", 2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-20 00:53:12 +08:00
|
|
|
t.Assert(len(one), 3)
|
|
|
|
t.Assert(one["passport"], "user_2")
|
|
|
|
t.Assert(one["password"], "pass_2")
|
|
|
|
t.Assert(one["nickname"], "name_2")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
one, err := db.Model(table).Fields(&B{}).Where("id", 2).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-11-20 00:53:12 +08:00
|
|
|
t.Assert(len(one), 3)
|
|
|
|
t.Assert(one["passport"], "user_2")
|
|
|
|
t.Assert(one["password"], "pass_2")
|
|
|
|
t.Assert(one["nickname"], "name_2")
|
|
|
|
})
|
|
|
|
}
|
2021-11-16 17:21:13 +08:00
|
|
|
|
2020-04-27 17:56:04 +08:00
|
|
|
func Test_Model_NullField(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport *string
|
|
|
|
}
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": nil,
|
|
|
|
}
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Data(data).Insert()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-27 17:56:04 +08:00
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-27 17:56:04 +08:00
|
|
|
|
|
|
|
var user *User
|
|
|
|
err = one.Struct(&user)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-04-27 17:56:04 +08:00
|
|
|
t.Assert(user.Id, data["id"])
|
|
|
|
t.Assert(user.Passport, data["passport"])
|
|
|
|
})
|
|
|
|
}
|
2020-07-11 09:53:16 +08:00
|
|
|
|
|
|
|
func Test_Model_Empty_Slice_Argument(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Where(`id`, g.Slice{}).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Assert(len(result), 0)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Where(`id in(?)`, g.Slice{}).All()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-11 09:53:16 +08:00
|
|
|
t.Assert(len(result), 0)
|
|
|
|
})
|
|
|
|
}
|
2020-07-21 12:37:04 +08:00
|
|
|
|
|
|
|
func Test_Model_HasTable(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2023-11-20 20:47:26 +08:00
|
|
|
t.AssertNil(db.GetCore().ClearCacheAll(ctx))
|
2021-05-21 13:25:53 +08:00
|
|
|
result, err := db.GetCore().HasTable(table)
|
2020-07-21 12:37:04 +08:00
|
|
|
t.Assert(result, true)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-21 12:37:04 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2023-11-20 20:47:26 +08:00
|
|
|
t.AssertNil(db.GetCore().ClearCacheAll(ctx))
|
2021-05-21 13:25:53 +08:00
|
|
|
result, err := db.GetCore().HasTable("table12321")
|
2020-07-21 12:37:04 +08:00
|
|
|
t.Assert(result, false)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-21 12:37:04 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_HasField(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).HasField("id")
|
2020-07-21 12:37:04 +08:00
|
|
|
t.Assert(result, true)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-21 12:37:04 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).HasField("id123")
|
2020-07-21 12:37:04 +08:00
|
|
|
t.Assert(result, false)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-07-21 12:37:04 +08:00
|
|
|
})
|
2020-07-21 13:28:25 +08:00
|
|
|
}
|
2020-11-21 13:20:32 +08:00
|
|
|
|
2020-11-27 22:51:34 +08:00
|
|
|
func createTableForTimeZoneTest() string {
|
|
|
|
tableName := "user_" + gtime.Now().TimestampNanoStr()
|
2021-09-29 20:39:02 +08:00
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
2020-11-27 22:51:34 +08:00
|
|
|
CREATE TABLE %s (
|
|
|
|
id int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
|
|
passport varchar(45) NULL,
|
|
|
|
password char(32) NULL,
|
|
|
|
nickname varchar(45) NULL,
|
2023-03-20 09:57:37 +08:00
|
|
|
created_at timestamp(6) NULL,
|
|
|
|
updated_at timestamp(6) NULL,
|
|
|
|
deleted_at timestamp(6) NULL,
|
2020-11-27 22:51:34 +08:00
|
|
|
PRIMARY KEY (id)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
|
|
`, tableName,
|
|
|
|
)); err != nil {
|
|
|
|
gtest.Fatal(err)
|
|
|
|
}
|
|
|
|
return tableName
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1012
|
|
|
|
func Test_TimeZoneInsert(t *testing.T) {
|
|
|
|
tableName := createTableForTimeZoneTest()
|
|
|
|
defer dropTable(tableName)
|
|
|
|
|
2022-04-03 16:00:19 +08:00
|
|
|
tokyoLoc, err := time.LoadLocation("Asia/Tokyo")
|
2021-02-07 14:39:32 +08:00
|
|
|
gtest.AssertNil(err)
|
2020-11-27 22:51:34 +08:00
|
|
|
|
|
|
|
CreateTime := "2020-11-22 12:23:45"
|
2024-09-10 17:51:22 +08:00
|
|
|
UpdateTime := "2020-11-22 13:23:46"
|
|
|
|
DeleteTime := "2020-11-22 14:23:47"
|
2020-11-27 22:51:34 +08:00
|
|
|
type User struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
CreatedAt *gtime.Time `json:"created_at"`
|
|
|
|
UpdatedAt gtime.Time `json:"updated_at"`
|
|
|
|
DeletedAt time.Time `json:"deleted_at"`
|
|
|
|
}
|
2022-04-03 16:00:19 +08:00
|
|
|
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", CreateTime, tokyoLoc)
|
|
|
|
t2, _ := time.ParseInLocation("2006-01-02 15:04:05", UpdateTime, tokyoLoc)
|
|
|
|
t3, _ := time.ParseInLocation("2006-01-02 15:04:05", DeleteTime, tokyoLoc)
|
2020-11-27 22:51:34 +08:00
|
|
|
u := &User{
|
|
|
|
Id: 1,
|
2020-11-28 00:11:22 +08:00
|
|
|
CreatedAt: gtime.New(t1.UTC()),
|
|
|
|
UpdatedAt: *gtime.New(t2.UTC()),
|
|
|
|
DeletedAt: t3.UTC(),
|
2020-11-27 22:51:34 +08:00
|
|
|
}
|
|
|
|
|
2020-11-21 13:20:32 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2024-09-10 17:51:22 +08:00
|
|
|
_, err = db.Model(tableName).Unscoped().Insert(u)
|
|
|
|
t.AssertNil(err)
|
2020-11-27 22:51:34 +08:00
|
|
|
userEntity := &User{}
|
2024-09-10 17:51:22 +08:00
|
|
|
err = db.Model(tableName).Where("id", 1).Unscoped().Scan(&userEntity)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2022-04-03 16:00:19 +08:00
|
|
|
t.Assert(userEntity.CreatedAt.String(), "2020-11-22 11:23:45")
|
2024-09-10 17:51:22 +08:00
|
|
|
t.Assert(userEntity.UpdatedAt.String(), "2020-11-22 12:23:46")
|
|
|
|
t.Assert(gtime.NewFromTime(userEntity.DeletedAt).String(), "2020-11-22 13:23:47")
|
2020-11-21 13:20:32 +08:00
|
|
|
})
|
|
|
|
}
|
2020-12-04 14:22:50 +08:00
|
|
|
|
|
|
|
func Test_Model_Fields_Map_Struct(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
// map
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-02-27 23:58:36 +08:00
|
|
|
result, err := db.Model(table).Fields(g.Map{
|
2020-12-04 14:22:50 +08:00
|
|
|
"ID": 1,
|
|
|
|
"PASSPORT": 1,
|
|
|
|
"NONE_EXIST": 1,
|
|
|
|
}).Where("id", 1).One()
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-12-04 14:22:50 +08:00
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result["id"], 1)
|
|
|
|
t.Assert(result["passport"], "user_1")
|
|
|
|
})
|
|
|
|
// struct
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type A struct {
|
|
|
|
ID int
|
|
|
|
PASSPORT string
|
|
|
|
XXX_TYPE int
|
|
|
|
}
|
2021-11-16 17:21:13 +08:00
|
|
|
a := A{}
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Fields(a).Where("id", 1).Scan(&a)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-12-04 14:22:50 +08:00
|
|
|
t.Assert(a.ID, 1)
|
|
|
|
t.Assert(a.PASSPORT, "user_1")
|
|
|
|
t.Assert(a.XXX_TYPE, 0)
|
|
|
|
})
|
|
|
|
// *struct
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type A struct {
|
|
|
|
ID int
|
|
|
|
PASSPORT string
|
|
|
|
XXX_TYPE int
|
|
|
|
}
|
|
|
|
var a *A
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Fields(a).Where("id", 1).Scan(&a)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-12-04 14:22:50 +08:00
|
|
|
t.Assert(a.ID, 1)
|
|
|
|
t.Assert(a.PASSPORT, "user_1")
|
|
|
|
t.Assert(a.XXX_TYPE, 0)
|
|
|
|
})
|
|
|
|
// **struct
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type A struct {
|
|
|
|
ID int
|
|
|
|
PASSPORT string
|
|
|
|
XXX_TYPE int
|
|
|
|
}
|
|
|
|
var a *A
|
2021-06-22 21:48:56 +08:00
|
|
|
err := db.Model(table).Fields(&a).Where("id", 1).Scan(&a)
|
2021-02-07 14:39:32 +08:00
|
|
|
t.AssertNil(err)
|
2020-12-04 14:22:50 +08:00
|
|
|
t.Assert(a.ID, 1)
|
|
|
|
t.Assert(a.PASSPORT, "user_1")
|
|
|
|
t.Assert(a.XXX_TYPE, 0)
|
|
|
|
})
|
|
|
|
}
|
2021-01-20 13:09:59 +08:00
|
|
|
|
2021-05-02 12:17:06 +08:00
|
|
|
func Test_Model_WhereIn(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereIn("id", g.Slice{1, 2, 3, 4}).WhereIn("id", g.Slice{3, 4, 5}).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0]["id"], 3)
|
|
|
|
t.Assert(result[1]["id"], 4)
|
|
|
|
})
|
2021-12-28 17:15:01 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereIn("id", g.Slice{}).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 0)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).OmitEmptyWhere().WhereIn("id", g.Slice{}).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
})
|
2021-05-02 12:17:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereNotIn(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereNotIn("id", g.Slice{1, 2, 3, 4}).WhereNotIn("id", g.Slice{3, 4, 5}).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 5)
|
|
|
|
t.Assert(result[0]["id"], 6)
|
|
|
|
t.Assert(result[1]["id"], 7)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrIn(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereOrIn("id", g.Slice{1, 2, 3, 4}).WhereOrIn("id", g.Slice{3, 4, 5}).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 5)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[4]["id"], 5)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrNotIn(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereOrNotIn("id", g.Slice{1, 2, 3, 4}).WhereOrNotIn("id", g.Slice{3, 4, 5}).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 8)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[4]["id"], 7)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereBetween(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereBetween("id", 1, 4).WhereBetween("id", 3, 5).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0]["id"], 3)
|
|
|
|
t.Assert(result[1]["id"], 4)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereNotBetween(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereNotBetween("id", 2, 8).WhereNotBetween("id", 3, 100).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 1)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrBetween(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereOrBetween("id", 1, 4).WhereOrBetween("id", 3, 5).OrderDesc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 5)
|
|
|
|
t.Assert(result[0]["id"], 5)
|
|
|
|
t.Assert(result[4]["id"], 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrNotBetween(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
2021-11-13 23:23:55 +08:00
|
|
|
// db.SetDebug(true)
|
2021-05-02 12:17:06 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereOrNotBetween("id", 1, 4).WhereOrNotBetween("id", 3, 5).OrderDesc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 8)
|
|
|
|
t.Assert(result[0]["id"], 10)
|
|
|
|
t.Assert(result[4]["id"], 6)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereLike(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereLike("nickname", "name%").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[TableSize-1]["id"], TableSize)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereNotLike(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereNotLike("nickname", "name%").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrLike(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereOrLike("nickname", "namexxx%").WhereOrLike("nickname", "name%").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[TableSize-1]["id"], TableSize)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrNotLike(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereOrNotLike("nickname", "namexxx%").WhereOrNotLike("nickname", "name%").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[TableSize-1]["id"], TableSize)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereNull(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereNull("nickname").WhereNull("passport").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereNotNull(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereNotNull("nickname").WhereNotNull("passport").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[TableSize-1]["id"], TableSize)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrNull(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereOrNull("nickname").WhereOrNull("passport").OrderAsc("id").OrderRandom().All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrNotNull(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereOrNotNull("nickname").WhereOrNotNull("passport").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), TableSize)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[TableSize-1]["id"], TableSize)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-04 09:27:41 +08:00
|
|
|
func Test_Model_WhereLT(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereLT("id", 3).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereLTE(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereLTE("id", 3).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereGT(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereGT("id", 8).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 2)
|
|
|
|
t.Assert(result[0]["id"], 9)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereGTE(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereGTE("id", 8).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"], 8)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrLT(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereLT("id", 3).WhereOrLT("id", 4).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[2]["id"], 3)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrLTE(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereLTE("id", 3).WhereOrLTE("id", 4).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 4)
|
|
|
|
t.Assert(result[0]["id"], 1)
|
|
|
|
t.Assert(result[3]["id"], 4)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrGT(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereGT("id", 8).WhereOrGT("id", 7).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 3)
|
|
|
|
t.Assert(result[0]["id"], 8)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrGTE(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereGTE("id", 8).WhereOrGTE("id", 7).OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(result), 4)
|
|
|
|
t.Assert(result[0]["id"], 7)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-02 12:17:06 +08:00
|
|
|
func Test_Model_Min_Max_Avg_Sum(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Min("id")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(result, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Max("id")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(result, TableSize)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Avg("id")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(result, 5.5)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Sum("id")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(result, 55)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_CountColumn(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).CountColumn("id")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(result, TableSize)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).CountColumn("id")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(result, 3)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_InsertAndGetId(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
id, err := db.Model(table).Data(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "user_1",
|
|
|
|
"password": "pass_1",
|
|
|
|
"nickname": "name_1",
|
|
|
|
}).InsertAndGetId()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(id, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
id, err := db.Model(table).Data(g.Map{
|
|
|
|
"passport": "user_2",
|
|
|
|
"password": "pass_2",
|
|
|
|
"nickname": "name_2",
|
|
|
|
}).InsertAndGetId()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(id, 2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Increment_Decrement(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Where("id", 1).Increment("id", 100)
|
|
|
|
t.AssertNil(err)
|
|
|
|
rows, _ := result.RowsAffected()
|
|
|
|
t.Assert(rows, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
result, err := db.Model(table).Where("id", 101).Decrement("id", 10)
|
|
|
|
t.AssertNil(err)
|
|
|
|
rows, _ := result.RowsAffected()
|
|
|
|
t.Assert(rows, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", 91).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(1))
|
2021-05-02 12:17:06 +08:00
|
|
|
})
|
|
|
|
}
|
2021-06-16 21:44:31 +08:00
|
|
|
|
|
|
|
func Test_Model_OnDuplicate(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2024-02-27 21:07:31 +08:00
|
|
|
// string type 1.
|
2021-06-16 21:44:31 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicate("passport,password").Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-06-16 21:44:31 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["password"], data["password"])
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
|
2024-02-27 21:07:31 +08:00
|
|
|
// string type 2.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicate("passport", "password").Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["password"], data["password"])
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
|
2021-06-16 21:44:31 +08:00
|
|
|
// slice.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicate(g.Slice{"passport", "password"}).Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-06-16 21:44:31 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["password"], data["password"])
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
|
|
|
|
// map.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicate(g.Map{
|
|
|
|
"passport": "nickname",
|
|
|
|
"password": "nickname",
|
|
|
|
}).Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-06-16 21:44:31 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["nickname"])
|
|
|
|
t.Assert(one["password"], data["nickname"])
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
|
|
|
|
// map+raw.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.MapStrStr{
|
|
|
|
"id": "1",
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicate(g.Map{
|
|
|
|
"passport": gdb.Raw("CONCAT(VALUES(`passport`), '1')"),
|
|
|
|
"password": gdb.Raw("CONCAT(VALUES(`password`), '2')"),
|
|
|
|
}).Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-06-16 21:44:31 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["passport"]+"1")
|
|
|
|
t.Assert(one["password"], data["password"]+"2")
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_OnDuplicateEx(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2024-02-27 21:07:31 +08:00
|
|
|
// string type 1.
|
2021-06-16 21:44:31 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicateEx("nickname,create_time").Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-06-16 21:44:31 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["password"], data["password"])
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
|
2024-02-27 21:07:31 +08:00
|
|
|
// string type 2.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicateEx("nickname", "create_time").Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["password"], data["password"])
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
|
2021-06-16 21:44:31 +08:00
|
|
|
// slice.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicateEx(g.Slice{"nickname", "create_time"}).Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-06-16 21:44:31 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["password"], data["password"])
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
|
|
|
|
// map.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"passport": "pp1",
|
|
|
|
"password": "pw1",
|
|
|
|
"nickname": "n1",
|
|
|
|
"create_time": "2016-06-06",
|
|
|
|
}
|
|
|
|
_, err := db.Model(table).OnDuplicateEx(g.Map{
|
|
|
|
"nickname": "nickname",
|
|
|
|
"create_time": "nickname",
|
|
|
|
}).Data(data).Save()
|
|
|
|
t.AssertNil(err)
|
2021-09-29 20:39:02 +08:00
|
|
|
one, err := db.Model(table).WherePri(1).One()
|
2021-06-16 21:44:31 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["passport"], data["passport"])
|
|
|
|
t.Assert(one["password"], data["password"])
|
|
|
|
t.Assert(one["nickname"], "name_1")
|
|
|
|
})
|
|
|
|
}
|
2021-06-18 15:20:27 +08:00
|
|
|
|
|
|
|
func Test_Model_Raw(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
all, err := db.
|
|
|
|
Raw(fmt.Sprintf("select * from %s where id in (?)", table), g.Slice{1, 5, 7, 8, 9, 10}).
|
|
|
|
WhereLT("id", 8).
|
|
|
|
WhereIn("id", g.Slice{1, 2, 3, 4, 5, 6, 7}).
|
|
|
|
OrderDesc("id").
|
|
|
|
Limit(2).
|
|
|
|
All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(all), 2)
|
|
|
|
t.Assert(all[0]["id"], 7)
|
|
|
|
t.Assert(all[1]["id"], 5)
|
|
|
|
})
|
2021-06-21 19:21:38 +08:00
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.
|
|
|
|
Raw(fmt.Sprintf("select * from %s where id in (?)", table), g.Slice{1, 5, 7, 8, 9, 10}).
|
|
|
|
WhereLT("id", 8).
|
|
|
|
WhereIn("id", g.Slice{1, 2, 3, 4, 5, 6, 7}).
|
|
|
|
OrderDesc("id").
|
|
|
|
Limit(2).
|
|
|
|
Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(6))
|
2021-06-21 19:21:38 +08:00
|
|
|
})
|
2021-06-18 15:20:27 +08:00
|
|
|
}
|
2021-08-01 10:17:03 +08:00
|
|
|
|
|
|
|
func Test_Model_Handler(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
m := db.Model(table).Safe().Handler(
|
|
|
|
func(m *gdb.Model) *gdb.Model {
|
|
|
|
return m.Page(0, 3)
|
|
|
|
},
|
|
|
|
func(m *gdb.Model) *gdb.Model {
|
|
|
|
return m.Where("id", g.Slice{1, 2, 3, 4, 5, 6})
|
|
|
|
},
|
|
|
|
func(m *gdb.Model) *gdb.Model {
|
|
|
|
return m.OrderDesc("id")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
all, err := m.All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(all), 3)
|
|
|
|
t.Assert(all[0]["id"], 6)
|
|
|
|
t.Assert(all[2]["id"], 4)
|
|
|
|
})
|
|
|
|
}
|
2021-08-12 19:42:44 +08:00
|
|
|
|
|
|
|
func Test_Model_FieldCount(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
all, err := db.Model(table).Fields("id").FieldCount("id", "total").Group("id").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(all), TableSize)
|
|
|
|
t.Assert(all[0]["id"], 1)
|
2023-06-28 10:06:33 +08:00
|
|
|
t.Assert(all[0]["total"].Int(), 1)
|
2021-08-12 19:42:44 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_FieldMax(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
all, err := db.Model(table).Fields("id").FieldMax("id", "total").Group("id").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(all), TableSize)
|
|
|
|
t.Assert(all[0]["id"], 1)
|
2023-06-28 10:06:33 +08:00
|
|
|
t.Assert(all[0]["total"].Int(), 1)
|
2021-08-12 19:42:44 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_FieldMin(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
all, err := db.Model(table).Fields("id").FieldMin("id", "total").Group("id").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(all), TableSize)
|
|
|
|
t.Assert(all[0]["id"], 1)
|
2023-06-28 10:06:33 +08:00
|
|
|
t.Assert(all[0]["total"].Int(), 1)
|
2021-08-12 19:42:44 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_FieldAvg(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
all, err := db.Model(table).Fields("id").FieldAvg("id", "total").Group("id").OrderAsc("id").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(all), TableSize)
|
|
|
|
t.Assert(all[0]["id"], 1)
|
2023-06-28 10:06:33 +08:00
|
|
|
t.Assert(all[0]["total"].Int(), 1)
|
2021-08-12 19:42:44 +08:00
|
|
|
})
|
|
|
|
}
|
2021-08-30 22:26:02 +08:00
|
|
|
|
2021-10-29 10:50:14 +08:00
|
|
|
func Test_Model_OmitEmptyWhere(t *testing.T) {
|
2021-10-28 23:18:23 +08:00
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
2021-10-29 10:50:14 +08:00
|
|
|
// Basic type where.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", 0).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(0))
|
2021-10-29 10:50:14 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).OmitEmptyWhere().Where("id", 0).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2021-10-29 10:50:14 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).OmitEmptyWhere().Where("id", 0).Where("nickname", "").Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2021-10-29 10:50:14 +08:00
|
|
|
})
|
|
|
|
// Slice where.
|
2021-10-28 23:18:23 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", g.Slice{1, 2, 3}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(3))
|
2021-10-28 23:18:23 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", g.Slice{}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(0))
|
2021-10-28 23:18:23 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-10-29 10:50:14 +08:00
|
|
|
count, err := db.Model(table).OmitEmptyWhere().Where("id", g.Slice{}).Count()
|
2021-10-28 23:18:23 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2021-10-28 23:18:23 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2021-10-29 10:50:14 +08:00
|
|
|
count, err := db.Model(table).Where("id", g.Slice{}).OmitEmptyWhere().Count()
|
2021-10-28 23:18:23 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2021-10-28 23:18:23 +08:00
|
|
|
})
|
|
|
|
// Struct Where.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type Input struct {
|
2021-10-29 10:50:14 +08:00
|
|
|
Id []int
|
|
|
|
Name []string
|
2021-10-28 23:18:23 +08:00
|
|
|
}
|
|
|
|
count, err := db.Model(table).Where(Input{}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(0))
|
2021-10-28 23:18:23 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type Input struct {
|
2021-10-29 10:50:14 +08:00
|
|
|
Id []int
|
|
|
|
Name []string
|
2021-10-28 23:18:23 +08:00
|
|
|
}
|
2021-10-29 10:50:14 +08:00
|
|
|
count, err := db.Model(table).Where(Input{}).OmitEmptyWhere().Count()
|
2021-10-28 23:18:23 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2021-10-28 23:18:23 +08:00
|
|
|
})
|
|
|
|
// Map Where.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where(g.Map{
|
2021-10-29 10:50:14 +08:00
|
|
|
"id": []int{},
|
|
|
|
"nickname": []string{},
|
2021-10-28 23:18:23 +08:00
|
|
|
}).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(0))
|
2021-10-28 23:18:23 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type Input struct {
|
|
|
|
Id []int
|
|
|
|
}
|
|
|
|
count, err := db.Model(table).Where(g.Map{
|
|
|
|
"id": []int{},
|
2021-10-29 10:50:14 +08:00
|
|
|
}).OmitEmptyWhere().Count()
|
2021-10-28 23:18:23 +08:00
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2021-10-28 23:18:23 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:26:02 +08:00
|
|
|
// https://github.com/gogf/gf/issues/1387
|
|
|
|
func Test_Model_GTime_DefaultValue(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type User struct {
|
|
|
|
Id int
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
CreateTime *gtime.Time
|
|
|
|
}
|
|
|
|
data := User{
|
|
|
|
Id: 1,
|
|
|
|
Passport: "user_1",
|
|
|
|
Password: "pass_1",
|
|
|
|
Nickname: "name_1",
|
|
|
|
}
|
|
|
|
// Insert
|
|
|
|
_, err := db.Model(table).Data(data).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
// Select
|
|
|
|
var (
|
|
|
|
user *User
|
|
|
|
)
|
|
|
|
err = db.Model(table).Scan(&user)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(user.Passport, data.Passport)
|
|
|
|
t.Assert(user.Password, data.Password)
|
|
|
|
t.Assert(user.CreateTime, data.CreateTime)
|
|
|
|
t.Assert(user.Nickname, data.Nickname)
|
|
|
|
|
|
|
|
// Insert
|
|
|
|
user.Id = 2
|
|
|
|
_, err = db.Model(table).Data(user).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
})
|
|
|
|
}
|
2021-11-17 22:01:17 +08:00
|
|
|
|
|
|
|
// Using filter does not affect the outside value inside function.
|
|
|
|
func Test_Model_Insert_Filter(t *testing.T) {
|
|
|
|
// map
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
data := g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"uid": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
}
|
|
|
|
result, err := db.Model(table).Data(data).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
n, _ := result.LastInsertId()
|
|
|
|
t.Assert(n, 1)
|
|
|
|
|
|
|
|
t.Assert(data["uid"], 1)
|
|
|
|
})
|
|
|
|
// slice
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
data := g.List{
|
|
|
|
g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"uid": 1,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
},
|
|
|
|
g.Map{
|
|
|
|
"id": 2,
|
|
|
|
"uid": 2,
|
|
|
|
"passport": "t1",
|
|
|
|
"password": "25d55ad283aa400af464c76d713c07ad",
|
|
|
|
"nickname": "name_1",
|
|
|
|
"create_time": gtime.Now().String(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := db.Model(table).Data(data).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
n, _ := result.LastInsertId()
|
|
|
|
t.Assert(n, 2)
|
|
|
|
|
|
|
|
t.Assert(data[0]["uid"], 1)
|
|
|
|
t.Assert(data[1]["uid"], 2)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_Embedded_Filter(t *testing.T) {
|
|
|
|
table := createTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type Base struct {
|
|
|
|
Id int
|
|
|
|
Uid int
|
|
|
|
CreateTime string
|
|
|
|
NoneExist string
|
|
|
|
}
|
|
|
|
type User struct {
|
|
|
|
Base
|
|
|
|
Passport string
|
|
|
|
Password string
|
|
|
|
Nickname string
|
|
|
|
}
|
|
|
|
result, err := db.Model(table).Data(User{
|
|
|
|
Passport: "john-test",
|
|
|
|
Password: "123456",
|
|
|
|
Nickname: "John",
|
|
|
|
Base: Base{
|
|
|
|
Id: 100,
|
|
|
|
Uid: 100,
|
|
|
|
CreateTime: gtime.Now().String(),
|
|
|
|
},
|
|
|
|
}).Insert()
|
|
|
|
t.AssertNil(err)
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
t.Assert(n, 1)
|
|
|
|
|
|
|
|
var user *User
|
|
|
|
err = db.Model(table).Fields(user).Where("id=100").Scan(&user)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(user.Passport, "john-test")
|
|
|
|
t.Assert(user.Id, 100)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is no longer used as the filter feature is automatically enabled from GoFrame v1.16.0.
|
|
|
|
// func Test_Model_Insert_KeyFieldNameMapping_Error(t *testing.T) {
|
|
|
|
// table := createTable()
|
|
|
|
// defer dropTable(table)
|
|
|
|
//
|
|
|
|
// gtest.C(t, func(t *gtest.T) {
|
|
|
|
// type User struct {
|
|
|
|
// Id int
|
|
|
|
// Passport string
|
|
|
|
// Password string
|
|
|
|
// Nickname string
|
|
|
|
// CreateTime string
|
2023-11-08 21:26:51 +08:00
|
|
|
// NoneExistField string
|
2021-11-17 22:01:17 +08:00
|
|
|
// }
|
|
|
|
// data := User{
|
|
|
|
// Id: 1,
|
|
|
|
// Passport: "user_1",
|
|
|
|
// Password: "pass_1",
|
|
|
|
// Nickname: "name_1",
|
|
|
|
// CreateTime: "2020-10-10 12:00:01",
|
|
|
|
// }
|
|
|
|
// _, err := db.Model(table).Data(data).Insert()
|
|
|
|
// t.AssertNE(err, nil)
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
|
|
|
func Test_Model_Fields_AutoFilterInJoinStatement(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var err error
|
|
|
|
table1 := "user"
|
|
|
|
table2 := "score"
|
|
|
|
table3 := "info"
|
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
|
|
|
CREATE TABLE IF NOT EXISTS %s (
|
|
|
|
id int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
name varchar(500) NOT NULL DEFAULT '',
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
|
|
|
`, table1,
|
|
|
|
)); err != nil {
|
|
|
|
t.AssertNil(err)
|
|
|
|
}
|
|
|
|
defer dropTable(table1)
|
|
|
|
_, err = db.Model(table1).Insert(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"name": "john",
|
|
|
|
})
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
|
|
|
CREATE TABLE IF NOT EXISTS %s (
|
|
|
|
id int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
user_id int(11) NOT NULL DEFAULT 0,
|
|
|
|
number varchar(500) NOT NULL DEFAULT '',
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
|
|
|
`, table2,
|
|
|
|
)); err != nil {
|
|
|
|
t.AssertNil(err)
|
|
|
|
}
|
|
|
|
defer dropTable(table2)
|
|
|
|
_, err = db.Model(table2).Insert(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"user_id": 1,
|
|
|
|
"number": "n",
|
|
|
|
})
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
|
|
|
CREATE TABLE IF NOT EXISTS %s (
|
|
|
|
id int(11) NOT NULL AUTO_INCREMENT,
|
|
|
|
user_id int(11) NOT NULL DEFAULT 0,
|
|
|
|
description varchar(500) NOT NULL DEFAULT '',
|
|
|
|
PRIMARY KEY (id)
|
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
|
|
|
`, table3,
|
|
|
|
)); err != nil {
|
|
|
|
t.AssertNil(err)
|
|
|
|
}
|
|
|
|
defer dropTable(table3)
|
|
|
|
_, err = db.Model(table3).Insert(g.Map{
|
|
|
|
"id": 1,
|
|
|
|
"user_id": 1,
|
|
|
|
"description": "brief",
|
|
|
|
})
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
one, err := db.Model("user").
|
|
|
|
Where("user.id", 1).
|
|
|
|
Fields("score.number,user.name").
|
|
|
|
LeftJoin("score", "user.id=score.user_id").
|
|
|
|
LeftJoin("info", "info.id=info.user_id").
|
|
|
|
Order("user.id asc").
|
|
|
|
One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(one), 2)
|
|
|
|
t.Assert(one["name"].String(), "john")
|
|
|
|
t.Assert(one["number"].String(), "n")
|
|
|
|
|
|
|
|
one, err = db.Model("user").
|
|
|
|
LeftJoin("score", "user.id=score.user_id").
|
|
|
|
LeftJoin("info", "info.id=info.user_id").
|
|
|
|
Fields("score.number,user.name").
|
|
|
|
One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(one), 2)
|
|
|
|
t.Assert(one["name"].String(), "john")
|
|
|
|
t.Assert(one["number"].String(), "n")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WherePrefix(t *testing.T) {
|
|
|
|
var (
|
|
|
|
table1 = gtime.TimestampNanoStr() + "_table1"
|
|
|
|
table2 = gtime.TimestampNanoStr() + "_table2"
|
|
|
|
)
|
|
|
|
createInitTable(table1)
|
|
|
|
defer dropTable(table1)
|
|
|
|
createInitTable(table2)
|
|
|
|
defer dropTable(table2)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
r, err := db.Model(table1).
|
|
|
|
FieldsPrefix(table1, "*").
|
|
|
|
LeftJoinOnField(table2, "id").
|
|
|
|
WherePrefix(table2, g.Map{
|
|
|
|
"id": g.Slice{1, 2},
|
|
|
|
}).
|
|
|
|
Order("id asc").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(r), 2)
|
|
|
|
t.Assert(r[0]["id"], "1")
|
|
|
|
t.Assert(r[1]["id"], "2")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WhereOrPrefix(t *testing.T) {
|
|
|
|
var (
|
|
|
|
table1 = gtime.TimestampNanoStr() + "_table1"
|
|
|
|
table2 = gtime.TimestampNanoStr() + "_table2"
|
|
|
|
)
|
|
|
|
createInitTable(table1)
|
|
|
|
defer dropTable(table1)
|
|
|
|
createInitTable(table2)
|
|
|
|
defer dropTable(table2)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
r, err := db.Model(table1).
|
|
|
|
FieldsPrefix(table1, "*").
|
|
|
|
LeftJoinOnField(table2, "id").
|
|
|
|
WhereOrPrefix(table1, g.Map{
|
|
|
|
"id": g.Slice{1, 2},
|
|
|
|
}).
|
|
|
|
WhereOrPrefix(table2, g.Map{
|
|
|
|
"id": g.Slice{8, 9},
|
|
|
|
}).
|
|
|
|
Order("id asc").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(r), 4)
|
|
|
|
t.Assert(r[0]["id"], "1")
|
|
|
|
t.Assert(r[1]["id"], "2")
|
|
|
|
t.Assert(r[2]["id"], "8")
|
|
|
|
t.Assert(r[3]["id"], "9")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Model_WherePrefixLike(t *testing.T) {
|
|
|
|
var (
|
|
|
|
table1 = gtime.TimestampNanoStr() + "_table1"
|
|
|
|
table2 = gtime.TimestampNanoStr() + "_table2"
|
|
|
|
)
|
|
|
|
createInitTable(table1)
|
|
|
|
defer dropTable(table1)
|
|
|
|
createInitTable(table2)
|
|
|
|
defer dropTable(table2)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
r, err := db.Model(table1).
|
|
|
|
FieldsPrefix(table1, "*").
|
|
|
|
LeftJoinOnField(table2, "id").
|
|
|
|
WherePrefix(table1, g.Map{
|
|
|
|
"id": g.Slice{1, 2, 3},
|
|
|
|
}).
|
|
|
|
WherePrefix(table2, g.Map{
|
|
|
|
"id": g.Slice{3, 4, 5},
|
|
|
|
}).
|
|
|
|
WherePrefixLike(table2, "nickname", "name%").
|
|
|
|
Order("id asc").All()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(r), 1)
|
|
|
|
t.Assert(r[0]["id"], "3")
|
|
|
|
})
|
|
|
|
}
|
2022-04-08 18:11:17 +08:00
|
|
|
|
2022-05-10 15:38:08 +08:00
|
|
|
// https://github.com/gogf/gf/issues/1159
|
|
|
|
func Test_ScanList_NoRecreate_PtrAttribute(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type S1 struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
type S3 struct {
|
|
|
|
One *S1
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
s []*S3
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
r1 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(1),
|
|
|
|
"name": gvar.New("john"),
|
|
|
|
"age": gvar.New(16),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(2),
|
|
|
|
"name": gvar.New("smith"),
|
|
|
|
"age": gvar.New(18),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r1.ScanList(&s, "One")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 16)
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 18)
|
|
|
|
|
|
|
|
r2 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(1),
|
|
|
|
"age": gvar.New(20),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(2),
|
|
|
|
"age": gvar.New(21),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r2.ScanList(&s, "One", "One", "id:Id")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 20)
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 21)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1159
|
|
|
|
func Test_ScanList_NoRecreate_StructAttribute(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type S1 struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
type S3 struct {
|
|
|
|
One S1
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
s []*S3
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
r1 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(1),
|
|
|
|
"name": gvar.New("john"),
|
|
|
|
"age": gvar.New(16),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(2),
|
|
|
|
"name": gvar.New("smith"),
|
|
|
|
"age": gvar.New(18),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r1.ScanList(&s, "One")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 16)
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 18)
|
|
|
|
|
|
|
|
r2 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(1),
|
|
|
|
"age": gvar.New(20),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(2),
|
|
|
|
"age": gvar.New(21),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r2.ScanList(&s, "One", "One", "id:Id")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 20)
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 21)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1159
|
|
|
|
func Test_ScanList_NoRecreate_SliceAttribute_Ptr(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type S1 struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
type S2 struct {
|
|
|
|
Id int
|
|
|
|
Pid int
|
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
type S3 struct {
|
|
|
|
One *S1
|
|
|
|
Many []*S2
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
s []*S3
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
r1 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(1),
|
|
|
|
"name": gvar.New("john"),
|
|
|
|
"age": gvar.New(16),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(2),
|
|
|
|
"name": gvar.New("smith"),
|
|
|
|
"age": gvar.New(18),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r1.ScanList(&s, "One")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 16)
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 18)
|
|
|
|
|
|
|
|
r2 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(100),
|
|
|
|
"pid": gvar.New(1),
|
|
|
|
"age": gvar.New(30),
|
|
|
|
"name": gvar.New("john"),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(200),
|
|
|
|
"pid": gvar.New(1),
|
|
|
|
"age": gvar.New(31),
|
|
|
|
"name": gvar.New("smith"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r2.ScanList(&s, "Many", "One", "pid:Id")
|
|
|
|
// fmt.Printf("%+v", err)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 16)
|
|
|
|
t.Assert(len(s[0].Many), 2)
|
|
|
|
t.Assert(s[0].Many[0].Name, "john")
|
|
|
|
t.Assert(s[0].Many[0].Age, 30)
|
|
|
|
t.Assert(s[0].Many[1].Name, "smith")
|
|
|
|
t.Assert(s[0].Many[1].Age, 31)
|
|
|
|
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 18)
|
|
|
|
t.Assert(len(s[1].Many), 0)
|
|
|
|
|
|
|
|
r3 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(100),
|
|
|
|
"pid": gvar.New(1),
|
|
|
|
"age": gvar.New(40),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(200),
|
|
|
|
"pid": gvar.New(1),
|
|
|
|
"age": gvar.New(41),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r3.ScanList(&s, "Many", "One", "pid:Id")
|
|
|
|
// fmt.Printf("%+v", err)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 16)
|
|
|
|
t.Assert(len(s[0].Many), 2)
|
|
|
|
t.Assert(s[0].Many[0].Name, "john")
|
|
|
|
t.Assert(s[0].Many[0].Age, 40)
|
|
|
|
t.Assert(s[0].Many[1].Name, "smith")
|
|
|
|
t.Assert(s[0].Many[1].Age, 41)
|
|
|
|
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 18)
|
|
|
|
t.Assert(len(s[1].Many), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/gogf/gf/issues/1159
|
|
|
|
func Test_ScanList_NoRecreate_SliceAttribute_Struct(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
type S1 struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
type S2 struct {
|
|
|
|
Id int
|
|
|
|
Pid int
|
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
type S3 struct {
|
|
|
|
One S1
|
|
|
|
Many []S2
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
s []S3
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
r1 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(1),
|
|
|
|
"name": gvar.New("john"),
|
|
|
|
"age": gvar.New(16),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(2),
|
|
|
|
"name": gvar.New("smith"),
|
|
|
|
"age": gvar.New(18),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r1.ScanList(&s, "One")
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 16)
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 18)
|
|
|
|
|
|
|
|
r2 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(100),
|
|
|
|
"pid": gvar.New(1),
|
|
|
|
"age": gvar.New(30),
|
|
|
|
"name": gvar.New("john"),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(200),
|
|
|
|
"pid": gvar.New(1),
|
|
|
|
"age": gvar.New(31),
|
|
|
|
"name": gvar.New("smith"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r2.ScanList(&s, "Many", "One", "pid:Id")
|
|
|
|
// fmt.Printf("%+v", err)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 16)
|
|
|
|
t.Assert(len(s[0].Many), 2)
|
|
|
|
t.Assert(s[0].Many[0].Name, "john")
|
|
|
|
t.Assert(s[0].Many[0].Age, 30)
|
|
|
|
t.Assert(s[0].Many[1].Name, "smith")
|
|
|
|
t.Assert(s[0].Many[1].Age, 31)
|
|
|
|
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 18)
|
|
|
|
t.Assert(len(s[1].Many), 0)
|
|
|
|
|
|
|
|
r3 := gdb.Result{
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(100),
|
|
|
|
"pid": gvar.New(1),
|
|
|
|
"age": gvar.New(40),
|
|
|
|
},
|
|
|
|
gdb.Record{
|
|
|
|
"id": gvar.New(200),
|
|
|
|
"pid": gvar.New(1),
|
|
|
|
"age": gvar.New(41),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err = r3.ScanList(&s, "Many", "One", "pid:Id")
|
|
|
|
// fmt.Printf("%+v", err)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(s), 2)
|
|
|
|
t.Assert(s[0].One.Name, "john")
|
|
|
|
t.Assert(s[0].One.Age, 16)
|
|
|
|
t.Assert(len(s[0].Many), 2)
|
|
|
|
t.Assert(s[0].Many[0].Name, "john")
|
|
|
|
t.Assert(s[0].Many[0].Age, 40)
|
|
|
|
t.Assert(s[0].Many[1].Name, "smith")
|
|
|
|
t.Assert(s[0].Many[1].Age, 41)
|
|
|
|
|
|
|
|
t.Assert(s[1].One.Name, "smith")
|
|
|
|
t.Assert(s[1].One.Age, 18)
|
|
|
|
t.Assert(len(s[1].Many), 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResult_Structs1(t *testing.T) {
|
|
|
|
type A struct {
|
|
|
|
Id int `orm:"id"`
|
|
|
|
}
|
|
|
|
type B struct {
|
|
|
|
*A
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
r := gdb.Result{
|
|
|
|
gdb.Record{"id": gvar.New(nil), "name": gvar.New("john")},
|
2024-04-01 19:07:08 +08:00
|
|
|
gdb.Record{"id": gvar.New(1), "name": gvar.New("smith")},
|
2022-05-10 15:38:08 +08:00
|
|
|
}
|
|
|
|
array := make([]*B, 2)
|
|
|
|
err := r.Structs(&array)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(array[0].Id, 0)
|
2024-04-01 19:07:08 +08:00
|
|
|
t.Assert(array[1].Id, 1)
|
2022-05-10 15:38:08 +08:00
|
|
|
t.Assert(array[0].Name, "john")
|
|
|
|
t.Assert(array[1].Name, "smith")
|
|
|
|
})
|
|
|
|
}
|
2022-10-12 20:30:47 +08:00
|
|
|
|
|
|
|
func Test_Builder_OmitEmptyWhere(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", 1).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(1))
|
2022-10-12 20:30:47 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
count, err := db.Model(table).Where("id", 0).OmitEmptyWhere().Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2022-10-12 20:30:47 +08:00
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
builder := db.Model(table).OmitEmptyWhere().Builder()
|
|
|
|
count, err := db.Model(table).Where(
|
|
|
|
builder.Where("id", 0),
|
|
|
|
).Count()
|
|
|
|
t.AssertNil(err)
|
2022-11-17 19:47:17 +08:00
|
|
|
t.Assert(count, int64(TableSize))
|
2022-10-12 20:30:47 +08:00
|
|
|
})
|
|
|
|
}
|
2023-08-17 20:29:06 +08:00
|
|
|
|
|
|
|
func Test_Scan_Nil_Result_Error(t *testing.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
type S struct {
|
|
|
|
Id int
|
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
Score int
|
|
|
|
}
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var s *S
|
|
|
|
err := db.Model(table).Where("id", 1).Scan(&s)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(s.Id, 1)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var s *S
|
|
|
|
err := db.Model(table).Where("id", 100).Scan(&s)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(s, nil)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var s S
|
|
|
|
err := db.Model(table).Where("id", 100).Scan(&s)
|
|
|
|
t.Assert(err, sql.ErrNoRows)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var ss []*S
|
|
|
|
err := db.Model(table).Scan(&ss)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(ss), TableSize)
|
|
|
|
})
|
|
|
|
// If the result is empty, it returns error.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var ss = make([]*S, 10)
|
|
|
|
err := db.Model(table).WhereGT("id", 100).Scan(&ss)
|
|
|
|
t.Assert(err, sql.ErrNoRows)
|
|
|
|
})
|
|
|
|
}
|
2023-11-20 20:47:26 +08:00
|
|
|
|
|
|
|
func Test_Model_FixGdbJoin(t *testing.T) {
|
|
|
|
array := gstr.SplitAndTrim(gtest.DataContent(`fix_gdb_join.sql`), ";")
|
|
|
|
for _, v := range array {
|
|
|
|
if _, err := db.Exec(ctx, v); err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer dropTable(`common_resource`)
|
|
|
|
defer dropTable(`managed_resource`)
|
|
|
|
defer dropTable(`rules_template`)
|
|
|
|
defer dropTable(`resource_mark`)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.AssertNil(db.GetCore().ClearCacheAll(ctx))
|
|
|
|
sqlSlice, err := gdb.CatchSQL(ctx, func(ctx context.Context) error {
|
|
|
|
orm := db.Model(`managed_resource`).Ctx(ctx).
|
|
|
|
LeftJoinOnField(`common_resource`, `resource_id`).
|
|
|
|
LeftJoinOnFields(`resource_mark`, `resource_mark_id`, `=`, `id`).
|
|
|
|
LeftJoinOnFields(`rules_template`, `rule_template_id`, `=`, `template_id`).
|
|
|
|
FieldsPrefix(
|
|
|
|
`managed_resource`,
|
|
|
|
"resource_id", "user", "status", "status_message", "safe_publication", "rule_template_id",
|
|
|
|
"created_at", "comments", "expired_at", "resource_mark_id", "instance_id", "resource_name",
|
|
|
|
"pay_mode").
|
|
|
|
FieldsPrefix(`resource_mark`, "mark_name", "color").
|
|
|
|
FieldsPrefix(`rules_template`, "name").
|
|
|
|
FieldsPrefix(`common_resource`, `src_instance_id`, "database_kind", "source_type", "ip", "port")
|
|
|
|
all, err := orm.OrderAsc("src_instance_id").All()
|
|
|
|
t.Assert(len(all), 4)
|
|
|
|
t.Assert(all[0]["pay_mode"], 1)
|
|
|
|
t.Assert(all[0]["src_instance_id"], 2)
|
|
|
|
t.Assert(all[3]["instance_id"], "dmcins-jxy0x75m")
|
|
|
|
t.Assert(all[3]["src_instance_id"], "vdb-6b6m3u1u")
|
|
|
|
t.Assert(all[3]["resource_mark_id"], "11")
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
t.AssertNil(err)
|
|
|
|
|
|
|
|
t.Assert(gtest.DataContent(`fix_gdb_join_expect.sql`), sqlSlice[len(sqlSlice)-1])
|
|
|
|
})
|
|
|
|
}
|
2024-09-25 16:33:52 +08:00
|
|
|
|
|
|
|
func Test_Model_Year_Date_Time_DateTime_Timestamp(t *testing.T) {
|
|
|
|
table := "date_time_example"
|
|
|
|
array := gstr.SplitAndTrim(gtest.DataContent(`date_time_example.sql`), ";")
|
|
|
|
for _, v := range array {
|
|
|
|
if _, err := db.Exec(ctx, v); err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer dropTable(table)
|
|
|
|
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
// insert.
|
|
|
|
var now = gtime.Now()
|
|
|
|
_, err := db.Model("date_time_example").Insert(g.Map{
|
|
|
|
"year": now,
|
|
|
|
"date": now,
|
|
|
|
"time": now,
|
|
|
|
"datetime": now,
|
|
|
|
"timestamp": now,
|
|
|
|
})
|
|
|
|
t.AssertNil(err)
|
|
|
|
// select.
|
|
|
|
one, err := db.Model("date_time_example").One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["year"].String(), now.Format("Y"))
|
|
|
|
t.Assert(one["date"].String(), now.Format("Y-m-d"))
|
|
|
|
t.Assert(one["time"].String(), now.Format("H:i:s"))
|
|
|
|
t.AssertLT(one["datetime"].GTime().Sub(now).Seconds(), 5)
|
|
|
|
t.AssertLT(one["timestamp"].GTime().Sub(now).Seconds(), 5)
|
|
|
|
})
|
|
|
|
}
|
2024-09-28 12:06:22 +08:00
|
|
|
|
|
|
|
func Test_OrderBy_Statement_Generated(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
array := gstr.SplitAndTrim(gtest.DataContent(`fix_gdb_order_by.sql`), ";")
|
|
|
|
for _, v := range array {
|
|
|
|
if _, err := db.Exec(ctx, v); err != nil {
|
|
|
|
gtest.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer dropTable(`employee`)
|
|
|
|
sqlArray, _ := gdb.CatchSQL(ctx, func(ctx context.Context) error {
|
|
|
|
g.DB("default").Ctx(ctx).Model("employee").Order("name asc", "age desc").All()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
rawSql := strings.ReplaceAll(sqlArray[len(sqlArray)-1], " ", "")
|
|
|
|
expectSql := strings.ReplaceAll("SELECT * FROM `employee` ORDER BY `name` asc, `age` desc", " ", "")
|
|
|
|
t.Assert(rawSql, expectSql)
|
|
|
|
})
|
|
|
|
}
|
2024-10-21 09:22:31 +08:00
|
|
|
|
|
|
|
func Test_Fields_Raw(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
table := createInitTable()
|
|
|
|
defer dropTable(table)
|
|
|
|
one, err := db.Model(table).Fields(gdb.Raw("1")).One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["1"], 1)
|
|
|
|
|
|
|
|
one, err = db.Model(table).Fields(gdb.Raw("2")).One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["2"], 2)
|
|
|
|
|
|
|
|
one, err = db.Model(table).Fields(gdb.Raw("2")).Where("id", 2).One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(one["2"], 2)
|
|
|
|
|
|
|
|
one, err = db.Model(table).Fields(gdb.Raw("2")).Where("id", 10000000000).One()
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(len(one), 0)
|
|
|
|
})
|
|
|
|
}
|