Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
2e75c04f
Commit
2e75c04f
authored
Sep 04, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1345 from f0r/develop
为querySeter添加GroupBy方法
parents
9038cdfa
a074df9c
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
2 deletions
+36
-2
db.go
orm/db.go
+4
-2
db_tables.go
orm/db_tables.go
+24
-0
orm_queryset.go
orm/orm_queryset.go
+7
-0
types.go
orm/types.go
+1
-0
No files found.
orm/db.go
View file @
2e75c04f
...
...
@@ -802,6 +802,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
()
...
...
@@ -818,7 +819,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
if
qs
.
distinct
{
sqlSelect
+=
" DISTINCT"
}
query
:=
fmt
.
Sprintf
(
"%s %s FROM %s%s%s T0 %s%s%s%s
"
,
sqlSelect
,
sels
,
Q
,
mi
.
table
,
Q
,
join
,
where
,
orderBy
,
limit
)
query
:=
fmt
.
Sprintf
(
"%s %s FROM %s%s%s T0 %s%s%s%s
%s"
,
sqlSelect
,
sels
,
Q
,
mi
.
table
,
Q
,
join
,
where
,
groupBy
,
orderBy
,
limit
)
d
.
ins
.
ReplaceMarks
(
&
query
)
...
...
@@ -1448,13 +1449,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
View file @
2e75c04f
...
...
@@ -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
View file @
2e75c04f
...
...
@@ -60,6 +60,7 @@ type querySet struct {
relDepth
int
limit
int64
offset
int64
groups
[]
string
orders
[]
string
distinct
bool
orm
*
orm
...
...
@@ -106,6 +107,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/types.go
View file @
2e75c04f
...
...
@@ -66,6 +66,7 @@ type QuerySeter interface {
SetCond
(
*
Condition
)
QuerySeter
Limit
(
interface
{},
...
interface
{})
QuerySeter
Offset
(
interface
{})
QuerySeter
GroupBy
(
...
string
)
QuerySeter
OrderBy
(
...
string
)
QuerySeter
Distinct
()
QuerySeter
RelatedSel
(
...
interface
{})
QuerySeter
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment