mirror of
https://gitee.com/johng/gf
synced 2026-07-03 11:51:04 +08:00
Compare commits
6 Commits
contrib/dr
...
v2.6.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 85d95e3e27 | |||
| 4cce8557e6 | |||
| d60d7674d7 | |||
| 7f9467d1f8 | |||
| d08e3ef838 | |||
| 645c5ff5b5 |
@ -3,13 +3,13 @@ module github.com/gogf/gf/cmd/gf/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.0
|
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.0
|
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.0
|
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.0
|
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.0
|
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.0
|
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.1
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f
|
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f
|
||||||
github.com/olekukonko/tablewriter v0.0.5
|
github.com/olekukonko/tablewriter v0.0.5
|
||||||
golang.org/x/mod v0.9.0
|
golang.org/x/mod v0.9.0
|
||||||
|
|||||||
@ -6,6 +6,33 @@
|
|||||||
|
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
var ctx = context.Background()
|
"github.com/gogf/gf/v2/database/gdb"
|
||||||
|
"github.com/gogf/gf/v2/test/gtest"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ctx = context.Background()
|
||||||
|
testDB gdb.DB
|
||||||
|
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
testDB, err = gdb.New(gdb.ConfigNode{
|
||||||
|
Link: link,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dropTableWithDb(db gdb.DB, table string) {
|
||||||
|
dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)
|
||||||
|
if _, err := db.Exec(ctx, dropTableStmt); err != nil {
|
||||||
|
gtest.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -12,7 +12,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao"
|
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao"
|
||||||
"github.com/gogf/gf/v2/database/gdb"
|
|
||||||
"github.com/gogf/gf/v2/os/gfile"
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
"github.com/gogf/gf/v2/test/gtest"
|
"github.com/gogf/gf/v2/test/gtest"
|
||||||
"github.com/gogf/gf/v2/text/gstr"
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
@ -20,22 +19,11 @@ import (
|
|||||||
"github.com/gogf/gf/v2/util/gutil"
|
"github.com/gogf/gf/v2/util/gutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func dropTableWithDb(db gdb.DB, table string) {
|
|
||||||
dropTableStmt := fmt.Sprintf("DROP TABLE IF EXISTS `%s`", table)
|
|
||||||
if _, err := db.Exec(ctx, dropTableStmt); err != nil {
|
|
||||||
gtest.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_Gen_Dao_Default(t *testing.T) {
|
func Test_Gen_Dao_Default(t *testing.T) {
|
||||||
link := "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
|
|
||||||
db, err := gdb.New(gdb.ConfigNode{
|
|
||||||
Link: link,
|
|
||||||
})
|
|
||||||
gtest.AssertNil(err)
|
|
||||||
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
var (
|
var (
|
||||||
|
err error
|
||||||
|
db = testDB
|
||||||
table = "table_user"
|
table = "table_user"
|
||||||
sqlContent = fmt.Sprintf(
|
sqlContent = fmt.Sprintf(
|
||||||
gtest.DataContent(`gendao`, `user.tpl.sql`),
|
gtest.DataContent(`gendao`, `user.tpl.sql`),
|
||||||
@ -123,14 +111,10 @@ func Test_Gen_Dao_Default(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Gen_Dao_TypeMapping(t *testing.T) {
|
func Test_Gen_Dao_TypeMapping(t *testing.T) {
|
||||||
link := "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
|
|
||||||
db, err := gdb.New(gdb.ConfigNode{
|
|
||||||
Link: link,
|
|
||||||
})
|
|
||||||
gtest.AssertNil(err)
|
|
||||||
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
var (
|
var (
|
||||||
|
err error
|
||||||
|
db = testDB
|
||||||
table = "table_user"
|
table = "table_user"
|
||||||
sqlContent = fmt.Sprintf(
|
sqlContent = fmt.Sprintf(
|
||||||
gtest.DataContent(`gendao`, `user.tpl.sql`),
|
gtest.DataContent(`gendao`, `user.tpl.sql`),
|
||||||
|
|||||||
70
cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go
Normal file
70
cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
// Copyright GoFrame gf Author(https://goframe.org). 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.
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gogf/gf/v2/os/gcmd"
|
||||||
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
|
"github.com/gogf/gf/v2/test/gtest"
|
||||||
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
|
"github.com/gogf/gf/v2/util/guid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_Gen_Pbentity_NameCase(t *testing.T) {
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
db = testDB
|
||||||
|
table = "table_user"
|
||||||
|
sqlContent = fmt.Sprintf(
|
||||||
|
gtest.DataContent(`genpbentity`, `user.tpl.sql`),
|
||||||
|
table,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
dropTableWithDb(db, table)
|
||||||
|
array := gstr.SplitAndTrim(sqlContent, ";")
|
||||||
|
for _, v := range array {
|
||||||
|
if _, err = db.Exec(ctx, v); err != nil {
|
||||||
|
t.AssertNil(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer dropTableWithDb(db, table)
|
||||||
|
var path = gfile.Temp(guid.S())
|
||||||
|
err = gfile.Mkdir(path)
|
||||||
|
t.AssertNil(err)
|
||||||
|
defer gfile.Remove(path)
|
||||||
|
|
||||||
|
root, err := gcmd.NewFromObject(GF)
|
||||||
|
t.AssertNil(err)
|
||||||
|
err = root.AddObject(
|
||||||
|
Gen,
|
||||||
|
)
|
||||||
|
t.AssertNil(err)
|
||||||
|
os.Args = []string{"gf", "gen", "pbentity", "-l", link, "-p", path, "-package=unittest", "-nameCase=SnakeScreaming"}
|
||||||
|
|
||||||
|
err = root.RunWithError(ctx)
|
||||||
|
t.AssertNil(err)
|
||||||
|
|
||||||
|
files := []string{
|
||||||
|
filepath.FromSlash(path + "/table_user.proto"),
|
||||||
|
}
|
||||||
|
|
||||||
|
testPath := gtest.DataPath("genpbentity", "generated_user")
|
||||||
|
expectFiles := []string{
|
||||||
|
filepath.FromSlash(testPath + "/table_user.proto"),
|
||||||
|
}
|
||||||
|
// check files content
|
||||||
|
for i := range files {
|
||||||
|
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -40,7 +40,7 @@ type (
|
|||||||
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenPbEntityBriefRemovePrefix}"`
|
RemovePrefix string `name:"removePrefix" short:"r" brief:"{CGenPbEntityBriefRemovePrefix}"`
|
||||||
RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenPbEntityBriefRemoveFieldPrefix}"`
|
RemoveFieldPrefix string `name:"removeFieldPrefix" short:"rf" brief:"{CGenPbEntityBriefRemoveFieldPrefix}"`
|
||||||
NameCase string `name:"nameCase" short:"n" brief:"{CGenPbEntityBriefNameCase}" d:"Camel"`
|
NameCase string `name:"nameCase" short:"n" brief:"{CGenPbEntityBriefNameCase}" d:"Camel"`
|
||||||
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"CamelLower"`
|
JsonCase string `name:"jsonCase" short:"j" brief:"{CGenPbEntityBriefJsonCase}" d:"none"`
|
||||||
Option string `name:"option" short:"o" brief:"{CGenPbEntityBriefOption}"`
|
Option string `name:"option" short:"o" brief:"{CGenPbEntityBriefOption}"`
|
||||||
}
|
}
|
||||||
CGenPbEntityOutput struct{}
|
CGenPbEntityOutput struct{}
|
||||||
@ -342,6 +342,7 @@ func generateMessageFieldForPbEntity(index int, field *gdb.TableField, in CGenPb
|
|||||||
comment = gstr.Replace(comment, `\n`, " ")
|
comment = gstr.Replace(comment, `\n`, " ")
|
||||||
comment, _ = gregex.ReplaceString(`\s{2,}`, ` `, comment)
|
comment, _ = gregex.ReplaceString(`\s{2,}`, ` `, comment)
|
||||||
if jsonTagName := formatCase(field.Name, in.JsonCase); jsonTagName != "" {
|
if jsonTagName := formatCase(field.Name, in.JsonCase); jsonTagName != "" {
|
||||||
|
jsonTagStr = fmt.Sprintf(`[json_name = "%s"]`, jsonTagName)
|
||||||
// beautiful indent.
|
// beautiful indent.
|
||||||
if index < 10 {
|
if index < 10 {
|
||||||
// 3 spaces
|
// 3 spaces
|
||||||
@ -378,32 +379,10 @@ func getTplPbEntityContent(tplEntityPath string) string {
|
|||||||
|
|
||||||
// formatCase call gstr.Case* function to convert the s to specified case.
|
// formatCase call gstr.Case* function to convert the s to specified case.
|
||||||
func formatCase(str, caseStr string) string {
|
func formatCase(str, caseStr string) string {
|
||||||
switch gstr.ToLower(caseStr) {
|
if caseStr == "none" {
|
||||||
case gstr.ToLower("Camel"):
|
|
||||||
return gstr.CaseCamel(str)
|
|
||||||
|
|
||||||
case gstr.ToLower("CamelLower"):
|
|
||||||
return gstr.CaseCamelLower(str)
|
|
||||||
|
|
||||||
case gstr.ToLower("Kebab"):
|
|
||||||
return gstr.CaseKebab(str)
|
|
||||||
|
|
||||||
case gstr.ToLower("KebabScreaming"):
|
|
||||||
return gstr.CaseKebabScreaming(str)
|
|
||||||
|
|
||||||
case gstr.ToLower("Snake"):
|
|
||||||
return gstr.CaseSnake(str)
|
|
||||||
|
|
||||||
case gstr.ToLower("SnakeFirstUpper"):
|
|
||||||
return gstr.CaseSnakeFirstUpper(str)
|
|
||||||
|
|
||||||
case gstr.ToLower("SnakeScreaming"):
|
|
||||||
return gstr.CaseSnakeScreaming(str)
|
|
||||||
|
|
||||||
case "none":
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return str
|
return gstr.CaseConvert(str, gstr.CaseTypeMatch(caseStr))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sortFieldKeyForPbEntity(fieldMap map[string]*gdb.TableField) []string {
|
func sortFieldKeyForPbEntity(fieldMap map[string]*gdb.TableField) []string {
|
||||||
|
|||||||
21
cmd/gf/internal/cmd/testdata/genpbentity/generated_user/table_user.proto
vendored
Normal file
21
cmd/gf/internal/cmd/testdata/genpbentity/generated_user/table_user.proto
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// ==========================================================================
|
||||||
|
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||||
|
// ==========================================================================
|
||||||
|
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package unittest;
|
||||||
|
|
||||||
|
option go_package = "unittest";
|
||||||
|
|
||||||
|
import "google/protobuf/timestamp.proto";
|
||||||
|
|
||||||
|
message TableUser {
|
||||||
|
uint32 ID = 1; // User ID
|
||||||
|
string PASSPORT = 2; // User Passport
|
||||||
|
string PASSWORD = 3; // User Password
|
||||||
|
string NICKNAME = 4; // User Nickname
|
||||||
|
string SCORE = 5; // Total score amount.
|
||||||
|
google.protobuf.Timestamp CREATE_AT = 6; // Created Time
|
||||||
|
google.protobuf.Timestamp UPDATE_AT = 7; // Updated Time
|
||||||
|
}
|
||||||
10
cmd/gf/internal/cmd/testdata/genpbentity/user.tpl.sql
vendored
Normal file
10
cmd/gf/internal/cmd/testdata/genpbentity/user.tpl.sql
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
CREATE TABLE `%s` (
|
||||||
|
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'User ID',
|
||||||
|
`passport` varchar(45) NOT NULL COMMENT 'User Passport',
|
||||||
|
`password` varchar(45) NOT NULL COMMENT 'User Password',
|
||||||
|
`nickname` varchar(45) NOT NULL COMMENT 'User Nickname',
|
||||||
|
`score` decimal(10,2) unsigned DEFAULT NULL COMMENT 'Total score amount.',
|
||||||
|
`create_at` datetime DEFAULT NULL COMMENT 'Created Time',
|
||||||
|
`update_at` datetime DEFAULT NULL COMMENT 'Updated Time',
|
||||||
|
PRIMARY KEY (`id`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
@ -4,7 +4,7 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/apolloconfig/agollo/v4 v4.3.1
|
github.com/apolloconfig/agollo/v4 v4.3.1
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/consul/v2
|
|||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/hashicorp/consul/api v1.24.0
|
github.com/hashicorp/consul/api v1.24.0
|
||||||
github.com/hashicorp/go-cleanhttp v0.5.2
|
github.com/hashicorp/go-cleanhttp v0.5.2
|
||||||
)
|
)
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/kubecm/v2
|
|||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
k8s.io/api v0.27.4
|
k8s.io/api v0.27.4
|
||||||
k8s.io/apimachinery v0.27.4
|
k8s.io/apimachinery v0.27.4
|
||||||
k8s.io/client-go v0.27.4
|
k8s.io/client-go v0.27.4
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/nacos/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/nacos-group/nacos-sdk-go v1.1.4
|
github.com/nacos-group/nacos-sdk-go v1.1.4
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/polaris/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/polarismesh/polaris-go v1.5.5
|
github.com/polarismesh/polaris-go v1.5.5
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ClickHouse/clickhouse-go/v2 v2.0.15
|
github.com/ClickHouse/clickhouse-go/v2 v2.0.15
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/google/uuid v1.3.0
|
github.com/google/uuid v1.3.0
|
||||||
github.com/shopspring/decimal v1.3.1
|
github.com/shopspring/decimal v1.3.1
|
||||||
)
|
)
|
||||||
|
|||||||
@ -74,9 +74,16 @@ func (d *Driver) Open(config *gdb.ConfigNode) (db *sql.DB, err error) {
|
|||||||
}
|
}
|
||||||
// Data Source Name of DM8:
|
// Data Source Name of DM8:
|
||||||
// dm://userName:password@ip:port/dbname
|
// dm://userName:password@ip:port/dbname
|
||||||
|
// dm://userName:password@DW/dbname?DW=(192.168.1.1:5236,192.168.1.2:5236)
|
||||||
|
var domain string
|
||||||
|
if config.Port != "" {
|
||||||
|
domain = fmt.Sprintf("%s:%s", config.Host, config.Port)
|
||||||
|
} else {
|
||||||
|
domain = config.Host
|
||||||
|
}
|
||||||
source = fmt.Sprintf(
|
source = fmt.Sprintf(
|
||||||
"dm://%s:%s@%s:%s/%s?charset=%s&schema=%s",
|
"dm://%s:%s@%s/%s?charset=%s&schema=%s",
|
||||||
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset, config.Name,
|
config.User, config.Pass, domain, config.Name, config.Charset, config.Name,
|
||||||
)
|
)
|
||||||
// Demo of timezone setting:
|
// Demo of timezone setting:
|
||||||
// &loc=Asia/Shanghai
|
// &loc=Asia/Shanghai
|
||||||
@ -210,7 +217,11 @@ func (d *Driver) DoFilter(ctx context.Context, link gdb.Link, sql string, args [
|
|||||||
// TODO The current approach is too rough. We should deal with the GROUP_CONCAT function and the parsing of the index field from within the select from match.
|
// TODO The current approach is too rough. We should deal with the GROUP_CONCAT function and the parsing of the index field from within the select from match.
|
||||||
// (GROUP_CONCAT DM does not approve; index cannot be used as a query column name, and security characters need to be added, such as "index")
|
// (GROUP_CONCAT DM does not approve; index cannot be used as a query column name, and security characters need to be added, such as "index")
|
||||||
l, r := d.GetChars()
|
l, r := d.GetChars()
|
||||||
newSql = gstr.ReplaceI(newSql, "INDEX", l+"INDEX"+r)
|
if strings.Contains(newSql, "INDEX") || strings.Contains(newSql, "index") {
|
||||||
|
if !(strings.Contains(newSql, "_INDEX") || strings.Contains(newSql, "_index")) {
|
||||||
|
newSql = gstr.ReplaceI(newSql, "INDEX", l+"INDEX"+r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO i tried to do but it never work:
|
// TODO i tried to do but it never work:
|
||||||
// array, err := gregex.MatchAllString(`SELECT (.*INDEX.*) FROM .*`, newSql)
|
// array, err := gregex.MatchAllString(`SELECT (.*INDEX.*) FROM .*`, newSql)
|
||||||
|
|||||||
@ -21,38 +21,44 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db gdb.DB
|
db gdb.DB
|
||||||
dblink gdb.DB
|
dblink gdb.DB
|
||||||
dbErr gdb.DB
|
dbErr gdb.DB
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
TableSize = 10
|
TableSize = 10
|
||||||
|
|
||||||
// TableName = "inf_group"
|
|
||||||
// TableNamePrefix = "t_"
|
|
||||||
// TestSchema = "SYSDBADP"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
TestDbIP = "127.0.0.1"
|
TestDBHost = "127.0.0.1"
|
||||||
TestDbPort = "5236"
|
TestDBPort = "5236"
|
||||||
TestDbUser = "SYSDBA"
|
TestDBUser = "SYSDBA"
|
||||||
TestDbPass = "SYSDBA001"
|
TestDBPass = "SYSDBA001"
|
||||||
TestDbName = "SYSDBA"
|
TestDBName = "SYSDBA"
|
||||||
TestDbType = "dm"
|
TestDBType = "dm"
|
||||||
TestCharset = "utf8"
|
TestCharset = "utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID int64 `orm:"id"`
|
||||||
|
AccountName string `orm:"account_name"`
|
||||||
|
PwdReset int64 `orm:"pwd_reset"`
|
||||||
|
AttrIndex int64 `orm:"attr_index"`
|
||||||
|
Enabled int64 `orm:"enabled"`
|
||||||
|
Deleted int64 `orm:"deleted"`
|
||||||
|
CreatedBy string `orm:"created_by"`
|
||||||
|
CreatedTime time.Time `orm:"created_time"`
|
||||||
|
UpdatedBy string `orm:"updated_by"`
|
||||||
|
UpdatedTime time.Time `orm:"updated_time"`
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
node := gdb.ConfigNode{
|
node := gdb.ConfigNode{
|
||||||
Host: TestDbIP,
|
Host: TestDBHost,
|
||||||
Port: TestDbPort,
|
Port: TestDBPort,
|
||||||
User: TestDbUser,
|
User: TestDBUser,
|
||||||
Pass: TestDbPass,
|
Pass: TestDBPass,
|
||||||
Name: TestDbName,
|
Name: TestDBName,
|
||||||
Type: TestDbType,
|
Type: TestDBType,
|
||||||
Role: "master",
|
Role: "master",
|
||||||
Charset: TestCharset,
|
Charset: TestCharset,
|
||||||
Weight: 1,
|
Weight: 1,
|
||||||
@ -62,22 +68,23 @@ func init() {
|
|||||||
UpdatedAt: "updated_time",
|
UpdatedAt: "updated_time",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo
|
||||||
nodeLink := gdb.ConfigNode{
|
nodeLink := gdb.ConfigNode{
|
||||||
Type: TestDbType,
|
Type: TestDBType,
|
||||||
Name: TestDbName,
|
Name: TestDBName,
|
||||||
Link: fmt.Sprintf(
|
Link: fmt.Sprintf(
|
||||||
"dm:%s:%s@tcp(%s:%s)/%s?charset=%s",
|
"dm:%s:%s@tcp(%s:%s)/%s?charset=%s",
|
||||||
TestDbUser, TestDbPass, TestDbIP, TestDbPort, TestDbName, TestCharset,
|
TestDBUser, TestDBPass, TestDBHost, TestDBPort, TestDBName, TestCharset,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeErr := gdb.ConfigNode{
|
nodeErr := gdb.ConfigNode{
|
||||||
Host: TestDbIP,
|
Host: TestDBHost,
|
||||||
Port: TestDbPort,
|
Port: TestDBPort,
|
||||||
User: TestDbUser,
|
User: TestDBUser,
|
||||||
Pass: "1234",
|
Pass: "1234",
|
||||||
Name: TestDbName,
|
Name: TestDBName,
|
||||||
Type: TestDbType,
|
Type: TestDBType,
|
||||||
Role: "master",
|
Role: "master",
|
||||||
Charset: TestCharset,
|
Charset: TestCharset,
|
||||||
Weight: 1,
|
Weight: 1,
|
||||||
@ -107,6 +114,23 @@ func init() {
|
|||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dropTable(table string) {
|
||||||
|
count, err := db.GetCount(
|
||||||
|
ctx,
|
||||||
|
"SELECT COUNT(*) FROM all_tables WHERE owner = ? And table_name= ?", TestDBName, strings.ToUpper(table),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
gtest.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if count == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
|
||||||
|
gtest.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createTable(table ...string) (name string) {
|
func createTable(table ...string) (name string) {
|
||||||
if len(table) > 0 {
|
if len(table) > 0 {
|
||||||
name = table[0]
|
name = table[0]
|
||||||
@ -124,6 +148,7 @@ func createTable(table ...string) (name string) {
|
|||||||
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
|
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
|
||||||
"ENABLED" INT DEFAULT 1 NOT NULL,
|
"ENABLED" INT DEFAULT 1 NOT NULL,
|
||||||
"DELETED" INT DEFAULT 0 NOT NULL,
|
"DELETED" INT DEFAULT 0 NOT NULL,
|
||||||
|
"ATTR_INDEX" INT DEFAULT 0 ,
|
||||||
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
||||||
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
||||||
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
||||||
@ -136,18 +161,6 @@ NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
|
||||||
ID int64 `orm:"id"`
|
|
||||||
AccountName string `orm:"account_name"`
|
|
||||||
PwdReset int64 `orm:"pwd_reset"`
|
|
||||||
Enabled int64 `orm:"enabled"`
|
|
||||||
Deleted int64 `orm:"deleted"`
|
|
||||||
CreatedBy string `orm:"created_by"`
|
|
||||||
CreatedTime time.Time `orm:"created_time"`
|
|
||||||
UpdatedBy string `orm:"updated_by"`
|
|
||||||
UpdatedTime time.Time `orm:"updated_time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func createInitTable(table ...string) (name string) {
|
func createInitTable(table ...string) (name string) {
|
||||||
name = createTable(table...)
|
name = createTable(table...)
|
||||||
array := garray.New(true)
|
array := garray.New(true)
|
||||||
@ -156,10 +169,11 @@ func createInitTable(table ...string) (name string) {
|
|||||||
"id": i,
|
"id": i,
|
||||||
"account_name": fmt.Sprintf(`name_%d`, i),
|
"account_name": fmt.Sprintf(`name_%d`, i),
|
||||||
"pwd_reset": 0,
|
"pwd_reset": 0,
|
||||||
|
"attr_index": i,
|
||||||
"create_time": gtime.Now().String(),
|
"create_time": gtime.Now().String(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
result, err := db.Schema(TestDbName).Insert(context.Background(), name, array.Slice())
|
result, err := db.Schema(TestDBName).Insert(context.Background(), name, array.Slice())
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
|
|
||||||
n, e := result.RowsAffected()
|
n, e := result.RowsAffected()
|
||||||
@ -168,19 +182,34 @@ func createInitTable(table ...string) (name string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func dropTable(table string) {
|
func createTableFalse(table ...string) (name string, err error) {
|
||||||
count, err := db.GetCount(
|
if len(table) > 0 {
|
||||||
ctx,
|
name = table[0]
|
||||||
"SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = ?", strings.ToUpper(table),
|
} else {
|
||||||
)
|
name = fmt.Sprintf("random_%d", gtime.Timestamp())
|
||||||
if err != nil {
|
|
||||||
gtest.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if count == 0 {
|
dropTable(name)
|
||||||
return
|
|
||||||
}
|
if _, err := db.Exec(ctx, fmt.Sprintf(`
|
||||||
if _, err := db.Exec(ctx, fmt.Sprintf("DROP TABLE %s", table)); err != nil {
|
CREATE TABLE "%s"
|
||||||
gtest.Fatal(err)
|
(
|
||||||
|
"ID" BIGINT NOT NULL,
|
||||||
|
"ACCOUNT_NAME" VARCHAR(128) DEFAULT '' NOT NULL,
|
||||||
|
"PWD_RESET" TINYINT DEFAULT 0 NOT NULL,
|
||||||
|
"ENABLED" INT DEFAULT 1 NOT NULL,
|
||||||
|
"DELETED" INT DEFAULT 0 NOT NULL,
|
||||||
|
"INDEX" INT DEFAULT 0 ,
|
||||||
|
"ATTR_INDEX" INT DEFAULT 0 ,
|
||||||
|
"CREATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
||||||
|
"CREATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
||||||
|
"UPDATED_BY" VARCHAR(32) DEFAULT '' NOT NULL,
|
||||||
|
"UPDATED_TIME" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP() NOT NULL,
|
||||||
|
NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
|
||||||
|
`, name)); err != nil {
|
||||||
|
// gtest.Fatal(err)
|
||||||
|
return name, fmt.Errorf("createTableFalse")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,13 +27,11 @@ func Test_DB_Ping(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTables(t *testing.T) {
|
func TestTables(t *testing.T) {
|
||||||
|
tables := []string{"A_tables", "A_tables2"}
|
||||||
|
for _, v := range tables {
|
||||||
|
createInitTable(v)
|
||||||
|
}
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
tables := []string{"A_tables", "A_tables2"}
|
|
||||||
|
|
||||||
for _, v := range tables {
|
|
||||||
createInitTable(v)
|
|
||||||
// createTable(v)
|
|
||||||
}
|
|
||||||
result, err := db.Tables(ctx)
|
result, err := db.Tables(ctx)
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
|
|
||||||
@ -63,13 +61,31 @@ func TestTables(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTableFields(t *testing.T) {
|
// The test scenario index of this test case (exact matching field) is a keyword in the Dameng database and cannot exist as a field name.
|
||||||
|
// If the data structure previously migrated from mysql has an index (completely matching field), it will also be allowed.
|
||||||
|
// However, when processing the index (completely matching field), the adapter will automatically add security character
|
||||||
|
// In principle, such problems will not occur if you directly use Dameng database initialization instead of migrating the data structure from mysql.
|
||||||
|
// If so, the adapter has also taken care of it.
|
||||||
|
func TestTablesFalse(t *testing.T) {
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
tables := []string{"A_tables", "A_tables2"}
|
||||||
|
for _, v := range tables {
|
||||||
|
_, err := createTableFalse(v)
|
||||||
|
gtest.Assert(err, fmt.Errorf("createTableFalse"))
|
||||||
|
// createTable(v)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTableFields(t *testing.T) {
|
||||||
|
tables := "A_tables"
|
||||||
|
createInitTable(tables)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
tables := "A_tables"
|
|
||||||
var expect = map[string][]interface{}{
|
var expect = map[string][]interface{}{
|
||||||
"ID": {"BIGINT", false},
|
"ID": {"BIGINT", false},
|
||||||
"ACCOUNT_NAME": {"VARCHAR", false},
|
"ACCOUNT_NAME": {"VARCHAR", false},
|
||||||
"PWD_RESET": {"TINYINT", false},
|
"PWD_RESET": {"TINYINT", false},
|
||||||
|
"ATTR_INDEX": {"INT", true},
|
||||||
"DELETED": {"INT", false},
|
"DELETED": {"INT", false},
|
||||||
"CREATED_TIME": {"TIMESTAMP", false},
|
"CREATED_TIME": {"TIMESTAMP", false},
|
||||||
}
|
}
|
||||||
@ -98,8 +114,9 @@ func TestTableFields(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_Query(t *testing.T) {
|
func Test_DB_Query(t *testing.T) {
|
||||||
|
tableName := "A_tables"
|
||||||
|
createInitTable(tableName)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
tableName := "A_tables"
|
|
||||||
// createTable(tableName)
|
// createTable(tableName)
|
||||||
_, err := db.Query(ctx, fmt.Sprintf("SELECT * from %s", tableName))
|
_, err := db.Query(ctx, fmt.Sprintf("SELECT * from %s", tableName))
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
@ -121,16 +138,18 @@ func Test_DB_Query(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestModelSave(t *testing.T) {
|
func TestModelSave(t *testing.T) {
|
||||||
|
table := "A_tables"
|
||||||
|
createInitTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
// createTable("A_tables")
|
|
||||||
data := []User{
|
data := []User{
|
||||||
{
|
{
|
||||||
ID: 100,
|
ID: 100,
|
||||||
AccountName: "user_100",
|
AccountName: "user_100",
|
||||||
|
AttrIndex: 100,
|
||||||
CreatedTime: time.Now(),
|
CreatedTime: time.Now(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err := db.Model("A_tables").Data(data).Save()
|
_, err := db.Model(table).Data(data).Save()
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
|
|
||||||
data2 := []User{
|
data2 := []User{
|
||||||
@ -139,7 +158,7 @@ func TestModelSave(t *testing.T) {
|
|||||||
AccountName: "user_101",
|
AccountName: "user_101",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err = db.Model("A_tables").Data(&data2).Save()
|
_, err = db.Model(table).Data(&data2).Save()
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
|
|
||||||
data3 := []User{
|
data3 := []User{
|
||||||
@ -149,7 +168,7 @@ func TestModelSave(t *testing.T) {
|
|||||||
PwdReset: 10,
|
PwdReset: 10,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err = db.Model("A_tables").Save(data3)
|
_, err = db.Model(table).Save(data3)
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
|
|
||||||
data4 := []User{
|
data4 := []User{
|
||||||
@ -159,63 +178,68 @@ func TestModelSave(t *testing.T) {
|
|||||||
CreatedTime: time.Now(),
|
CreatedTime: time.Now(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err = db.Model("A_tables").Save(&data4)
|
_, err = db.Model(table).Save(&data4)
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
|
|
||||||
// TODO:: Should be Supported 'Replace' Operation
|
// TODO:: Should be Supported 'Replace' Operation
|
||||||
// _, err = db.Schema(TestDbName).Replace(ctx, "DoInsert", data, 10)
|
// _, err = db.Schema(TestDBName).Replace(ctx, "DoInsert", data, 10)
|
||||||
// gtest.Assert(err, nil)
|
// gtest.Assert(err, nil)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestModelInsert(t *testing.T) {
|
func TestModelInsert(t *testing.T) {
|
||||||
// g.Model.insert not lost default not null coloumn
|
// g.Model.insert not lost default not null coloumn
|
||||||
|
table := "A_tables"
|
||||||
|
createInitTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
// createTable("A_tables")
|
|
||||||
i := 200
|
i := 200
|
||||||
data := User{
|
data := User{
|
||||||
ID: int64(i),
|
ID: int64(i),
|
||||||
AccountName: fmt.Sprintf(`A%dtwo`, i),
|
AccountName: fmt.Sprintf(`A%dtwo`, i),
|
||||||
PwdReset: 0,
|
PwdReset: 0,
|
||||||
|
AttrIndex: 99,
|
||||||
// CreatedTime: time.Now(),
|
// CreatedTime: time.Now(),
|
||||||
UpdatedTime: time.Now(),
|
UpdatedTime: time.Now(),
|
||||||
}
|
}
|
||||||
// _, err := db.Schema(TestDbName).Model("A_tables").Data(data).Insert()
|
// _, err := db.Schema(TestDBName).Model(table).Data(data).Insert()
|
||||||
_, err := db.Model("A_tables").Insert(&data)
|
_, err := db.Model(table).Insert(&data)
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
})
|
})
|
||||||
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
// createTable("A_tables")
|
|
||||||
i := 201
|
i := 201
|
||||||
data := User{
|
data := User{
|
||||||
ID: int64(i),
|
ID: int64(i),
|
||||||
AccountName: fmt.Sprintf(`A%dtwoONE`, i),
|
AccountName: fmt.Sprintf(`A%dtwoONE`, i),
|
||||||
PwdReset: 1,
|
PwdReset: 1,
|
||||||
CreatedTime: time.Now(),
|
CreatedTime: time.Now(),
|
||||||
|
AttrIndex: 98,
|
||||||
// UpdatedTime: time.Now(),
|
// UpdatedTime: time.Now(),
|
||||||
}
|
}
|
||||||
// _, err := db.Schema(TestDbName).Model("A_tables").Data(data).Insert()
|
// _, err := db.Schema(TestDBName).Model(table).Data(data).Insert()
|
||||||
_, err := db.Model("A_tables").Data(&data).Insert()
|
_, err := db.Model(table).Data(&data).Insert()
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDBInsert(t *testing.T) {
|
func TestDBInsert(t *testing.T) {
|
||||||
|
table := "A_tables"
|
||||||
|
createInitTable("A_tables")
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
// createTable("A_tables")
|
|
||||||
i := 300
|
i := 300
|
||||||
data := g.Map{
|
data := g.Map{
|
||||||
"ID": i,
|
"ID": i,
|
||||||
"ACCOUNT_NAME": fmt.Sprintf(`A%dthress`, i),
|
"ACCOUNT_NAME": fmt.Sprintf(`A%dthress`, i),
|
||||||
"PWD_RESET": 3,
|
"PWD_RESET": 3,
|
||||||
|
"ATTR_INDEX": 98,
|
||||||
}
|
}
|
||||||
_, err := db.Insert(ctx, "A_tables", &data)
|
_, err := db.Insert(ctx, table, &data)
|
||||||
gtest.Assert(err, nil)
|
gtest.Assert(err, nil)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_Exec(t *testing.T) {
|
func Test_DB_Exec(t *testing.T) {
|
||||||
|
createInitTable("A_tables")
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
_, err := db.Exec(ctx, "SELECT ? from dual", 1)
|
_, err := db.Exec(ctx, "SELECT ? from dual", 1)
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
@ -226,18 +250,18 @@ func Test_DB_Exec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_Insert(t *testing.T) {
|
func Test_DB_Insert(t *testing.T) {
|
||||||
// table := createTable()
|
table := "A_tables"
|
||||||
// defer dropTable(table)
|
createInitTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
// normal map
|
// normal map
|
||||||
_, err := db.Insert(ctx, "A_tables", g.Map{
|
_, err := db.Insert(ctx, table, g.Map{
|
||||||
"ID": 1000,
|
"ID": 1000,
|
||||||
"ACCOUNT_NAME": "map1",
|
"ACCOUNT_NAME": "map1",
|
||||||
"CREATED_TIME": gtime.Now(),
|
"CREATED_TIME": gtime.Now(),
|
||||||
})
|
})
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
|
|
||||||
result, err := db.Insert(ctx, "A_tables", g.Map{
|
result, err := db.Insert(ctx, table, g.Map{
|
||||||
"ID": "2000",
|
"ID": "2000",
|
||||||
"ACCOUNT_NAME": "map2",
|
"ACCOUNT_NAME": "map2",
|
||||||
"CREATED_TIME": gtime.Now(),
|
"CREATED_TIME": gtime.Now(),
|
||||||
@ -246,7 +270,7 @@ func Test_DB_Insert(t *testing.T) {
|
|||||||
n, _ := result.RowsAffected()
|
n, _ := result.RowsAffected()
|
||||||
t.Assert(n, 1)
|
t.Assert(n, 1)
|
||||||
|
|
||||||
result, err = db.Insert(ctx, "A_tables", g.Map{
|
result, err = db.Insert(ctx, table, g.Map{
|
||||||
"ID": 3000,
|
"ID": 3000,
|
||||||
"ACCOUNT_NAME": "map3",
|
"ACCOUNT_NAME": "map3",
|
||||||
// "CREATED_TIME": gtime.Now(),
|
// "CREATED_TIME": gtime.Now(),
|
||||||
@ -256,7 +280,7 @@ func Test_DB_Insert(t *testing.T) {
|
|||||||
t.Assert(n, 1)
|
t.Assert(n, 1)
|
||||||
|
|
||||||
// struct
|
// struct
|
||||||
result, err = db.Insert(ctx, "A_tables", User{
|
result, err = db.Insert(ctx, table, User{
|
||||||
ID: 4000,
|
ID: 4000,
|
||||||
AccountName: "struct_4",
|
AccountName: "struct_4",
|
||||||
// CreatedTime: timeStr,
|
// CreatedTime: timeStr,
|
||||||
@ -266,7 +290,7 @@ func Test_DB_Insert(t *testing.T) {
|
|||||||
n, _ = result.RowsAffected()
|
n, _ = result.RowsAffected()
|
||||||
t.Assert(n, 1)
|
t.Assert(n, 1)
|
||||||
|
|
||||||
ones, err := db.Model("A_tables").Where("ID", 4000).All()
|
ones, err := db.Model(table).Where("ID", 4000).All()
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(ones[0]["ID"].Int(), 4000)
|
t.Assert(ones[0]["ID"].Int(), 4000)
|
||||||
t.Assert(ones[0]["ACCOUNT_NAME"].String(), "struct_4")
|
t.Assert(ones[0]["ACCOUNT_NAME"].String(), "struct_4")
|
||||||
@ -276,7 +300,7 @@ func Test_DB_Insert(t *testing.T) {
|
|||||||
|
|
||||||
// *struct
|
// *struct
|
||||||
timeStr := time.Now()
|
timeStr := time.Now()
|
||||||
result, err = db.Insert(ctx, "A_tables", &User{
|
result, err = db.Insert(ctx, table, &User{
|
||||||
ID: 5000,
|
ID: 5000,
|
||||||
AccountName: "struct_5",
|
AccountName: "struct_5",
|
||||||
CreatedTime: timeStr,
|
CreatedTime: timeStr,
|
||||||
@ -286,13 +310,13 @@ func Test_DB_Insert(t *testing.T) {
|
|||||||
n, _ = result.RowsAffected()
|
n, _ = result.RowsAffected()
|
||||||
t.Assert(n, 1)
|
t.Assert(n, 1)
|
||||||
|
|
||||||
one, err := db.Model("A_tables").Where("ID", 5000).One()
|
one, err := db.Model(table).Where("ID", 5000).One()
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(one["ID"].Int(), 5000)
|
t.Assert(one["ID"].Int(), 5000)
|
||||||
t.Assert(one["ACCOUNT_NAME"].String(), "struct_5")
|
t.Assert(one["ACCOUNT_NAME"].String(), "struct_5")
|
||||||
|
|
||||||
// batch with Insert
|
// batch with Insert
|
||||||
r, err := db.Insert(ctx, "A_tables", g.Slice{
|
r, err := db.Insert(ctx, table, g.Slice{
|
||||||
g.Map{
|
g.Map{
|
||||||
"ID": 6000,
|
"ID": 6000,
|
||||||
"ACCOUNT_NAME": "t6000",
|
"ACCOUNT_NAME": "t6000",
|
||||||
@ -306,7 +330,7 @@ func Test_DB_Insert(t *testing.T) {
|
|||||||
n, _ = r.RowsAffected()
|
n, _ = r.RowsAffected()
|
||||||
t.Assert(n, 2)
|
t.Assert(n, 2)
|
||||||
|
|
||||||
one, err = db.Model("A_tables").Where("ID", 6000).One()
|
one, err = db.Model(table).Where("ID", 6000).One()
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(one["ID"].Int(), 6000)
|
t.Assert(one["ID"].Int(), 6000)
|
||||||
t.Assert(one["ACCOUNT_NAME"].String(), "t6000")
|
t.Assert(one["ACCOUNT_NAME"].String(), "t6000")
|
||||||
@ -314,8 +338,9 @@ func Test_DB_Insert(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_BatchInsert(t *testing.T) {
|
func Test_DB_BatchInsert(t *testing.T) {
|
||||||
|
table := "A_tables"
|
||||||
|
createInitTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
table := "A_tables"
|
|
||||||
r, err := db.Insert(ctx, table, g.List{
|
r, err := db.Insert(ctx, table, g.List{
|
||||||
{
|
{
|
||||||
"ID": 400,
|
"ID": 400,
|
||||||
@ -334,9 +359,6 @@ func Test_DB_BatchInsert(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
table := "A_tables"
|
|
||||||
// table := createTable()
|
|
||||||
// defer dropTable(table)
|
|
||||||
// []interface{}
|
// []interface{}
|
||||||
r, err := db.Insert(ctx, table, g.Slice{
|
r, err := db.Insert(ctx, table, g.Slice{
|
||||||
g.Map{
|
g.Map{
|
||||||
@ -357,9 +379,6 @@ func Test_DB_BatchInsert(t *testing.T) {
|
|||||||
|
|
||||||
// batch insert map
|
// batch insert map
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
table := "A_tables"
|
|
||||||
// table := createTable()
|
|
||||||
// defer dropTable(table)
|
|
||||||
result, err := db.Insert(ctx, table, g.Map{
|
result, err := db.Insert(ctx, table, g.Map{
|
||||||
"ID": 600,
|
"ID": 600,
|
||||||
"ACCOUNT_NAME": "600_batch_600",
|
"ACCOUNT_NAME": "600_batch_600",
|
||||||
@ -373,10 +392,9 @@ func Test_DB_BatchInsert(t *testing.T) {
|
|||||||
|
|
||||||
func Test_DB_BatchInsert_Struct(t *testing.T) {
|
func Test_DB_BatchInsert_Struct(t *testing.T) {
|
||||||
// batch insert struct
|
// batch insert struct
|
||||||
|
table := "A_tables"
|
||||||
|
createInitTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
table := "A_tables"
|
|
||||||
// table := createTable()
|
|
||||||
// defer dropTable(table)
|
|
||||||
user := &User{
|
user := &User{
|
||||||
ID: 700,
|
ID: 700,
|
||||||
AccountName: "BatchInsert_Struct_700",
|
AccountName: "BatchInsert_Struct_700",
|
||||||
@ -391,26 +409,25 @@ func Test_DB_BatchInsert_Struct(t *testing.T) {
|
|||||||
|
|
||||||
func Test_DB_Update(t *testing.T) {
|
func Test_DB_Update(t *testing.T) {
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
// table := createInitTable()
|
createInitTable(table)
|
||||||
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
result, err := db.Update(ctx, table, "pwd_reset=7", "id=700")
|
result, err := db.Update(ctx, table, "pwd_reset=7", "id=7")
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
n, _ := result.RowsAffected()
|
n, _ := result.RowsAffected()
|
||||||
t.Assert(n, 1)
|
t.Assert(n, 1)
|
||||||
|
|
||||||
one, err := db.Model(table).Where("ID", 700).One()
|
one, err := db.Model(table).Where("ID", 7).One()
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(one["ID"].Int(), 700)
|
t.Assert(one["ID"].Int(), 7)
|
||||||
t.Assert(one["ACCOUNT_NAME"].String(), "BatchInsert_Struct_700")
|
t.Assert(one["ACCOUNT_NAME"].String(), "name_7")
|
||||||
t.Assert(one["PWD_RESET"].String(), "7")
|
t.Assert(one["PWD_RESET"].String(), "7")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_GetAll(t *testing.T) {
|
func Test_DB_GetAll(t *testing.T) {
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
// table := createInitTable()
|
createInitTable(table)
|
||||||
// defer dropTable(table)
|
|
||||||
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
result, err := db.GetAll(ctx, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
|
||||||
@ -459,41 +476,38 @@ func Test_DB_GetAll(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_GetOne(t *testing.T) {
|
func Test_DB_GetOne(t *testing.T) {
|
||||||
// table := createInitTable()
|
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
|
createInitTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE account_name=?", table), "struct_4")
|
record, err := db.GetOne(ctx, fmt.Sprintf("SELECT * FROM %s WHERE account_name=?", table), "name_4")
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(record["ACCOUNT_NAME"].String(), "struct_4")
|
t.Assert(record["ACCOUNT_NAME"].String(), "name_4")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_GetValue(t *testing.T) {
|
func Test_DB_GetValue(t *testing.T) {
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
// table := createInitTable()
|
createInitTable(table)
|
||||||
// defer dropTable(table)
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE account_name=?", table), "map2")
|
value, err := db.GetValue(ctx, fmt.Sprintf("SELECT id FROM %s WHERE account_name=?", table), "name_2")
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(value.Int(), 2000)
|
t.Assert(value.Int(), 2)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_GetCount(t *testing.T) {
|
func Test_DB_GetCount(t *testing.T) {
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
// table := createInitTable()
|
createInitTable(table)
|
||||||
// defer dropTable(table)
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
count, err := db.GetCount(ctx, fmt.Sprintf("SELECT * FROM %s", table))
|
count, err := db.GetCount(ctx, fmt.Sprintf("SELECT * FROM %s", table))
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(count, 28)
|
t.Assert(count, 10)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_GetStruct(t *testing.T) {
|
func Test_DB_GetStruct(t *testing.T) {
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
// table := createInitTable()
|
createInitTable(table)
|
||||||
// defer dropTable(table)
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
user := new(User)
|
user := new(User)
|
||||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||||
@ -502,33 +516,31 @@ func Test_DB_GetStruct(t *testing.T) {
|
|||||||
})
|
})
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
user := new(User)
|
user := new(User)
|
||||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 200)
|
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 2)
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(user.AccountName, "A200two")
|
t.Assert(user.AccountName, "name_2")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_GetStructs(t *testing.T) {
|
func Test_DB_GetStructs(t *testing.T) {
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
// table := createInitTable()
|
createInitTable(table)
|
||||||
// defer dropTable(table)
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
var users []User
|
var users []User
|
||||||
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 4000)
|
err := db.GetScan(ctx, &users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 4)
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
t.Assert(users[0].ID, 5000)
|
t.Assert(users[0].ID, 5)
|
||||||
t.Assert(users[1].ID, 6000)
|
t.Assert(users[1].ID, 6)
|
||||||
t.Assert(users[2].ID, 6001)
|
t.Assert(users[2].ID, 7)
|
||||||
t.Assert(users[0].AccountName, "struct_5")
|
t.Assert(users[0].AccountName, "name_5")
|
||||||
t.Assert(users[1].AccountName, "t6000")
|
t.Assert(users[1].AccountName, "name_6")
|
||||||
t.Assert(users[2].AccountName, "t6001")
|
t.Assert(users[2].AccountName, "name_7")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_GetScan(t *testing.T) {
|
func Test_DB_GetScan(t *testing.T) {
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
// table := createInitTable()
|
createInitTable(table)
|
||||||
// defer dropTable(table)
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
user := new(User)
|
user := new(User)
|
||||||
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
err := db.GetScan(ctx, user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
|
||||||
@ -555,17 +567,17 @@ func Test_DB_GetScan(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_DB_Delete(t *testing.T) {
|
func Test_DB_Delete(t *testing.T) {
|
||||||
// table := createInitTable()
|
table := "A_tables"
|
||||||
// defer dropTable(table)
|
createInitTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
result, err := db.Delete(ctx, "A_tables", "id=32")
|
result, err := db.Delete(ctx, table, "id=32")
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
n, _ := result.RowsAffected()
|
n, _ := result.RowsAffected()
|
||||||
t.Assert(n, 0)
|
t.Assert(n, 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
result, err := db.Model("A_tables").Where("id", 33).Delete()
|
result, err := db.Model(table).Where("id", 33).Delete()
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
n, _ := result.RowsAffected()
|
n, _ := result.RowsAffected()
|
||||||
t.Assert(n, 0)
|
t.Assert(n, 0)
|
||||||
@ -574,8 +586,7 @@ func Test_DB_Delete(t *testing.T) {
|
|||||||
|
|
||||||
func Test_Empty_Slice_Argument(t *testing.T) {
|
func Test_Empty_Slice_Argument(t *testing.T) {
|
||||||
table := "A_tables"
|
table := "A_tables"
|
||||||
// table := createInitTable()
|
createInitTable(table)
|
||||||
// defer dropTable(table)
|
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
result, err := db.GetAll(ctx, fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
|
result, err := db.GetAll(ctx, fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
|
|||||||
@ -6,7 +6,7 @@ replace github.com/gogf/gf/v2 => ../../../
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
gitee.com/chunanyong/dm v1.8.12
|
gitee.com/chunanyong/dm v1.8.12
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@ -4,7 +4,7 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/denisenkom/go-mssqldb v0.12.3
|
github.com/denisenkom/go-mssqldb v0.12.3
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@ -4,7 +4,7 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-sql-driver/mysql v1.7.1
|
github.com/go-sql-driver/mysql v1.7.1
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@ -55,7 +55,6 @@ func init() {
|
|||||||
nodeInvalid := gdb.ConfigNode{
|
nodeInvalid := gdb.ConfigNode{
|
||||||
Link: fmt.Sprintf("mysql:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
|
Link: fmt.Sprintf("mysql:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
|
||||||
}
|
}
|
||||||
|
|
||||||
gdb.AddConfigNode("test", nodeDefault)
|
gdb.AddConfigNode("test", nodeDefault)
|
||||||
gdb.AddConfigNode("prefix", nodePrefix)
|
gdb.AddConfigNode("prefix", nodePrefix)
|
||||||
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
|
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
package mysql_test
|
package mysql_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -886,7 +887,8 @@ func Test_Issue3086(t *testing.T) {
|
|||||||
func Test_Issue3204(t *testing.T) {
|
func Test_Issue3204(t *testing.T) {
|
||||||
table := createInitTable()
|
table := createInitTable()
|
||||||
defer dropTable(table)
|
defer dropTable(table)
|
||||||
db.SetDebug(true)
|
|
||||||
|
// where
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
type User struct {
|
type User struct {
|
||||||
g.Meta `orm:"do:true"`
|
g.Meta `orm:"do:true"`
|
||||||
@ -905,4 +907,95 @@ func Test_Issue3204(t *testing.T) {
|
|||||||
t.Assert(len(all), 1)
|
t.Assert(len(all), 1)
|
||||||
t.Assert(all[0]["id"], 2)
|
t.Assert(all[0]["id"], 2)
|
||||||
})
|
})
|
||||||
|
// data
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
type User struct {
|
||||||
|
g.Meta `orm:"do:true"`
|
||||||
|
Id interface{} `orm:"id,omitempty"`
|
||||||
|
Passport interface{} `orm:"passport,omitempty"`
|
||||||
|
Password interface{} `orm:"password,omitempty"`
|
||||||
|
Nickname interface{} `orm:"nickname,omitempty"`
|
||||||
|
CreateTime interface{} `orm:"create_time,omitempty"`
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
sqlArray []string
|
||||||
|
insertId int64
|
||||||
|
data = User{
|
||||||
|
Id: 20,
|
||||||
|
Passport: "passport_20",
|
||||||
|
Password: "",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
sqlArray, err = gdb.CatchSQL(ctx, func(ctx context.Context) error {
|
||||||
|
insertId, err = db.Ctx(ctx).Model(table).Data(data).InsertAndGetId()
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
t.AssertNil(err)
|
||||||
|
t.Assert(insertId, 20)
|
||||||
|
t.Assert(
|
||||||
|
gstr.Contains(sqlArray[len(sqlArray)-1], "(`id`,`passport`) VALUES(20,'passport_20')"),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
// update data
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
type User struct {
|
||||||
|
g.Meta `orm:"do:true"`
|
||||||
|
Id interface{} `orm:"id,omitempty"`
|
||||||
|
Passport interface{} `orm:"passport,omitempty"`
|
||||||
|
Password interface{} `orm:"password,omitempty"`
|
||||||
|
Nickname interface{} `orm:"nickname,omitempty"`
|
||||||
|
CreateTime interface{} `orm:"create_time,omitempty"`
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
sqlArray []string
|
||||||
|
data = User{
|
||||||
|
Passport: "passport_1",
|
||||||
|
Password: "",
|
||||||
|
Nickname: "",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
sqlArray, err = gdb.CatchSQL(ctx, func(ctx context.Context) error {
|
||||||
|
_, err = db.Ctx(ctx).Model(table).Data(data).WherePri(1).Update()
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
t.AssertNil(err)
|
||||||
|
t.Assert(
|
||||||
|
gstr.Contains(sqlArray[len(sqlArray)-1], "SET `passport`='passport_1' WHERE `id`=1"),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://github.com/gogf/gf/issues/3218
|
||||||
|
func Test_Issue3218(t *testing.T) {
|
||||||
|
table := "issue3218_sys_config"
|
||||||
|
array := gstr.SplitAndTrim(gtest.DataContent(`issue3218.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) {
|
||||||
|
type SysConfigInfo struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Value map[string]string `json:"value"`
|
||||||
|
}
|
||||||
|
var configData *SysConfigInfo
|
||||||
|
err := db.Model(table).Scan(&configData)
|
||||||
|
t.AssertNil(err)
|
||||||
|
t.Assert(configData, &SysConfigInfo{
|
||||||
|
Name: "site",
|
||||||
|
Value: map[string]string{
|
||||||
|
"fixed_page": "",
|
||||||
|
"site_name": "22",
|
||||||
|
"version": "22",
|
||||||
|
"banned_ip": "22",
|
||||||
|
"filings": "2222",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
14
contrib/drivers/mysql/testdata/issue3218.sql
vendored
Normal file
14
contrib/drivers/mysql/testdata/issue3218.sql
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
CREATE TABLE `issue3218_sys_config` (
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '配置名称',
|
||||||
|
`value` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '配置值',
|
||||||
|
`created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
|
||||||
|
`updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
|
||||||
|
PRIMARY KEY (`id`) USING BTREE,
|
||||||
|
UNIQUE INDEX `name`(`name`(191)) USING BTREE
|
||||||
|
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
|
||||||
|
|
||||||
|
-- ----------------------------
|
||||||
|
-- Records of sys_config
|
||||||
|
-- ----------------------------
|
||||||
|
INSERT INTO `issue3218_sys_config` VALUES (49, 'site', '{\"banned_ip\":\"22\",\"filings\":\"2222\",\"fixed_page\":\"\",\"site_name\":\"22\",\"version\":\"22\"}', '2023-12-19 14:08:25', '2023-12-19 14:08:25');
|
||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/oracle/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/sijms/go-ora/v2 v2.7.10
|
github.com/sijms/go-ora/v2 v2.7.10
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/pgsql/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/lib/pq v1.10.9
|
github.com/lib/pq v1.10.9
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/glebarez/go-sqlite v1.21.2
|
github.com/glebarez/go-sqlite v1.21.2
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/sqlitecgo/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/mattn/go-sqlite3 v1.14.17
|
github.com/mattn/go-sqlite3 v1.14.17
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/nosql/redis/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/redis/go-redis/v9 v9.2.1
|
github.com/redis/go-redis/v9 v9.2.1
|
||||||
go.opentelemetry.io/otel v1.14.0
|
go.opentelemetry.io/otel v1.14.0
|
||||||
go.opentelemetry.io/otel/trace v1.14.0
|
go.opentelemetry.io/otel/trace v1.14.0
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/etcd/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
go.etcd.io/etcd/client/v3 v3.5.7
|
go.etcd.io/etcd/client/v3 v3.5.7
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@ module github.com/gogf/gf/contrib/registry/file/v2
|
|||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require github.com/gogf/gf/v2 v2.6.0
|
require github.com/gogf/gf/v2 v2.6.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.2.0 // indirect
|
github.com/BurntSushi/toml v1.2.0 // indirect
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/nacos/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/joy999/nacos-sdk-go v0.0.0-20231120071639-10a34b3e7288
|
github.com/joy999/nacos-sdk-go v0.0.0-20231120071639-10a34b3e7288
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/polaris/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/polarismesh/polaris-go v1.5.5
|
github.com/polarismesh/polaris-go v1.5.5
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ go 1.18
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-zookeeper/zk v1.0.3
|
github.com/go-zookeeper/zk v1.0.3
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
golang.org/x/sync v0.4.0
|
golang.org/x/sync v0.4.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -3,8 +3,8 @@ module github.com/gogf/gf/contrib/rpc/grpcx/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/contrib/registry/file/v2 v2.6.0
|
github.com/gogf/gf/contrib/registry/file/v2 v2.6.1
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
go.opentelemetry.io/otel v1.14.0
|
go.opentelemetry.io/otel v1.14.0
|
||||||
go.opentelemetry.io/otel/trace v1.14.0
|
go.opentelemetry.io/otel/trace v1.14.0
|
||||||
google.golang.org/grpc v1.57.2
|
google.golang.org/grpc v1.57.2
|
||||||
|
|||||||
@ -2,7 +2,7 @@ module github.com/gogf/gf/contrib/sdk/httpclient/v2
|
|||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require github.com/gogf/gf/v2 v2.6.0
|
require github.com/gogf/gf/v2 v2.6.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.2.0 // indirect
|
github.com/BurntSushi/toml v1.2.0 // indirect
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/jaeger/v2
|
|||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
go.opentelemetry.io/otel v1.14.0
|
go.opentelemetry.io/otel v1.14.0
|
||||||
go.opentelemetry.io/otel/exporters/jaeger v1.14.0
|
go.opentelemetry.io/otel/exporters/jaeger v1.14.0
|
||||||
go.opentelemetry.io/otel/sdk v1.14.0
|
go.opentelemetry.io/otel/sdk v1.14.0
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/otlpgrpc/v2
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
go.opentelemetry.io/otel v1.19.0
|
go.opentelemetry.io/otel v1.19.0
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0
|
||||||
|
|||||||
@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/otlphttp/v2
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
go.opentelemetry.io/otel v1.19.0
|
go.opentelemetry.io/otel v1.19.0
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0
|
||||||
|
|||||||
@ -60,7 +60,7 @@ func (c *Core) GetFieldType(ctx context.Context, fieldName, table, schema string
|
|||||||
func (c *Core) ConvertDataForRecord(ctx context.Context, value interface{}, table string) (map[string]interface{}, error) {
|
func (c *Core) ConvertDataForRecord(ctx context.Context, value interface{}, table string) (map[string]interface{}, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
data = MapOrStructToMapDeep(value, false)
|
data = MapOrStructToMapDeep(value, true)
|
||||||
)
|
)
|
||||||
for fieldName, fieldValue := range data {
|
for fieldName, fieldValue := range data {
|
||||||
data[fieldName], err = c.db.ConvertValueForField(
|
data[fieldName], err = c.db.ConvertValueForField(
|
||||||
|
|||||||
@ -96,7 +96,9 @@ func DBFromCtx(ctx context.Context) DB {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToSQL formats and returns the last one of sql statements in given closure function without truly executing it.
|
// ToSQL formats and returns the last one of sql statements in given closure function
|
||||||
|
// WITHOUT TRULY EXECUTING IT.
|
||||||
|
// Be caution that, all the following sql statements should use the context object passing by function `f`.
|
||||||
func ToSQL(ctx context.Context, f func(ctx context.Context) error) (sql string, err error) {
|
func ToSQL(ctx context.Context, f func(ctx context.Context) error) (sql string, err error) {
|
||||||
var manager = &CatchSQLManager{
|
var manager = &CatchSQLManager{
|
||||||
SQLArray: garray.NewStrArray(),
|
SQLArray: garray.NewStrArray(),
|
||||||
@ -108,7 +110,8 @@ func ToSQL(ctx context.Context, f func(ctx context.Context) error) (sql string,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// CatchSQL catches and returns all sql statements that are executed in given closure function.
|
// CatchSQL catches and returns all sql statements that are EXECUTED in given closure function.
|
||||||
|
// Be caution that, all the following sql statements should use the context object passing by function `f`.
|
||||||
func CatchSQL(ctx context.Context, f func(ctx context.Context) error) (sqlArray []string, err error) {
|
func CatchSQL(ctx context.Context, f func(ctx context.Context) error) (sqlArray []string, err error) {
|
||||||
var manager = &CatchSQLManager{
|
var manager = &CatchSQLManager{
|
||||||
SQLArray: garray.NewStrArray(),
|
SQLArray: garray.NewStrArray(),
|
||||||
@ -210,12 +213,15 @@ func GetInsertOperationByOption(option InsertOption) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func anyValueToMapBeforeToRecord(value interface{}) map[string]interface{} {
|
func anyValueToMapBeforeToRecord(value interface{}) map[string]interface{} {
|
||||||
return gconv.Map(value, gconv.MapOption{Tags: structTagPriority})
|
return gconv.Map(value, gconv.MapOption{
|
||||||
|
Tags: structTagPriority,
|
||||||
|
OmitEmpty: true, // To be compatible with old version from v2.6.0.
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DaToMapDeep is deprecated, use MapOrStructToMapDeep instead.
|
// DaToMapDeep is deprecated, use MapOrStructToMapDeep instead.
|
||||||
func DaToMapDeep(value interface{}) map[string]interface{} {
|
func DaToMapDeep(value interface{}) map[string]interface{} {
|
||||||
return MapOrStructToMapDeep(value, false)
|
return MapOrStructToMapDeep(value, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MapOrStructToMapDeep converts `value` to map type recursively(if attribute struct is embedded).
|
// MapOrStructToMapDeep converts `value` to map type recursively(if attribute struct is embedded).
|
||||||
|
|||||||
@ -3,21 +3,21 @@ module github.com/gogf/gf/example
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.6.0
|
github.com/gogf/gf/contrib/config/apollo/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/config/consul/v2 v2.6.0
|
github.com/gogf/gf/contrib/config/consul/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/config/kubecm/v2 v2.6.0
|
github.com/gogf/gf/contrib/config/kubecm/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/config/nacos/v2 v2.6.0
|
github.com/gogf/gf/contrib/config/nacos/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/config/polaris/v2 v2.6.0
|
github.com/gogf/gf/contrib/config/polaris/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.0
|
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/nosql/redis/v2 v2.6.0
|
github.com/gogf/gf/contrib/nosql/redis/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/registry/etcd/v2 v2.6.0
|
github.com/gogf/gf/contrib/registry/etcd/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/registry/file/v2 v2.6.0
|
github.com/gogf/gf/contrib/registry/file/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/registry/nacos/v2 v2.5.6
|
github.com/gogf/gf/contrib/registry/nacos/v2 v2.5.6
|
||||||
github.com/gogf/gf/contrib/registry/polaris/v2 v2.6.0
|
github.com/gogf/gf/contrib/registry/polaris/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/rpc/grpcx/v2 v2.6.0
|
github.com/gogf/gf/contrib/rpc/grpcx/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 v2.6.0
|
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 v2.6.1
|
||||||
github.com/gogf/gf/contrib/trace/otlphttp/v2 v2.6.0
|
github.com/gogf/gf/contrib/trace/otlphttp/v2 v2.6.1
|
||||||
github.com/gogf/gf/v2 v2.6.0
|
github.com/gogf/gf/v2 v2.6.1
|
||||||
github.com/hashicorp/consul/api v1.24.0
|
github.com/hashicorp/consul/api v1.24.0
|
||||||
github.com/hashicorp/go-cleanhttp v0.5.2
|
github.com/hashicorp/go-cleanhttp v0.5.2
|
||||||
github.com/nacos-group/nacos-sdk-go v1.1.4
|
github.com/nacos-group/nacos-sdk-go v1.1.4
|
||||||
|
|||||||
44
example/httpserver/upload_file/main.go
Normal file
44
example/httpserver/upload_file/main.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/gogf/gf/v2/net/ghttp"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UploadReq struct {
|
||||||
|
g.Meta `path:"/upload" method:"POST" tags:"Upload" mime:"multipart/form-data" summary:"上传文件"`
|
||||||
|
File *ghttp.UploadFile `p:"file" type:"file" dc:"选择上传文件"`
|
||||||
|
Msg string `dc:"消息"`
|
||||||
|
}
|
||||||
|
type UploadRes struct {
|
||||||
|
FileName string `json:"fileName"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type cUpload struct{}
|
||||||
|
|
||||||
|
func (u cUpload) Upload(ctx context.Context, req *UploadReq) (*UploadRes, error) {
|
||||||
|
if req.File != nil {
|
||||||
|
return &UploadRes{
|
||||||
|
FileName: req.File.Filename,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s := g.Server()
|
||||||
|
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||||
|
group.Middleware(ghttp.MiddlewareHandlerResponse)
|
||||||
|
group.Bind(cUpload{})
|
||||||
|
})
|
||||||
|
s.SetClientMaxBodySize(600 * 1024 * 1024) // 600M
|
||||||
|
s.SetPort(8199)
|
||||||
|
s.SetAccessLogEnabled(true)
|
||||||
|
s.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
// curl --location 'http://127.0.0.1:8199/upload' \
|
||||||
|
// --form 'file=@"/D:/下载/goframe-v2.5.pdf"' \
|
||||||
|
// --form 'msg="666"'
|
||||||
@ -269,8 +269,6 @@ func (r *Request) parseForm() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if contentType := r.Header.Get("Content-Type"); contentType != "" {
|
if contentType := r.Header.Get("Content-Type"); contentType != "" {
|
||||||
r.MakeBodyRepeatableRead(true)
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if gstr.Contains(contentType, "multipart/") {
|
if gstr.Contains(contentType, "multipart/") {
|
||||||
// multipart/form-data, multipart/mixed
|
// multipart/form-data, multipart/mixed
|
||||||
|
|||||||
@ -43,6 +43,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Create a new request object.
|
// Create a new request object.
|
||||||
request := newRequest(s, r, w)
|
request := newRequest(s, r, w)
|
||||||
|
|
||||||
|
// Get sessionId before user handler
|
||||||
|
sessionId := request.GetSessionId()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
request.LeaveTime = gtime.TimestampMilli()
|
request.LeaveTime = gtime.TimestampMilli()
|
||||||
// error log handling.
|
// error log handling.
|
||||||
@ -176,10 +179,17 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Automatically set the session id to cookie
|
// Automatically set the session id to cookie
|
||||||
// if it creates a new session id in this request
|
// if it creates a new session id in this request
|
||||||
// and SessionCookieOutput is enabled.
|
// and SessionCookieOutput is enabled.
|
||||||
if s.config.SessionCookieOutput &&
|
if s.config.SessionCookieOutput && request.Session.IsDirty() {
|
||||||
request.Session.IsDirty() &&
|
// Can change by r.Session.SetId("") before init session
|
||||||
request.Session.MustId() != request.GetSessionId() {
|
// Can change by r.Cookie.SetSessionId("")
|
||||||
request.Cookie.SetSessionId(request.Session.MustId())
|
sidFromSession, sidFromRequest := request.Session.MustId(), request.GetSessionId()
|
||||||
|
if sidFromSession != sidFromRequest {
|
||||||
|
if sidFromSession != sessionId {
|
||||||
|
request.Cookie.SetSessionId(sidFromSession)
|
||||||
|
} else {
|
||||||
|
request.Cookie.SetSessionId(sidFromRequest)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Output the cookie content to the client.
|
// Output the cookie content to the client.
|
||||||
request.Cookie.Flush()
|
request.Cookie.Flush()
|
||||||
|
|||||||
@ -8,6 +8,7 @@ package ghttp_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -154,8 +155,11 @@ func Test_ClientMaxBodySize_File(t *testing.T) {
|
|||||||
t.Assert(gfile.PutBytes(path, data), nil)
|
t.Assert(gfile.PutBytes(path, data), nil)
|
||||||
defer gfile.Remove(path)
|
defer gfile.Remove(path)
|
||||||
t.Assert(
|
t.Assert(
|
||||||
gstr.Trim(c.PostContent(ctx, "/", "name=john&file=@file:"+path)),
|
true,
|
||||||
"Read from request Body failed: http: request body too large",
|
strings.Contains(
|
||||||
|
gstr.Trim(c.PostContent(ctx, "/", "name=john&file=@file:"+path)),
|
||||||
|
"http: request body too large",
|
||||||
|
),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -189,3 +189,92 @@ func Test_Session_Custom_Id(t *testing.T) {
|
|||||||
t.Assert(client.GetContent(ctx, "/value"), value)
|
t.Assert(client.GetContent(ctx, "/value"), value)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_Session_New_Id(t *testing.T) {
|
||||||
|
var (
|
||||||
|
sessionId = "1234567890"
|
||||||
|
newSessionId = "0987654321"
|
||||||
|
newSessionId2 = "abcdefghij"
|
||||||
|
key = "key"
|
||||||
|
value = "value"
|
||||||
|
s = g.Server(guid.S())
|
||||||
|
)
|
||||||
|
s.BindHandler("/id", func(r *ghttp.Request) {
|
||||||
|
if err := r.Session.SetId(sessionId); err != nil {
|
||||||
|
r.Response.WriteExit(err.Error())
|
||||||
|
}
|
||||||
|
if err := r.Session.Set(key, value); err != nil {
|
||||||
|
r.Response.WriteExit(err.Error())
|
||||||
|
}
|
||||||
|
r.Response.WriteExit(r.Session.Id())
|
||||||
|
})
|
||||||
|
|
||||||
|
s.BindHandler("/newIdBySession", func(r *ghttp.Request) {
|
||||||
|
// Use before session init
|
||||||
|
if err := r.Session.SetId(newSessionId); err != nil {
|
||||||
|
r.Response.WriteExit(err.Error())
|
||||||
|
}
|
||||||
|
if err := r.Session.Set(key, value); err != nil {
|
||||||
|
r.Response.WriteExit(err.Error())
|
||||||
|
}
|
||||||
|
r.Response.WriteExit(r.Session.Id())
|
||||||
|
})
|
||||||
|
|
||||||
|
s.BindHandler("/newIdByCookie", func(r *ghttp.Request) {
|
||||||
|
if err := r.Session.Remove("someKey"); err != nil {
|
||||||
|
r.Response.WriteExit(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Cookie.SetSessionId(newSessionId2)
|
||||||
|
//r.Response.WriteExit(r.Session.Id()) // only change in cookie
|
||||||
|
|
||||||
|
r.Response.WriteExit(newSessionId2)
|
||||||
|
})
|
||||||
|
|
||||||
|
s.BindHandler("/value", func(r *ghttp.Request) {
|
||||||
|
r.Response.WriteExit(r.Session.Get(key))
|
||||||
|
})
|
||||||
|
s.SetDumpRouterMap(false)
|
||||||
|
s.Start()
|
||||||
|
defer s.Shutdown()
|
||||||
|
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
client := g.Client()
|
||||||
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||||
|
r, err := client.Get(ctx, "/id")
|
||||||
|
t.AssertNil(err)
|
||||||
|
defer r.Close()
|
||||||
|
t.Assert(r.ReadAllString(), sessionId)
|
||||||
|
t.Assert(r.GetCookie(s.GetSessionIdName()), sessionId)
|
||||||
|
})
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
client := g.Client()
|
||||||
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||||
|
client.SetHeader(s.GetSessionIdName(), sessionId)
|
||||||
|
t.Assert(client.GetContent(ctx, "/value"), value)
|
||||||
|
})
|
||||||
|
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
client := g.Client()
|
||||||
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||||
|
client.SetHeader(s.GetSessionIdName(), sessionId)
|
||||||
|
r, err := client.Get(ctx, "/newIdBySession")
|
||||||
|
t.AssertNil(err)
|
||||||
|
defer r.Close()
|
||||||
|
t.Assert(r.ReadAllString(), newSessionId)
|
||||||
|
t.Assert(r.GetCookie(s.GetSessionIdName()), newSessionId)
|
||||||
|
})
|
||||||
|
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
client := g.Client()
|
||||||
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
|
||||||
|
r, err := client.Get(ctx, "/newIdByCookie")
|
||||||
|
client.SetHeader(s.GetSessionIdName(), sessionId)
|
||||||
|
t.AssertNil(err)
|
||||||
|
defer r.Close()
|
||||||
|
t.Assert(r.ReadAllString(), newSessionId2)
|
||||||
|
t.Assert(r.GetCookie(s.GetSessionIdName()), newSessionId2)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@ -8,6 +8,11 @@ package gconv
|
|||||||
|
|
||||||
import "github.com/gogf/gf/v2/os/gtime"
|
import "github.com/gogf/gf/v2/os/gtime"
|
||||||
|
|
||||||
|
// iVal is used for type assert api for String().
|
||||||
|
type iVal interface {
|
||||||
|
Val() interface{}
|
||||||
|
}
|
||||||
|
|
||||||
// iString is used for type assert api for String().
|
// iString is used for type assert api for String().
|
||||||
type iString interface {
|
type iString interface {
|
||||||
String() string
|
String() string
|
||||||
|
|||||||
@ -29,7 +29,7 @@ type MapOption struct {
|
|||||||
// a map[string]interface{} type variable.
|
// a map[string]interface{} type variable.
|
||||||
Deep bool
|
Deep bool
|
||||||
|
|
||||||
// OmitEmpty ignores the attributes that has json omitempty tag.
|
// OmitEmpty ignores the attributes that has json `omitempty` tag.
|
||||||
OmitEmpty bool
|
OmitEmpty bool
|
||||||
|
|
||||||
// Tags specifies the converted map key name by struct tag name.
|
// Tags specifies the converted map key name by struct tag name.
|
||||||
@ -64,8 +64,15 @@ func doMapConvert(value interface{}, recursive recursiveType, mustMapReturn bool
|
|||||||
if value == nil {
|
if value == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var usedOption = getUsedMapOption(option...)
|
// It redirects to its underlying value if it has implemented interface iVal.
|
||||||
newTags := StructTagPriority
|
if v, ok := value.(iVal); ok {
|
||||||
|
value = v.Val()
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
usedOption = getUsedMapOption(option...)
|
||||||
|
newTags = StructTagPriority
|
||||||
|
)
|
||||||
switch len(usedOption.Tags) {
|
switch len(usedOption.Tags) {
|
||||||
case 0:
|
case 0:
|
||||||
// No need handling.
|
// No need handling.
|
||||||
|
|||||||
@ -2,5 +2,5 @@ package gf
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// VERSION is the current GoFrame version.
|
// VERSION is the current GoFrame version.
|
||||||
VERSION = "v2.6.0"
|
VERSION = "v2.6.1"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user