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
9013f5c6
Commit
9013f5c6
authored
Sep 09, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #808 from ZhengYang/develop
more complete support for sql language
parents
fb0cc558
29b7ff84
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
37 deletions
+80
-37
qb.go
orm/qb.go
+10
-3
qb_mysql.go
orm/qb_mysql.go
+70
-34
No files found.
orm/qb.go
View file @
9013f5c6
...
...
@@ -19,15 +19,22 @@ import "errors"
type
QueryBuilder
interface
{
Select
(
fields
...
string
)
QueryBuilder
From
(
tables
...
string
)
QueryBuilder
Where
(
cond
string
)
QueryBuilder
LimitOffset
(
limit
int
,
offset
int
)
QueryBuilder
InnerJoin
(
table
string
)
QueryBuilder
LeftJoin
(
table
string
)
QueryBuilder
RightJoin
(
table
string
)
QueryBuilder
On
(
cond
string
)
QueryBuilder
Where
(
cond
string
)
QueryBuilder
And
(
cond
string
)
QueryBuilder
Or
(
cond
string
)
QueryBuilder
In
(
vals
...
string
)
QueryBuilder
Subquery
(
query
string
,
rename
string
)
string
OrderBy
(
fields
...
string
)
QueryBuilder
Asc
()
QueryBuilder
Desc
()
QueryBuilder
Limit
(
limit
int
)
QueryBuilder
Offset
(
offset
int
)
QueryBuilder
GroupBy
(
fields
...
string
)
QueryBuilder
Having
(
cond
string
)
QueryBuilder
Subquery
(
sub
string
,
alias
string
)
string
String
()
string
}
...
...
orm/qb_mysql.go
View file @
9013f5c6
...
...
@@ -16,70 +16,106 @@ package orm
import
(
"fmt"
"strconv"
"strings"
)
type
MySQLQueryBuilder
struct
{
Query
String
[]
string
Query
Tokens
[]
string
}
func
(
q
w
*
MySQLQueryBuilder
)
Select
(
fields
...
string
)
QueryBuilder
{
func
(
q
b
*
MySQLQueryBuilder
)
Select
(
fields
...
string
)
QueryBuilder
{
segment
:=
fmt
.
Sprintf
(
"SELECT %s"
,
strings
.
Join
(
fields
,
", "
))
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
segment
)
return
q
w
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
segment
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
From
(
tables
...
string
)
QueryBuilder
{
func
(
q
b
*
MySQLQueryBuilder
)
From
(
tables
...
string
)
QueryBuilder
{
segment
:=
fmt
.
Sprintf
(
"FROM %s"
,
strings
.
Join
(
tables
,
", "
))
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
segment
)
return
q
w
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
segment
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
Where
(
cond
string
)
QueryBuilder
{
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
"WHERE "
+
cond
)
return
q
w
func
(
q
b
*
MySQLQueryBuilder
)
InnerJoin
(
table
string
)
QueryBuilder
{
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"INNER JOIN "
+
table
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
LimitOffset
(
limit
int
,
offset
int
)
QueryBuilder
{
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
fmt
.
Sprintf
(
"LIMIT %d OFFSET %d"
,
limit
,
offset
)
)
return
q
w
func
(
q
b
*
MySQLQueryBuilder
)
LeftJoin
(
table
string
)
QueryBuilder
{
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"LEFT JOIN "
+
table
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
Inner
Join
(
table
string
)
QueryBuilder
{
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
"INNER
JOIN "
+
table
)
return
q
w
func
(
q
b
*
MySQLQueryBuilder
)
Right
Join
(
table
string
)
QueryBuilder
{
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"RIGHT
JOIN "
+
table
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
LeftJoin
(
table
string
)
QueryBuilder
{
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
"LEFT JOIN "
+
table
)
return
q
w
func
(
q
b
*
MySQLQueryBuilder
)
On
(
cond
string
)
QueryBuilder
{
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"ON "
+
cond
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
On
(
cond
string
)
QueryBuilder
{
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
"ON
"
+
cond
)
return
q
w
func
(
q
b
*
MySQLQueryBuilder
)
Where
(
cond
string
)
QueryBuilder
{
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"WHERE
"
+
cond
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
And
(
cond
string
)
QueryBuilder
{
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
"AND "
+
cond
)
return
q
w
func
(
q
b
*
MySQLQueryBuilder
)
And
(
cond
string
)
QueryBuilder
{
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"AND "
+
cond
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
Or
(
cond
string
)
QueryBuilder
{
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
"OR "
+
cond
)
return
q
w
func
(
q
b
*
MySQLQueryBuilder
)
Or
(
cond
string
)
QueryBuilder
{
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"OR "
+
cond
)
return
q
b
}
func
(
q
w
*
MySQLQueryBuilder
)
In
(
vals
...
string
)
QueryBuilder
{
func
(
q
b
*
MySQLQueryBuilder
)
In
(
vals
...
string
)
QueryBuilder
{
segment
:=
fmt
.
Sprintf
(
"IN (%s)"
,
strings
.
Join
(
vals
,
", "
))
q
w
.
QueryString
=
append
(
qw
.
QueryString
,
segment
)
return
q
w
q
b
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
segment
)
return
q
b
}
func
(
qw
*
MySQLQueryBuilder
)
Subquery
(
sub
string
,
alias
string
)
string
{
func
(
qb
*
MySQLQueryBuilder
)
OrderBy
(
fields
...
string
)
QueryBuilder
{
qb
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"ORDER BY "
+
strings
.
Join
(
fields
,
", "
))
return
qb
}
func
(
qb
*
MySQLQueryBuilder
)
Asc
()
QueryBuilder
{
qb
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"ASC"
)
return
qb
}
func
(
qb
*
MySQLQueryBuilder
)
Desc
()
QueryBuilder
{
qb
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"DESC"
)
return
qb
}
func
(
qb
*
MySQLQueryBuilder
)
Limit
(
limit
int
)
QueryBuilder
{
qb
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"LIMIT "
+
strconv
.
Itoa
(
limit
))
return
qb
}
func
(
qb
*
MySQLQueryBuilder
)
Offset
(
offset
int
)
QueryBuilder
{
qb
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"OFFSET "
+
strconv
.
Itoa
(
offset
))
return
qb
}
func
(
qb
*
MySQLQueryBuilder
)
GroupBy
(
fields
...
string
)
QueryBuilder
{
qb
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"GROUP BY "
+
strings
.
Join
(
fields
,
", "
))
return
qb
}
func
(
qb
*
MySQLQueryBuilder
)
Having
(
cond
string
)
QueryBuilder
{
qb
.
QueryTokens
=
append
(
qb
.
QueryTokens
,
"HAVING "
+
cond
)
return
qb
}
func
(
qb
*
MySQLQueryBuilder
)
Subquery
(
sub
string
,
alias
string
)
string
{
return
fmt
.
Sprintf
(
"(%s) AS %s"
,
sub
,
alias
)
}
func
(
q
w
*
MySQLQueryBuilder
)
String
()
string
{
return
strings
.
Join
(
q
w
.
QueryString
,
" "
)
func
(
q
b
*
MySQLQueryBuilder
)
String
()
string
{
return
strings
.
Join
(
q
b
.
QueryTokens
,
" "
)
}
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