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
8296713b
Commit
8296713b
authored
Jan 25, 2014
by
slene
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #477 from kylemcc/read_or_create
Add a ReadOrCreate method:
parents
edf79825
190039b6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
0 deletions
+53
-0
orm.go
orm/orm.go
+14
-0
orm_test.go
orm/orm_test.go
+38
-0
types.go
orm/types.go
+1
-0
No files found.
orm/orm.go
View file @
8296713b
...
...
@@ -74,6 +74,20 @@ func (o *orm) Read(md interface{}, cols ...string) error {
return
nil
}
// Try to read a row from the database, or insert one if it doesn't exist
func
(
o
*
orm
)
ReadOrCreate
(
md
interface
{},
col1
string
,
cols
...
string
)
(
bool
,
int64
,
error
)
{
cols
=
append
([]
string
{
col1
},
cols
...
)
mi
,
ind
:=
o
.
getMiInd
(
md
,
true
)
err
:=
o
.
alias
.
DbBaser
.
Read
(
o
.
db
,
mi
,
ind
,
o
.
alias
.
TZ
,
cols
)
if
err
==
ErrNoRows
{
// Create
id
,
err
:=
o
.
Insert
(
md
)
return
(
err
==
nil
),
id
,
err
}
return
false
,
ind
.
Field
(
mi
.
fields
.
pk
.
fieldIndex
)
.
Int
(),
err
}
// insert model data to database
func
(
o
*
orm
)
Insert
(
md
interface
{})
(
int64
,
error
)
{
mi
,
ind
:=
o
.
getMiInd
(
md
,
true
)
...
...
orm/orm_test.go
View file @
8296713b
...
...
@@ -1642,3 +1642,41 @@ func TestTransaction(t *testing.T) {
throwFail
(
t
,
AssertIs
(
num
,
1
))
}
func
TestReadOrCreate
(
t
*
testing
.
T
)
{
u
:=
&
User
{
UserName
:
"Kyle"
,
Email
:
"kylemcc@gmail.com"
,
Password
:
"other_pass"
,
Status
:
7
,
IsStaff
:
false
,
IsActive
:
true
,
}
created
,
pk
,
err
:=
dORM
.
ReadOrCreate
(
u
,
"UserName"
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
created
,
true
))
throwFail
(
t
,
AssertIs
(
u
.
UserName
,
"Kyle"
))
throwFail
(
t
,
AssertIs
(
u
.
Email
,
"kylemcc@gmail.com"
))
throwFail
(
t
,
AssertIs
(
u
.
Password
,
"other_pass"
))
throwFail
(
t
,
AssertIs
(
u
.
Status
,
7
))
throwFail
(
t
,
AssertIs
(
u
.
IsStaff
,
false
))
throwFail
(
t
,
AssertIs
(
u
.
IsActive
,
true
))
throwFail
(
t
,
AssertIs
(
u
.
Created
.
In
(
DefaultTimeLoc
),
u
.
Created
.
In
(
DefaultTimeLoc
),
test_Date
))
throwFail
(
t
,
AssertIs
(
u
.
Updated
.
In
(
DefaultTimeLoc
),
u
.
Updated
.
In
(
DefaultTimeLoc
),
test_DateTime
))
nu
:=
&
User
{
UserName
:
u
.
UserName
,
Email
:
"someotheremail@gmail.com"
}
created
,
pk
,
err
=
dORM
.
ReadOrCreate
(
nu
,
"UserName"
)
throwFail
(
t
,
err
)
throwFail
(
t
,
AssertIs
(
created
,
false
))
throwFail
(
t
,
AssertIs
(
nu
.
Id
,
u
.
Id
))
throwFail
(
t
,
AssertIs
(
pk
,
u
.
Id
))
throwFail
(
t
,
AssertIs
(
nu
.
UserName
,
u
.
UserName
))
throwFail
(
t
,
AssertIs
(
nu
.
Email
,
u
.
Email
))
// should contain the value in the table, not the one specified above
throwFail
(
t
,
AssertIs
(
nu
.
Password
,
u
.
Password
))
throwFail
(
t
,
AssertIs
(
nu
.
Status
,
u
.
Status
))
throwFail
(
t
,
AssertIs
(
nu
.
IsStaff
,
u
.
IsStaff
))
throwFail
(
t
,
AssertIs
(
nu
.
IsActive
,
u
.
IsActive
))
dORM
.
Delete
(
u
)
}
orm/types.go
View file @
8296713b
...
...
@@ -23,6 +23,7 @@ type Fielder interface {
// orm struct
type
Ormer
interface
{
Read
(
interface
{},
...
string
)
error
ReadOrCreate
(
interface
{},
string
,
...
string
)
(
bool
,
int64
,
error
)
Insert
(
interface
{})
(
int64
,
error
)
InsertMulti
(
int
,
interface
{})
(
int64
,
error
)
Update
(
interface
{},
...
string
)
(
int64
,
error
)
...
...
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