Compare commits

..

102 Commits

Author SHA1 Message Date
9a6aa01115 constant name changes; version update 2020-12-15 20:16:17 +08:00
4a88e0255d improve shutdown feature for ghttp.Server 2020-12-15 19:24:38 +08:00
edc56949b7 improve package glog for rotation feature 2020-12-15 13:03:12 +08:00
63bc06d0fe improve package gview 2020-12-15 00:09:55 +08:00
9b58b66172 fix issue of HAVING statement before ORDER BY 2020-12-14 23:34:23 +08:00
3517295e96 improve package gcdm 2020-12-14 23:00:22 +08:00
c685876e6f improve package gparser 2020-12-14 21:22:04 +08:00
cb2c9c43a8 rename constant names 2020-12-14 19:34:02 +08:00
751a567e84 add Is* functions for package gvar 2020-12-14 18:54:14 +08:00
0a99bb9a7d improve gconv.Interfaces 2020-12-14 16:56:41 +08:00
a2e7aec37f change constant variable names 2020-12-14 13:26:48 +08:00
102e2d07d9 improve package gcfg for automatic configuration file type checks; change const variables for package gcfg/glog/gtimer/gmode/gins/gi18n 2020-12-14 13:02:08 +08:00
bb39ed136f Merge branch 'fix/json' 2020-12-12 01:42:56 +08:00
363f6eba44 remove automatic stack content printing for error that has stack infomation 2020-12-11 01:12:53 +08:00
0ca305a1bf improve package gerror 2020-12-11 01:08:15 +08:00
1d1e64b834 improve package gerror 2020-12-11 00:45:15 +08:00
688e327f15 improve unit testing case for package ghttp 2020-12-10 23:38:59 +08:00
8c7ec0e7d9 Merge branch 'master' of https://github.com/gogf/gf 2020-12-10 23:33:43 +08:00
84fef8dea3 add error code feature for package gerror 2020-12-10 23:33:24 +08:00
c1d2ad68b3 Merge pull request #1051 from dozysun/master
fix InsertIgnore didn't work
2020-12-10 20:17:43 +08:00
a577605726 improve package gtimer 2020-12-10 20:10:07 +08:00
71444736ae fix InsertIgnore didn't work
fix InsertIgnore didn't work
2020-12-10 14:40:58 +08:00
b7e41ec32c improve package gvalid 2020-12-09 21:00:30 +08:00
7fa09596b0 improve package gsession 2020-12-09 16:22:03 +08:00
7316e6648f change errors to gerror; update copyright in comment 2020-12-09 16:04:29 +08:00
e9d346ce4f improve package glog 2020-12-09 13:54:27 +08:00
4b91e709f7 Merge branch 'master' of https://github.com/gogf/gf into develop 2020-12-09 13:48:50 +08:00
7de89286da Merge pull request #1045 from prcseraph/master 2020-12-09 13:48:33 +08:00
569a953b43 inprove package gconv for empty string converting to number array 2020-12-09 13:39:09 +08:00
1e7f795c69 remove package github.com/json-iterator/go 2020-12-09 01:38:56 +08:00
d2ae383b83 improve unit testing case for package gdb 2020-12-09 01:33:02 +08:00
8978112433 add gdb.Raw for raw sql instead of prepare argument feature 2020-12-09 01:29:23 +08:00
117eaea720 add support for cert and key file reading from resource manager for ghttp.Server 2020-12-09 00:06:56 +08:00
278e85357d * 日志根据大小切片时,文件重命名读写冲突。 2020-12-08 18:54:42 +08:00
60ec59fa4a improve package gsession 2020-12-07 20:39:52 +08:00
0aa82ad020 improve package gpool 2020-12-07 18:57:40 +08:00
7bd319ddc7 Merge branch 'master' of https://github.com/gogf/gf into develop 2020-12-07 13:19:42 +08:00
7ceb667486 Merge pull request #1040 from Wlvs530/master
Not Callback ExpireFunc In Pool.Get()
2020-12-07 13:18:48 +08:00
80c4786afd Not Callback ExpireFunc In Pool.Get() 2020-12-05 13:21:18 +08:00
2f741d3b24 add genv.SetMap 2020-12-05 00:06:03 +08:00
d2b65f0f91 add gcmd.GetWithEnv/genv.GetWithCmd; remove package cmdenv; 2020-12-04 23:44:54 +08:00
c226782f23 improve package gcmd,internal/cmdenv 2020-12-04 23:29:20 +08:00
57a82ebcc0 improve gconv.Struct 2020-12-04 18:17:11 +08:00
416885a726 gdb driver example updates 2020-12-04 15:42:35 +08:00
9b4d2d9172 fix issue in unit testing case for package gjson/gparser 2020-12-04 15:41:26 +08:00
d4b2bf20bb gdb driver example updates 2020-12-04 15:37:14 +08:00
ce9a0555c5 improve gconv.Interfaces by adding support for type map as its parameter 2020-12-04 14:33:47 +08:00
5171250a9d improve gutil.Keys,gdb.Model.Fields,internal/structs 2020-12-04 14:22:50 +08:00
80b629916a fix issue in function Clone for package gmap/garray/gtree 2020-12-02 21:38:29 +08:00
ecaf0da228 improve package glog; add more unit testing case for package gmlock 2020-12-02 21:33:07 +08:00
8c0a905a9f improve package gconv for custom type converting 2020-12-01 15:57:06 +08:00
5a0326f666 fix issue in unit testing case for package gconv 2020-11-30 09:07:44 +08:00
790a651ac1 fix typo 2020-11-29 23:55:32 +08:00
6f93bd17f2 add context feature for package gdb 2020-11-29 23:54:38 +08:00
3419d66c4b add context feature for package gdb 2020-11-29 23:50:16 +08:00
cabf684ec9 add context feature for package gdb 2020-11-29 23:47:57 +08:00
2b6e6ce28e improve update counter feature for package gdb 2020-11-29 22:26:16 +08:00
32101189a2 Merge branch 'master' of https://github.com/gogf/gf into develop 2020-11-29 21:46:36 +08:00
600c081801 Merge pull request #1028 from arieslee/gdb-counter
add update counter method for package gdb.
2020-11-29 21:46:17 +08:00
0899a9d49a Merge pull request #1027 from arieslee/gvalid-phone-loose
Add 172-segment verification rules, add loose mobile number verificat…
2020-11-29 21:45:28 +08:00
c56f4eabca inprove gconv.Interfaces for struct type 2020-11-29 21:34:28 +08:00
bfe89e0b12 add build-in fuction json for package gview 2020-11-28 22:48:01 +08:00
6cb38cfa92 add update counter method for package gdb. 2020-11-28 14:56:21 +08:00
09bb0c9397 更新gdb.Update方法 2020-11-28 13:52:17 +08:00
fa47b0306d Add 172-segment verification rules, add loose mobile number verification rules. 2020-11-28 12:23:18 +08:00
55429ad589 fix data race issue in unit testing case of package gdb 2020-11-28 00:11:22 +08:00
9592fb099f Change time string argument wrapping with TO_DATE function for DriverOracle in package gdb 2020-11-27 23:11:55 +08:00
18ec6116ad improve time argument timezone handling for package gdb 2020-11-27 22:51:34 +08:00
9d0f306684 improve time argument timezone handling for package gdb 2020-11-27 21:03:22 +08:00
3eba8d690f add TimeMaintainDisabled configuration for automatic time maintaining feature 2020-11-27 13:28:18 +08:00
c0b59007ce improve configuration for package gdb 2020-11-27 13:24:05 +08:00
3485ba2a5d improve package gtime 2020-11-26 22:40:55 +08:00
43ecfc7484 Merge branch 'develop' of https://github.com/gogf/gf into develop 2020-11-26 22:12:48 +08:00
5ba53e56c9 improve join feature for sub-query for gdb.Model 2020-11-26 22:11:58 +08:00
af1d14ace6 Merge pull request #1021 from tiansin/develop
fix: 🐛 fix model chunk page
2020-11-26 21:40:45 +08:00
c02bf715c5 improve gutil.ListItemValues/ListItemValuesUnique supporting retrieving values from slice attributes 2020-11-26 21:13:44 +08:00
0c0e902b07 if CreatedAt/UpdatedAt/DeletedAt field name configured, just use it, ignore the default field names 2020-11-26 20:17:24 +08:00
750b53d7aa improve debug infor for argument of time.Time for package gdb 2020-11-26 19:53:44 +08:00
1d807c095a Merge branch 'master' into develop 2020-11-26 19:28:25 +08:00
69b5873bf9 fix: 🐛 fix model chunk page 2020-11-26 14:26:32 +08:00
60fc9b6417 add gerror.ApiCurrent impements for gerror.Error 2020-11-25 19:06:29 +08:00
0bc8944a08 change ghttp.NewClient to g.Client 2020-11-25 16:40:45 +08:00
33292f54e0 improve status handler by supporting multiple status handler for package ghttp 2020-11-25 16:37:41 +08:00
fc215ef0b2 add function ghttp.RouterGroup.ALLMap 2020-11-24 21:19:01 +08:00
65c67427d4 compatible with Case of https://github.com/pkg/errors 2020-11-23 16:32:57 +08:00
bc8142974f version update 2020-11-21 14:08:29 +08:00
fadb7a8f8f fix issue 1002 2020-11-21 13:20:32 +08:00
e1bfe90833 improve package gdb for structire retrieving in transaction operations 2020-11-21 12:24:32 +08:00
042dc0b33f improve package gdb for structire retrieving in transaction operations 2020-11-21 11:42:50 +08:00
8ca535dbf0 fix issue in session cookie feature 2020-11-21 11:18:21 +08:00
e20183e7a1 improve gutil.Keys/Values for embedded struct 2020-11-20 00:53:12 +08:00
df86ffb61e improve package glog for nil file pointer 2020-11-19 20:43:44 +08:00
bfcf133c91 improve sqlite.Open 2020-11-19 20:37:27 +08:00
24a377d3a8 fix issue in custom mapping for gconv.Struct 2020-11-19 14:52:19 +08:00
baf51bc68f merge develop 2020-11-18 10:57:14 +08:00
5f4b585164 version updates 2020-11-18 10:52:53 +08:00
17a11187b0 fix issue in gdb.Model.Fields 2020-11-18 00:32:09 +08:00
7caf7976cf add gdb.Model.Args 2020-11-18 00:16:34 +08:00
1a31792c14 add SpecialCharsMapOrStruct for package ghtml 2020-11-15 16:49:44 +08:00
d56eb49e41 improve performance for gconv.Struct/Structs using directly reflect set 2020-11-15 15:13:40 +08:00
a1236b5e16 donator updates 2020-11-15 11:08:26 +08:00
8f278be0dc improve build-in varables for view of ghttp 2020-11-14 10:24:06 +08:00
252 changed files with 3926 additions and 1707 deletions

View File

@ -9,7 +9,6 @@ package driver
import (
"database/sql"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
)
@ -51,14 +50,13 @@ func (d *MyDriver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
func (d *MyDriver) DoQuery(link gdb.Link, sql string, args ...interface{}) (rows *sql.Rows, err error) {
tsMilli := gtime.TimestampMilli()
rows, err = d.DriverMysql.DoQuery(link, sql, args...)
if _, err := d.DriverMysql.InsertIgnore("monitor", g.Map{
"sql": gdb.FormatSqlWithArgs(sql, args),
"cost": gtime.TimestampMilli() - tsMilli,
"time": gtime.Now(),
"error": err.Error(),
}); err != nil {
panic(err)
}
link.Exec(
"INSERT INTO `monitor`(`sql`,`cost`,`time`,`error`) VALUES(?,?,?,?)",
gdb.FormatSqlWithArgs(sql, args),
gtime.TimestampMilli()-tsMilli,
gtime.Now(),
err,
)
return
}
@ -67,13 +65,12 @@ func (d *MyDriver) DoQuery(link gdb.Link, sql string, args ...interface{}) (rows
func (d *MyDriver) DoExec(link gdb.Link, sql string, args ...interface{}) (result sql.Result, err error) {
tsMilli := gtime.TimestampMilli()
result, err = d.DriverMysql.DoExec(link, sql, args...)
if _, err := d.DriverMysql.InsertIgnore("monitor", g.Map{
"sql": gdb.FormatSqlWithArgs(sql, args),
"cost": gtime.TimestampMilli() - tsMilli,
"time": gtime.Now(),
"error": err.Error(),
}); err != nil {
panic(err)
}
link.Exec(
"INSERT INTO `monitor`(`sql`,`cost`,`time`,`error`) VALUES(?,?,?,?)",
gdb.FormatSqlWithArgs(sql, args),
gtime.TimestampMilli()-tsMilli,
gtime.Now(),
err,
)
return
}

View File

@ -2,7 +2,7 @@
# MySQL.
[database]
debug = true
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local"
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true"
MaxOpen = 100
# Redis.

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/database/gdb"
"time"
"github.com/gogf/gf/frame/g"
)
func main() {
@ -18,10 +18,9 @@ func main() {
db.SetDebug(true)
type User struct {
CreateTime time.Time `orm:"create_time"`
}
r, e := db.Table("user").Data(User{CreateTime: time.Now()}).Insert()
r, e := db.Table("user").Data(g.Map{
"create_at": "now()",
}).Unscoped().Insert()
if e != nil {
panic(e)
}

View File

@ -1,12 +1,15 @@
package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
)
func main() {
db := g.DB()
db.SetDebug(true)
db.Table("user").Data("num=num+1").Where("id", 8).Update()
one, err := g.DB().Table("carlist c").
LeftJoin("cardetail d", "c.postid=d.carid").
Where("c.postid", "142039140032006").
Fields("c.*,d.*").One()
fmt.Println(err)
g.Dump(one)
}

View File

@ -15,10 +15,10 @@ func main() {
r.Response.Writeln("end")
})
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
ghttp.HookBeforeServe: func(r *ghttp.Request) {
glog.To(r.Response.Writer).Println("BeforeServe")
},
ghttp.HOOK_AFTER_SERVE: func(r *ghttp.Request) {
ghttp.HookAfterServe: func(r *ghttp.Request) {
glog.To(r.Response.Writer).Println("AfterServe")
},
})

View File

@ -12,7 +12,7 @@ func Order(r *ghttp.Request) {
func main() {
s := g.Server()
s.Group("/api.v1", func(group *ghttp.RouterGroup) {
group.Hook("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
group.Hook("/*any", ghttp.HookBeforeServe, func(r *ghttp.Request) {
r.Response.CORSDefault()
})
g.GET("/order", Order)

View File

@ -11,10 +11,10 @@ func main() {
p := "/:name/info/{uid}"
s := g.Server()
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_BEFORE_SERVE) },
ghttp.HOOK_AFTER_SERVE: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_SERVE) },
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_BEFORE_OUTPUT) },
ghttp.HOOK_AFTER_OUTPUT: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_OUTPUT) },
ghttp.HookBeforeServe: func(r *ghttp.Request) { glog.Println(ghttp.HookBeforeServe) },
ghttp.HookAfterServe: func(r *ghttp.Request) { glog.Println(ghttp.HookAfterServe) },
ghttp.HookBeforeOutput: func(r *ghttp.Request) { glog.Println(ghttp.HookBeforeOutput) },
ghttp.HookAfterOutput: func(r *ghttp.Request) { glog.Println(ghttp.HookAfterOutput) },
})
s.BindHandler(p, func(r *ghttp.Request) {
r.Response.Write("用户:", r.Get("name"), ", uid:", r.Get("uid"))

View File

@ -11,7 +11,7 @@ func main() {
// 多事件回调示例事件1
pattern1 := "/:name/info"
s.BindHookHandlerByMap(pattern1, map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
ghttp.HookBeforeServe: func(r *ghttp.Request) {
r.SetParam("uid", 1000)
},
})
@ -22,7 +22,7 @@ func main() {
// 多事件回调示例事件2
pattern2 := "/{object}/list/{page}.java"
s.BindHookHandlerByMap(pattern2, map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) {
ghttp.HookBeforeOutput: func(r *ghttp.Request) {
r.Response.SetBuffer([]byte(
fmt.Sprintf("通过事件修改输出内容, object:%s, page:%s", r.Get("object"), r.GetRouterString("page"))),
)

View File

@ -7,10 +7,10 @@ import (
func main() {
s := g.Server()
s.BindHookHandler("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
s.BindHookHandler("/*any", ghttp.HookBeforeServe, func(r *ghttp.Request) {
r.Response.Writeln("/*any")
})
s.BindHookHandler("/v1/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
s.BindHookHandler("/v1/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
r.Response.Writeln("/v1/*")
r.ExitHook()
})

View File

@ -11,7 +11,7 @@ func main() {
r.Response.Writeln(r.Get("name"))
})
s.BindHookHandlerByMap("/", map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
ghttp.HookBeforeServe: func(r *ghttp.Request) {
r.SetParam("name", "john")
},
})

View File

@ -12,17 +12,17 @@ func main() {
})
s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
ghttp.HookBeforeServe: func(r *ghttp.Request) {
r.Response.Writeln("/priority/:name")
},
})
s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
ghttp.HookBeforeServe: func(r *ghttp.Request) {
r.Response.Writeln("/priority/*any")
},
})
s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) {
ghttp.HookBeforeServe: func(r *ghttp.Request) {
r.Response.Writeln("/priority/show")
},
})

View File

@ -27,10 +27,10 @@ func main() {
})
})
group.Group("/hook", func(group *ghttp.RouterGroup) {
group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
group.Hook("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
r.Response.Write("hook any")
})
group.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
group.Hook("/:name", ghttp.HookBeforeServe, func(r *ghttp.Request) {
r.Response.Write("hook name")
})
})

View File

@ -18,7 +18,7 @@ func main() {
s := g.Server()
s.SetIndexFolder(true)
s.SetServerRoot("root")
s.BindHookHandler("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
s.BindHookHandler("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
fmt.Println(r.URL.Path, r.IsFileRequest())
})
s.BindHandler("/template", func(r *ghttp.Request) {

View File

@ -27,7 +27,7 @@ func main() {
s := g.Server()
obj := new(Object)
s.Group("/api").Bind([]ghttp.GroupItem{
{"ALL", "*", HookHandler, ghttp.HOOK_BEFORE_SERVE},
{"ALL", "*", HookHandler, ghttp.HookBeforeServe},
{"ALL", "/handler", Handler},
{"ALL", "/obj", obj},
{"GET", "/obj/show", obj, "Show"},

View File

@ -54,10 +54,10 @@ func main() {
})
})
group.Group("/hook", func(group *ghttp.RouterGroup) {
group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
group.Hook("/*", ghttp.HookBeforeServe, func(r *ghttp.Request) {
r.Response.Write("hook any")
})
group.Hook("/:name", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
group.Hook("/:name", ghttp.HookBeforeServe, func(r *ghttp.Request) {
r.Response.Write("hook name")
})
})

View File

@ -9,7 +9,7 @@ import (
func main() {
s := g.Server()
s.BindHookHandler("/*any", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
s.BindHookHandler("/*any", ghttp.HookBeforeServe, func(r *ghttp.Request) {
fmt.Println(r.Router)
fmt.Println(r.Get("customer_id"))
})

View File

@ -13,7 +13,7 @@ func main() {
key := "key"
// 第一次锁带时间
gmlock.Lock(key, 1000)
gmlock.Lock(key)
glog.Println("lock1")
// 这个时候上一次的计时解锁已失效
gmlock.Unlock(key)

View File

@ -140,6 +140,7 @@ please note your github/gitee account in your payment bill. All the donations wi
|RAGGA-TIME|alipay|¥50.00|
|[ChArmy](https://gitee.com/charmy)|alipay|¥50.00|
|[sanfenzui](https://gitee.com/sanfenzui)|alipay|¥88.00|
|刘宇|wechat|¥30.00|请你喝咖啡

View File

@ -125,6 +125,7 @@ The concurrency starts from `100` to `10000`.
- [LeYouJia](https://www.leyoujia.com/)
- [IGG](https://igg.com)
- [XiMaLaYa](https://www.ximalaya.com)
- [ZYBang](https://www.zybang.com/)
> We list part of the users here, if your company or products are using `GoFrame`, please let us know [here](https://github.com/gogf/gf/issues/168).

View File

@ -145,6 +145,7 @@ ab -t 10 -c 100 http://127.0.0.1:3000/json
- [乐有家](https://www.leyoujia.com/)
- [IGG](https://igg.com)
- [喜马拉雅](https://www.ximalaya.com)
- [作业帮](https://www.zybang.com/)
> 在这里只列举了部分知名的用户,如果您的企业或者产品正在使用`GoFrame`,欢迎到 [这里](https://github.com/gogf/gf/issues/168) 留言。

View File

@ -453,7 +453,7 @@ func (a *Array) Clone() (newArray *Array) {
array := make([]interface{}, len(a.array))
copy(array, a.array)
a.mu.RUnlock()
return NewArrayFrom(array, !a.mu.IsSafe())
return NewArrayFrom(array, a.mu.IsSafe())
}
// Clear deletes all items of current array.

View File

@ -469,7 +469,7 @@ func (a *IntArray) Clone() (newArray *IntArray) {
array := make([]int, len(a.array))
copy(array, a.array)
a.mu.RUnlock()
return NewIntArrayFrom(array, !a.mu.IsSafe())
return NewIntArrayFrom(array, a.mu.IsSafe())
}
// Clear deletes all items of current array.

View File

@ -457,7 +457,7 @@ func (a *StrArray) Clone() (newArray *StrArray) {
array := make([]string, len(a.array))
copy(array, a.array)
a.mu.RUnlock()
return NewStrArrayFrom(array, !a.mu.IsSafe())
return NewStrArrayFrom(array, a.mu.IsSafe())
}
// Clear deletes all items of current array.

View File

@ -499,7 +499,7 @@ func (a *SortedArray) Clone() (newArray *SortedArray) {
array := make([]interface{}, len(a.array))
copy(array, a.array)
a.mu.RUnlock()
return NewSortedArrayFrom(array, a.comparator, !a.mu.IsSafe())
return NewSortedArrayFrom(array, a.comparator, a.mu.IsSafe())
}
// Clear deletes all items of current array.

View File

@ -496,7 +496,7 @@ func (a *SortedIntArray) Clone() (newArray *SortedIntArray) {
array := make([]int, len(a.array))
copy(array, a.array)
a.mu.RUnlock()
return NewSortedIntArrayFrom(array, !a.mu.IsSafe())
return NewSortedIntArrayFrom(array, a.mu.IsSafe())
}
// Clear deletes all items of current array.

View File

@ -498,7 +498,7 @@ func (a *SortedStrArray) Clone() (newArray *SortedStrArray) {
array := make([]string, len(a.array))
copy(array, a.array)
a.mu.RUnlock()
return NewSortedStrArrayFrom(array, !a.mu.IsSafe())
return NewSortedStrArrayFrom(array, a.mu.IsSafe())
}
// Clear deletes all items of current array.

View File

@ -56,7 +56,7 @@ func (m *IntAnyMap) Iterator(f func(k int, v interface{}) bool) {
// Clone returns a new hash map with copy of current map data.
func (m *IntAnyMap) Clone() *IntAnyMap {
return NewIntAnyMapFrom(m.MapCopy(), !m.mu.IsSafe())
return NewIntAnyMapFrom(m.MapCopy(), m.mu.IsSafe())
}
// Map returns the underlying data map.

View File

@ -54,7 +54,7 @@ func (m *IntIntMap) Iterator(f func(k int, v int) bool) {
// Clone returns a new hash map with copy of current map data.
func (m *IntIntMap) Clone() *IntIntMap {
return NewIntIntMapFrom(m.MapCopy(), !m.mu.IsSafe())
return NewIntIntMapFrom(m.MapCopy(), m.mu.IsSafe())
}
// Map returns the underlying data map.

View File

@ -54,7 +54,7 @@ func (m *IntStrMap) Iterator(f func(k int, v string) bool) {
// Clone returns a new hash map with copy of current map data.
func (m *IntStrMap) Clone() *IntStrMap {
return NewIntStrMapFrom(m.MapCopy(), !m.mu.IsSafe())
return NewIntStrMapFrom(m.MapCopy(), m.mu.IsSafe())
}
// Map returns the underlying data map.

View File

@ -56,7 +56,7 @@ func (m *StrAnyMap) Iterator(f func(k string, v interface{}) bool) {
// Clone returns a new hash map with copy of current map data.
func (m *StrAnyMap) Clone() *StrAnyMap {
return NewStrAnyMapFrom(m.MapCopy(), !m.mu.IsSafe())
return NewStrAnyMapFrom(m.MapCopy(), m.mu.IsSafe())
}
// Map returns the underlying data map.

View File

@ -54,7 +54,7 @@ func (m *StrIntMap) Iterator(f func(k string, v int) bool) {
// Clone returns a new hash map with copy of current map data.
func (m *StrIntMap) Clone() *StrIntMap {
return NewStrIntMapFrom(m.MapCopy(), !m.mu.IsSafe())
return NewStrIntMapFrom(m.MapCopy(), m.mu.IsSafe())
}
// Map returns the underlying data map.

View File

@ -55,7 +55,7 @@ func (m *StrStrMap) Iterator(f func(k string, v string) bool) {
// Clone returns a new hash map with copy of current map data.
func (m *StrStrMap) Clone() *StrStrMap {
return NewStrStrMapFrom(m.MapCopy(), !m.mu.IsSafe())
return NewStrStrMapFrom(m.MapCopy(), m.mu.IsSafe())
}
// Map returns the underlying data map.

View File

@ -1,4 +1,4 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame gf 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,
@ -19,18 +19,10 @@ import (
// Pool is an Object-Reusable Pool.
type Pool struct {
// Available/idle items list.
list *glist.List
// Whether the pool is closed.
closed *gtype.Bool
// Time To Live for pool items.
TTL time.Duration
// Callback function to create pool item.
NewFunc func() (interface{}, error)
list *glist.List // Available/idle items list.
closed *gtype.Bool // Whether the pool is closed.
TTL time.Duration // Time To Live for pool items.
NewFunc func() (interface{}, error) // Callback function to create pool item.
// ExpireFunc is the for expired items destruction.
// This function needs to be defined when the pool items
// need to perform additional destruction operations.
@ -40,8 +32,8 @@ type Pool struct {
// Pool item.
type poolItem struct {
expire int64 // Expire timestamp in milliseconds.
value interface{} // Item value.
value interface{} // Item value.
expireAt int64 // Expire timestamp in milliseconds.
}
// Creation function for object.
@ -80,11 +72,11 @@ func (p *Pool) Put(value interface{}) error {
value: value,
}
if p.TTL == 0 {
item.expire = 0
item.expireAt = 0
} else {
// As for Golang version < 1.13, there's no method Milliseconds for time.Duration.
// So we need calculate the milliseconds using its nanoseconds value.
item.expire = gtime.TimestampMilli() + p.TTL.Nanoseconds()/1000000
item.expireAt = gtime.TimestampMilli() + p.TTL.Nanoseconds()/1000000
}
p.list.PushBack(item)
return nil
@ -112,8 +104,11 @@ func (p *Pool) Get() (interface{}, error) {
for !p.closed.Val() {
if r := p.list.PopFront(); r != nil {
f := r.(*poolItem)
if f.expire == 0 || f.expire > gtime.TimestampMilli() {
if f.expireAt == 0 || f.expireAt > gtime.TimestampMilli() {
return f.value, nil
} else if p.ExpireFunc != nil {
// TODO: move expire function calling asynchronously from `Get` operation.
p.ExpireFunc(f.value)
}
} else {
break
@ -169,9 +164,9 @@ func (p *Pool) checkExpireItems() {
}
if r := p.list.PopFront(); r != nil {
item := r.(*poolItem)
latestExpire = item.expire
latestExpire = item.expireAt
// TODO improve the auto-expiration mechanism of the pool.
if item.expire > timestampMilli {
if item.expireAt > timestampMilli {
p.list.PushFront(item)
break
}

View File

@ -56,7 +56,7 @@ func NewAVLTreeFrom(comparator func(v1, v2 interface{}) int, data map[interface{
// Clone returns a new tree with a copy of current tree.
func (tree *AVLTree) Clone() *AVLTree {
newTree := NewAVLTree(tree.comparator, !tree.mu.IsSafe())
newTree := NewAVLTree(tree.comparator, tree.mu.IsSafe())
newTree.Sets(tree.Map())
return newTree
}
@ -441,9 +441,9 @@ func (tree *AVLTree) MapStrAny() map[string]interface{} {
func (tree *AVLTree) Flip(comparator ...func(v1, v2 interface{}) int) {
t := (*AVLTree)(nil)
if len(comparator) > 0 {
t = NewAVLTree(comparator[0], !tree.mu.IsSafe())
t = NewAVLTree(comparator[0], tree.mu.IsSafe())
} else {
t = NewAVLTree(tree.comparator, !tree.mu.IsSafe())
t = NewAVLTree(tree.comparator, tree.mu.IsSafe())
}
tree.IteratorAsc(func(key, value interface{}) bool {
t.put(value, key, nil, &t.root)

View File

@ -68,7 +68,7 @@ func NewBTreeFrom(m int, comparator func(v1, v2 interface{}) int, data map[inter
// Clone returns a new tree with a copy of current tree.
func (tree *BTree) Clone() *BTree {
newTree := NewBTree(tree.m, tree.comparator, !tree.mu.IsSafe())
newTree := NewBTree(tree.m, tree.comparator, tree.mu.IsSafe())
newTree.Sets(tree.Map())
return newTree
}

View File

@ -83,7 +83,7 @@ func (tree *RedBlackTree) SetComparator(comparator func(a, b interface{}) int) {
// Clone returns a new tree with a copy of current tree.
func (tree *RedBlackTree) Clone() *RedBlackTree {
newTree := NewRedBlackTree(tree.comparator, !tree.mu.IsSafe())
newTree := NewRedBlackTree(tree.comparator, tree.mu.IsSafe())
newTree.Sets(tree.Map())
return newTree
}
@ -656,9 +656,9 @@ func (tree *RedBlackTree) Search(key interface{}) (value interface{}, found bool
func (tree *RedBlackTree) Flip(comparator ...func(v1, v2 interface{}) int) {
t := (*RedBlackTree)(nil)
if len(comparator) > 0 {
t = NewRedBlackTree(comparator[0], !tree.mu.IsSafe())
t = NewRedBlackTree(comparator[0], tree.mu.IsSafe())
} else {
t = NewRedBlackTree(tree.comparator, !tree.mu.IsSafe())
t = NewRedBlackTree(tree.comparator, tree.mu.IsSafe())
}
tree.IteratorAsc(func(key, value interface{}) bool {
t.doSet(value, key)

View File

@ -1,4 +1,4 @@
// Copyright 2018-2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -11,8 +11,6 @@ import (
"github.com/gogf/gf/internal/json"
"time"
"github.com/gogf/gf/internal/empty"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
@ -88,16 +86,6 @@ func (v *Var) Interface() interface{} {
return v.Val()
}
// IsNil checks whether <v> is nil.
func (v *Var) IsNil() bool {
return v.Val() == nil
}
// IsEmpty checks whether <v> is empty.
func (v *Var) IsEmpty() bool {
return empty.IsEmpty(v.Val())
}
// Bytes converts and returns <v> as []byte.
func (v *Var) Bytes() []byte {
return gconv.Bytes(v.Val())

98
container/gvar/gvar_is.go Normal file
View File

@ -0,0 +1,98 @@
// 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.
package gvar
import (
"github.com/gogf/gf/internal/empty"
"reflect"
)
// IsNil checks whether <v> is nil.
func (v *Var) IsNil() bool {
return v.Val() == nil
}
// IsEmpty checks whether <v> is empty.
func (v *Var) IsEmpty() bool {
return empty.IsEmpty(v.Val())
}
// IsInt checks whether <v> is type of int.
func (v *Var) IsInt() bool {
switch v.Val().(type) {
case int, *int, int8, *int8, int16, *int16, int32, *int32, int64, *int64:
return true
}
return false
}
// IsUint checks whether <v> is type of uint.
func (v *Var) IsUint() bool {
switch v.Val().(type) {
case uint, *uint, uint8, *uint8, uint16, *uint16, uint32, *uint32, uint64, *uint64:
return true
}
return false
}
// IsFloat checks whether <v> is type of float.
func (v *Var) IsFloat() bool {
switch v.Val().(type) {
case float32, *float32, float64, *float64:
return true
}
return false
}
// IsSlice checks whether <v> is type of slice.
func (v *Var) IsSlice() bool {
var (
reflectValue = reflect.ValueOf(v.Val())
reflectKind = reflectValue.Kind()
)
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
}
switch reflectKind {
case reflect.Slice, reflect.Array:
return true
}
return false
}
// IsMap checks whether <v> is type of map.
func (v *Var) IsMap() bool {
var (
reflectValue = reflect.ValueOf(v.Val())
reflectKind = reflectValue.Kind()
)
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
}
switch reflectKind {
case reflect.Map:
return true
}
return false
}
// IsStruct checks whether <v> is type of struct.
func (v *Var) IsStruct() bool {
var (
reflectValue = reflect.ValueOf(v.Val())
reflectKind = reflectValue.Kind()
)
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
switch reflectKind {
case reflect.Struct:
return true
}
return false
}

View File

@ -0,0 +1,170 @@
// 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.
package gvar_test
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/test/gtest"
"testing"
)
func TestVar_IsNil(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(0).IsNil(), false)
t.Assert(g.NewVar(nil).IsNil(), true)
t.Assert(g.NewVar(g.Map{}).IsNil(), false)
t.Assert(g.NewVar(g.Slice{}).IsNil(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(1).IsNil(), false)
t.Assert(g.NewVar(0.1).IsNil(), false)
t.Assert(g.NewVar(g.Map{"k": "v"}).IsNil(), false)
t.Assert(g.NewVar(g.Slice{0}).IsNil(), false)
})
}
func TestVar_IsEmpty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(0).IsEmpty(), true)
t.Assert(g.NewVar(nil).IsEmpty(), true)
t.Assert(g.NewVar(g.Map{}).IsEmpty(), true)
t.Assert(g.NewVar(g.Slice{}).IsEmpty(), true)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(1).IsEmpty(), false)
t.Assert(g.NewVar(0.1).IsEmpty(), false)
t.Assert(g.NewVar(g.Map{"k": "v"}).IsEmpty(), false)
t.Assert(g.NewVar(g.Slice{0}).IsEmpty(), false)
})
}
func TestVar_IsInt(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(0).IsInt(), true)
t.Assert(g.NewVar(nil).IsInt(), false)
t.Assert(g.NewVar(g.Map{}).IsInt(), false)
t.Assert(g.NewVar(g.Slice{}).IsInt(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(1).IsInt(), true)
t.Assert(g.NewVar(-1).IsInt(), true)
t.Assert(g.NewVar(0.1).IsInt(), false)
t.Assert(g.NewVar(g.Map{"k": "v"}).IsInt(), false)
t.Assert(g.NewVar(g.Slice{0}).IsInt(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(int8(1)).IsInt(), true)
t.Assert(g.NewVar(uint8(1)).IsInt(), false)
})
}
func TestVar_IsUint(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(0).IsUint(), false)
t.Assert(g.NewVar(nil).IsUint(), false)
t.Assert(g.NewVar(g.Map{}).IsUint(), false)
t.Assert(g.NewVar(g.Slice{}).IsUint(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(1).IsUint(), false)
t.Assert(g.NewVar(-1).IsUint(), false)
t.Assert(g.NewVar(0.1).IsUint(), false)
t.Assert(g.NewVar(g.Map{"k": "v"}).IsUint(), false)
t.Assert(g.NewVar(g.Slice{0}).IsUint(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(int8(1)).IsUint(), false)
t.Assert(g.NewVar(uint8(1)).IsUint(), true)
})
}
func TestVar_IsFloat(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(0).IsFloat(), false)
t.Assert(g.NewVar(nil).IsFloat(), false)
t.Assert(g.NewVar(g.Map{}).IsFloat(), false)
t.Assert(g.NewVar(g.Slice{}).IsFloat(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(1).IsFloat(), false)
t.Assert(g.NewVar(-1).IsFloat(), false)
t.Assert(g.NewVar(0.1).IsFloat(), true)
t.Assert(g.NewVar(float64(1)).IsFloat(), true)
t.Assert(g.NewVar(g.Map{"k": "v"}).IsFloat(), false)
t.Assert(g.NewVar(g.Slice{0}).IsFloat(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(int8(1)).IsFloat(), false)
t.Assert(g.NewVar(uint8(1)).IsFloat(), false)
})
}
func TestVar_IsSlice(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(0).IsSlice(), false)
t.Assert(g.NewVar(nil).IsSlice(), false)
t.Assert(g.NewVar(g.Map{}).IsSlice(), false)
t.Assert(g.NewVar(g.Slice{}).IsSlice(), true)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(1).IsSlice(), false)
t.Assert(g.NewVar(-1).IsSlice(), false)
t.Assert(g.NewVar(0.1).IsSlice(), false)
t.Assert(g.NewVar(float64(1)).IsSlice(), false)
t.Assert(g.NewVar(g.Map{"k": "v"}).IsSlice(), false)
t.Assert(g.NewVar(g.Slice{0}).IsSlice(), true)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(int8(1)).IsSlice(), false)
t.Assert(g.NewVar(uint8(1)).IsSlice(), false)
})
}
func TestVar_IsMap(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(0).IsMap(), false)
t.Assert(g.NewVar(nil).IsMap(), false)
t.Assert(g.NewVar(g.Map{}).IsMap(), true)
t.Assert(g.NewVar(g.Slice{}).IsMap(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(1).IsMap(), false)
t.Assert(g.NewVar(-1).IsMap(), false)
t.Assert(g.NewVar(0.1).IsMap(), false)
t.Assert(g.NewVar(float64(1)).IsMap(), false)
t.Assert(g.NewVar(g.Map{"k": "v"}).IsMap(), true)
t.Assert(g.NewVar(g.Slice{0}).IsMap(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(int8(1)).IsMap(), false)
t.Assert(g.NewVar(uint8(1)).IsMap(), false)
})
}
func TestVar_IsStruct(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(0).IsStruct(), false)
t.Assert(g.NewVar(nil).IsStruct(), false)
t.Assert(g.NewVar(g.Map{}).IsStruct(), false)
t.Assert(g.NewVar(g.Slice{}).IsStruct(), false)
})
gtest.C(t, func(t *gtest.T) {
t.Assert(g.NewVar(1).IsStruct(), false)
t.Assert(g.NewVar(-1).IsStruct(), false)
t.Assert(g.NewVar(0.1).IsStruct(), false)
t.Assert(g.NewVar(float64(1)).IsStruct(), false)
t.Assert(g.NewVar(g.Map{"k": "v"}).IsStruct(), false)
t.Assert(g.NewVar(g.Slice{0}).IsStruct(), false)
})
gtest.C(t, func(t *gtest.T) {
a := &struct {
}{}
t.Assert(g.NewVar(a).IsStruct(), true)
t.Assert(g.NewVar(*a).IsStruct(), true)
t.Assert(g.NewVar(&a).IsStruct(), true)
})
}

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,10 +8,11 @@
package gdb
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/internal/cmdenv"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/os/gcmd"
"time"
"github.com/gogf/gf/container/gvar"
@ -42,6 +43,12 @@ type DB interface {
// Note that it is not recommended using the this function manually.
Open(config *ConfigNode) (*sql.DB, error)
// Ctx is a chaining function, which creates and returns a new DB that is a shallow copy
// of current DB object and with given context in it.
// Note that this returned DB object can be used only once, so do not assign it to
// a global or package variable for long using.
Ctx(ctx context.Context) DB
// ===========================================================================
// Query APIs.
// ===========================================================================
@ -137,6 +144,7 @@ type DB interface {
// Utility methods.
// ===========================================================================
GetCtx() context.Context
GetChars() (charLeft string, charRight string)
GetMaster(schema ...string) (*sql.DB, error)
GetSlave(schema ...string) (*sql.DB, error)
@ -154,28 +162,24 @@ type DB interface {
HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{})
// ===========================================================================
// Internal methods.
// Internal methods, for internal usage purpose, you do not need consider it.
// ===========================================================================
mappingAndFilterData(schema, table string, data map[string]interface{}, filter bool) (map[string]interface{}, error)
convertValue(fieldValue interface{}, fieldType string) interface{}
rowsToResult(rows *sql.Rows) (Result, error)
convertFieldValueToLocalValue(fieldValue interface{}, fieldType string) interface{}
convertRowsToResult(rows *sql.Rows) (Result, error)
}
// Core is the base struct for database management.
type Core struct {
DB DB // DB interface object.
group string // Configuration group name.
debug *gtype.Bool // Enable debug mode for the database.
cache *gcache.Cache // Cache manager, SQL result cache only.
schema *gtype.String // Custom schema for this object.
dryrun *gtype.Bool // Dry run.
prefix string // Table prefix.
logger *glog.Logger // Logger.
config *ConfigNode // Current config node.
maxIdleConnCount int // Max idle connection count.
maxOpenConnCount int // Max open connection count.
maxConnLifetime time.Duration // Max TTL for a connection.
DB DB // DB interface object.
group string // Configuration group name.
debug *gtype.Bool // Enable debug mode for the database, which can be changed in runtime.
cache *gcache.Cache // Cache manager, SQL result cache only.
schema *gtype.String // Custom schema for this object.
logger *glog.Logger // Logger.
config *ConfigNode // Current config node.
ctx context.Context // Context for chaining operation only.
}
// Driver is the interface for integrating sql drivers into package gdb.
@ -214,22 +218,19 @@ type Link interface {
Prepare(sql string) (*sql.Stmt, error)
}
// Counter is the type for update count.
type Counter struct {
Field string
Value float64
}
type (
// Value is the field value type.
Value = *gvar.Var
// Record is the row record of the table.
Record map[string]Value
// Result is the row record array.
Result []Record
// Map is alias of map[string]interface{},
// which is the most common usage map type.
Map = map[string]interface{}
// List is type of map array.
List = []Map
Raw string // Raw is a raw sql that will not be treated as argument but as a direct sql part.
Value = *gvar.Var // Value is the field value type.
Record map[string]Value // Record is the row record of the table.
Result []Record // Result is the row record array.
Map = map[string]interface{} // Map is alias of map[string]interface{}, which is the most common usage map type.
List = []Map // List is type of map array.
)
const (
@ -237,10 +238,10 @@ const (
insertOptionReplace = 1
insertOptionSave = 2
insertOptionIgnore = 3
defaultBatchNumber = 10 // Per count for batch insert/replace/save.
defaultMaxIdleConnCount = 10 // Max idle connection count in pool.
defaultMaxOpenConnCount = 100 // Max open connection count in pool.
defaultMaxConnLifeTime = 30 // Max life time for per connection in pool in seconds.
defaultBatchNumber = 10 // Per count for batch insert/replace/save.
defaultMaxIdleConnCount = 10 // Max idle connection count in pool.
defaultMaxOpenConnCount = 100 // Max open connection count in pool.
defaultMaxConnLifeTime = 30 * time.Second // Max life time for per connection in pool in seconds.
)
var (
@ -265,7 +266,7 @@ var (
// regularFieldNameRegPattern is the regular expression pattern for a string
// which is a regular field name of table.
regularFieldNameRegPattern = `^[\w\.\-]+$`
regularFieldNameRegPattern = `^[\w\.\-\_]+$`
// internalCache is the memory cache for internal usage.
internalCache = gcache.New()
@ -277,7 +278,7 @@ var (
func init() {
// allDryRun is initialized from environment or command options.
allDryRun = cmdenv.Get("gf.gdb.dryrun", false).Bool()
allDryRun = gcmd.GetWithEnv("gf.gdb.dryrun", false).Bool()
}
// Register registers custom database driver to gdb.
@ -288,7 +289,7 @@ func Register(name string, driver Driver) error {
// New creates and returns an ORM object with global configurations.
// The parameter <name> specifies the configuration group name,
// which is DEFAULT_GROUP_NAME in default.
// which is DefaultGroupName in default.
func New(group ...string) (db DB, err error) {
groupName := configs.group
if len(group) > 0 && group[0] != "" {
@ -298,22 +299,17 @@ func New(group ...string) (db DB, err error) {
defer configs.RUnlock()
if len(configs.config) < 1 {
return nil, errors.New("empty database configuration")
return nil, gerror.New("empty database configuration")
}
if _, ok := configs.config[groupName]; ok {
if node, err := getConfigNodeByGroup(groupName, true); err == nil {
c := &Core{
group: groupName,
debug: gtype.NewBool(),
cache: gcache.New(),
schema: gtype.NewString(),
dryrun: gtype.NewBool(),
logger: glog.New(),
prefix: node.Prefix,
config: node,
maxIdleConnCount: defaultMaxIdleConnCount,
maxOpenConnCount: defaultMaxOpenConnCount,
maxConnLifetime: defaultMaxConnLifeTime, // Default max connection life time if user does not configure.
group: groupName,
debug: gtype.NewBool(),
cache: gcache.New(),
schema: gtype.NewString(),
logger: glog.New(),
config: node,
}
if v, ok := driverMap[node.Type]; ok {
c.DB, err = v.New(c, node)
@ -322,19 +318,19 @@ func New(group ...string) (db DB, err error) {
}
return c.DB, nil
} else {
return nil, errors.New(fmt.Sprintf(`unsupported database type "%s"`, node.Type))
return nil, gerror.New(fmt.Sprintf(`unsupported database type "%s"`, node.Type))
}
} else {
return nil, err
}
} else {
return nil, errors.New(fmt.Sprintf(`database configuration node "%s" is not found`, groupName))
return nil, gerror.New(fmt.Sprintf(`database configuration node "%s" is not found`, groupName))
}
}
// Instance returns an instance for DB operations.
// The parameter <name> specifies the configuration group name,
// which is DEFAULT_GROUP_NAME in default.
// which is DefaultGroupName in default.
func Instance(name ...string) (db DB, err error) {
group := configs.group
if len(name) > 0 && name[0] != "" {
@ -368,7 +364,7 @@ func getConfigNodeByGroup(group string, master bool) (*ConfigNode, error) {
}
}
if len(masterList) < 1 {
return nil, errors.New("at least one master node configuration's need to make sense")
return nil, gerror.New("at least one master node configuration's need to make sense")
}
if len(slaveList) < 1 {
slaveList = masterList
@ -379,7 +375,7 @@ func getConfigNodeByGroup(group string, master bool) (*ConfigNode, error) {
return getConfigNodeByWeight(slaveList), nil
}
} else {
return nil, errors.New(fmt.Sprintf("empty database configuration for item name '%s'", group))
return nil, gerror.New(fmt.Sprintf("empty database configuration for item name '%s'", group))
}
}
@ -452,22 +448,26 @@ func (c *Core) getSqlDb(master bool, schema ...string) (sqlDb *sql.DB, err error
intlog.Printf("DB open failed: %v, %+v", err, node)
return nil, err
}
if c.maxIdleConnCount > 0 {
sqlDb.SetMaxIdleConns(c.maxIdleConnCount)
} else if node.MaxIdleConnCount > 0 {
sqlDb.SetMaxIdleConns(node.MaxIdleConnCount)
if c.config.MaxIdleConnCount > 0 {
sqlDb.SetMaxIdleConns(c.config.MaxIdleConnCount)
} else {
sqlDb.SetMaxIdleConns(defaultMaxIdleConnCount)
}
if c.maxOpenConnCount > 0 {
sqlDb.SetMaxOpenConns(c.maxOpenConnCount)
} else if node.MaxOpenConnCount > 0 {
sqlDb.SetMaxOpenConns(node.MaxOpenConnCount)
if c.config.MaxOpenConnCount > 0 {
sqlDb.SetMaxOpenConns(c.config.MaxOpenConnCount)
} else {
sqlDb.SetMaxOpenConns(defaultMaxOpenConnCount)
}
if c.maxConnLifetime > 0 {
sqlDb.SetConnMaxLifetime(c.maxConnLifetime * time.Second)
} else if node.MaxConnLifetime > 0 {
sqlDb.SetConnMaxLifetime(node.MaxConnLifetime * time.Second)
if c.config.MaxConnLifetime > 0 {
// Automatically checks whether MaxConnLifetime is configured using string like: "30s", "60s", etc.
// Or else it is configured just using number, which means value in seconds.
if c.config.MaxConnLifetime > time.Second {
sqlDb.SetConnMaxLifetime(c.config.MaxConnLifetime)
} else {
sqlDb.SetConnMaxLifetime(c.config.MaxConnLifetime * time.Second)
}
} else {
sqlDb.SetConnMaxLifetime(defaultMaxConnLifeTime)
}
return sqlDb, nil
}, 0)

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,9 +8,10 @@
package gdb
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/text/gstr"
"reflect"
"strings"
@ -23,6 +24,35 @@ import (
"github.com/gogf/gf/util/gconv"
)
// Ctx is a chaining function, which creates and returns a new DB that is a shallow copy
// of current DB object and with given context in it.
// Note that this returned DB object can be used only once, so do not assign it to
// a global or package variable for long using.
func (c *Core) Ctx(ctx context.Context) DB {
if ctx == nil {
return c.DB
}
var (
err error
newCore = &Core{}
configNode = c.DB.GetConfig()
)
*newCore = *c
newCore.ctx = ctx
newCore.DB, err = driverMap[configNode.Type].New(newCore, configNode)
// Seldom error, just log it.
if err != nil {
c.DB.GetLogger().Ctx(ctx).Error(err)
}
return newCore.DB
}
// GetCtx returns the context for current DB.
// Note that it might be nil.
func (c *Core) GetCtx() context.Context {
return c.ctx
}
// Master creates and returns a connection from master node if master-slave configured.
// It returns the default connection if master-slave not configured.
func (c *Core) Master() (*sql.DB, error) {
@ -164,7 +194,7 @@ func (c *Core) DoGetAll(link Link, sql string, args ...interface{}) (result Resu
return nil, err
}
defer rows.Close()
return c.DB.rowsToResult(rows)
return c.DB.convertRowsToResult(rows)
}
// GetOne queries and returns one record from database.
@ -437,10 +467,10 @@ func (c *Core) DoInsert(link Link, table string, data interface{}, option int, b
case reflect.Map:
dataMap = ConvertDataForTableRecord(data)
default:
return result, errors.New(fmt.Sprint("unsupported data type:", reflectKind))
return result, gerror.New(fmt.Sprint("unsupported data type:", reflectKind))
}
if len(dataMap) == 0 {
return nil, errors.New("data cannot be empty")
return nil, gerror.New("data cannot be empty")
}
var (
charL, charR = c.DB.GetChars()
@ -449,8 +479,12 @@ func (c *Core) DoInsert(link Link, table string, data interface{}, option int, b
)
for k, v := range dataMap {
fields = append(fields, charL+k+charR)
values = append(values, "?")
params = append(params, v)
if s, ok := v.(Raw); ok {
values = append(values, gconv.String(s))
} else {
values = append(values, "?")
params = append(params, v)
}
}
if option == insertOptionSave {
for k, _ := range dataMap {
@ -573,11 +607,11 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i
listMap = List{ConvertDataForTableRecord(value)}
}
default:
return result, errors.New(fmt.Sprint("unsupported list type:", kind))
return result, gerror.New(fmt.Sprint("unsupported list type:", kind))
}
}
if len(listMap) < 1 {
return result, errors.New("data list cannot be empty")
return result, gerror.New("data list cannot be empty")
}
if link == nil {
if link, err = c.DB.Master(); err != nil {
@ -585,19 +619,16 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i
}
}
// Handle the field names and place holders.
holders := []string(nil)
for k, _ := range listMap[0] {
keys = append(keys, k)
holders = append(holders, "?")
}
// Prepare the batch result pointer.
var (
charL, charR = c.DB.GetChars()
batchResult = new(SqlResult)
keysStr = charL + strings.Join(keys, charR+","+charL) + charR
valueHolderStr = "(" + strings.Join(holders, ",") + ")"
operation = GetInsertOperationByOption(option)
updateStr = ""
charL, charR = c.DB.GetChars()
batchResult = new(SqlResult)
keysStr = charL + strings.Join(keys, charR+","+charL) + charR
operation = GetInsertOperationByOption(option)
updateStr = ""
)
if option == insertOptionSave {
for _, k := range keys {
@ -621,23 +652,30 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i
if len(batch) > 0 && batch[0] > 0 {
batchNum = batch[0]
}
listMapLen := len(listMap)
var (
listMapLen = len(listMap)
valueHolder = make([]string, 0)
)
for i := 0; i < listMapLen; i++ {
values = values[:0]
// Note that the map type is unordered,
// so it should use slice+key to retrieve the value.
for _, k := range keys {
params = append(params, listMap[i][k])
if s, ok := listMap[i][k].(Raw); ok {
values = append(values, gconv.String(s))
} else {
values = append(values, "?")
params = append(params, listMap[i][k])
}
}
values = append(values, valueHolderStr)
valueHolder = append(valueHolder, "("+gstr.Join(values, ",")+")")
if len(values) == batchNum || (i == listMapLen-1 && len(values) > 0) {
r, err := c.DB.DoExec(
link,
fmt.Sprintf(
"%s INTO %s(%s) VALUES%s %s",
operation,
table,
keysStr,
strings.Join(values, ","),
operation, table, keysStr,
gstr.Join(valueHolder, ","),
updateStr,
),
params...,
@ -652,7 +690,7 @@ func (c *Core) DoBatchInsert(link Link, table string, list interface{}, option i
batchResult.affected += n
}
params = params[:0]
values = values[:0]
valueHolder = valueHolder[:0]
}
}
return batchResult, nil
@ -699,15 +737,35 @@ func (c *Core) DoUpdate(link Link, table string, data interface{}, condition str
dataMap = ConvertDataForTableRecord(data)
)
for k, v := range dataMap {
fields = append(fields, c.DB.QuoteWord(k)+"=?")
params = append(params, v)
switch value := v.(type) {
case *Counter:
if value.Value != 0 {
column := c.DB.QuoteWord(value.Field)
fields = append(fields, fmt.Sprintf("%s=%s+?", column, column))
params = append(params, value.Value)
}
case Counter:
if value.Value != 0 {
column := c.DB.QuoteWord(value.Field)
fields = append(fields, fmt.Sprintf("%s=%s+?", column, column))
params = append(params, value.Value)
}
default:
if s, ok := v.(Raw); ok {
fields = append(fields, c.DB.QuoteWord(k)+"="+gconv.String(s))
} else {
fields = append(fields, c.DB.QuoteWord(k)+"=?")
params = append(params, v)
}
}
}
updates = strings.Join(fields, ",")
default:
updates = gconv.String(data)
}
if len(updates) == 0 {
return nil, errors.New("data cannot be empty")
return nil, gerror.New("data cannot be empty")
}
if len(params) > 0 {
args = append(params, args...)
@ -752,8 +810,8 @@ func (c *Core) DoDelete(link Link, table string, condition string, args ...inter
return c.DB.DoExec(link, fmt.Sprintf("DELETE FROM %s%s", table, condition), args...)
}
// rowsToResult converts underlying data record type sql.Rows to Result type.
func (c *Core) rowsToResult(rows *sql.Rows) (Result, error) {
// convertRowsToResult converts underlying data record type sql.Rows to Result type.
func (c *Core) convertRowsToResult(rows *sql.Rows) (Result, error) {
if !rows.Next() {
return nil, nil
}
@ -785,7 +843,7 @@ func (c *Core) rowsToResult(rows *sql.Rows) (Result, error) {
if value == nil {
row[columnNames[i]] = gvar.New(nil)
} else {
row[columnNames[i]] = gvar.New(c.DB.convertValue(value, columnTypes[i]))
row[columnNames[i]] = gvar.New(c.DB.convertFieldValueToLocalValue(value, columnTypes[i]))
}
}
records = append(records, row)
@ -806,14 +864,14 @@ func (c *Core) MarshalJSON() ([]byte, error) {
}
// writeSqlToLogger outputs the sql object to logger.
// It is enabled when configuration "debug" is true.
// It is enabled only if configuration "debug" is true.
func (c *Core) writeSqlToLogger(v *Sql) {
s := fmt.Sprintf("[%3d ms] [%s] %s", v.End-v.Start, v.Group, v.Format)
if v.Error != nil {
s += "\nError: " + v.Error.Error()
c.logger.Error(s)
c.logger.Ctx(c.DB.GetCtx()).Error(s)
} else {
c.logger.Debug(s)
c.logger.Ctx(c.DB.GetCtx()).Debug(s)
}
}

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -16,7 +16,8 @@ import (
)
const (
DEFAULT_GROUP_NAME = "default" // Default group name.
DEFAULT_GROUP_NAME = "default" // Deprecated, use DefaultGroupName instead.
DefaultGroupName = "default" // Default group name.
)
// Config is the configuration management object.
@ -27,25 +28,26 @@ type ConfigGroup []ConfigNode
// ConfigNode is configuration for one node.
type ConfigNode struct {
Host string // Host of server, ip or domain like: 127.0.0.1, localhost
Port string // Port, it's commonly 3306.
User string // Authentication username.
Pass string // Authentication password.
Name string // Default used database name.
Type string // Database type: mysql, sqlite, mssql, pgsql, oracle.
Role string // (Optional, "master" in default) Node role, used for master-slave mode: master, slave.
Debug bool // (Optional) Debug mode enables debug information logging and output.
Prefix string // (Optional) Table prefix.
DryRun bool // (Optional) Dry run, which does SELECT but no INSERT/UPDATE/DELETE statements.
Weight int // (Optional) Weight for load balance calculating, it's useless if there's just one node.
Charset string // (Optional, "utf8mb4" in default) Custom charset when operating on database.
CreatedAt string // (Optional) The filed name of table for automatic-filled created datetime.
UpdatedAt string // (Optional) The filed name of table for automatic-filled updated datetime.
DeletedAt string // (Optional) The filed name of table for automatic-filled updated datetime.
LinkInfo string `json:"link"` // (Optional) Custom link information, when it is used, configuration Host/Port/User/Pass/Name are ignored.
MaxIdleConnCount int `json:"maxidle"` // (Optional) Max idle connection configuration for underlying connection pool.
MaxOpenConnCount int `json:"maxopen"` // (Optional) Max open connection configuration for underlying connection pool.
MaxConnLifetime time.Duration `json:"maxlifetime"` // (Optional) Max connection TTL configuration for underlying connection pool.
Host string // Host of server, ip or domain like: 127.0.0.1, localhost
Port string // Port, it's commonly 3306.
User string // Authentication username.
Pass string // Authentication password.
Name string // Default used database name.
Type string // Database type: mysql, sqlite, mssql, pgsql, oracle.
Role string // (Optional, "master" in default) Node role, used for master-slave mode: master, slave.
Debug bool // (Optional) Debug mode enables debug information logging and output.
Prefix string // (Optional) Table prefix.
DryRun bool // (Optional) Dry run, which does SELECT but no INSERT/UPDATE/DELETE statements.
Weight int // (Optional) Weight for load balance calculating, it's useless if there's just one node.
Charset string // (Optional, "utf8mb4" in default) Custom charset when operating on database.
LinkInfo string `json:"link"` // (Optional) Custom link information, when it is used, configuration Host/Port/User/Pass/Name are ignored.
MaxIdleConnCount int `json:"maxidle"` // (Optional) Max idle connection configuration for underlying connection pool.
MaxOpenConnCount int `json:"maxopen"` // (Optional) Max open connection configuration for underlying connection pool.
MaxConnLifetime time.Duration `json:"maxlifetime"` // (Optional) Max connection TTL configuration for underlying connection pool.
CreatedAt string // (Optional) The filed name of table for automatic-filled created datetime.
UpdatedAt string // (Optional) The filed name of table for automatic-filled updated datetime.
DeletedAt string // (Optional) The filed name of table for automatic-filled updated datetime.
TimeMaintainDisabled bool // (Optional) Disable the automatic time maintaining feature.
}
// configs is internal used configuration object.
@ -57,7 +59,7 @@ var configs struct {
func init() {
configs.config = make(Config)
configs.group = DEFAULT_GROUP_NAME
configs.group = DefaultGroupName
}
// SetConfig sets the global configuration for package.
@ -87,12 +89,12 @@ func AddConfigNode(group string, node ConfigNode) {
// AddDefaultConfigNode adds one node configuration to configuration of default group.
func AddDefaultConfigNode(node ConfigNode) {
AddConfigNode(DEFAULT_GROUP_NAME, node)
AddConfigNode(DefaultGroupName, node)
}
// AddDefaultConfigGroup adds multiple node configurations to configuration of default group.
func AddDefaultConfigGroup(nodes ConfigGroup) {
SetConfigGroup(DEFAULT_GROUP_NAME, nodes)
SetConfigGroup(DefaultGroupName, nodes)
}
// GetConfig retrieves and returns the configuration of given group.
@ -138,18 +140,18 @@ func (c *Core) GetLogger() *glog.Logger {
// SetMaxIdleConnCount sets the max idle connection count for underlying connection pool.
func (c *Core) SetMaxIdleConnCount(n int) {
c.maxIdleConnCount = n
c.config.MaxIdleConnCount = n
}
// SetMaxOpenConnCount sets the max open connection count for underlying connection pool.
func (c *Core) SetMaxOpenConnCount(n int) {
c.maxOpenConnCount = n
c.config.MaxOpenConnCount = n
}
// SetMaxConnLifetime sets the connection TTL for underlying connection pool.
// If parameter <d> <= 0, it means the connection never expires.
func (c *Core) SetMaxConnLifetime(d time.Duration) {
c.maxConnLifetime = d
c.config.MaxConnLifetime = d
}
// String returns the node as string.
@ -165,6 +167,11 @@ func (node *ConfigNode) String() string {
)
}
// GetConfig returns the current used node configuration.
func (c *Core) GetConfig() *ConfigNode {
return c.config
}
// SetDebug enables/disables the debug mode.
func (c *Core) SetDebug(debug bool) {
c.debug.Set(debug)
@ -180,28 +187,27 @@ func (c *Core) GetCache() *gcache.Cache {
return c.cache
}
// GetPrefix returns the table prefix string configured.
func (c *Core) GetPrefix() string {
return c.prefix
}
// GetGroup returns the group string configured.
func (c *Core) GetGroup() string {
return c.group
}
// SetDryRun enables/disables the DryRun feature.
func (c *Core) SetDryRun(dryrun bool) {
c.dryrun.Set(dryrun)
// Deprecated, use GetConfig instead.
func (c *Core) SetDryRun(enabled bool) {
c.config.DryRun = enabled
}
// GetDryRun returns the DryRun value.
// Deprecated, use GetConfig instead.
func (c *Core) GetDryRun() bool {
if allDryRun {
// Globally set.
return true
}
return c.dryrun.Val()
return c.config.DryRun
}
// GetPrefix returns the table prefix string configured.
// Deprecated, use GetConfig instead.
func (c *Core) GetPrefix() string {
return c.config.Prefix
}
// SetSchema changes the schema for this database connection object.
@ -215,8 +221,3 @@ func (c *Core) SetSchema(schema string) {
func (c *Core) GetSchema() string {
return c.schema.Val()
}
// GetConfig returns the current used node configuration.
func (c *Core) GetConfig() *ConfigNode {
return c.config
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -22,9 +22,9 @@ import (
"github.com/gogf/gf/util/gconv"
)
// convertValue automatically checks and converts field value from database type
// convertFieldValueToLocalValue automatically checks and converts field value from database type
// to golang variable type.
func (c *Core) convertValue(fieldValue interface{}, fieldType string) interface{} {
func (c *Core) convertFieldValueToLocalValue(fieldValue interface{}, fieldType string) interface{} {
// If there's no type retrieved, it returns the <fieldValue> directly
// to use its original data type, as <fieldValue> is type of interface{}.
if fieldType == "" {
@ -146,7 +146,8 @@ func (c *Core) convertValue(fieldValue interface{}, fieldType string) interface{
}
}
// filterFields removes all key-value pairs which are not the field of given table.
// mappingAndFilterData automatically mappings the map key to table field and removes
// all key-value pairs that are not the field of given table.
func (c *Core) mappingAndFilterData(schema, table string, data map[string]interface{}, filter bool) (map[string]interface{}, error) {
if fieldsMap, err := c.DB.TableFields(table, schema); err == nil {
fieldsKeyMap := make(map[string]interface{}, len(fieldsMap))

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -13,8 +13,8 @@ package gdb
import (
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"strconv"
"strings"
@ -192,14 +192,14 @@ func (d *DriverMssql) TableFields(table string, schema ...string) (fields map[st
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, errors.New("function TableFields supports only single table operations")
return nil, gerror.New("function TableFields supports only single table operations")
}
checkSchema := d.DB.GetSchema()
if len(schema) > 0 && schema[0] != "" {
checkSchema = schema[0]
}
v, _ := internalCache.GetOrSetFunc(
fmt.Sprintf(`mssql_table_fields_%s_%s`, table, checkSchema),
fmt.Sprintf(`mssql_table_fields_%s_%s@group:%s`, table, checkSchema, d.GetGroup()),
func() (interface{}, error) {
var (
result Result

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,8 +8,8 @@ package gdb
import (
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/text/gstr"
@ -31,6 +31,7 @@ func (d *DriverMysql) New(core *Core, node *ConfigNode) (DB, error) {
}
// Open creates and returns a underlying sql.DB object for mysql.
// Note that it converts time.Time argument to local timezone in default.
func (d *DriverMysql) Open(config *ConfigNode) (*sql.DB, error) {
var source string
if config.LinkInfo != "" {
@ -41,7 +42,7 @@ func (d *DriverMysql) Open(config *ConfigNode) (*sql.DB, error) {
}
} else {
source = fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true&loc=Local",
"%s:%s@tcp(%s:%s)/%s?charset=%s&multiStatements=true&parseTime=true",
config.User, config.Pass, config.Host, config.Port, config.Name, config.Charset,
)
}
@ -96,14 +97,14 @@ func (d *DriverMysql) TableFields(table string, schema ...string) (fields map[st
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, errors.New("function TableFields supports only single table operations")
return nil, gerror.New("function TableFields supports only single table operations")
}
checkSchema := d.schema.Val()
if len(schema) > 0 && schema[0] != "" {
checkSchema = schema[0]
}
v, _ := internalCache.GetOrSetFunc(
fmt.Sprintf(`mysql_table_fields_%s_%s`, table, checkSchema),
fmt.Sprintf(`mysql_table_fields_%s_%s@group:%s`, table, checkSchema, d.GetGroup()),
func() (interface{}, error) {
var (
result Result

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -13,10 +13,11 @@ package gdb
import (
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"reflect"
"strconv"
"strings"
@ -72,6 +73,15 @@ func (d *DriverOracle) HandleSqlBeforeCommit(link Link, sql string, args []inter
return fmt.Sprintf(":v%d", index)
})
str, _ = gregex.ReplaceString("\"", "", str)
// Change time string argument wrapping with TO_DATE function.
for i, v := range args {
if reflect.TypeOf(v).Kind() == reflect.String {
valueStr := gconv.String(v)
if gregex.IsMatchString(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`, valueStr) {
args[i] = fmt.Sprintf(`TO_DATE('%s','yyyy-MM-dd HH:MI:SS')`, valueStr)
}
}
}
return d.parseSql(str), args
}
@ -152,14 +162,14 @@ func (d *DriverOracle) TableFields(table string, schema ...string) (fields map[s
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, errors.New("function TableFields supports only single table operations")
return nil, gerror.New("function TableFields supports only single table operations")
}
checkSchema := d.DB.GetSchema()
if len(schema) > 0 && schema[0] != "" {
checkSchema = schema[0]
}
v, _ := internalCache.GetOrSetFunc(
fmt.Sprintf(`oracle_table_fields_%s_%s`, table, checkSchema),
fmt.Sprintf(`oracle_table_fields_%s_%s@group:%s`, table, checkSchema, d.GetGroup()),
func() (interface{}, error) {
result := (Result)(nil)
structureSql := fmt.Sprintf(`
@ -242,7 +252,7 @@ func (d *DriverOracle) DoInsert(link Link, table string, data interface{}, optio
case reflect.Struct:
dataMap = ConvertDataForTableRecord(data)
default:
return result, errors.New(fmt.Sprint("unsupported data type:", kind))
return result, gerror.New(fmt.Sprint("unsupported data type:", kind))
}
indexs := make([]string, 0)
@ -367,12 +377,12 @@ func (d *DriverOracle) DoBatchInsert(link Link, table string, list interface{},
case reflect.Struct:
listMap = List{Map(ConvertDataForTableRecord(list))}
default:
return result, errors.New(fmt.Sprint("unsupported list type:", kind))
return result, gerror.New(fmt.Sprint("unsupported list type:", kind))
}
}
// 判断长度
if len(listMap) < 1 {
return result, errors.New("empty data list")
return result, gerror.New("empty data list")
}
if link == nil {
if link, err = d.DB.Master(); err != nil {

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -13,8 +13,8 @@ package gdb
import (
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/text/gstr"
"strings"
@ -100,7 +100,7 @@ func (d *DriverPgsql) TableFields(table string, schema ...string) (fields map[st
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, errors.New("function TableFields supports only single table operations")
return nil, gerror.New("function TableFields supports only single table operations")
}
table, _ = gregex.ReplaceString("\"", "", table)
checkSchema := d.DB.GetSchema()
@ -108,7 +108,7 @@ func (d *DriverPgsql) TableFields(table string, schema ...string) (fields map[st
checkSchema = schema[0]
}
v, _ := internalCache.GetOrSetFunc(
fmt.Sprintf(`pgsql_table_fields_%s_%s`, table, checkSchema),
fmt.Sprintf(`pgsql_table_fields_%s_%s@group:%s`, table, checkSchema, d.GetGroup()),
func() (interface{}, error) {
var (
result Result

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -12,8 +12,8 @@ package gdb
import (
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/text/gstr"
@ -36,15 +36,14 @@ func (d *DriverSqlite) New(core *Core, node *ConfigNode) (DB, error) {
// Open creates and returns a underlying sql.DB object for sqlite.
func (d *DriverSqlite) Open(config *ConfigNode) (*sql.DB, error) {
var source string
var err error
if config.LinkInfo != "" {
source = config.LinkInfo
} else {
source = config.Name
}
source, err = gfile.Search(source)
if err != nil {
return nil, err
// It searches the source file to locate its absolute path..
if absolutePath, _ := gfile.Search(source); absolutePath != "" {
source = absolutePath
}
intlog.Printf("Open: %s", source)
if db, err := sql.Open("sqlite3", source); err == nil {
@ -92,14 +91,14 @@ func (d *DriverSqlite) TableFields(table string, schema ...string) (fields map[s
charL, charR := d.GetChars()
table = gstr.Trim(table, charL+charR)
if gstr.Contains(table, " ") {
return nil, errors.New("function TableFields supports only single table operations")
return nil, gerror.New("function TableFields supports only single table operations")
}
checkSchema := d.DB.GetSchema()
if len(schema) > 0 && schema[0] != "" {
checkSchema = schema[0]
}
v, _ := internalCache.GetOrSetFunc(
fmt.Sprintf(`sqlite_table_fields_%s_%s`, table, checkSchema),
fmt.Sprintf(`sqlite_table_fields_%s_%s@group:%s`, table, checkSchema, d.GetGroup()),
func() (interface{}, error) {
var (
result Result

View File

@ -1,4 +1,4 @@
// Copyright 2017-2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,8 +8,8 @@ package gdb
import (
"bytes"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/internal/empty"
"github.com/gogf/gf/internal/json"
"github.com/gogf/gf/internal/utils"
@ -127,6 +127,8 @@ func ConvertDataForTableRecord(value interface{}) map[string]interface{} {
switch v.(type) {
case time.Time, *time.Time, gtime.Time, *gtime.Time:
continue
case Counter, *Counter:
continue
default:
// Use string conversion in default.
if s, ok := v.(apiString); ok {
@ -499,7 +501,11 @@ func formatWhereInterfaces(db DB, where []interface{}, buffer *bytes.Buffer, new
} else {
buffer.WriteString(db.QuoteWord(str) + "=?")
}
newArgs = append(newArgs, where[i+1])
if s, ok := where[i+1].(Raw); ok {
buffer.WriteString(gconv.String(s))
} else {
newArgs = append(newArgs, where[i+1])
}
}
return newArgs
}
@ -567,14 +573,18 @@ func formatWhereKeyValue(db DB, buffer *bytes.Buffer, newArgs []interface{}, key
} else {
buffer.WriteString(quotedKey)
}
newArgs = append(newArgs, value)
if s, ok := value.(Raw); ok {
buffer.WriteString(gconv.String(s))
} else {
newArgs = append(newArgs, value)
}
}
}
return newArgs
}
// handleArguments is a nice function which handles the query and its arguments before committing to
// underlying driver.
// handleArguments is an important function, which handles the sql and all its arguments
// before committing them to underlying driver.
func handleArguments(sql string, args []interface{}) (newSql string, newArgs []interface{}) {
newSql = sql
// insertHolderCount is used to calculate the inserting position for the '?' holder.
@ -583,14 +593,14 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
if len(args) > 0 {
for index, arg := range args {
var (
rv = reflect.ValueOf(arg)
kind = rv.Kind()
reflectValue = reflect.ValueOf(arg)
reflectKind = reflectValue.Kind()
)
if kind == reflect.Ptr {
rv = rv.Elem()
kind = rv.Kind()
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
switch kind {
switch reflectKind {
case reflect.Slice, reflect.Array:
// It does not split the type of []byte.
// Eg: table.Where("name = ?", []byte("john"))
@ -599,7 +609,7 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
continue
}
if rv.Len() == 0 {
if reflectValue.Len() == 0 {
// Empty slice argument, it converts the sql to a false sql.
// Eg:
// Query("select * from xxx where id in(?)", g.Slice{}) -> select * from xxx where 0=1
@ -613,15 +623,15 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
}
}
} else {
for i := 0; i < rv.Len(); i++ {
newArgs = append(newArgs, rv.Index(i).Interface())
for i := 0; i < reflectValue.Len(); i++ {
newArgs = append(newArgs, reflectValue.Index(i).Interface())
}
}
// If the '?' holder count equals the length of the slice,
// it does not implement the arguments splitting logic.
// Eg: db.Query("SELECT ?+?", g.Slice{1, 2})
if len(args) == 1 && gstr.Count(newSql, "?") == rv.Len() {
if len(args) == 1 && gstr.Count(newSql, "?") == reflectValue.Len() {
break
}
// counter is used to finding the inserting position for the '?' holder.
@ -636,36 +646,37 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
counter++
if counter == index+insertHolderCount+1 {
replaced = true
insertHolderCount += rv.Len() - 1
return "?" + strings.Repeat(",?", rv.Len()-1)
insertHolderCount += reflectValue.Len() - 1
return "?" + strings.Repeat(",?", reflectValue.Len()-1)
}
return s
})
// Special struct handling.
case reflect.Struct:
// The underlying driver supports time.Time/*time.Time types.
if _, ok := arg.(time.Time); ok {
newArgs = append(newArgs, arg)
continue
}
if _, ok := arg.(*time.Time); ok {
newArgs = append(newArgs, arg)
continue
}
switch v := arg.(type) {
// The underlying driver supports time.Time/*time.Time types.
case time.Time, *time.Time:
newArgs = append(newArgs, arg)
continue
// Special handling for gtime.Time.
// Special handling for gtime.Time/*gtime.Time.
//
// DO NOT use its underlying gtime.Time.Time as its argument,
// because the std time.Time will be converted to certain timezone
// according to underlying driver. And the underlying driver also
// converts the time.Time to string automatically as the following does.
case gtime.Time:
newArgs = append(newArgs, v.String())
continue
case *gtime.Time:
newArgs = append(newArgs, v.String())
continue
default:
// It converts the struct to string in default
// if it implements the String interface.
// if it has implemented the String interface.
if v, ok := arg.(apiString); ok {
newArgs = append(newArgs, v.String())
continue
@ -684,7 +695,7 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
// formatError customizes and returns the SQL error.
func formatError(err error, sql string, args ...interface{}) error {
if err != nil && err != ErrNoRows {
return errors.New(fmt.Sprintf("%s, %s\n", err.Error(), FormatSqlWithArgs(sql, args)))
return gerror.New(fmt.Sprintf("%s, %s\n", err.Error(), FormatSqlWithArgs(sql, args)))
}
return err
}
@ -716,7 +727,7 @@ func FormatSqlWithArgs(sql string, args []interface{}) string {
return `'` + gstr.QuoteMeta(gconv.String(args[index]), `'`) + `'`
case reflect.Struct:
if t, ok := args[index].(time.Time); ok {
return `'` + gtime.NewFromTime(t).String() + `'`
return `'` + t.Format(`2006-01-02 15:04:05`) + `'`
}
return `'` + gstr.QuoteMeta(gconv.String(args[index]), `'`) + `'`
}
@ -727,9 +738,9 @@ func FormatSqlWithArgs(sql string, args []interface{}) string {
return newQuery
}
// mapToStruct maps the <data> to given struct.
// convertMapToStruct maps the <data> to given struct.
// Note that the given parameter <pointer> should be a pointer to s struct.
func mapToStruct(data map[string]interface{}, pointer interface{}) error {
func convertMapToStruct(data map[string]interface{}, pointer interface{}) error {
tagNameMap, err := structs.TagMapName(pointer, []string{ORM_TAG_FOR_STRUCT})
if err != nil {
return err

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -7,6 +7,7 @@
package gdb
import (
"context"
"fmt"
"github.com/gogf/gf/text/gregex"
"time"
@ -52,13 +53,14 @@ type whereHolder struct {
}
const (
gLINK_TYPE_MASTER = 1
gLINK_TYPE_SLAVE = 2
gWHERE_HOLDER_WHERE = 1
gWHERE_HOLDER_AND = 2
gWHERE_HOLDER_OR = 3
OPTION_OMITEMPTY = 1 << iota
OPTION_ALLOWEMPTY
OPTION_OMITEMPTY = 1
OPTION_ALLOWEMPTY = 2
linkTypeMaster = 1
linkTypeSlave = 2
whereHolderWhere = 1
whereHolderAnd = 2
whereHolderOr = 3
)
// Table creates and returns a new ORM model from given schema.
@ -112,6 +114,16 @@ func (tx *TX) Model(table ...string) *Model {
return tx.Table(table...)
}
// Ctx sets the context for current operation.
func (m *Model) Ctx(ctx context.Context) *Model {
if ctx == nil {
return m
}
model := m.getModel()
model.db = model.db.Ctx(ctx)
return model
}
// As sets an alias name for current table.
func (m *Model) As(as string) *Model {
if m.tables != "" {
@ -141,6 +153,7 @@ func (m *Model) DB(db DB) *Model {
// TX sets/changes the transaction for current operation.
func (m *Model) TX(tx *TX) *Model {
model := m.getModel()
model.db = tx.db
model.tx = tx
return model
}
@ -177,7 +190,7 @@ func (m *Model) Clone() *Model {
// Master marks the following operation on master node.
func (m *Model) Master() *Model {
model := m.getModel()
model.linkType = gLINK_TYPE_MASTER
model.linkType = linkTypeMaster
return model
}
@ -185,7 +198,7 @@ func (m *Model) Master() *Model {
// Note that it makes sense only if there's any slave node configured.
func (m *Model) Slave() *Model {
model := m.getModel()
model.linkType = gLINK_TYPE_SLAVE
model.linkType = linkTypeSlave
return model
}
@ -199,3 +212,10 @@ func (m *Model) Safe(safe ...bool) *Model {
}
return m
}
// Args sets custom arguments for model operation.
func (m *Model) Args(args ...interface{}) *Model {
model := m.getModel()
model.extraArgs = append(model.extraArgs, args)
return model
}

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -27,7 +27,7 @@ func (m *Model) Where(where interface{}, args ...interface{}) *Model {
model.whereHolder = make([]*whereHolder, 0)
}
model.whereHolder = append(model.whereHolder, &whereHolder{
operator: gWHERE_HOLDER_WHERE,
operator: whereHolderWhere,
where: where,
args: args,
})
@ -65,7 +65,7 @@ func (m *Model) And(where interface{}, args ...interface{}) *Model {
model.whereHolder = make([]*whereHolder, 0)
}
model.whereHolder = append(model.whereHolder, &whereHolder{
operator: gWHERE_HOLDER_AND,
operator: whereHolderAnd,
where: where,
args: args,
})
@ -79,7 +79,7 @@ func (m *Model) Or(where interface{}, args ...interface{}) *Model {
model.whereHolder = make([]*whereHolder, 0)
}
model.whereHolder = append(model.whereHolder, &whereHolder{
operator: gWHERE_HOLDER_OR,
operator: whereHolderOr,
where: where,
args: args,
})

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -33,17 +33,21 @@ func (m *Model) Fields(fieldNamesOrMapStruct ...interface{}) *Model {
return m
}
switch {
// String slice.
case length >= 2:
model := m.getModel()
model.fields = gstr.Join(m.mappingToTableFields(gconv.Strings(fieldNamesOrMapStruct)), ",")
model.fields = gstr.Join(m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct)), ",")
return model
// It need type asserting.
case length == 1:
model := m.getModel()
switch r := fieldNamesOrMapStruct[0].(type) {
case string:
model.fields = gstr.Join(m.mappingToTableFields([]string{r}), ",")
model.fields = gstr.Join(m.mappingAndFilterToTableFields([]string{r}), ",")
case []string:
model.fields = gstr.Join(m.mappingAndFilterToTableFields(r), ",")
default:
model.fields = gstr.Join(m.mappingToTableFields(gutil.Keys(r)), ",")
model.fields = gstr.Join(m.mappingAndFilterToTableFields(gutil.Keys(r)), ",")
}
return model
}
@ -61,14 +65,16 @@ func (m *Model) FieldsEx(fieldNamesOrMapStruct ...interface{}) *Model {
model := m.getModel()
switch {
case length >= 2:
model.fieldsEx = gstr.Join(m.mappingToTableFields(gconv.Strings(fieldNamesOrMapStruct)), ",")
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gconv.Strings(fieldNamesOrMapStruct)), ",")
return model
case length == 1:
switch r := fieldNamesOrMapStruct[0].(type) {
case string:
model.fieldsEx = gstr.Join(m.mappingToTableFields([]string{r}), ",")
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields([]string{r}), ",")
case []string:
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(r), ",")
default:
model.fieldsEx = gstr.Join(m.mappingToTableFields(gutil.Keys(r)), ",")
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(gutil.Keys(r)), ",")
}
return model
}

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,7 +8,7 @@ package gdb
import (
"database/sql"
"errors"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
@ -109,7 +109,7 @@ func (m *Model) Insert(data ...interface{}) (result sql.Result, err error) {
// see Model.Data.
func (m *Model) InsertIgnore(data ...interface{}) (result sql.Result, err error) {
if len(data) > 0 {
return m.Data(data...).Insert()
return m.Data(data...).InsertIgnore()
}
return m.doInsertWithOption(insertOptionIgnore, data...)
}
@ -145,7 +145,7 @@ func (m *Model) doInsertWithOption(option int, data ...interface{}) (result sql.
}
}()
if m.data == nil {
return nil, errors.New("inserting into table with empty data")
return nil, gerror.New("inserting into table with empty data")
}
var (
nowString = gtime.Now().String()
@ -207,5 +207,5 @@ func (m *Model) doInsertWithOption(option int, data ...interface{}) (result sql.
option,
)
}
return nil, errors.New("inserting into table with invalid data type")
return nil, gerror.New("inserting into table with invalid data type")
}

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -13,7 +13,7 @@ import (
// isSubQuery checks and returns whether given string a sub-query sql string.
func isSubQuery(s string) bool {
s = gstr.TrimLeft(s)
s = gstr.TrimLeft(s, "()")
if p := gstr.Pos(s, " "); p != -1 {
if gstr.Equal(s[:p], "select") {
return true
@ -27,35 +27,9 @@ func isSubQuery(s string) bool {
// and also with its alias name, like:
// Table("user").LeftJoin("user_detail", "user_detail.uid=user.uid")
// Table("user", "u").LeftJoin("user_detail", "ud", "ud.uid=u.uid")
// Table("user", "u").LeftJoin("SELECT xxx FROM xxx AS a", "a.uid=u.uid")
func (m *Model) LeftJoin(table ...string) *Model {
var (
model = m.getModel()
joinStr = ""
)
if len(table) > 0 {
if isSubQuery(table[0]) {
joinStr = "(" + table[0] + ")"
} else {
joinStr = m.db.QuotePrefixTableName(table[0])
}
}
if len(table) > 2 {
model.tables += fmt.Sprintf(
" LEFT JOIN %s AS %s ON (%s)",
joinStr, m.db.QuoteWord(table[1]), table[2],
)
} else if len(table) == 2 {
model.tables += fmt.Sprintf(
" LEFT JOIN %s ON (%s)",
joinStr, table[1],
)
} else if len(table) == 1 {
model.tables += fmt.Sprintf(
" LEFT JOIN %s",
joinStr,
)
}
return model
return m.doJoin("LEFT", table...)
}
// RightJoin does "RIGHT JOIN ... ON ..." statement on the model.
@ -63,35 +37,9 @@ func (m *Model) LeftJoin(table ...string) *Model {
// and also with its alias name, like:
// Table("user").RightJoin("user_detail", "user_detail.uid=user.uid")
// Table("user", "u").RightJoin("user_detail", "ud", "ud.uid=u.uid")
// Table("user", "u").RightJoin("SELECT xxx FROM xxx AS a", "a.uid=u.uid")
func (m *Model) RightJoin(table ...string) *Model {
var (
model = m.getModel()
joinStr = ""
)
if len(table) > 0 {
if isSubQuery(table[0]) {
joinStr = "(" + table[0] + ")"
} else {
joinStr = m.db.QuotePrefixTableName(table[0])
}
}
if len(table) > 2 {
model.tables += fmt.Sprintf(
" RIGHT JOIN %s AS %s ON (%s)",
joinStr, m.db.QuoteWord(table[1]), table[2],
)
} else if len(table) == 2 {
model.tables += fmt.Sprintf(
" RIGHT JOIN %s ON (%s)",
joinStr, table[1],
)
} else if len(table) == 1 {
model.tables += fmt.Sprintf(
" RIGHT JOIN %s",
joinStr,
)
}
return model
return m.doJoin("RIGHT", table...)
}
// InnerJoin does "INNER JOIN ... ON ..." statement on the model.
@ -99,32 +47,47 @@ func (m *Model) RightJoin(table ...string) *Model {
// and also with its alias name, like:
// Table("user").InnerJoin("user_detail", "user_detail.uid=user.uid")
// Table("user", "u").InnerJoin("user_detail", "ud", "ud.uid=u.uid")
// Table("user", "u").InnerJoin("SELECT xxx FROM xxx AS a", "a.uid=u.uid")
func (m *Model) InnerJoin(table ...string) *Model {
return m.doJoin("INNER", table...)
}
// doJoin does "LEFT/RIGHT/INNER JOIN ... ON ..." statement on the model.
// The parameter <table> can be joined table and its joined condition,
// and also with its alias name, like:
// Table("user").InnerJoin("user_detail", "user_detail.uid=user.uid")
// Table("user", "u").InnerJoin("user_detail", "ud", "ud.uid=u.uid")
// Table("user", "u").InnerJoin("SELECT xxx FROM xxx AS a", "a.uid=u.uid")
// Related issues:
// https://github.com/gogf/gf/issues/1024
func (m *Model) doJoin(operator string, table ...string) *Model {
var (
model = m.getModel()
joinStr = ""
)
if len(table) > 0 {
if isSubQuery(table[0]) {
joinStr = "(" + table[0] + ")"
joinStr = gstr.Trim(table[0])
if joinStr[0] != '(' {
joinStr = "(" + joinStr + ")"
}
} else {
joinStr = m.db.QuotePrefixTableName(table[0])
}
}
if len(table) > 2 {
model.tables += fmt.Sprintf(
" INNER JOIN %s AS %s ON (%s)",
joinStr, m.db.QuoteWord(table[1]), table[2],
" %s JOIN %s AS %s ON (%s)",
operator, joinStr, m.db.QuoteWord(table[1]), table[2],
)
} else if len(table) == 2 {
model.tables += fmt.Sprintf(
" INNER JOIN %s ON (%s)",
joinStr, table[1],
" %s JOIN %s ON (%s)",
operator, joinStr, table[1],
)
} else if len(table) == 1 {
model.tables += fmt.Sprintf(
" INNER JOIN %s",
joinStr,
" %s JOIN %s", operator, joinStr,
)
}
return model

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -123,7 +123,7 @@ func (m *Model) getFieldsFiltered() string {
// Chunk iterates the query result with given size and callback function.
func (m *Model) Chunk(limit int, callback func(result Result, err error) bool) {
page := m.start
if page == 0 {
if page <= 0 {
page = 1
}
model := m

View File

@ -1,4 +1,4 @@
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -32,6 +32,10 @@ func (m *Model) Unscoped() *Model {
// If there's no field name for storing creating time, it returns an empty string.
// It checks the key with or without cases or chars '-'/'_'/'.'/' '.
func (m *Model) getSoftFieldNameCreated(table ...string) string {
// It checks whether this feature disabled.
if m.db.GetConfig().TimeMaintainDisabled {
return ""
}
tableName := ""
if len(table) > 0 {
tableName = table[0]
@ -40,7 +44,7 @@ func (m *Model) getSoftFieldNameCreated(table ...string) string {
}
config := m.db.GetConfig()
if config.CreatedAt != "" {
return m.getSoftFieldName(tableName, append([]string{config.CreatedAt}, createdFiledNames...))
return m.getSoftFieldName(tableName, []string{config.CreatedAt})
}
return m.getSoftFieldName(tableName, createdFiledNames)
}
@ -49,6 +53,10 @@ func (m *Model) getSoftFieldNameCreated(table ...string) string {
// If there's no field name for storing updating time, it returns an empty string.
// It checks the key with or without cases or chars '-'/'_'/'.'/' '.
func (m *Model) getSoftFieldNameUpdated(table ...string) (field string) {
// It checks whether this feature disabled.
if m.db.GetConfig().TimeMaintainDisabled {
return ""
}
tableName := ""
if len(table) > 0 {
tableName = table[0]
@ -57,7 +65,7 @@ func (m *Model) getSoftFieldNameUpdated(table ...string) (field string) {
}
config := m.db.GetConfig()
if config.UpdatedAt != "" {
return m.getSoftFieldName(tableName, append([]string{config.UpdatedAt}, updatedFiledNames...))
return m.getSoftFieldName(tableName, []string{config.UpdatedAt})
}
return m.getSoftFieldName(tableName, updatedFiledNames)
}
@ -66,6 +74,10 @@ func (m *Model) getSoftFieldNameUpdated(table ...string) (field string) {
// If there's no field name for storing deleting time, it returns an empty string.
// It checks the key with or without cases or chars '-'/'_'/'.'/' '.
func (m *Model) getSoftFieldNameDeleted(table ...string) (field string) {
// It checks whether this feature disabled.
if m.db.GetConfig().TimeMaintainDisabled {
return ""
}
tableName := ""
if len(table) > 0 {
tableName = table[0]
@ -74,7 +86,7 @@ func (m *Model) getSoftFieldNameDeleted(table ...string) (field string) {
}
config := m.db.GetConfig()
if config.UpdatedAt != "" {
return m.getSoftFieldName(tableName, append([]string{config.DeletedAt}, deletedFiledNames...))
return m.getSoftFieldName(tableName, []string{config.DeletedAt})
}
return m.getSoftFieldName(tableName, deletedFiledNames)
}

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,7 +8,6 @@ package gdb
import (
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/os/gtime"
@ -39,7 +38,7 @@ func (m *Model) Update(dataAndWhere ...interface{}) (result sql.Result, err erro
}
}()
if m.data == nil {
return nil, errors.New("updating table with empty data")
return nil, gerror.New("updating table with empty data")
}
var (
updateData = m.data

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -11,6 +11,7 @@ import (
"github.com/gogf/gf/container/gset"
"github.com/gogf/gf/internal/empty"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
@ -27,31 +28,35 @@ func (m *Model) getModel() *Model {
}
}
// mappingToTableFields mappings and changes given field name to really table field name.
func (m *Model) mappingToTableFields(fields []string) []string {
// mappingAndFilterToTableFields mappings and changes given field name to really table field name.
func (m *Model) mappingAndFilterToTableFields(fields []string) []string {
fieldsMap, err := m.db.TableFields(m.tables)
if err != nil || len(fieldsMap) == 0 {
return fields
}
var (
foundKey = ""
fieldsArray = gstr.SplitAndTrim(gstr.Join(fields, ","), ",")
inputFieldsArray = gstr.SplitAndTrim(gstr.Join(fields, ","), ",")
outputFieldsArray = make([]string, 0, len(inputFieldsArray))
)
if fieldsMap, err := m.db.TableFields(m.tables); err == nil {
fieldsKeyMap := make(map[string]interface{}, len(fieldsMap))
for k, _ := range fieldsMap {
fieldsKeyMap[k] = nil
}
for i, v := range fieldsArray {
if _, ok := fieldsKeyMap[v]; !ok {
if gstr.Contains(v, " ") || gstr.Contains(v, ".") {
continue
}
foundKey, _ = gutil.MapPossibleItemByKey(fieldsKeyMap, v)
if foundKey != "" {
fieldsArray[i] = foundKey
fieldsKeyMap := make(map[string]interface{}, len(fieldsMap))
for k, _ := range fieldsMap {
fieldsKeyMap[k] = nil
}
for _, field := range inputFieldsArray {
if _, ok := fieldsKeyMap[field]; !ok {
if !gregex.IsMatchString(regularFieldNameRegPattern, field) {
outputFieldsArray = append(outputFieldsArray, field)
continue
} else {
if foundKey, _ := gutil.MapPossibleItemByKey(fieldsKeyMap, field); foundKey != "" {
outputFieldsArray = append(outputFieldsArray, foundKey)
}
}
} else {
outputFieldsArray = append(outputFieldsArray, field)
}
}
return fieldsArray
return outputFieldsArray
}
// filterDataForInsertOrUpdate does filter feature with data for inserting/updating operations.
@ -149,19 +154,19 @@ func (m *Model) getLink(master bool) Link {
linkType := m.linkType
if linkType == 0 {
if master {
linkType = gLINK_TYPE_MASTER
linkType = linkTypeMaster
} else {
linkType = gLINK_TYPE_SLAVE
linkType = linkTypeSlave
}
}
switch linkType {
case gLINK_TYPE_MASTER:
case linkTypeMaster:
link, err := m.db.GetMaster(m.schema)
if err != nil {
panic(err)
}
return link
case gLINK_TYPE_SLAVE:
case linkTypeSlave:
link, err := m.db.GetSlave(m.schema)
if err != nil {
panic(err)
@ -196,7 +201,7 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
if len(m.whereHolder) > 0 {
for _, v := range m.whereHolder {
switch v.operator {
case gWHERE_HOLDER_WHERE:
case whereHolderWhere:
if conditionWhere == "" {
newWhere, newArgs := formatWhere(
m.db, v.where, v.args, m.option&OPTION_OMITEMPTY > 0,
@ -209,7 +214,7 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
}
fallthrough
case gWHERE_HOLDER_AND:
case whereHolderAnd:
newWhere, newArgs := formatWhere(
m.db, v.where, v.args, m.option&OPTION_OMITEMPTY > 0,
)
@ -224,7 +229,7 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
conditionArgs = append(conditionArgs, newArgs...)
}
case gWHERE_HOLDER_OR:
case whereHolderOr:
newWhere, newArgs := formatWhere(
m.db, v.where, v.args, m.option&OPTION_OMITEMPTY > 0,
)
@ -247,9 +252,6 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
if m.groupBy != "" {
conditionExtra += " GROUP BY " + m.groupBy
}
if m.orderBy != "" {
conditionExtra += " ORDER BY " + m.orderBy
}
if len(m.having) > 0 {
havingStr, havingArgs := formatWhere(
m.db, m.having[0], gconv.Interfaces(m.having[1]), m.option&OPTION_OMITEMPTY > 0,
@ -259,6 +261,9 @@ func (m *Model) formatCondition(limit1 bool, isCountStatement bool) (conditionWh
conditionArgs = append(conditionArgs, havingArgs...)
}
}
if m.orderBy != "" {
conditionExtra += " ORDER BY " + m.orderBy
}
if !isCountStatement {
if m.limit != 0 {
if m.start >= 0 {

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -59,7 +59,7 @@ func (tx *TX) GetAll(sql string, args ...interface{}) (Result, error) {
return nil, err
}
defer rows.Close()
return tx.db.rowsToResult(rows)
return tx.db.convertRowsToResult(rows)
}
// GetOne queries and returns one record from database.

View File

@ -1,4 +1,4 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,9 +8,9 @@ package gdb
import (
"database/sql"
"errors"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/encoding/gparser"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/util/gconv"
"reflect"
)
@ -52,24 +52,24 @@ func (r Record) Struct(pointer interface{}) error {
}
// Special handling for parameter type: reflect.Value
if _, ok := pointer.(reflect.Value); ok {
return mapToStruct(r.Map(), pointer)
return convertMapToStruct(r.Map(), pointer)
}
var (
reflectValue = reflect.ValueOf(pointer)
reflectKind = reflectValue.Kind()
)
if reflectKind != reflect.Ptr {
return errors.New("parameter should be type of *struct/**struct")
return gerror.New("parameter should be type of *struct/**struct")
}
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
if reflectKind == reflect.Invalid {
return errors.New("parameter is an invalid pointer, maybe nil")
return gerror.New("parameter is an invalid pointer, maybe nil")
}
if reflectKind != reflect.Ptr && reflectKind != reflect.Struct {
return errors.New("parameter should be type of *struct/**struct")
return gerror.New("parameter should be type of *struct/**struct")
}
return mapToStruct(r.Map(), pointer)
return convertMapToStruct(r.Map(), pointer)
}
// IsEmpty checks and returns whether <r> is empty.

View File

@ -1,4 +1,4 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -224,19 +224,16 @@ func (r Result) Structs(pointer interface{}) (err error) {
itemKind = itemType.Kind()
)
for i := 0; i < length; i++ {
var elem reflect.Value
if itemKind == reflect.Ptr {
e := reflect.New(itemType.Elem()).Elem()
if err = r[i].Struct(e); err != nil {
return fmt.Errorf(`slice element conversion failed: %s`, err.Error())
}
array.Index(i).Set(e.Addr())
elem = reflect.New(itemType.Elem())
} else {
e := reflect.New(itemType).Elem()
if err = r[i].Struct(e); err != nil {
return fmt.Errorf(`slice element conversion failed: %s`, err.Error())
}
array.Index(i).Set(e)
elem = reflect.New(itemType).Elem()
}
if err = r[i].Struct(elem); err != nil {
return fmt.Errorf(`slice element conversion failed: %s`, err.Error())
}
array.Index(i).Set(elem)
}
reflect.ValueOf(pointer).Elem().Set(array)
return nil

View File

@ -1,4 +1,4 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,8 +8,8 @@ package gdb
import (
"database/sql"
"errors"
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
@ -45,14 +45,14 @@ import (
func (r Result) ScanList(listPointer interface{}, attributeName string, relation ...string) (err error) {
// Necessary checks for parameters.
if attributeName == "" {
return errors.New(`attributeName should not be empty`)
return gerror.New(`attributeName should not be empty`)
}
if len(relation) > 0 {
if len(relation) < 2 {
return errors.New(`relation name and key should are both necessary`)
return gerror.New(`relation name and key should are both necessary`)
}
if relation[0] == "" || relation[1] == "" {
return errors.New(`relation name and key should not be empty`)
return gerror.New(`relation name and key should not be empty`)
}
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -57,7 +57,7 @@ func init() {
nodePrefix.Prefix = PREFIX1
gdb.AddConfigNode("test", configNode)
gdb.AddConfigNode("prefix", nodePrefix)
gdb.AddConfigNode(gdb.DEFAULT_GROUP_NAME, configNode)
gdb.AddConfigNode(gdb.DefaultGroupName, configNode)
// Default db.
if r, err := gdb.New(); err != nil {
gtest.Error(err)

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -0,0 +1,64 @@
// 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.
package gdb_test
import (
"context"
"testing"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/test/gtest"
)
func Test_Ctx(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
db, err := gdb.Instance()
t.Assert(err, nil)
err1 := db.PingMaster()
err2 := db.PingSlave()
t.Assert(err1, nil)
t.Assert(err2, nil)
newDb := db.Ctx(context.Background())
t.AssertNE(newDb, nil)
})
}
func Test_Ctx_Query(t *testing.T) {
db.GetLogger().SetCtxKeys("SpanId", "TraceId")
gtest.C(t, func(t *gtest.T) {
db.SetDebug(true)
defer db.SetDebug(false)
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
ctx = context.WithValue(ctx, "SpanId", "0.1")
db.Ctx(ctx).Query("select 1")
})
gtest.C(t, func(t *gtest.T) {
db.SetDebug(true)
defer db.SetDebug(false)
db.Query("select 2")
})
}
func Test_Ctx_Model(t *testing.T) {
table := createInitTable()
defer dropTable(table)
db.GetLogger().SetCtxKeys("SpanId", "TraceId")
gtest.C(t, func(t *gtest.T) {
db.SetDebug(true)
defer db.SetDebug(false)
ctx := context.WithValue(context.Background(), "TraceId", "12345678")
ctx = context.WithValue(ctx, "SpanId", "0.1")
db.Model(table).Ctx(ctx).All()
})
gtest.C(t, func(t *gtest.T) {
db.SetDebug(true)
defer db.SetDebug(false)
db.Model(table).All()
})
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -9,6 +9,7 @@ package gdb
import (
"fmt"
"github.com/go-sql-driver/mysql"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/os/gcmd"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/test/gtest"
@ -46,7 +47,7 @@ func init() {
MaxOpenConnCount: 10,
MaxConnLifetime: 600,
}
AddConfigNode(DEFAULT_GROUP_NAME, configNode)
AddConfigNode(DefaultGroupName, configNode)
// Default db.
if r, err := New(); err != nil {
gtest.Error(err)
@ -312,3 +313,26 @@ func Test_isSubQuery(t *testing.T) {
t.Assert(isSubQuery("select 1"), true)
})
}
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 := Result{
Record{"id": gvar.New(nil), "name": gvar.New("john")},
Record{"id": gvar.New(nil), "name": gvar.New("smith")},
}
array := make([]*B, 2)
err := r.Structs(&array)
t.Assert(err, nil)
t.Assert(array[0].Id, 0)
t.Assert(array[1].Id, 0)
t.Assert(array[0].Name, "john")
t.Assert(array[1].Name, "smith")
})
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -1405,3 +1405,67 @@ func Test_Empty_Slice_Argument(t *testing.T) {
t.Assert(len(result), 0)
})
}
// update counter test
func Test_DB_UpdateCounter(t *testing.T) {
tableName := "gf_update_counter_test_" + gtime.TimestampNanoStr()
_, err := db.Exec(fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
id int(10) unsigned NOT NULL,
views int(8) unsigned DEFAULT '0' NOT NULL ,
updated_time int(10) unsigned DEFAULT '0' NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableName))
if err != nil {
gtest.Fatal(err)
}
defer dropTable(tableName)
gtest.C(t, func(t *gtest.T) {
insertData := g.Map{
"id": 1,
"views": 0,
"updated_time": 0,
}
_, err = db.Insert(tableName, insertData)
t.Assert(err, nil)
})
gtest.C(t, func(t *gtest.T) {
gdbCounter := &gdb.Counter{
Field: "views",
Value: 1,
}
updateData := g.Map{
"views": gdbCounter,
"updated_time": gtime.Now().Unix(),
}
result, err := db.Update(tableName, updateData, "id", 1)
t.Assert(err, nil)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Table(tableName).Where("id", 1).One()
t.Assert(err, nil)
t.Assert(one["id"].Int(), 1)
t.Assert(one["views"].Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
gdbCounter := &gdb.Counter{
Field: "views",
Value: -1,
}
updateData := g.Map{
"views": gdbCounter,
"updated_time": gtime.Now().Unix(),
}
result, err := db.Update(tableName, updateData, "id", 1)
t.Assert(err, nil)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Table(tableName).Where("id", 1).One()
t.Assert(err, nil)
t.Assert(one["id"].Int(), 1)
t.Assert(one["views"].Int(), 0)
})
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -2717,6 +2717,49 @@ func Test_Model_FieldsEx_AutoMapping(t *testing.T) {
})
}
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) {
one, err := db.Table(table).Fields(A{}).Where("id", 2).One()
t.Assert(err, nil)
t.Assert(len(one), 2)
t.Assert(one["passport"], "user_2")
t.Assert(one["password"], "pass_2")
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).Fields(&A{}).Where("id", 2).One()
t.Assert(err, nil)
t.Assert(len(one), 2)
t.Assert(one["passport"], "user_2")
t.Assert(one["password"], "pass_2")
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).Fields(B{}).Where("id", 2).One()
t.Assert(err, nil)
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) {
one, err := db.Table(table).Fields(&B{}).Where("id", 2).One()
t.Assert(err, nil)
t.Assert(len(one), 3)
t.Assert(one["passport"], "user_2")
t.Assert(one["password"], "pass_2")
t.Assert(one["nickname"], "name_2")
})
}
func Test_Model_NullField(t *testing.T) {
table := createTable()
defer dropTable(table)
@ -2793,3 +2836,204 @@ func Test_Model_HasField(t *testing.T) {
t.Assert(err, nil)
})
}
// Issue: https://github.com/gogf/gf/issues/1002
func Test_Model_Issue1002(t *testing.T) {
table := createTable()
defer dropTable(table)
result, err := db.Table(table).Data(g.Map{
"id": 1,
"passport": "port_1",
"password": "pass_1",
"nickname": "name_2",
"create_time": "2020-10-27 19:03:33",
}).Insert()
gtest.Assert(err, nil)
n, _ := result.RowsAffected()
gtest.Assert(n, 1)
// where + string.
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").Value()
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").FindValue()
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").FindValue("id")
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
// where + string arguments.
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", "2020-10-27 19:03:32", "2020-10-27 19:03:34").Value()
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", "2020-10-27 19:03:32", "2020-10-27 19:03:34").FindValue()
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Where("create_time>? and create_time<?", "2020-10-27 19:03:32", "2020-10-27 19:03:34").FindValue("id")
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
// where + gtime.Time arguments.
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", gtime.New("2020-10-27 19:03:32"), gtime.New("2020-10-27 19:03:34")).Value()
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", gtime.New("2020-10-27 19:03:32"), gtime.New("2020-10-27 19:03:34")).FindValue()
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Where("create_time>? and create_time<?", gtime.New("2020-10-27 19:03:32"), gtime.New("2020-10-27 19:03:34")).FindValue("id")
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
// where + time.Time arguments, UTC.
t1, _ := time.Parse("2006-01-02 15:04:05", "2020-10-27 19:03:32")
t2, _ := time.Parse("2006-01-02 15:04:05", "2020-10-27 19:03:34")
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", t1, t2).Value()
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Fields("id").Where("create_time>? and create_time<?", t1, t2).FindValue()
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
v, err := db.Table(table).Where("create_time>? and create_time<?", t1, t2).FindValue("id")
t.Assert(err, nil)
t.Assert(v.Int(), 1)
})
}
func createTableForTimeZoneTest() string {
tableName := "user_" + gtime.Now().TimestampNanoStr()
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,
created_at timestamp NULL,
updated_at timestamp NULL,
deleted_at timestamp NULL,
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)
asiaLocal, err := time.LoadLocation("Asia/Shanghai")
gtest.Assert(err, nil)
CreateTime := "2020-11-22 12:23:45"
UpdateTime := "2020-11-22 13:23:45"
DeleteTime := "2020-11-22 14:23:45"
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"`
}
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", CreateTime, asiaLocal)
t2, _ := time.ParseInLocation("2006-01-02 15:04:05", UpdateTime, asiaLocal)
t3, _ := time.ParseInLocation("2006-01-02 15:04:05", DeleteTime, asiaLocal)
u := &User{
Id: 1,
CreatedAt: gtime.New(t1.UTC()),
UpdatedAt: *gtime.New(t2.UTC()),
DeletedAt: t3.UTC(),
}
gtest.C(t, func(t *gtest.T) {
_, _ = db.Table(tableName).Unscoped().Insert(u)
userEntity := &User{}
err := db.Table(tableName).Where("id", 1).Unscoped().Struct(&userEntity)
t.Assert(err, nil)
t.Assert(userEntity.CreatedAt.String(), "2020-11-22 04:23:45")
t.Assert(userEntity.UpdatedAt.String(), "2020-11-22 05:23:45")
t.Assert(gtime.NewFromTime(userEntity.DeletedAt).String(), "2020-11-22 06:23:45")
})
}
func Test_Model_Fields_Map_Struct(t *testing.T) {
table := createInitTable()
defer dropTable(table)
// map
gtest.C(t, func(t *gtest.T) {
result, err := db.Table(table).Fields(g.Map{
"ID": 1,
"PASSPORT": 1,
"NONE_EXIST": 1,
}).Where("id", 1).One()
t.Assert(err, nil)
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
}
var a = A{}
err := db.Table(table).Fields(a).Where("id", 1).Struct(&a)
t.Assert(err, nil)
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
err := db.Table(table).Fields(a).Where("id", 1).Struct(&a)
t.Assert(err, nil)
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
err := db.Table(table).Fields(&a).Where("id", 1).Struct(&a)
t.Assert(err, nil)
t.Assert(a.ID, 1)
t.Assert(a.PASSPORT, "user_1")
t.Assert(a.XXX_TYPE, 0)
})
}

View File

@ -0,0 +1,86 @@
// 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.
package gdb_test
import (
"github.com/gogf/gf/frame/g"
"testing"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/test/gtest"
)
func Test_Insert_Raw(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
user := db.Table(table)
result, err := user.Filter().Data(g.Map{
"id": gdb.Raw("id+2"),
"passport": "port_1",
"password": "pass_1",
"nickname": "name_1",
"create_time": gdb.Raw("now()"),
}).Insert()
t.Assert(err, nil)
n, _ := result.LastInsertId()
t.Assert(n, 2)
})
}
func Test_BatchInsert_Raw(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
user := db.Table(table)
result, err := user.Filter().Data(
g.List{
g.Map{
"id": gdb.Raw("id+2"),
"passport": "port_2",
"password": "pass_2",
"nickname": "name_2",
"create_time": gdb.Raw("now()"),
},
g.Map{
"id": gdb.Raw("id+4"),
"passport": "port_4",
"password": "pass_4",
"nickname": "name_4",
"create_time": gdb.Raw("now()"),
},
},
).Insert()
t.Assert(err, nil)
n, _ := result.LastInsertId()
t.Assert(n, 4)
})
}
func Test_Update_Raw(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
user := db.Table(table)
result, err := user.Data(g.Map{
"id": gdb.Raw("id+100"),
"create_time": gdb.Raw("now()"),
}).Where("id", 1).Update()
t.Assert(err, nil)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
gtest.C(t, func(t *gtest.T) {
user := db.Table(table)
n, err := user.Where("id", 101).Count()
t.Assert(err, nil)
t.Assert(n, 1)
})
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -7,9 +7,9 @@
package gdb_test
import (
"errors"
"fmt"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/errors/gerror"
"testing"
"github.com/gogf/gf/frame/g"
@ -731,7 +731,7 @@ func Test_Transaction(t *testing.T) {
}); err != nil {
t.Error(err)
}
return errors.New("error")
return gerror.New("error")
})
t.AssertNE(err, nil)

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,

View File

@ -19,8 +19,8 @@ import (
)
const (
DEFAULT_GROUP_NAME = "default" // Default configuration group name.
DEFAULT_REDIS_PORT = 6379 // Default redis port configuration if not passed.
DefaultGroupName = "default" // Default configuration group name.
DefaultRedisPort = 6379 // Default redis port configuration if not passed.
)
var (
@ -31,7 +31,7 @@ var (
// SetConfig sets the global configuration for specified group.
// If <name> is not passed, it sets configuration for the default group name.
func SetConfig(config Config, name ...string) {
group := DEFAULT_GROUP_NAME
group := DefaultGroupName
if len(name) > 0 {
group = name[0]
}
@ -44,7 +44,7 @@ func SetConfig(config Config, name ...string) {
// SetConfigByStr sets the global configuration for specified group with string.
// If <name> is not passed, it sets configuration for the default group name.
func SetConfigByStr(str string, name ...string) error {
group := DEFAULT_GROUP_NAME
group := DefaultGroupName
if len(name) > 0 {
group = name[0]
}
@ -60,7 +60,7 @@ func SetConfigByStr(str string, name ...string) error {
// GetConfig returns the global configuration with specified group name.
// If <name> is not passed, it returns configuration of the default group name.
func GetConfig(name ...string) (config Config, ok bool) {
group := DEFAULT_GROUP_NAME
group := DefaultGroupName
if len(name) > 0 {
group = name[0]
}
@ -73,7 +73,7 @@ func GetConfig(name ...string) (config Config, ok bool) {
// RemoveConfig removes the global configuration with specified group.
// If <name> is not passed, it removes configuration of the default group name.
func RemoveConfig(name ...string) {
group := DEFAULT_GROUP_NAME
group := DefaultGroupName
if len(name) > 0 {
group = name[0]
}
@ -96,7 +96,7 @@ func ConfigFromStr(str string) (config Config, err error) {
Pass: array[4],
}
if config.Port == 0 {
config.Port = DEFAULT_REDIS_PORT
config.Port = DefaultRedisPort
}
if v, ok := parse["maxIdle"]; ok {
config.MaxIdle = gconv.Int(v)
@ -127,7 +127,7 @@ func ConfigFromStr(str string) (config Config, err error) {
Pass: array[4],
}
if config.Port == 0 {
config.Port = DEFAULT_REDIS_PORT
config.Port = DefaultRedisPort
}
} else {
err = gerror.Newf(`invalid redis configuration: "%s"`, str)

View File

@ -17,7 +17,7 @@ var (
// The <name> param is unnecessary, if <name> is not passed,
// it returns a redis instance with default configuration group.
func Instance(name ...string) *Redis {
group := DEFAULT_GROUP_NAME
group := DefaultGroupName
if len(name) > 0 && name[0] != "" {
group = name[0]
}

View File

@ -9,6 +9,7 @@ package ghtml
import (
"html"
"reflect"
"strings"
strip "github.com/grokify/html-strip-tags-go"
@ -57,3 +58,46 @@ func SpecialCharsDecode(s string) string {
"&#39;", "'",
).Replace(s)
}
// SpecialCharsMapOrStruct automatically encodes string values/attributes for map/struct.
func SpecialCharsMapOrStruct(mapOrStruct interface{}) error {
var (
reflectValue = reflect.ValueOf(mapOrStruct)
reflectKind = reflectValue.Kind()
)
for reflectValue.IsValid() && (reflectKind == reflect.Ptr || reflectKind == reflect.Interface) {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
switch reflectKind {
case reflect.Map:
var (
mapKeys = reflectValue.MapKeys()
mapValue reflect.Value
)
for _, key := range mapKeys {
mapValue = reflectValue.MapIndex(key)
switch mapValue.Kind() {
case reflect.String:
reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.String())))
case reflect.Interface:
if mapValue.Elem().Kind() == reflect.String {
reflectValue.SetMapIndex(key, reflect.ValueOf(SpecialChars(mapValue.Elem().String())))
}
}
}
case reflect.Struct:
var (
fieldValue reflect.Value
)
for i := 0; i < reflectValue.NumField(); i++ {
fieldValue = reflectValue.Field(i)
switch fieldValue.Kind() {
case reflect.String:
fieldValue.Set(reflect.ValueOf(SpecialChars(fieldValue.String())))
}
}
}
return nil
}

View File

@ -7,13 +7,14 @@
package ghtml_test
import (
"github.com/gogf/gf/frame/g"
"testing"
"github.com/gogf/gf/encoding/ghtml"
"github.com/gogf/gf/test/gtest"
)
func TestStripTags(t *testing.T) {
func Test_StripTags(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
src := `<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>`
dst := `Test paragraph. Other text`
@ -21,7 +22,7 @@ func TestStripTags(t *testing.T) {
})
}
func TestEntities(t *testing.T) {
func Test_Entities(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
@ -30,7 +31,7 @@ func TestEntities(t *testing.T) {
})
}
func TestSpecialChars(t *testing.T) {
func Test_SpecialChars(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
@ -38,3 +39,43 @@ func TestSpecialChars(t *testing.T) {
t.Assert(ghtml.SpecialCharsDecode(dst), src)
})
}
func Test_SpecialCharsMapOrStruct_Map(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
a := g.Map{
"Title": "<h1>T</h1>",
"Content": "<div>C</div>",
}
err := ghtml.SpecialCharsMapOrStruct(a)
t.Assert(err, nil)
t.Assert(a["Title"], `&lt;h1&gt;T&lt;/h1&gt;`)
t.Assert(a["Content"], `&lt;div&gt;C&lt;/div&gt;`)
})
gtest.C(t, func(t *gtest.T) {
a := g.MapStrStr{
"Title": "<h1>T</h1>",
"Content": "<div>C</div>",
}
err := ghtml.SpecialCharsMapOrStruct(a)
t.Assert(err, nil)
t.Assert(a["Title"], `&lt;h1&gt;T&lt;/h1&gt;`)
t.Assert(a["Content"], `&lt;div&gt;C&lt;/div&gt;`)
})
}
func Test_SpecialCharsMapOrStruct_Struct(t *testing.T) {
type A struct {
Title string
Content string
}
gtest.C(t, func(t *gtest.T) {
a := &A{
Title: "<h1>T</h1>",
Content: "<div>C</div>",
}
err := ghtml.SpecialCharsMapOrStruct(a)
t.Assert(err, nil)
t.Assert(a.Title, `&lt;h1&gt;T&lt;/h1&gt;`)
t.Assert(a.Content, `&lt;div&gt;C&lt;/div&gt;`)
})
}

View File

@ -1,4 +1,4 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -370,99 +370,60 @@ func (j *Json) GetMapToMapsDeep(pattern string, pointer interface{}, mapping ...
return gconv.MapToMapsDeep(j.Get(pattern), pointer, mapping...)
}
// ToMap converts current Json object to map[string]interface{}.
// Map converts current Json object to map[string]interface{}.
// It returns nil if fails.
func (j *Json) ToMap() map[string]interface{} {
func (j *Json) Map() map[string]interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Map(*(j.p))
}
// ToArray converts current Json object to []interface{}.
// Array converts current Json object to []interface{}.
// It returns nil if fails.
func (j *Json) ToArray() []interface{} {
func (j *Json) Array() []interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Interfaces(*(j.p))
}
// ToStruct converts current Json object to specified object.
// Struct converts current Json object to specified object.
// The <pointer> should be a pointer type of *struct.
func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error {
func (j *Json) Struct(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Struct(*(j.p), pointer, mapping...)
}
// ToStructDeep converts current Json object to specified object recursively.
// The <pointer> should be a pointer type of *struct.
// Deprecated, use ToStruct instead.
func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.StructDeep(*(j.p), pointer, mapping...)
}
// ToStructs converts current Json object to specified object slice.
// Structs converts current Json object to specified object slice.
// The <pointer> should be a pointer type of []struct/*struct.
func (j *Json) ToStructs(pointer interface{}, mapping ...map[string]string) error {
func (j *Json) Structs(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Structs(*(j.p), pointer, mapping...)
}
// ToStructsDeep converts current Json object to specified object slice recursively.
// The <pointer> should be a pointer type of []struct/*struct.
func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.StructsDeep(*(j.p), pointer, mapping...)
}
// ToScan automatically calls Struct or Structs function according to the type of parameter
// Scan automatically calls Struct or Structs function according to the type of parameter
// <pointer> to implement the converting..
func (j *Json) ToScan(pointer interface{}, mapping ...map[string]string) error {
func (j *Json) Scan(pointer interface{}, mapping ...map[string]string) error {
return gconv.Scan(*(j.p), pointer, mapping...)
}
// ToScanDeep automatically calls StructDeep or StructsDeep function according to the type of
// parameter <pointer> to implement the converting..
func (j *Json) ToScanDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.ScanDeep(*(j.p), pointer, mapping...)
}
// ToMapToMap converts current Json object to specified map variable.
// MapToMap converts current Json object to specified map variable.
// The parameter of <pointer> should be type of *map.
func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
func (j *Json) MapToMap(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMap(*(j.p), pointer, mapping...)
}
// ToMapToMapDeep converts current Json object to specified map variable recursively.
// The parameter of <pointer> should be type of *map.
func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMapDeep(*(j.p), pointer, mapping...)
}
// ToMapToMaps converts current Json object to specified map variable slice.
// MapToMaps converts current Json object to specified map variable slice.
// The parameter of <pointer> should be type of []map/*map.
func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
func (j *Json) MapToMaps(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMaps(*(j.p), pointer, mapping...)
}
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
// The parameter of <pointer> should be type of []map/*map.
func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMapsDeep(*(j.p), pointer, mapping...)
}
// Dump prints current Json object with more manually readable.
func (j *Json) Dump() {
j.mu.RLock()

View File

@ -0,0 +1,113 @@
// 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.
package gjson
import "github.com/gogf/gf/util/gconv"
// ToMap converts current Json object to map[string]interface{}.
// It returns nil if fails.
// Deprecated, use Map instead.
func (j *Json) ToMap() map[string]interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Map(*(j.p))
}
// ToArray converts current Json object to []interface{}.
// It returns nil if fails.
// Deprecated, use Array instead.
func (j *Json) ToArray() []interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Interfaces(*(j.p))
}
// ToStruct converts current Json object to specified object.
// The <pointer> should be a pointer type of *struct.
// Deprecated, use Struct instead.
func (j *Json) ToStruct(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Struct(*(j.p), pointer, mapping...)
}
// ToStructDeep converts current Json object to specified object recursively.
// The <pointer> should be a pointer type of *struct.
// Deprecated, use Struct instead.
func (j *Json) ToStructDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.StructDeep(*(j.p), pointer, mapping...)
}
// ToStructs converts current Json object to specified object slice.
// The <pointer> should be a pointer type of []struct/*struct.
// Deprecated, use Structs instead.
func (j *Json) ToStructs(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.Structs(*(j.p), pointer, mapping...)
}
// ToStructsDeep converts current Json object to specified object slice recursively.
// The <pointer> should be a pointer type of []struct/*struct.
// Deprecated, use Structs instead.
func (j *Json) ToStructsDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.StructsDeep(*(j.p), pointer, mapping...)
}
// ToScan automatically calls Struct or Structs function according to the type of parameter
// <pointer> to implement the converting..
// Deprecated, use Scan instead.
func (j *Json) ToScan(pointer interface{}, mapping ...map[string]string) error {
return gconv.Scan(*(j.p), pointer, mapping...)
}
// ToScanDeep automatically calls StructDeep or StructsDeep function according to the type of
// parameter <pointer> to implement the converting..
// Deprecated, use Scan instead.
func (j *Json) ToScanDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.ScanDeep(*(j.p), pointer, mapping...)
}
// ToMapToMap converts current Json object to specified map variable.
// The parameter of <pointer> should be type of *map.
// Deprecated, use MapToMap instead.
func (j *Json) ToMapToMap(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMap(*(j.p), pointer, mapping...)
}
// ToMapToMapDeep converts current Json object to specified map variable recursively.
// The parameter of <pointer> should be type of *map.
// Deprecated, use MapToMap instead.
func (j *Json) ToMapToMapDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMapDeep(*(j.p), pointer, mapping...)
}
// ToMapToMaps converts current Json object to specified map variable slice.
// The parameter of <pointer> should be type of []map/*map.
// Deprecated, use MapToMaps instead.
func (j *Json) ToMapToMaps(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMaps(*(j.p), pointer, mapping...)
}
// ToMapToMapsDeep converts current Json object to specified map variable slice recursively.
// The parameter of <pointer> should be type of []map/*map.
// Deprecated, use MapToMaps instead.
func (j *Json) ToMapToMapsDeep(pointer interface{}, mapping ...map[string]string) error {
j.mu.RLock()
defer j.mu.RUnlock()
return gconv.MapToMapsDeep(*(j.p), pointer, mapping...)
}

View File

@ -7,41 +7,91 @@
package gjson_test
import (
json2 "encoding/json"
"testing"
"github.com/gogf/gf/encoding/gjson"
)
var (
jsonStr1 = `[1,2,3]`
jsonStr1 = `{"name":"john","slice":[1,2,3]}`
jsonStr2 = `{"CallbackCommand":"Group.CallbackAfterSendMsg","From_Account":"61934946","GroupId":"@TGS#2FLGX67FD","MsgBody":[{"MsgContent":{"Text":"是的"},"MsgType":"TIMTextElem"}],"MsgSeq":23,"MsgTime":1567032819,"Operator_Account":"61934946","Random":2804799576,"Type":"Public"}`
jsonObj1 = gjson.New(jsonStr1)
jsonObj2 = gjson.New(jsonStr2)
)
func Benchmark_Validate1(b *testing.B) {
func Benchmark_Validate_Simple_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
gjson.Valid(jsonStr1)
}
}
func Benchmark_Validate2(b *testing.B) {
func Benchmark_Validate_Complicated_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
gjson.Valid(jsonStr2)
}
}
func Benchmark_Set1(b *testing.B) {
func Benchmark_Get_Simple_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
p := gjson.New(map[string]string{
"k1": "v1",
"k2": "v2",
})
p.Set("k1.k11", []int{1, 2, 3})
jsonObj1.Get("name")
}
}
func Benchmark_Set2(b *testing.B) {
func Benchmark_Get_Complicated_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
p := gjson.New([]string{"a"})
jsonObj2.Get("GroupId")
}
}
func Benchmark_Stdlib_Json_Unmarshal_Simple_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
var m map[string]interface{}
json2.Unmarshal([]byte(jsonStr1), &m)
}
}
func Benchmark_Stdlib_Json_Unmarshal_Complicated_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
var m map[string]interface{}
json2.Unmarshal([]byte(jsonStr2), &m)
}
}
func Benchmark_New_Simple_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
gjson.New(jsonStr1)
}
}
func Benchmark_New_Complicated_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
gjson.New(jsonStr2)
}
}
func Benchmark_Remove_Simple_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
jsonObj1.Remove("name")
}
}
func Benchmark_Remove_Complicated_Json(b *testing.B) {
for i := 0; i < b.N; i++ {
jsonObj2.Remove("GroupId")
}
}
func Benchmark_New_Nil_And_Set_Simple(b *testing.B) {
for i := 0; i < b.N; i++ {
p := gjson.New(nil)
p.Set("k", "v")
}
}
func Benchmark_New_Nil_And_Set_Multiple_Level(b *testing.B) {
for i := 0; i < b.N; i++ {
p := gjson.New(nil)
p.Set("0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0", []int{1, 2, 3})
}
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// Copyright GoFrame gf 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,
@ -14,67 +14,71 @@ import (
"fmt"
)
// ApiStack is the interface for Stack feature.
type ApiStack interface {
Error() string // It should be en error.
// apiCode is the interface for Code feature.
type apiCode interface {
Error() string // It should be an error.
Code() int
}
// apiStack is the interface for Stack feature.
type apiStack interface {
Error() string // It should be an error.
Stack() string
}
// ApiCause is the interface for Cause feature.
type ApiCause interface {
Error() string // It should be en error.
// apiCause is the interface for Cause feature.
type apiCause interface {
Error() string // It should be an error.
Cause() error
}
// ApiLevel is the interface for Current/Next feature.
type ApiLevel interface {
// apiCurrent is the interface for Current feature.
type apiCurrent interface {
Error() string // It should be an error.
Current() error
}
// apiNext is the interface for Next feature.
type apiNext interface {
Error() string // It should be an error.
Next() error
}
// New creates and returns an error which is formatted from given text.
func New(text string) error {
if text == "" {
return nil
}
return &Error{
stack: callers(),
text: text,
code: -1,
}
}
// Newf returns an error that formats as the given format and args.
func Newf(format string, args ...interface{}) error {
return &Error{
stack: callers(),
text: fmt.Sprintf(format, args...),
code: -1,
}
}
// NewSkip creates and returns an error which is formatted from given text.
// The parameter <skip> specifies the stack callers skipped amount.
func NewSkip(skip int, text string) error {
if text == "" {
return nil
}
return &Error{
stack: callers(skip),
text: text,
code: -1,
}
}
// Newf returns an error that formats as the given format and args.
func Newf(format string, args ...interface{}) error {
if format == "" {
return nil
}
return &Error{
stack: callers(),
text: fmt.Sprintf(format, args...),
}
}
// NewfSkip returns an error that formats as the given format and args.
// NewSkipf returns an error that formats as the given format and args.
// The parameter <skip> specifies the stack callers skipped amount.
func NewfSkip(skip int, format string, args ...interface{}) error {
if format == "" {
return nil
}
func NewSkipf(skip int, format string, args ...interface{}) error {
return &Error{
stack: callers(skip),
text: fmt.Sprintf(format, args...),
code: -1,
}
}
@ -88,6 +92,7 @@ func Wrap(err error, text string) error {
error: err,
stack: callers(),
text: text,
code: -1,
}
}
@ -102,13 +107,91 @@ func Wrapf(err error, format string, args ...interface{}) error {
error: err,
stack: callers(),
text: fmt.Sprintf(format, args...),
code: -1,
}
}
// NewCode creates and returns an error that has error code and given text.
func NewCode(code int, text string) error {
return &Error{
stack: callers(),
text: text,
code: code,
}
}
// NewCodef returns an error that has error code and formats as the given format and args.
func NewCodef(code int, format string, args ...interface{}) error {
return &Error{
stack: callers(),
text: fmt.Sprintf(format, args...),
code: code,
}
}
// NewCodeSkip creates and returns an error which has error code and is formatted from given text.
// The parameter <skip> specifies the stack callers skipped amount.
func NewCodeSkip(code, skip int, text string) error {
return &Error{
stack: callers(skip),
text: text,
code: code,
}
}
// NewCodeSkipf returns an error that has error code and formats as the given format and args.
// The parameter <skip> specifies the stack callers skipped amount.
func NewCodeSkipf(code, skip int, format string, args ...interface{}) error {
return &Error{
stack: callers(skip),
text: fmt.Sprintf(format, args...),
code: code,
}
}
// WrapCode wraps error with code and text.
// It returns nil if given err is nil.
func WrapCode(code int, err error, text string) error {
if err == nil {
return nil
}
return &Error{
error: err,
stack: callers(),
text: text,
code: code,
}
}
// WrapCodef wraps error with code and format specifier.
// It returns nil if given <err> is nil.
func WrapCodef(code int, err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &Error{
error: err,
stack: callers(),
text: fmt.Sprintf(format, args...),
code: code,
}
}
// Cause returns the error code of current error.
// It returns -1 if it has no error code or it does not implements interface Code.
func Code(err error) int {
if err != nil {
if e, ok := err.(apiCode); ok {
return e.Code()
}
}
return -1
}
// Cause returns the root cause error of <err>.
func Cause(err error) error {
if err != nil {
if e, ok := err.(ApiCause); ok {
if e, ok := err.(apiCause); ok {
return e.Cause()
}
}
@ -121,7 +204,7 @@ func Stack(err error) string {
if err == nil {
return ""
}
if e, ok := err.(ApiStack); ok {
if e, ok := err.(apiStack); ok {
return e.Stack()
}
return ""
@ -133,7 +216,7 @@ func Current(err error) error {
if err == nil {
return nil
}
if e, ok := err.(ApiLevel); ok {
if e, ok := err.(apiCurrent); ok {
return e.Current()
}
return err
@ -145,7 +228,7 @@ func Next(err error) error {
if err == nil {
return nil
}
if e, ok := err.(ApiLevel); ok {
if e, ok := err.(apiNext); ok {
return e.Next()
}
return nil

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -8,6 +8,7 @@ package gerror
import (
"bytes"
"errors"
"fmt"
"io"
"runtime"
@ -19,10 +20,11 @@ type Error struct {
error error // Wrapped error.
stack stack // Stack array, which records the stack information when this error is created or wrapped.
text string // Error text, which is created by New* functions.
code int // Error code if necessary.
}
const (
gFILTER_KEY = "/errors/gerror/gerror"
stackFilterKey = "/errors/gerror/gerror"
)
var (
@ -42,13 +44,23 @@ func (err *Error) Error() string {
if err == nil {
return ""
}
if err.text != "" {
if err.error != nil {
return err.text + ": " + err.error.Error()
errStr := err.text
if err.error != nil {
if err.text != "" {
errStr += ": "
}
return err.text
errStr += err.error.Error()
}
return err.error.Error()
return errStr
}
// Code returns the error code.
// It returns -1 if it has no error code.
func (err *Error) Code() int {
if err == nil {
return -1
}
return err.code
}
// Cause returns the root cause error.
@ -60,12 +72,18 @@ func (err *Error) Cause() error {
for loop != nil {
if loop.error != nil {
if e, ok := loop.error.(*Error); ok {
// Internal Error struct.
loop = e
} else if e, ok := loop.error.(apiCause); ok {
// Other Error that implements ApiCause interface.
return e.Cause()
} else {
return loop.error
}
} else {
return loop
// return loop
// To be compatible with Case of https://github.com/pkg/errors.
return errors.New(loop.text)
}
}
return nil
@ -105,9 +123,11 @@ func (err *Error) Stack() string {
if err == nil {
return ""
}
loop := err
index := 1
buffer := bytes.NewBuffer(nil)
var (
loop = err
index = 1
buffer = bytes.NewBuffer(nil)
)
for loop != nil {
buffer.WriteString(fmt.Sprintf("%d. %-v\n", index, loop))
index++
@ -156,7 +176,7 @@ func formatSubStack(st stack, buffer *bytes.Buffer) {
for _, p := range st {
if fn := runtime.FuncForPC(p - 1); fn != nil {
file, line := fn.FileLine(p - 1)
if strings.Contains(file, gFILTER_KEY) {
if strings.Contains(file, stackFilterKey) {
continue
}
// Avoid stack string like "<autogenerated>"

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -12,13 +12,15 @@ import "runtime"
type stack []uintptr
const (
gMAX_STACK_DEPTH = 32
// maxStackDepth marks the max stack depth for error back traces.
maxStackDepth = 32
)
// callers returns the stack callers.
// Note that it here just retrieves the caller memory address array not the caller information.
func callers(skip ...int) stack {
var (
pcs [gMAX_STACK_DEPTH]uintptr
pcs [maxStackDepth]uintptr
n = 3
)
if len(skip) > 0 {

View File

@ -0,0 +1,17 @@
// 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.
package gerror
import (
"testing"
)
func Benchmark_Stack(b *testing.B) {
for i := 0; i < b.N; i++ {
callers()
}
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -34,7 +34,6 @@ func Test_Wrap(t *testing.T) {
t.AssertNE(err, nil)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
err = gerror.Wrap(err, "2")
@ -42,6 +41,12 @@ func Test_Wrap(t *testing.T) {
t.AssertNE(err, nil)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.New("1")
err = gerror.Wrap(err, "")
t.AssertNE(err, nil)
t.Assert(err.Error(), "1")
})
}
func Test_Cause(t *testing.T) {
@ -155,3 +160,45 @@ func Test_Next(t *testing.T) {
t.Assert(err, nil)
})
}
func Test_Code(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := errors.New("123")
t.Assert(gerror.Code(err), -1)
t.Assert(err.Error(), "123")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.NewCode(1, "123")
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "123")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.NewCodef(1, "%s", "123")
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "123")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.NewCodeSkip(1, 0, "123")
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "123")
})
gtest.C(t, func(t *gtest.T) {
err := gerror.NewCodeSkipf(1, 0, "%s", "123")
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "123")
})
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
err = gerror.Wrap(err, "2")
err = gerror.WrapCode(1, err, "3")
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "3: 2: 1")
})
gtest.C(t, func(t *gtest.T) {
err := errors.New("1")
err = gerror.Wrap(err, "2")
err = gerror.WrapCodef(1, err, "%s", "3")
t.Assert(gerror.Code(err), 1)
t.Assert(err.Error(), "3: 2: 1")
})
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -18,18 +18,18 @@ import (
)
const (
gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database"
gDATABASE_NODE_NAME = "database"
frameCoreComponentNameDatabase = "gf.core.component.database"
configNodeNameDatabase = "database"
)
// Database returns an instance of database ORM object
// with specified configuration group name.
func Database(name ...string) gdb.DB {
group := gdb.DEFAULT_GROUP_NAME
group := gdb.DefaultGroupName
if len(name) > 0 && name[0] != "" {
group = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group)
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameDatabase, group)
db := instances.GetOrSetFuncLock(instanceKey, func() interface{} {
var (
configMap map[string]interface{}
@ -37,14 +37,17 @@ func Database(name ...string) gdb.DB {
)
// It firstly searches the configuration of the instance name.
if Config().Available() {
configNodeKey, _ = gutil.MapPossibleItemByKey(Config().GetMap("."), gDATABASE_NODE_NAME)
configNodeKey, _ = gutil.MapPossibleItemByKey(
Config().GetMap("."),
configNodeNameDatabase,
)
if configNodeKey == "" {
configNodeKey = gDATABASE_NODE_NAME
configNodeKey = configNodeNameDatabase
}
configMap = Config().GetMap(configNodeKey)
}
if len(configMap) == 0 && !gdb.IsConfigured() {
panic(fmt.Sprintf(`database init failed: "%s" node not found, is config file or configuration missing?`, gDATABASE_NODE_NAME))
panic(fmt.Sprintf(`database init failed: "%s" node not found, is config file or configuration missing?`, configNodeNameDatabase))
}
if len(configMap) == 0 {
configMap = make(map[string]interface{})
@ -84,11 +87,11 @@ func Database(name ...string) gdb.DB {
if len(cg) > 0 {
if gdb.GetConfig(group) == nil {
intlog.Printf("add configuration for group: %s, %#v", gdb.DEFAULT_GROUP_NAME, cg)
gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg)
intlog.Printf("add configuration for group: %s, %#v", gdb.DefaultGroupName, cg)
gdb.SetConfigGroup(gdb.DefaultGroupName, cg)
} else {
intlog.Printf("ignore configuration as it already exists for group: %s, %#v", gdb.DEFAULT_GROUP_NAME, cg)
intlog.Printf("%s, %#v", gdb.DEFAULT_GROUP_NAME, cg)
intlog.Printf("ignore configuration as it already exists for group: %s, %#v", gdb.DefaultGroupName, cg)
intlog.Printf("%s, %#v", gdb.DefaultGroupName, cg)
}
}
}
@ -97,7 +100,7 @@ func Database(name ...string) gdb.DB {
if Config().Available() {
// Initialize logger for ORM.
var loggerConfigMap map[string]interface{}
loggerConfigMap = Config().GetMap(fmt.Sprintf("%s.%s", configNodeKey, gLOGGER_NODE_NAME))
loggerConfigMap = Config().GetMap(fmt.Sprintf("%s.%s", configNodeKey, configNodeNameLogger))
if len(loggerConfigMap) == 0 {
loggerConfigMap = Config().GetMap(configNodeKey)
}

View File

@ -1,4 +1,4 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
// 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,
@ -13,26 +13,26 @@ import (
)
const (
gFRAME_CORE_COMPONENT_NAME_LOGGER = "gf.core.component.logger"
gLOGGER_NODE_NAME = "logger"
frameCoreComponentNameLogger = "gf.core.component.logger"
configNodeNameLogger = "logger"
)
// Log returns an instance of glog.Logger.
// The parameter <name> is the name for the instance.
func Log(name ...string) *glog.Logger {
instanceName := glog.DEFAULT_NAME
instanceName := glog.DefaultName
if len(name) > 0 && name[0] != "" {
instanceName = name[0]
}
instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName)
instanceKey := fmt.Sprintf("%s.%s", frameCoreComponentNameLogger, instanceName)
return instances.GetOrSetFuncLock(instanceKey, func() interface{} {
logger := glog.Instance(instanceName)
// To avoid file no found error while it's not necessary.
if Config().Available() {
var m map[string]interface{}
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), gLOGGER_NODE_NAME)
nodeKey, _ := gutil.MapPossibleItemByKey(Config().GetMap("."), configNodeNameLogger)
if nodeKey == "" {
nodeKey = gLOGGER_NODE_NAME
nodeKey = configNodeNameLogger
}
m = Config().GetMap(fmt.Sprintf(`%s.%s`, nodeKey, instanceName))
if len(m) == 0 {

Some files were not shown because too many files have changed in this diff Show More