Skip to content

Commit cccc9a8

Browse files
authored
Merge branch 'master' into fix/issue-4725-schema-quote
2 parents 0890702 + f67b2dc commit cccc9a8

38 files changed

Lines changed: 20648 additions & 15 deletions

contrib/drivers/mariadb/mariadb_unit_init_test.go

Lines changed: 118 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package mariadb_test
99
import (
1010
"context"
1111
"fmt"
12+
"testing"
1213
"time"
1314

1415
_ "github.com/gogf/gf/contrib/drivers/mariadb/v2"
@@ -21,18 +22,24 @@ import (
2122
)
2223

2324
const (
24-
TableSize = 10
25-
TableName = "user"
26-
TestSchema1 = "test1"
27-
TestSchema2 = "test2"
28-
TestDbPass = "12345678"
29-
CreateTime = "2018-10-24 10:00:00"
25+
TableSize = 10
26+
TableName = "user"
27+
TestSchema1 = "test1"
28+
TestSchema2 = "test2"
29+
TestPartitionDB = "test3"
30+
TableNamePrefix1 = "gf_"
31+
TestDbUser = "root"
32+
TestDbPass = "12345678"
33+
CreateTime = "2018-10-24 10:00:00"
3034
)
3135

3236
var (
33-
db gdb.DB
34-
db2 gdb.DB
35-
ctx = context.TODO()
37+
db gdb.DB
38+
db2 gdb.DB
39+
db3 gdb.DB
40+
dbPrefix gdb.DB
41+
dbInvalid gdb.DB
42+
ctx = context.TODO()
3643
)
3744

3845
func init() {
@@ -41,10 +48,26 @@ func init() {
4148
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
4249
TranTimeout: time.Second * 3,
4350
}
44-
err := gdb.AddConfigNode(gdb.DefaultGroupName, nodeDefault)
45-
if err != nil {
46-
panic(err)
51+
partitionDefault := gdb.ConfigNode{
52+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
53+
Debug: true,
54+
TranTimeout: time.Second * 3,
55+
}
56+
nodePrefix := gdb.ConfigNode{
57+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
58+
TranTimeout: time.Second * 3,
4759
}
60+
nodePrefix.Prefix = TableNamePrefix1
61+
62+
nodeInvalid := gdb.ConfigNode{
63+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3317)/?loc=Local&parseTime=true", TestDbPass),
64+
TranTimeout: time.Second * 3,
65+
}
66+
gdb.AddConfigNode("test", nodeDefault)
67+
gdb.AddConfigNode("prefix", nodePrefix)
68+
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
69+
gdb.AddConfigNode("partition", partitionDefault)
70+
gdb.AddConfigNode(gdb.DefaultGroupName, nodeDefault)
4871

4972
// Default db.
5073
if r, err := gdb.NewByGroup(); err != nil {
@@ -59,8 +82,33 @@ func init() {
5982
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
6083
gtest.Error(err)
6184
}
85+
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, TestPartitionDB)); err != nil {
86+
gtest.Error(err)
87+
}
6288
db = db.Schema(TestSchema1)
6389
db2 = db.Schema(TestSchema2)
90+
db3 = db.Schema(TestPartitionDB)
91+
// Prefix db.
92+
if r, err := gdb.NewByGroup("prefix"); err != nil {
93+
gtest.Error(err)
94+
} else {
95+
dbPrefix = r
96+
}
97+
if _, err := dbPrefix.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema1)); err != nil {
98+
gtest.Error(err)
99+
}
100+
if _, err := dbPrefix.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
101+
gtest.Error(err)
102+
}
103+
dbPrefix = dbPrefix.Schema(TestSchema1)
104+
105+
// Invalid db.
106+
if r, err := gdb.NewByGroup("nodeinvalid"); err != nil {
107+
gtest.Error(err)
108+
} else {
109+
dbInvalid = r
110+
}
111+
dbInvalid = dbInvalid.Schema(TestSchema1)
64112
}
65113

66114
func createTable(table ...string) string {
@@ -126,3 +174,61 @@ func dropTableWithDb(db gdb.DB, table string) {
126174
gtest.Error(err)
127175
}
128176
}
177+
178+
func Test_PartitionTable(t *testing.T) {
179+
dropShopDBTable()
180+
createShopDBTable()
181+
insertShopDBData()
182+
183+
// defer dropShopDBTable()
184+
gtest.C(t, func(t *gtest.T) {
185+
data, err := db3.Ctx(ctx).Model("dbx_order").Partition("p3", "p4").All()
186+
t.AssertNil(err)
187+
dataLen := len(data)
188+
t.Assert(dataLen, 5)
189+
data, err = db3.Ctx(ctx).Model("dbx_order").Partition("p3").All()
190+
t.AssertNil(err)
191+
dataLen = len(data)
192+
t.Assert(dataLen, 5)
193+
})
194+
}
195+
196+
func createShopDBTable() {
197+
sql := `CREATE TABLE dbx_order (
198+
id int(11) NOT NULL,
199+
sales_date date DEFAULT NULL,
200+
amount decimal(10,2) DEFAULT NULL
201+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
202+
PARTITION BY RANGE (YEAR(sales_date))
203+
(PARTITION p1 VALUES LESS THAN (2020) ENGINE = InnoDB,
204+
PARTITION p2 VALUES LESS THAN (2021) ENGINE = InnoDB,
205+
PARTITION p3 VALUES LESS THAN (2022) ENGINE = InnoDB,
206+
PARTITION p4 VALUES LESS THAN MAXVALUE ENGINE = InnoDB);`
207+
_, err := db3.Exec(ctx, sql)
208+
if err != nil {
209+
gtest.Fatal(err.Error())
210+
}
211+
}
212+
213+
func insertShopDBData() {
214+
data := g.Slice{}
215+
year := 2020
216+
for i := 1; i <= 5; i++ {
217+
year++
218+
data = append(data, g.Map{
219+
"id": i,
220+
"sales_date": fmt.Sprintf("%d-09-21", year),
221+
"amount": fmt.Sprintf("1%d.21", i),
222+
})
223+
}
224+
_, err := db3.Model("dbx_order").Ctx(ctx).Data(data).Insert()
225+
if err != nil {
226+
gtest.Error(err)
227+
}
228+
}
229+
230+
func dropShopDBTable() {
231+
if _, err := db3.Exec(ctx, "DROP TABLE IF EXISTS `dbx_order`"); err != nil {
232+
gtest.Error(err)
233+
}
234+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2+
//
3+
// This Source Code Form is subject to the terms of the MIT License.
4+
// If a copy of the MIT was not distributed with this file,
5+
// You can obtain one at https://github.com/gogf/gf.
6+
7+
package mariadb_test
8+
9+
import (
10+
"context"
11+
"testing"
12+
13+
"github.com/gogf/gf/v2/database/gdb"
14+
"github.com/gogf/gf/v2/test/gtest"
15+
)
16+
17+
func Test_Instance(t *testing.T) {
18+
gtest.C(t, func(t *gtest.T) {
19+
_, err := gdb.Instance("none")
20+
t.AssertNE(err, nil)
21+
22+
db, err := gdb.Instance()
23+
t.AssertNil(err)
24+
25+
err1 := db.PingMaster()
26+
err2 := db.PingSlave()
27+
t.Assert(err1, nil)
28+
t.Assert(err2, nil)
29+
})
30+
}
31+
32+
func Test_Func_FormatSqlWithArgs(t *testing.T) {
33+
// mysql
34+
gtest.C(t, func(t *gtest.T) {
35+
var s string
36+
s = gdb.FormatSqlWithArgs("select * from table where id>=? and sex=?", []any{100, 1})
37+
t.Assert(s, "select * from table where id>=100 and sex=1")
38+
})
39+
// mssql
40+
gtest.C(t, func(t *gtest.T) {
41+
var s string
42+
s = gdb.FormatSqlWithArgs("select * from table where id>=@p1 and sex=@p2", []any{100, 1})
43+
t.Assert(s, "select * from table where id>=100 and sex=1")
44+
})
45+
// pgsql
46+
gtest.C(t, func(t *gtest.T) {
47+
var s string
48+
s = gdb.FormatSqlWithArgs("select * from table where id>=$1 and sex=$2", []any{100, 1})
49+
t.Assert(s, "select * from table where id>=100 and sex=1")
50+
})
51+
// oracle
52+
gtest.C(t, func(t *gtest.T) {
53+
var s string
54+
s = gdb.FormatSqlWithArgs("select * from table where id>=:v1 and sex=:v2", []any{100, 1})
55+
t.Assert(s, "select * from table where id>=100 and sex=1")
56+
})
57+
}
58+
59+
func Test_Func_ToSQL(t *testing.T) {
60+
gtest.C(t, func(t *gtest.T) {
61+
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
62+
value, err := db.Ctx(ctx).Model(TableName).Fields("nickname").Where("id", 1).Value()
63+
t.Assert(value, nil)
64+
return err
65+
})
66+
t.AssertNil(err)
67+
t.Assert(sql, "SELECT `nickname` FROM `user` WHERE `id`=1 LIMIT 1")
68+
})
69+
}
70+
71+
func Test_Func_CatchSQL(t *testing.T) {
72+
table := createInitTable()
73+
defer dropTable(table)
74+
gtest.C(t, func(t *gtest.T) {
75+
array, err := gdb.CatchSQL(ctx, func(ctx context.Context) error {
76+
value, err := db.Ctx(ctx).Model(table).Fields("nickname").Where("id", 1).Value()
77+
t.Assert(value, "name_1")
78+
return err
79+
})
80+
t.AssertNil(err)
81+
t.AssertGE(len(array), 1)
82+
})
83+
}

0 commit comments

Comments
 (0)