gf/database/gdb/gdb_z_init_test.go

213 lines
5.1 KiB
Go
Raw Normal View History

// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
//
// 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.
2018-12-17 19:26:59 +08:00
package gdb_test
import (
2020-01-06 20:43:59 +08:00
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gcmd"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
)
const (
SIZE = 10
TABLE = "user"
SCHEMA1 = "test1"
SCHEMA2 = "test2"
PREFIX1 = "gf_"
2020-07-05 11:54:37 +08:00
USER = "root"
PASS = "12345678"
2020-01-06 20:43:59 +08:00
)
var (
db gdb.DB
dbPrefix gdb.DB
configNode gdb.ConfigNode
2019-03-17 22:26:41 +08:00
)
2019-08-19 19:21:27 +08:00
func init() {
2020-01-06 20:43:59 +08:00
parser, err := gcmd.Parse(g.MapStrBool{
"name": true,
"type": true,
}, false)
2020-03-20 08:49:40 +08:00
gtest.Assert(err, nil)
2020-01-06 20:43:59 +08:00
configNode = gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
2020-07-05 11:23:39 +08:00
User: USER,
Pass: PASS,
2020-01-06 20:43:59 +08:00
Name: parser.GetOpt("name", ""),
Type: parser.GetOpt("type", "mysql"),
Role: "master",
Charset: "utf8",
Weight: 1,
MaxIdleConnCount: 10,
MaxOpenConnCount: 10,
MaxConnLifetime: 600,
}
nodePrefix := configNode
nodePrefix.Prefix = PREFIX1
gdb.AddConfigNode("test", configNode)
gdb.AddConfigNode("prefix", nodePrefix)
2020-12-14 19:34:02 +08:00
gdb.AddConfigNode(gdb.DefaultGroupName, configNode)
2020-01-06 20:43:59 +08:00
// Default db.
if r, err := gdb.New(); err != nil {
gtest.Error(err)
} else {
db = r
}
schemaTemplate := "CREATE DATABASE IF NOT EXISTS `%s` CHARACTER SET UTF8"
if _, err := db.Exec(fmt.Sprintf(schemaTemplate, SCHEMA1)); err != nil {
gtest.Error(err)
}
if _, err := db.Exec(fmt.Sprintf(schemaTemplate, SCHEMA2)); err != nil {
gtest.Error(err)
}
db.SetSchema(SCHEMA1)
createTable(TABLE)
// Prefix db.
if r, err := gdb.New("prefix"); err != nil {
gtest.Error(err)
} else {
dbPrefix = r
}
if _, err := dbPrefix.Exec(fmt.Sprintf(schemaTemplate, SCHEMA1)); err != nil {
gtest.Error(err)
}
if _, err := dbPrefix.Exec(fmt.Sprintf(schemaTemplate, SCHEMA2)); err != nil {
gtest.Error(err)
}
dbPrefix.SetSchema(SCHEMA1)
createTable(TABLE)
}
func createTable(table ...string) string {
return createTableWithDb(db, table...)
}
func createInitTable(table ...string) string {
return createInitTableWithDb(db, table...)
}
func dropTable(table string) {
dropTableWithDb(db, table)
}
func createTableWithDb(db gdb.DB, table ...string) (name string) {
if len(table) > 0 {
name = table[0]
} else {
name = fmt.Sprintf(`%s_%d`, TABLE, gtime.TimestampNano())
}
dropTableWithDb(db, name)
switch configNode.Type {
case "sqlite":
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
id bigint unsigned NOT NULL AUTO_INCREMENT,
2020-01-06 20:43:59 +08:00
passport varchar(45),
password char(32) NOT NULL,
nickname varchar(45) NOT NULL,
create_time timestamp NOT NULL,
PRIMARY KEY (id)
) ;`, name,
)); err != nil {
gtest.Fatal(err)
}
case "pgsql":
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
id bigint NOT NULL,
passport varchar(45),
password char(32) NOT NULL,
nickname varchar(45) NOT NULL,
create_time timestamp NOT NULL,
PRIMARY KEY (id)
) ;`, name,
)); err != nil {
gtest.Fatal(err)
}
case "mssql":
if _, err := db.Exec(fmt.Sprintf(`
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='%s' and xtype='U')
CREATE TABLE %s (
ID numeric(10,0) NOT NULL,
PASSPORT VARCHAR(45) NOT NULL,
PASSWORD CHAR(32) NOT NULL,
NICKNAME VARCHAR(45) NOT NULL,
CREATE_TIME datetime NOT NULL,
PRIMARY KEY (ID))`,
name, name,
)); err != nil {
gtest.Fatal(err)
}
case "oracle":
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
ID NUMBER(10) NOT NULL,
PASSPORT VARCHAR(45) NOT NULL,
PASSWORD CHAR(32) NOT NULL,
NICKNAME VARCHAR(45) NOT NULL,
CREATE_TIME varchar(45) NOT NULL,
PRIMARY KEY (ID))
`, name,
)); err != nil {
gtest.Fatal(err)
}
case "mysql":
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
passport varchar(45) NULL,
password char(32) NULL,
nickname varchar(45) NULL,
create_time timestamp NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, name,
)); err != nil {
gtest.Fatal(err)
}
}
return
}
func createInitTableWithDb(db gdb.DB, table ...string) (name string) {
name = createTableWithDb(db, table...)
array := garray.New(true)
for i := 1; i <= SIZE; i++ {
array.Append(g.Map{
"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(),
})
}
result, err := db.BatchInsert(name, array.Slice())
2020-03-19 23:53:03 +08:00
gtest.Assert(err, nil)
2020-01-06 20:43:59 +08:00
n, e := result.RowsAffected()
2020-03-19 23:53:03 +08:00
gtest.Assert(e, nil)
gtest.Assert(n, SIZE)
2020-01-06 20:43:59 +08:00
return
}
func dropTableWithDb(db gdb.DB, table string) {
if _, err := db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)); err != nil {
gtest.Error(err)
}
}