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
b2cdabb8
Commit
b2cdabb8
authored
Sep 08, 2014
by
Zheng Yang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added query builder for orm
parent
647a4751
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
115 additions
and
0 deletions
+115
-0
qb.go
orm/qb.go
+30
-0
qb_mysql.go
orm/qb_mysql.go
+85
-0
No files found.
orm/qb.go
0 → 100644
View file @
b2cdabb8
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package
orm
type
QueryBuilder
interface
{
Select
(
fields
...
string
)
QueryWriter
From
(
tables
...
string
)
QueryWriter
Where
(
cond
string
)
QueryWriter
LimitOffset
(
limit
int
,
offset
int
)
QueryWriter
InnerJoin
(
table
string
)
QueryWriter
LeftJoin
(
table
string
)
QueryWriter
On
(
cond
string
)
QueryWriter
And
(
cond
string
)
QueryWriter
Or
(
cond
string
)
QueryWriter
In
(
vals
...
string
)
QueryWriter
Subquery
(
query
string
,
rename
string
)
string
String
()
string
}
orm/qb_mysql.go
0 → 100644
View file @
b2cdabb8
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package
orm
import
(
"fmt"
"strings"
)
type
MySQLQueryBuilder
struct
{
QueryString
[]
string
}
func
(
qw
*
MySQLQueryBuilder
)
Select
(
fields
...
string
)
QueryWriter
{
segment
:=
fmt
.
Sprintf
(
"SELECT %s"
,
strings
.
Join
(
fields
,
", "
))
qw
.
QueryString
=
append
(
qw
.
QueryString
,
segment
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
From
(
tables
...
string
)
QueryWriter
{
segment
:=
fmt
.
Sprintf
(
"FROM %s"
,
strings
.
Join
(
tables
,
", "
))
qw
.
QueryString
=
append
(
qw
.
QueryString
,
segment
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
Where
(
cond
string
)
QueryWriter
{
qw
.
QueryString
=
append
(
qw
.
QueryString
,
"WHERE "
+
cond
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
LimitOffset
(
limit
int
,
offset
int
)
QueryWriter
{
qw
.
QueryString
=
append
(
qw
.
QueryString
,
fmt
.
Sprintf
(
"LIMIT %d OFFSET %d"
,
limit
,
offset
))
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
InnerJoin
(
table
string
)
QueryWriter
{
qw
.
QueryString
=
append
(
qw
.
QueryString
,
"INNER JOIN "
+
table
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
LeftJoin
(
table
string
)
QueryWriter
{
qw
.
QueryString
=
append
(
qw
.
QueryString
,
"LEFT JOIN "
+
table
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
On
(
cond
string
)
QueryWriter
{
qw
.
QueryString
=
append
(
qw
.
QueryString
,
"ON "
+
cond
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
And
(
cond
string
)
QueryWriter
{
qw
.
QueryString
=
append
(
qw
.
QueryString
,
"AND "
+
cond
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
Or
(
cond
string
)
QueryWriter
{
qw
.
QueryString
=
append
(
qw
.
QueryString
,
"OR "
+
cond
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
In
(
vals
...
string
)
QueryWriter
{
segment
:=
fmt
.
Sprintf
(
"IN (%s)"
,
strings
.
Join
(
vals
,
", "
))
qw
.
QueryString
=
append
(
qw
.
QueryString
,
segment
)
return
qw
}
func
(
qw
*
MySQLQueryBuilder
)
Subquery
(
sub
string
,
alias
string
)
string
{
return
fmt
.
Sprintf
(
"(%s) AS %s"
,
sub
,
alias
)
}
func
(
qw
*
MySQLQueryBuilder
)
String
()
string
{
return
strings
.
Join
(
qw
.
QueryString
,
" "
)
}
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