Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
e56d1b71
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e56d1b71
编写于
11月 18, 2018
作者:
N
nlimpid
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add context for db operation
上级
f64e6b72
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
88 addition
and
21 deletion
+88
-21
orm/db.go
orm/db.go
+32
-10
orm/orm_log.go
orm/orm_log.go
+28
-0
orm/orm_queryset.go
orm/orm_queryset.go
+21
-11
orm/types.go
orm/types.go
+7
-0
未找到文件。
orm/db.go
浏览文件 @
e56d1b71
...
...
@@ -762,7 +762,13 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
}
d
.
ins
.
ReplaceMarks
(
&
query
)
res
,
err
:=
q
.
Exec
(
query
,
values
...
)
var
err
error
var
res
sql
.
Result
if
qs
.
forContext
{
res
,
err
=
q
.
ExecContext
(
qs
.
ctx
,
query
,
values
...
)
}
else
{
res
,
err
=
q
.
Exec
(
query
,
values
...
)
}
if
err
==
nil
{
return
res
.
RowsAffected
()
}
...
...
@@ -851,11 +857,16 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
for
i
:=
range
marks
{
marks
[
i
]
=
"?"
}
sql
:=
fmt
.
Sprintf
(
"IN (%s)"
,
strings
.
Join
(
marks
,
", "
))
query
=
fmt
.
Sprintf
(
"DELETE FROM %s%s%s WHERE %s%s%s %s"
,
Q
,
mi
.
table
,
Q
,
Q
,
mi
.
fields
.
pk
.
column
,
Q
,
sql
)
sql
In
:=
fmt
.
Sprintf
(
"IN (%s)"
,
strings
.
Join
(
marks
,
", "
))
query
=
fmt
.
Sprintf
(
"DELETE FROM %s%s%s WHERE %s%s%s %s"
,
Q
,
mi
.
table
,
Q
,
Q
,
mi
.
fields
.
pk
.
column
,
Q
,
sql
In
)
d
.
ins
.
ReplaceMarks
(
&
query
)
res
,
err
:=
q
.
Exec
(
query
,
args
...
)
var
res
sql
.
Result
if
qs
.
forContext
{
res
,
err
=
q
.
ExecContext
(
qs
.
ctx
,
query
,
args
...
)
}
else
{
res
,
err
=
q
.
Exec
(
query
,
args
...
)
}
if
err
==
nil
{
num
,
err
:=
res
.
RowsAffected
()
if
err
!=
nil
{
...
...
@@ -978,11 +989,18 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
d
.
ins
.
ReplaceMarks
(
&
query
)
var
rs
*
sql
.
Rows
r
,
err
:=
q
.
Query
(
query
,
args
...
)
if
err
!=
nil
{
return
0
,
err
var
err
error
if
qs
.
forContext
{
rs
,
err
=
q
.
QueryContext
(
qs
.
ctx
,
query
,
args
...
)
if
err
!=
nil
{
return
0
,
err
}
}
else
{
rs
,
err
=
q
.
Query
(
query
,
args
...
)
if
err
!=
nil
{
return
0
,
err
}
}
rs
=
r
refs
:=
make
([]
interface
{},
colsNum
)
for
i
:=
range
refs
{
...
...
@@ -1111,8 +1129,12 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition
d
.
ins
.
ReplaceMarks
(
&
query
)
row
:=
q
.
QueryRow
(
query
,
args
...
)
var
row
*
sql
.
Row
if
qs
.
forContext
{
row
=
q
.
QueryRowContext
(
qs
.
ctx
,
query
,
args
...
)
}
else
{
row
=
q
.
QueryRow
(
query
,
args
...
)
}
err
=
row
.
Scan
(
&
cnt
)
return
}
...
...
orm/orm_log.go
浏览文件 @
e56d1b71
...
...
@@ -123,6 +123,13 @@ func (d *dbQueryLog) Prepare(query string) (*sql.Stmt, error) {
return
stmt
,
err
}
func
(
d
*
dbQueryLog
)
PrepareContext
(
ctx
context
.
Context
,
query
string
)
(
*
sql
.
Stmt
,
error
)
{
a
:=
time
.
Now
()
stmt
,
err
:=
d
.
db
.
PrepareContext
(
ctx
,
query
)
debugLogQueies
(
d
.
alias
,
"db.Prepare"
,
query
,
a
,
err
)
return
stmt
,
err
}
func
(
d
*
dbQueryLog
)
Exec
(
query
string
,
args
...
interface
{})
(
sql
.
Result
,
error
)
{
a
:=
time
.
Now
()
res
,
err
:=
d
.
db
.
Exec
(
query
,
args
...
)
...
...
@@ -130,6 +137,13 @@ func (d *dbQueryLog) Exec(query string, args ...interface{}) (sql.Result, error)
return
res
,
err
}
func
(
d
*
dbQueryLog
)
ExecContext
(
ctx
context
.
Context
,
query
string
,
args
...
interface
{})
(
sql
.
Result
,
error
)
{
a
:=
time
.
Now
()
res
,
err
:=
d
.
db
.
ExecContext
(
ctx
,
query
,
args
...
)
debugLogQueies
(
d
.
alias
,
"db.Exec"
,
query
,
a
,
err
,
args
...
)
return
res
,
err
}
func
(
d
*
dbQueryLog
)
Query
(
query
string
,
args
...
interface
{})
(
*
sql
.
Rows
,
error
)
{
a
:=
time
.
Now
()
res
,
err
:=
d
.
db
.
Query
(
query
,
args
...
)
...
...
@@ -137,6 +151,13 @@ func (d *dbQueryLog) Query(query string, args ...interface{}) (*sql.Rows, error)
return
res
,
err
}
func
(
d
*
dbQueryLog
)
QueryContext
(
ctx
context
.
Context
,
query
string
,
args
...
interface
{})
(
*
sql
.
Rows
,
error
)
{
a
:=
time
.
Now
()
res
,
err
:=
d
.
db
.
QueryContext
(
ctx
,
query
,
args
...
)
debugLogQueies
(
d
.
alias
,
"db.Query"
,
query
,
a
,
err
,
args
...
)
return
res
,
err
}
func
(
d
*
dbQueryLog
)
QueryRow
(
query
string
,
args
...
interface
{})
*
sql
.
Row
{
a
:=
time
.
Now
()
res
:=
d
.
db
.
QueryRow
(
query
,
args
...
)
...
...
@@ -144,6 +165,13 @@ func (d *dbQueryLog) QueryRow(query string, args ...interface{}) *sql.Row {
return
res
}
func
(
d
*
dbQueryLog
)
QueryRowContext
(
ctx
context
.
Context
,
query
string
,
args
...
interface
{})
*
sql
.
Row
{
a
:=
time
.
Now
()
res
:=
d
.
db
.
QueryRowContext
(
ctx
,
query
,
args
...
)
debugLogQueies
(
d
.
alias
,
"db.QueryRow"
,
query
,
a
,
nil
,
args
...
)
return
res
}
func
(
d
*
dbQueryLog
)
Begin
()
(
*
sql
.
Tx
,
error
)
{
a
:=
time
.
Now
()
tx
,
err
:=
d
.
db
.
(
txer
)
.
Begin
()
...
...
orm/orm_queryset.go
浏览文件 @
e56d1b71
...
...
@@ -15,6 +15,7 @@
package
orm
import
(
"context"
"fmt"
)
...
...
@@ -55,17 +56,19 @@ func ColValue(opt operator, value interface{}) interface{} {
// real query struct
type
querySet
struct
{
mi
*
modelInfo
cond
*
Condition
related
[]
string
relDepth
int
limit
int64
offset
int64
groups
[]
string
orders
[]
string
distinct
bool
forupdate
bool
orm
*
orm
mi
*
modelInfo
cond
*
Condition
related
[]
string
relDepth
int
limit
int64
offset
int64
groups
[]
string
orders
[]
string
distinct
bool
forupdate
bool
orm
*
orm
ctx
context
.
Context
forContext
bool
}
var
_
QuerySeter
=
new
(
querySet
)
...
...
@@ -266,6 +269,13 @@ func (o *querySet) RowsToStruct(ptrStruct interface{}, keyCol, valueCol string)
panic
(
ErrNotImplement
)
}
// set context to QuerySeter.
func
(
o
querySet
)
WithContext
(
ctx
context
.
Context
)
QuerySeter
{
o
.
ctx
=
ctx
o
.
forContext
=
true
return
&
o
}
// create new QuerySeter.
func
newQuerySet
(
orm
*
orm
,
mi
*
modelInfo
)
QuerySeter
{
o
:=
new
(
querySet
)
...
...
orm/types.go
浏览文件 @
e56d1b71
...
...
@@ -390,16 +390,23 @@ type RawSeter interface {
type
stmtQuerier
interface
{
Close
()
error
Exec
(
args
...
interface
{})
(
sql
.
Result
,
error
)
//ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)
Query
(
args
...
interface
{})
(
*
sql
.
Rows
,
error
)
//QueryContext(args ...interface{}) (*sql.Rows, error)
QueryRow
(
args
...
interface
{})
*
sql
.
Row
//QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row
}
// db querier
type
dbQuerier
interface
{
Prepare
(
query
string
)
(
*
sql
.
Stmt
,
error
)
PrepareContext
(
ctx
context
.
Context
,
query
string
)
(
*
sql
.
Stmt
,
error
)
Exec
(
query
string
,
args
...
interface
{})
(
sql
.
Result
,
error
)
ExecContext
(
ctx
context
.
Context
,
query
string
,
args
...
interface
{})
(
sql
.
Result
,
error
)
Query
(
query
string
,
args
...
interface
{})
(
*
sql
.
Rows
,
error
)
QueryContext
(
ctx
context
.
Context
,
query
string
,
args
...
interface
{})
(
*
sql
.
Rows
,
error
)
QueryRow
(
query
string
,
args
...
interface
{})
*
sql
.
Row
QueryRowContext
(
ctx
context
.
Context
,
query
string
,
args
...
interface
{})
*
sql
.
Row
}
// type DB interface {
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录