Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
8f5ca303
authored
2013-08-30 12:32:05 +0800
by
slene
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
orm fix when use uint as pk
1 parent
dc8f9320
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
94 additions
and
44 deletions
orm/db.go
orm/db_utils.go
orm/models_test.go
orm/orm.go
orm/orm_object.go
orm/orm_test.go
orm/db.go
View file @
8f5ca30
...
...
@@ -304,8 +304,12 @@ func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.
if
num
>
0
{
if
mi
.
fields
.
pk
.
auto
{
if
mi
.
fields
.
pk
.
fieldType
&
IsPostiveIntegerField
>
0
{
ind
.
Field
(
mi
.
fields
.
pk
.
fieldIndex
)
.
SetUint
(
0
)
}
else
{
ind
.
Field
(
mi
.
fields
.
pk
.
fieldIndex
)
.
SetInt
(
0
)
}
}
err
:=
d
.
deleteRels
(
q
,
mi
,
[]
interface
{}{
pkValue
},
tz
)
if
err
!=
nil
{
...
...
orm/db_utils.go
View file @
8f5ca30
...
...
@@ -10,7 +10,11 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac
fi
:=
mi
.
fields
.
pk
v
:=
ind
.
Field
(
fi
.
fieldIndex
)
if
fi
.
fieldType
&
IsIntegerField
>
0
{
if
fi
.
fieldType
&
IsPostiveIntegerField
>
0
{
vu
:=
v
.
Uint
()
exist
=
vu
>
0
value
=
vu
}
else
if
fi
.
fieldType
&
IsIntegerField
>
0
{
vu
:=
v
.
Int
()
exist
=
vu
>
0
value
=
vu
...
...
orm/models_test.go
View file @
8f5ca30
...
...
@@ -61,6 +61,7 @@ type DataNull struct {
// only for mysql
type
UserBig
struct
{
Id
uint64
Name
string
}
type
User
struct
{
...
...
orm/orm.go
View file @
8f5ca30
...
...
@@ -70,9 +70,13 @@ func (o *orm) Insert(md interface{}) (int64, error) {
}
if
id
>
0
{
if
mi
.
fields
.
pk
.
auto
{
if
mi
.
fields
.
pk
.
fieldType
&
IsPostiveIntegerField
>
0
{
ind
.
Field
(
mi
.
fields
.
pk
.
fieldIndex
)
.
SetUint
(
uint64
(
id
))
}
else
{
ind
.
Field
(
mi
.
fields
.
pk
.
fieldIndex
)
.
SetInt
(
id
)
}
}
}
return
id
,
nil
}
...
...
@@ -93,9 +97,13 @@ func (o *orm) Delete(md interface{}) (int64, error) {
}
if
num
>
0
{
if
mi
.
fields
.
pk
.
auto
{
if
mi
.
fields
.
pk
.
fieldType
&
IsPostiveIntegerField
>
0
{
ind
.
Field
(
mi
.
fields
.
pk
.
fieldIndex
)
.
SetUint
(
0
)
}
else
{
ind
.
Field
(
mi
.
fields
.
pk
.
fieldIndex
)
.
SetInt
(
0
)
}
}
}
return
num
,
nil
}
...
...
orm/orm_object.go
View file @
8f5ca30
...
...
@@ -34,9 +34,13 @@ func (o *insertSet) Insert(md interface{}) (int64, error) {
}
if
id
>
0
{
if
o
.
mi
.
fields
.
pk
.
auto
{
if
o
.
mi
.
fields
.
pk
.
fieldType
&
IsPostiveIntegerField
>
0
{
ind
.
Field
(
o
.
mi
.
fields
.
pk
.
fieldIndex
)
.
SetUint
(
uint64
(
id
))
}
else
{
ind
.
Field
(
o
.
mi
.
fields
.
pk
.
fieldIndex
)
.
SetInt
(
id
)
}
}
}
return
id
,
nil
}
...
...
orm/orm_test.go
View file @
8f5ca30
...
...
@@ -211,6 +211,7 @@ func TestRegisterModels(t *testing.T) {
RegisterModel
(
new
(
Post
))
RegisterModel
(
new
(
Tag
))
RegisterModel
(
new
(
Comment
))
RegisterModel
(
new
(
UserBig
))
BootStrap
()
...
...
@@ -231,8 +232,7 @@ func TestModelSyntax(t *testing.T) {
}
}
func
TestDataTypes
(
t
*
testing
.
T
)
{
values
:=
map
[
string
]
interface
{}{
var
Data_Values
=
map
[
string
]
interface
{}{
"Boolean"
:
true
,
"Char"
:
"char"
,
"Text"
:
"text"
,
...
...
@@ -253,11 +253,13 @@ func TestDataTypes(t *testing.T) {
"Float32"
:
float32
(
100.1234
),
"Float64"
:
float64
(
100.1234
),
"Decimal"
:
float64
(
100.1234
),
}
}
func
TestDataTypes
(
t
*
testing
.
T
)
{
d
:=
Data
{}
ind
:=
reflect
.
Indirect
(
reflect
.
ValueOf
(
&
d
))
for
name
,
value
:=
range
v
alues
{
for
name
,
value
:=
range
Data_V
alues
{
e
:=
ind
.
FieldByName
(
name
)
e
.
Set
(
reflect
.
ValueOf
(
value
))
}
...
...
@@ -272,7 +274,7 @@ func TestDataTypes(t *testing.T) {
ind
=
reflect
.
Indirect
(
reflect
.
ValueOf
(
&
d
))
for
name
,
value
:=
range
v
alues
{
for
name
,
value
:=
range
Data_V
alues
{
e
:=
ind
.
FieldByName
(
name
)
vu
:=
e
.
Interface
()
switch
name
{
...
...
@@ -376,6 +378,17 @@ func TestCRUD(t *testing.T) {
u
=
&
User
{
Id
:
100
}
err
=
dORM
.
Read
(
u
)
throwFail
(
t
,
AssertIs
(
err
,
T_Equal
,
ErrNoRows
))
ub
:=
UserBig
{}
ub
.
Name
=
"name"
id
,
err
=
dORM
.
Insert
(
&
ub
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
id
,
T_Equal
,
1
))
ub
=
UserBig
{
Id
:
1
}
err
=
dORM
.
Read
(
&
ub
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
ub
.
Name
,
T_Equal
,
"name"
))
}
func
TestInsertTestData
(
t
*
testing
.
T
)
{
...
...
@@ -823,7 +836,15 @@ func TestPrepareInsert(t *testing.T) {
throwFail
(
t
,
AssertIs
(
err
,
T_Equal
,
ErrStmtClosed
))
}
func
TestRaw
(
t
*
testing
.
T
)
{
func
TestRawQueryRow
(
t
*
testing
.
T
)
{
}
func
TestRawQueryRows
(
t
*
testing
.
T
)
{
}
func
TestRawValues
(
t
*
testing
.
T
)
{
switch
{
case
IsMysql
||
IsSqlite
:
...
...
@@ -860,42 +881,7 @@ func TestRaw(t *testing.T) {
if
num
==
3
{
throwFail
(
t
,
AssertIs
(
list
[
0
],
T_Equal
,
"2"
))
throwFail
(
t
,
AssertIs
(
list
[
1
],
T_Equal
,
"3"
))
throwFail
(
t
,
AssertIs
(
list
[
2
],
T_Equal
,
""
))
}
pre
,
err
:=
dORM
.
Raw
(
"INSERT INTO tag (name) VALUES (?)"
)
.
Prepare
()
throwFail
(
t
,
err
)
if
pre
!=
nil
{
r
,
err
:=
pre
.
Exec
(
"name1"
)
throwFail
(
t
,
err
)
tid
,
err
:=
r
.
LastInsertId
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
tid
,
T_Large
,
0
))
r
,
err
=
pre
.
Exec
(
"name2"
)
throwFail
(
t
,
err
)
id
,
err
:=
r
.
LastInsertId
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
id
,
T_Equal
,
tid
+
1
))
r
,
err
=
pre
.
Exec
(
"name3"
)
throwFail
(
t
,
err
)
id
,
err
=
r
.
LastInsertId
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
id
,
T_Equal
,
tid
+
2
))
err
=
pre
.
Close
()
throwFail
(
t
,
err
)
res
,
err
:=
dORM
.
Raw
(
"DELETE FROM tag WHERE name IN (?, ?, ?)"
,
[]
string
{
"name1"
,
"name2"
,
"name3"
})
.
Exec
()
throwFail
(
t
,
err
)
num
,
err
:=
res
.
RowsAffected
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
T_Equal
,
3
))
throwFail
(
t
,
AssertIs
(
list
[
2
],
T_Equal
,
nil
))
}
case
IsPostgres
:
...
...
@@ -933,8 +919,51 @@ func TestRaw(t *testing.T) {
if
num
==
3
{
throwFail
(
t
,
AssertIs
(
list
[
0
],
T_Equal
,
"2"
))
throwFail
(
t
,
AssertIs
(
list
[
1
],
T_Equal
,
"3"
))
throwFail
(
t
,
AssertIs
(
list
[
2
],
T_Equal
,
""
))
throwFail
(
t
,
AssertIs
(
list
[
2
],
T_Equal
,
nil
))
}
}
}
func
TestRawPrepare
(
t
*
testing
.
T
)
{
switch
{
case
IsMysql
||
IsSqlite
:
pre
,
err
:=
dORM
.
Raw
(
"INSERT INTO tag (name) VALUES (?)"
)
.
Prepare
()
throwFail
(
t
,
err
)
if
pre
!=
nil
{
r
,
err
:=
pre
.
Exec
(
"name1"
)
throwFail
(
t
,
err
)
tid
,
err
:=
r
.
LastInsertId
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
tid
,
T_Large
,
0
))
r
,
err
=
pre
.
Exec
(
"name2"
)
throwFail
(
t
,
err
)
id
,
err
:=
r
.
LastInsertId
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
id
,
T_Equal
,
tid
+
1
))
r
,
err
=
pre
.
Exec
(
"name3"
)
throwFail
(
t
,
err
)
id
,
err
=
r
.
LastInsertId
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
id
,
T_Equal
,
tid
+
2
))
err
=
pre
.
Close
()
throwFail
(
t
,
err
)
res
,
err
:=
dORM
.
Raw
(
"DELETE FROM tag WHERE name IN (?, ?, ?)"
,
[]
string
{
"name1"
,
"name2"
,
"name3"
})
.
Exec
()
throwFail
(
t
,
err
)
num
,
err
:=
res
.
RowsAffected
()
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
num
,
T_Equal
,
3
))
}
case
IsPostgres
:
pre
,
err
:=
dORM
.
Raw
(
`INSERT INTO "tag" ("name") VALUES (?) RETURNING "id"`
)
.
Prepare
()
throwFail
(
t
,
err
)
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment