Merge pull request #9 from gogf/master

pr from master
This commit is contained in:
HaiLaz
2021-11-02 11:25:49 +08:00
committed by GitHub
1216 changed files with 30365 additions and 21833 deletions

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/v2/container/garray"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/v2/container/garray"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/v2/container/garray"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/v2/container/garray"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/garray"
"github.com/gogf/gf/v2/container/garray"
)
func main() {

View File

@ -2,7 +2,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/glist"
"github.com/gogf/gf/v2/container/glist"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/glist"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/container/glist"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/glist"
"github.com/gogf/gf/v2/container/glist"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/v2/container/gmap"
)
func main() {

View File

@ -3,9 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/v2/container/gmap"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/v2/container/gmap"
)
func main() {

View File

@ -1,8 +1,8 @@
package main
import (
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,9 +3,9 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {

View File

@ -4,7 +4,7 @@ import (
"fmt"
"time"
"github.com/gogf/gf/container/gpool"
"github.com/gogf/gf/v2/container/gpool"
)
func main() {

View File

@ -4,9 +4,9 @@ import (
"fmt"
"time"
"github.com/gogf/gf/container/gpool"
"github.com/gogf/gf/net/gtcp"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/v2/container/gpool"
"github.com/gogf/gf/v2/net/gtcp"
"github.com/gogf/gf/v2/os/glog"
)
func main() {
@ -14,7 +14,7 @@ func main() {
p := gpool.New(3000*time.Millisecond, func() (interface{}, error) {
return gtcp.NewConn("www.baidu.com:80")
}, func(i interface{}) {
glog.Println("expired")
glog.Print("expired")
i.(*gtcp.Conn).Close()
})
conn, err := p.Get()

View File

@ -4,9 +4,9 @@ import (
"fmt"
"time"
"github.com/gogf/gf/container/gqueue"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/os/gtimer"
"github.com/gogf/gf/v2/container/gqueue"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/os/gtimer"
)
func main() {

View File

@ -4,8 +4,8 @@ import (
"fmt"
"time"
"github.com/gogf/gf/container/gqueue"
"github.com/gogf/gf/os/gtimer"
"github.com/gogf/gf/v2/container/gqueue"
"github.com/gogf/gf/v2/os/gtimer"
)
func main() {

View File

@ -4,9 +4,9 @@ import (
"fmt"
"time"
"github.com/gogf/gf/container/gqueue"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/os/gtimer"
"github.com/gogf/gf/v2/container/gqueue"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/os/gtimer"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gring"
"github.com/gogf/gf/v2/container/gring"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gring"
"github.com/gogf/gf/v2/container/gring"
)
type Player struct {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gset"
"github.com/gogf/gf/v2/container/gset"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gset"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/gset"
"github.com/gogf/gf/v2/container/gset"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/gset"
"github.com/gogf/gf/v2/container/gset"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gtree"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gtree"
"github.com/gogf/gf/v2/container/gtree"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gtree"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/v2/container/gtree"
"github.com/gogf/gf/v2/util/gutil"
)
func main() {

View File

@ -1,7 +1,7 @@
package main
import (
"github.com/gogf/gf/container/gtree"
"github.com/gogf/gf/v2/container/gtree"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/v2/container/gtype"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/v2/container/gtype"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/v2/container/gtype"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -1,15 +1,10 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package driver
import (
"context"
"database/sql"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/os/gtime"
)
// MyDriver is a custom database driver, which is used for testing only.
@ -47,9 +42,9 @@ func (d *MyDriver) New(core *gdb.Core, node *gdb.ConfigNode) (gdb.DB, error) {
// DoQuery commits the sql string and its arguments to underlying driver
// through given link object and returns the execution result.
func (d *MyDriver) DoQuery(link gdb.Link, sql string, args ...interface{}) (rows *sql.Rows, err error) {
func (d *MyDriver) DoQuery(ctx context.Context, link gdb.Link, sql string, args ...interface{}) (rows *sql.Rows, err error) {
tsMilli := gtime.TimestampMilli()
rows, err = d.DriverMysql.DoQuery(link, sql, args...)
rows, err = d.DriverMysql.DoQuery(ctx, link, sql, args...)
link.Exec(
"INSERT INTO `monitor`(`sql`,`cost`,`time`,`error`) VALUES(?,?,?,?)",
gdb.FormatSqlWithArgs(sql, args),
@ -62,9 +57,9 @@ func (d *MyDriver) DoQuery(link gdb.Link, sql string, args ...interface{}) (rows
// DoExec commits the query string and its arguments to underlying driver
// through given link object and returns the execution result.
func (d *MyDriver) DoExec(link gdb.Link, sql string, args ...interface{}) (result sql.Result, err error) {
func (d *MyDriver) DoExec(ctx context.Context, link gdb.Link, sql string, args ...interface{}) (result sql.Result, err error) {
tsMilli := gtime.TimestampMilli()
result, err = d.DriverMysql.DoExec(link, sql, args...)
result, err = d.DriverMysql.DoExec(ctx, link, sql, args...)
link.Exec(
"INSERT INTO `monitor`(`sql`,`cost`,`time`,`error`) VALUES(?,?,?,?)",
gdb.FormatSqlWithArgs(sql, args),

View File

@ -2,22 +2,22 @@ package main
import (
"fmt"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/v2/os/gtime"
_ "github.com/denisenkom/go-mssqldb"
"github.com/gogf/gf/frame/g"
//_ "github.com/denisenkom/go-mssqldb"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
type Table2 struct {
Id string `orm:"id;pr" json:"id"` //ID
Createtime gtime.Time `orm:"createtime" json:"createtime"` //创建时间
Updatetime gtime.Time `orm:"updatetime" json:"updatetime"` //更新时间
CreateTime gtime.Time `orm:"createtime" json:"createtime"` //创建时间
UpdateTime gtime.Time `orm:"updatetime" json:"updatetime"` //更新时间
}
var table2 Table2
err := g.DB().Table("table2").Where("id=?", 1).Struct(&table2)
err := g.DB().Model("table2").Where("id", 1).Scan(&table2)
if err != nil {
panic(err)
}
fmt.Println(table2.Createtime)
fmt.Println(table2.CreateTime)
}

View File

@ -5,8 +5,8 @@ import (
"time"
//_ "github.com/denisenkom/go-mssqldb"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// 本文件用于gf框架的mssql数据库操作示例不作为单元测试使用

View File

@ -4,7 +4,7 @@
[database.logger]
Level = "all"
Stdout = true
CtxKeys = ["Trace-Id"]
CtxKeys = ["RequestId"]
[database.default]
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
debug = true

View File

@ -1,7 +1,8 @@
package main
import (
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/os/gctx"
"sync"
"time"
)
@ -24,13 +25,16 @@ func init() {
}
func main() {
wg := sync.WaitGroup{}
var (
wg = sync.WaitGroup{}
ctx = gctx.New()
)
for i := 0; i < 100000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(10 * time.Second)
db.Table("user").Where("id=1").All()
db.Ctx(ctx).Model("user").Where("id=1").All()
}()
}
wg.Wait()

View File

@ -2,27 +2,23 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
// 开启调试模式以便于记录所有执行的SQL
db.SetDebug(true)
r, e := db.GetAll("SELECT * from `user` where id in(?)", g.Slice{})
r, e := db.Ctx(ctx).GetAll("SELECT * from `user` where id in(?)", g.Slice{})
if e != nil {
fmt.Println(e)
}
if r != nil {
fmt.Println(r)
}
return
//r, e := db.Table("user").Where("id in(?)", g.Slice{}).All()
//if e != nil {
// fmt.Println(e)
//}
//if r != nil {
// fmt.Println(r.List())
//}
}

View File

@ -1,13 +1,19 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
db.Table("user").Where("nickname like ? and passport like ?", g.Slice{"T3", "t3"}).OrderBy("id asc").All()
db.Ctx(ctx).Model("user").
Where("nickname like ? and passport like ?", g.Slice{"T3", "t3"}).
OrderAsc("id").All()
conditions := g.Map{
"nickname like ?": "%T%",
@ -16,8 +22,8 @@ func main() {
"create_time > ?": 0,
"id in(?)": g.Slice{1, 2, 3},
}
db.Table("user").Where(conditions).OrderBy("id asc").All()
db.Ctx(ctx).Model("user").Where(conditions).OrderAsc("id").All()
var params []interface{}
db.Table("user").Where("1=1", params).OrderBy("id asc").All()
db.Ctx(ctx).Model("user").Where("1=1", params).OrderAsc("id").All()
}

View File

@ -2,11 +2,15 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
db.SetDebug(true)
list := make(g.List, 0)
for i := 0; i < 100; i++ {
@ -14,7 +18,7 @@ func main() {
"name": fmt.Sprintf(`name_%d`, i),
})
}
r, e := db.Table("user").Data(list).Batch(2).Insert()
r, e := db.Ctx(ctx).Model("user").Data(list).Batch(2).Insert()
if e != nil {
panic(e)
}

View File

@ -2,10 +2,11 @@ package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/crypto/gaes"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/crypto/gaes"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
@ -19,6 +20,9 @@ func main() {
Role: "master",
Charset: "utf8",
})
var (
ctx = gctx.New()
)
db, err := gdb.New()
if err != nil {
panic(err)
@ -33,7 +37,7 @@ func main() {
}
// 写入
r, err := db.Table("user").Data(g.Map{
r, err := db.Ctx(ctx).Model("user").Data(g.Map{
"uid": 1,
"name": encryptedName,
}).Save()
@ -43,9 +47,9 @@ func main() {
fmt.Println(r.RowsAffected())
// 查询
one, err := db.Table("user").Where("name=?", encryptedName).One()
one, err := db.Ctx(ctx).Model("user").Where("name=?", encryptedName).One()
if err != nil {
fmt.Println(err)
}
fmt.Println(one.ToMap())
fmt.Println(one.Map())
}

View File

@ -2,19 +2,23 @@ package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
db.SetDebug(true)
r, e := db.Table("test").All()
r, e := db.Ctx(ctx).Model("test").All()
if e != nil {
panic(e)
}
if r != nil {
fmt.Println(r.ToList())
fmt.Println(r.List())
}
}

View File

@ -1,8 +1,9 @@
package main
import (
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/util/gutil"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/util/gutil"
"time"
)
@ -17,6 +18,9 @@ func main() {
Role: "master",
Charset: "utf8",
})
var (
ctx = gctx.New()
)
db, err := gdb.New()
if err != nil {
panic(err)
@ -27,7 +31,7 @@ func main() {
// 执行2次查询并将查询结果缓存3秒并可执行缓存名称(可选)
for i := 0; i < 3; i++ {
r, _ := db.Table("user").Cache(3000*time.Second).Where("id=?", 1).One()
r, _ := db.Ctx(ctx).Model("user").Cache(3000*time.Second).Where("id=?", 1).One()
gutil.Dump(r.Map())
}

View File

@ -1,12 +1,12 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
// error!
r, err := g.DB().Table("user").Where(g.Map{
r, err := g.DB().Model("user").Where(g.Map{
"or": g.Map{
"nickname": "jim",
"create_time > ": "2019-10-01",

View File

@ -3,11 +3,11 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
if r, err := g.DB().Model("user").Where("uid=?", 1).One(); err == nil {
fmt.Println(r["uid"].Int())
fmt.Println(r["name"].String())
} else {

View File

@ -3,12 +3,12 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
g.Config().SetFileName("config2.toml")
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
if r, err := g.DB().Model("user").Where("uid=?", 1).One(); err == nil {
fmt.Println(r["uid"].Int())
fmt.Println(r["name"].String())
} else {

View File

@ -3,19 +3,19 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
g.Config().SetFileName("config3.toml")
if r, err := g.DB().Table("user").Where("uid=?", 1).One(); err == nil {
if r, err := g.DB().Model("user").Where("uid=?", 1).One(); err == nil {
fmt.Println(r["uid"].Int())
fmt.Println(r["name"].String())
} else {
fmt.Println(err)
}
if r, err := g.DB("user").Table("user").Where("uid=?", 1).One(); err == nil {
if r, err := g.DB("user").Model("user").Where("uid=?", 1).One(); err == nil {
fmt.Println(r["uid"].Int())
fmt.Println(r["name"].String())
} else {

View File

@ -2,11 +2,11 @@ package main
import (
"context"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
ctx := context.WithValue(context.Background(), "RequestId", "123456789")
_, err := g.DB().Ctx(ctx).Query("SELECT 1")
if err != nil {
panic(err)

View File

@ -2,11 +2,11 @@ package main
import (
"context"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
ctx := context.WithValue(context.Background(), "RequestId", "123456789")
_, err := g.DB().Model("user").Ctx(ctx).All()
if err != nil {
panic(err)

View File

@ -2,23 +2,20 @@ package main
import (
"fmt"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
)
func main() {
db := g.Database()
var (
db = g.DB()
ctx = gctx.New()
)
db.SetDebug(true)
//r, err := db.Table("user").Data("create_time", gtime.Now().String()).Insert()
//if err == nil {
// fmt.Println(r.LastInsertId())
//} else {
// panic(err)
//}
r, err := db.Table("user").Data(g.Map{
r, err := db.Ctx(ctx).Model("user").Data(g.Map{
"name": "john",
"create_time": gtime.Now().String(),
}).Insert()

View File

@ -1,9 +1,10 @@
package main
import (
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/glog"
)
func main() {
@ -17,6 +18,9 @@ func main() {
Role: "master",
Charset: "utf8",
})
var (
ctx = gctx.New()
)
db, err := gdb.New()
if err != nil {
panic(err)
@ -27,11 +31,11 @@ func main() {
// 执行3条SQL查询
for i := 1; i <= 3; i++ {
db.Table("user").Where("uid=?", i).One()
db.Ctx(ctx).Model("user").Where("uid=?", i).One()
}
// 构造一条错误查询
db.Table("user").Where("no_such_field=?", "just_test").One()
db.Model("user").Where("no_such_field=?", "just_test").One()
db.Table("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
db.Ctx(ctx).Model("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
}

View File

@ -1,18 +1,22 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
// 执行3条SQL查询
for i := 1; i <= 3; i++ {
db.Table("user").Where("id=?", i).One()
db.Ctx(ctx).Model("user").Where("id=?", i).One()
}
// 构造一条错误查询
db.Table("user").Where("no_such_field=?", "just_test").One()
db.Ctx(ctx).Model("user").Where("no_such_field=?", "just_test").One()
db.Table("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
db.Ctx(ctx).Model("user").Data(g.Map{"name": "smith"}).Where("uid=?", 1).Save()
}

View File

@ -1,7 +1,7 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -2,23 +2,23 @@ package main
import (
"fmt"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
//db := g.DB()
gdb.AddDefaultConfigNode(gdb.ConfigNode{
LinkInfo: "root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local",
Type: "mysql",
Charset: "utf8",
Link: "root:12345678@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Local",
Type: "mysql",
Charset: "utf8",
})
db, _ := gdb.New()
db.SetDebug(true)
r, e := db.Table("user").Data(g.Map{
r, e := db.Model("user").Data(g.Map{
"create_at": "now()",
}).Unscoped().Insert()
if e != nil {

View File

@ -3,12 +3,12 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
var (
tableName = "orders"
dao = g.DB().Table(tableName).Safe()
dao = g.DB().Model(tableName).Safe()
)
type OrderServiceEntity struct {

View File

@ -2,10 +2,11 @@ package main
import (
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/encoding/gparser"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
@ -19,19 +20,22 @@ func main() {
Role: "master",
Charset: "utf8",
})
db := g.DB()
one, err := db.Table("user").Where("id=?", 1).One()
var (
db = g.DB()
ctx = gctx.New()
)
one, err := db.Ctx(ctx).Model("user").Where("id=?", 1).One()
if err != nil {
panic(err)
}
// 使用内置方法转换为json/xml
fmt.Println(one.ToJson())
fmt.Println(one.ToXml())
fmt.Println(one.Json())
fmt.Println(one.Xml())
// 自定义方法方法转换为json/xml
jsonContent, _ := gparser.VarToJson(one.ToMap())
jsonContent, _ := gjson.New(one.Map()).ToJson()
fmt.Println(string(jsonContent))
xmlContent, _ := gparser.VarToXml(one.ToMap())
xmlContent, _ := gjson.New(one.Map()).ToJson()
fmt.Println(string(xmlContent))
}

View File

@ -1,20 +1,24 @@
package main
import (
"github.com/gogf/gf/v2/os/gctx"
"time"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
// 开启调试模式以便于记录所有执行的SQL
db.SetDebug(true)
for {
for i := 0; i < 10; i++ {
go db.Table("user").All()
go db.Ctx(ctx).Model("user").All()
}
time.Sleep(time.Millisecond * 100)
}

View File

@ -2,15 +2,19 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"time"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
db.SetDebug(true)
for {
r, err := db.Table("user").All()
r, err := db.Ctx(ctx).Model("user").All()
fmt.Println(err)
fmt.Println(r)
time.Sleep(time.Second * 10)

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
@ -17,7 +17,7 @@ func main() {
}
user := (*User)(nil)
fmt.Println(user)
err := db.Table("test").Where("id=1").Struct(&user)
err := db.Model("test").Where("id=1").Scan(&user)
fmt.Println(err)
fmt.Println(user)
}

View File

@ -1,14 +1,18 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
db.SetDebug(true)
tables, err := db.Tables()
tables, err := db.Tables(ctx)
if err != nil {
panic(err)
}

View File

@ -1,21 +1,25 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
db := g.DB()
var (
db = g.DB()
ctx = gctx.New()
)
db.SetDebug(true)
tables, e := db.Tables()
tables, e := db.Tables(ctx)
if e != nil {
panic(e)
}
if tables != nil {
g.Dump(tables)
for _, table := range tables {
fields, err := db.TableFields(table)
fields, err := db.TableFields(ctx, table)
if err != nil {
panic(err)
}

View File

@ -1,7 +1,7 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -1,26 +1,29 @@
package main
import (
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
)
func main() {
var (
err error
db = g.DB()
ctx = gctx.New()
table = "user"
)
if err = db.Transaction(func(tx *gdb.TX) error {
if err = db.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
// Nested transaction 1.
if err = tx.Transaction(func(tx *gdb.TX) error {
if err = tx.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
_, err = tx.Model(table).Data(g.Map{"id": 1, "name": "john"}).Insert()
return err
}); err != nil {
return err
}
// Nested transaction 2, panic.
if err = tx.Transaction(func(tx *gdb.TX) error {
if err = tx.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
_, err = tx.Model(table).Data(g.Map{"id": 2, "name": "smith"}).Insert()
// Create a panic that can make this transaction rollback automatically.
panic("error")

View File

@ -1,7 +1,7 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,16 +3,16 @@ package main
import (
"database/sql"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
db := g.DB()
table := "medicine_clinics_upload_yinchuan"
list, err := db.Table(table).All()
list, err := db.Model(table).All()
if err != nil && err != sql.ErrNoRows {
panic(err)
}

View File

@ -3,13 +3,13 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
db := g.DB()
db.SetDebug(true)
result, err := db.Table("pw_passageway m,pw_template t").Data("t.status", 99).Where("m.templateId=t.id AND m.status = 0").Update()
result, err := db.Model("pw_passageway m,pw_template t").Data("t.status", 99).Where("m.templateId=t.id AND m.status = 0").Update()
if err != nil {
panic(err)
}

View File

@ -2,11 +2,11 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
one, err := g.DB().Table("carlist c").
one, err := g.Model("carlist c").
LeftJoin("cardetail d", "c.postid=d.carid").
Where("c.postid", "142039140032006").
Fields("c.*,d.*").One()

View File

@ -1,10 +1,12 @@
package main
import (
"context"
"fmt"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gmeta"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/util/gmeta"
)
func main() {
@ -30,13 +32,13 @@ func main() {
}
db := g.DB()
db.Transaction(func(tx *gdb.TX) error {
err := db.Transaction(gctx.New(), func(ctx context.Context, tx *gdb.TX) error {
for i := 1; i <= 5; i++ {
// User.
user := User{
Name: fmt.Sprintf(`name_%d`, i),
}
lastInsertId, err := db.Model(user).Data(user).OmitEmpty().InsertAndGetId()
lastInsertId, err := db.Ctx(ctx).Model(user).Data(user).OmitEmpty().InsertAndGetId()
if err != nil {
return err
}
@ -45,7 +47,7 @@ func main() {
Uid: int(lastInsertId),
Address: fmt.Sprintf(`address_%d`, lastInsertId),
}
_, err = db.Model(userDetail).Data(userDetail).OmitEmpty().Insert()
_, err = db.Ctx(ctx).Model(userDetail).Data(userDetail).OmitEmpty().Insert()
if err != nil {
return err
}
@ -55,7 +57,7 @@ func main() {
Uid: int(lastInsertId),
Score: j,
}
_, err = db.Model(userScore).Data(userScore).OmitEmpty().Insert()
_, err = db.Ctx(ctx).Model(userScore).Data(userScore).OmitEmpty().Insert()
if err != nil {
return err
}
@ -63,4 +65,5 @@ func main() {
}
return nil
})
fmt.Println(err)
}

View File

@ -1,8 +1,8 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gmeta"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gmeta"
)
func main() {

View File

@ -1,7 +1,7 @@
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
"time"
)
@ -9,7 +9,7 @@ func test1() {
db := g.DB()
db.SetDebug(true)
time.Sleep(1 * time.Minute)
r, e := db.Table("test").Where("id", 10000).Count()
r, e := db.Model("test").Where("id", 10000).Count()
if e != nil {
panic(e)
}
@ -19,7 +19,7 @@ func test1() {
func test2() {
db := g.DB()
db.SetDebug(true)
dao := db.Table("test").Safe()
dao := db.Model("test").Safe()
time.Sleep(1 * time.Minute)
r, e := dao.Where("id", 10000).Count()
if e != nil {

View File

@ -5,8 +5,8 @@ import (
"time"
//_ "github.com/mattn/go-oci8"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
// 本文件用于gf框架的mysql数据库操作示例不作为单元测试使用

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/database/gdb"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
_ "github.com/mattn/go-sqlite3"
)

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/database/gredis"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/util/gconv"
)
// 使用原生gredis.New操作redis但是注意需要自己调用Close方法关闭redis链接池

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
// 使用框架封装的g.Redis()方法获得redis操作对象单例不需要开发者显示调用Close方法

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -2,7 +2,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/v2/debug/gdebug"
)
func main() {

View File

@ -2,7 +2,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/v2/debug/gdebug"
)
func main() {

View File

@ -3,7 +3,7 @@ package gbase64
import (
"fmt"
"github.com/gogf/gf/encoding/gbase64"
"github.com/gogf/gf/v2/encoding/gbase64"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gbinary"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/v2/encoding/gbinary"
"github.com/gogf/gf/v2/os/glog"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gbinary"
"github.com/gogf/gf/v2/encoding/gbinary"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gbinary"
"github.com/gogf/gf/v2/encoding/gbinary"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/v2/frame/g"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gcharset"
"github.com/gogf/gf/v2/encoding/gcharset"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gcompress"
"github.com/gogf/gf/v2/encoding/gcompress"
)
func main() {

View File

@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gcompress"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/v2/encoding/gcompress"
"github.com/gogf/gf/v2/os/gfile"
)
func main() {

View File

@ -3,7 +3,7 @@ package main
import (
"archive/zip"
"fmt"
"github.com/gogf/gf/encoding/gcompress"
"github.com/gogf/gf/v2/encoding/gcompress"
"io"
"os"
"path/filepath"

View File

@ -4,7 +4,7 @@ import (
"fmt"
"strconv"
"github.com/gogf/gf/encoding/ghash"
"github.com/gogf/gf/v2/encoding/ghash"
)
func main() {

View File

@ -2,7 +2,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gini"
"github.com/gogf/gf/v2/encoding/gini"
)
func main() {

View File

@ -3,9 +3,9 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/os/gtime"
)
func getByPattern() {

View File

@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/v2/encoding/gjson"
)
type XinYanModel struct {

View File

@ -1,9 +1,9 @@
package main
import (
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
)
type GameUser struct {

View File

@ -2,7 +2,7 @@ package main
import (
"fmt"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/v2/encoding/gjson"
)
func main() {

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