Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
3731088b
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,发现更多精彩内容 >>
提交
3731088b
编写于
1月 10, 2015
作者:
P
Peter Fern
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add GroupBy to QuerySeter
Adds support for GroupBy to QuerySeter SELECT operations.
上级
5d8187d0
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
59 addition
and
3 deletion
+59
-3
orm/db.go
orm/db.go
+6
-3
orm/db_tables.go
orm/db_tables.go
+24
-0
orm/orm_queryset.go
orm/orm_queryset.go
+7
-0
orm/orm_test.go
orm/orm_test.go
+21
-0
orm/types.go
orm/types.go
+1
-0
未找到文件。
orm/db.go
浏览文件 @
3731088b
...
...
@@ -800,6 +800,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
tables
.
parseRelated
(
qs
.
related
,
qs
.
relDepth
)
where
,
args
:=
tables
.
getCondSql
(
cond
,
false
,
tz
)
groupBy
:=
tables
.
getGroupSql
(
qs
.
groups
)
orderBy
:=
tables
.
getOrderSql
(
qs
.
orders
)
limit
:=
tables
.
getLimitSql
(
mi
,
offset
,
rlimit
)
join
:=
tables
.
getJoinSql
()
...
...
@@ -812,7 +813,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
}
}
query
:=
fmt
.
Sprintf
(
"SELECT %s FROM %s%s%s T0 %s%s%s%s
"
,
sels
,
Q
,
mi
.
table
,
Q
,
join
,
where
,
orderBy
,
limit
)
query
:=
fmt
.
Sprintf
(
"SELECT %s FROM %s%s%s T0 %s%s%s%s
%s"
,
sels
,
Q
,
mi
.
table
,
Q
,
join
,
where
,
groupBy
,
orderBy
,
limit
)
d
.
ins
.
ReplaceMarks
(
&
query
)
...
...
@@ -936,12 +937,13 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition
tables
.
parseRelated
(
qs
.
related
,
qs
.
relDepth
)
where
,
args
:=
tables
.
getCondSql
(
cond
,
false
,
tz
)
groupBy
:=
tables
.
getGroupSql
(
qs
.
groups
)
tables
.
getOrderSql
(
qs
.
orders
)
join
:=
tables
.
getJoinSql
()
Q
:=
d
.
ins
.
TableQuote
()
query
:=
fmt
.
Sprintf
(
"SELECT COUNT(*) FROM %s%s%s T0 %s%s
"
,
Q
,
mi
.
table
,
Q
,
join
,
where
)
query
:=
fmt
.
Sprintf
(
"SELECT COUNT(*) FROM %s%s%s T0 %s%s
%s"
,
Q
,
mi
.
table
,
Q
,
join
,
where
,
groupBy
)
d
.
ins
.
ReplaceMarks
(
&
query
)
...
...
@@ -1442,13 +1444,14 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
}
where
,
args
:=
tables
.
getCondSql
(
cond
,
false
,
tz
)
groupBy
:=
tables
.
getGroupSql
(
qs
.
groups
)
orderBy
:=
tables
.
getOrderSql
(
qs
.
orders
)
limit
:=
tables
.
getLimitSql
(
mi
,
qs
.
offset
,
qs
.
limit
)
join
:=
tables
.
getJoinSql
()
sels
:=
strings
.
Join
(
cols
,
", "
)
query
:=
fmt
.
Sprintf
(
"SELECT %s FROM %s%s%s T0 %s%s%s%s
"
,
sels
,
Q
,
mi
.
table
,
Q
,
join
,
where
,
orderBy
,
limit
)
query
:=
fmt
.
Sprintf
(
"SELECT %s FROM %s%s%s T0 %s%s%s%s
%s"
,
sels
,
Q
,
mi
.
table
,
Q
,
join
,
where
,
groupBy
,
orderBy
,
limit
)
d
.
ins
.
ReplaceMarks
(
&
query
)
...
...
orm/db_tables.go
浏览文件 @
3731088b
...
...
@@ -390,6 +390,30 @@ func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (whe
return
}
// generate group sql.
func
(
t
*
dbTables
)
getGroupSql
(
groups
[]
string
)
(
groupSql
string
)
{
if
len
(
groups
)
==
0
{
return
}
Q
:=
t
.
base
.
TableQuote
()
groupSqls
:=
make
([]
string
,
0
,
len
(
groups
))
for
_
,
group
:=
range
groups
{
exprs
:=
strings
.
Split
(
group
,
ExprSep
)
index
,
_
,
fi
,
suc
:=
t
.
parseExprs
(
t
.
mi
,
exprs
)
if
suc
==
false
{
panic
(
fmt
.
Errorf
(
"unknown field/column name `%s`"
,
strings
.
Join
(
exprs
,
ExprSep
)))
}
groupSqls
=
append
(
groupSqls
,
fmt
.
Sprintf
(
"%s.%s%s%s"
,
index
,
Q
,
fi
.
column
,
Q
))
}
groupSql
=
fmt
.
Sprintf
(
"GROUP BY %s "
,
strings
.
Join
(
groupSqls
,
", "
))
return
}
// generate order sql.
func
(
t
*
dbTables
)
getOrderSql
(
orders
[]
string
)
(
orderSql
string
)
{
if
len
(
orders
)
==
0
{
...
...
orm/orm_queryset.go
浏览文件 @
3731088b
...
...
@@ -60,6 +60,7 @@ type querySet struct {
relDepth
int
limit
int64
offset
int64
groups
[]
string
orders
[]
string
orm
*
orm
}
...
...
@@ -105,6 +106,12 @@ func (o querySet) Offset(offset interface{}) QuerySeter {
return
&
o
}
// add GROUP expression.
func
(
o
querySet
)
GroupBy
(
exprs
...
string
)
QuerySeter
{
o
.
groups
=
exprs
return
&
o
}
// add ORDER expression.
// "column" means ASC, "-column" means DESC.
func
(
o
querySet
)
OrderBy
(
exprs
...
string
)
QuerySeter
{
...
...
orm/orm_test.go
浏览文件 @
3731088b
...
...
@@ -845,6 +845,27 @@ func TestOffset(t *testing.T) {
throwFail
(
t
,
AssertIs
(
num
,
2
))
}
func
TestGroupBy
(
t
*
testing
.
T
)
{
var
users
[]
*
User
var
maps
[]
Params
qs
:=
dORM
.
QueryTable
(
"user"
)
num
,
err
:=
qs
.
GroupBy
(
"is_staff"
)
.
Filter
(
"user_name"
,
"nobody"
)
.
Count
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
1
))
num
,
err
=
qs
.
GroupBy
(
"is_staff"
)
.
Count
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
2
))
num
,
err
=
qs
.
GroupBy
(
"is_staff"
)
.
Values
(
&
maps
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
2
))
num
,
err
=
qs
.
GroupBy
(
"profile__age"
)
.
Filter
(
"user_name"
,
"astaxie"
)
.
All
(
&
users
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
1
))
}
func
TestOrderBy
(
t
*
testing
.
T
)
{
qs
:=
dORM
.
QueryTable
(
"user"
)
num
,
err
:=
qs
.
OrderBy
(
"-status"
)
.
Filter
(
"user_name"
,
"nobody"
)
.
Count
()
...
...
orm/types.go
浏览文件 @
3731088b
...
...
@@ -67,6 +67,7 @@ type QuerySeter interface {
SetCond
(
*
Condition
)
QuerySeter
Limit
(
interface
{},
...
interface
{})
QuerySeter
Offset
(
interface
{})
QuerySeter
GroupBy
(
...
string
)
QuerySeter
OrderBy
(
...
string
)
QuerySeter
RelatedSel
(
...
interface
{})
QuerySeter
Count
()
(
int64
,
error
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录