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
9765519f
Commit
9765519f
authored
May 17, 2017
by
astaxie
Committed by
GitHub
May 17, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2637 from alexsunxl/develop
allow o.Raw(sql).QueryRows(&container) pass nested struct
parents
3c9b6c99
23250901
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
11 deletions
+45
-11
orm_raw.go
orm/orm_raw.go
+25
-11
orm_test.go
orm/orm_test.go
+20
-0
No files found.
orm/orm_raw.go
View file @
9765519f
...
...
@@ -493,19 +493,33 @@ func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) {
}
}
}
else
{
for
i
:=
0
;
i
<
ind
.
NumField
();
i
++
{
f
:=
ind
.
Field
(
i
)
fe
:=
ind
.
Type
()
.
Field
(
i
)
_
,
tags
:=
parseStructTag
(
fe
.
Tag
.
Get
(
defaultStructTagName
))
var
col
string
if
col
=
tags
[
"column"
];
col
==
""
{
col
=
snakeString
(
fe
.
Name
)
}
if
v
,
ok
:=
columnsMp
[
col
];
ok
{
value
:=
reflect
.
ValueOf
(
v
)
.
Elem
()
.
Interface
()
o
.
setFieldValue
(
f
,
value
)
// define recursive function
var
recursiveSetField
func
(
rv
reflect
.
Value
)
recursiveSetField
=
func
(
rv
reflect
.
Value
)
{
for
i
:=
0
;
i
<
rv
.
NumField
();
i
++
{
f
:=
rv
.
Field
(
i
)
fe
:=
rv
.
Type
()
.
Field
(
i
)
// check if the field is a Struct
// recursive the Struct type
if
fe
.
Type
.
Kind
()
==
reflect
.
Struct
{
recursiveSetField
(
f
)
}
_
,
tags
:=
parseStructTag
(
fe
.
Tag
.
Get
(
defaultStructTagName
))
var
col
string
if
col
=
tags
[
"column"
];
col
==
""
{
col
=
snakeString
(
fe
.
Name
)
}
if
v
,
ok
:=
columnsMp
[
col
];
ok
{
value
:=
reflect
.
ValueOf
(
v
)
.
Elem
()
.
Interface
()
o
.
setFieldValue
(
f
,
value
)
}
}
}
// init call the recursive function
recursiveSetField
(
ind
)
}
if
eTyps
[
0
]
.
Kind
()
==
reflect
.
Ptr
{
...
...
orm/orm_test.go
View file @
9765519f
...
...
@@ -1661,6 +1661,13 @@ func TestRawQueryRow(t *testing.T) {
throwFail
(
t
,
AssertIs
(
pid
,
nil
))
}
// user_profile table
type
userProfile
struct
{
User
Age
int
Money
float64
}
func
TestQueryRows
(
t
*
testing
.
T
)
{
Q
:=
dDbBaser
.
TableQuote
()
...
...
@@ -1731,6 +1738,19 @@ func TestQueryRows(t *testing.T) {
throwFailNow
(
t
,
AssertIs
(
usernames
[
1
],
"astaxie"
))
throwFailNow
(
t
,
AssertIs
(
ids
[
2
],
4
))
throwFailNow
(
t
,
AssertIs
(
usernames
[
2
],
"nobody"
))
//test query rows by nested struct
var
l
[]
userProfile
query
=
fmt
.
Sprintf
(
"SELECT * FROM %suser_profile%s LEFT JOIN %suser%s ON %suser_profile%s.%sid%s = %suser%s.%sid%s"
,
Q
,
Q
,
Q
,
Q
,
Q
,
Q
,
Q
,
Q
,
Q
,
Q
,
Q
,
Q
)
num
,
err
=
dORM
.
Raw
(
query
)
.
QueryRows
(
&
l
)
throwFailNow
(
t
,
err
)
throwFailNow
(
t
,
AssertIs
(
num
,
2
))
throwFailNow
(
t
,
AssertIs
(
len
(
l
),
2
))
throwFailNow
(
t
,
AssertIs
(
l
[
0
]
.
UserName
,
"slene"
))
throwFailNow
(
t
,
AssertIs
(
l
[
0
]
.
Age
,
28
))
throwFailNow
(
t
,
AssertIs
(
l
[
1
]
.
UserName
,
"astaxie"
))
throwFailNow
(
t
,
AssertIs
(
l
[
1
]
.
Age
,
30
))
}
func
TestRawValues
(
t
*
testing
.
T
)
{
...
...
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