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
6b5a70d2
Unverified
Commit
6b5a70d2
authored
Nov 08, 2018
by
astaxie
Committed by
GitHub
Nov 08, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3378 from nukc/develop
orm: support filter raw sql
parents
8391d262
736e66fc
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
1 deletion
+49
-1
db_tables.go
orm/db_tables.go
+7
-1
orm_conds.go
orm/orm_conds.go
+11
-0
orm_queryset.go
orm/orm_queryset.go
+9
-0
orm_test.go
orm/orm_test.go
+17
-0
types.go
orm/types.go
+5
-0
No files found.
orm/db_tables.go
View file @
6b5a70d2
...
...
@@ -372,7 +372,13 @@ func (t *dbTables) getCondSQL(cond *Condition, sub bool, tz *time.Location) (whe
operator
=
"exact"
}
operSQL
,
args
:=
t
.
base
.
GenerateOperatorSQL
(
mi
,
fi
,
operator
,
p
.
args
,
tz
)
var
operSQL
string
var
args
[]
interface
{}
if
p
.
isRaw
{
operSQL
=
p
.
sql
}
else
{
operSQL
,
args
=
t
.
base
.
GenerateOperatorSQL
(
mi
,
fi
,
operator
,
p
.
args
,
tz
)
}
leftCol
:=
fmt
.
Sprintf
(
"%s.%s%s%s"
,
index
,
Q
,
fi
.
column
,
Q
)
t
.
base
.
GenerateOperatorLeftCol
(
fi
,
operator
,
&
leftCol
)
...
...
orm/orm_conds.go
View file @
6b5a70d2
...
...
@@ -31,6 +31,8 @@ type condValue struct {
isOr
bool
isNot
bool
isCond
bool
isRaw
bool
sql
string
}
// Condition struct.
...
...
@@ -45,6 +47,15 @@ func NewCondition() *Condition {
return
c
}
// Raw add raw sql to condition
func
(
c
Condition
)
Raw
(
expr
string
,
sql
string
)
*
Condition
{
if
len
(
sql
)
==
0
{
panic
(
fmt
.
Errorf
(
"<Condition.Raw> sql cannot empty"
))
}
c
.
params
=
append
(
c
.
params
,
condValue
{
exprs
:
strings
.
Split
(
expr
,
ExprSep
),
sql
:
sql
,
isRaw
:
true
})
return
&
c
}
// And add expression to condition
func
(
c
Condition
)
And
(
expr
string
,
args
...
interface
{})
*
Condition
{
if
expr
==
""
||
len
(
args
)
==
0
{
...
...
orm/orm_queryset.go
View file @
6b5a70d2
...
...
@@ -79,6 +79,15 @@ func (o querySet) Filter(expr string, args ...interface{}) QuerySeter {
return
&
o
}
// add raw sql to querySeter.
func
(
o
querySet
)
FilterRaw
(
expr
string
,
sql
string
)
QuerySeter
{
if
o
.
cond
==
nil
{
o
.
cond
=
NewCondition
()
}
o
.
cond
=
o
.
cond
.
Raw
(
expr
,
sql
)
return
&
o
}
// add NOT condition to querySeter.
func
(
o
querySet
)
Exclude
(
expr
string
,
args
...
interface
{})
QuerySeter
{
if
o
.
cond
==
nil
{
...
...
orm/orm_test.go
View file @
6b5a70d2
...
...
@@ -899,6 +899,18 @@ func TestOperators(t *testing.T) {
num
,
err
=
qs
.
Filter
(
"id__between"
,
[]
int
{
2
,
3
})
.
Count
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
2
))
num
,
err
=
qs
.
FilterRaw
(
"user_name"
,
"= 'slene'"
)
.
Count
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
1
))
num
,
err
=
qs
.
FilterRaw
(
"status"
,
"IN (1, 2)"
)
.
Count
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
2
))
num
,
err
=
qs
.
FilterRaw
(
"profile_id"
,
"IN (SELECT id FROM user_profile WHERE age=30)"
)
.
Count
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
1
))
}
func
TestSetCond
(
t
*
testing
.
T
)
{
...
...
@@ -924,6 +936,11 @@ func TestSetCond(t *testing.T) {
num
,
err
=
qs
.
SetCond
(
cond4
)
.
Count
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
3
))
cond5
:=
cond
.
Raw
(
"user_name"
,
"= 'slene'"
)
.
OrNotCond
(
cond
.
And
(
"user_name"
,
"slene"
))
num
,
err
=
qs
.
SetCond
(
cond5
)
.
Count
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
3
))
}
func
TestLimit
(
t
*
testing
.
T
)
{
...
...
orm/types.go
View file @
6b5a70d2
...
...
@@ -147,6 +147,11 @@ type QuerySeter interface {
// // time compare
// qs.Filter("created", time.Now())
Filter
(
string
,
...
interface
{})
QuerySeter
// add raw sql to querySeter.
// for example:
// qs.FilterRaw("user_id IN (SELECT id FROM profile WHERE age>=18)")
// //sql-> WHERE user_id IN (SELECT id FROM profile WHERE age>=18)
FilterRaw
(
string
,
string
)
QuerySeter
// add NOT condition to querySeter.
// have the same usage as Filter
Exclude
(
string
,
...
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