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
8296713b
authored
2014-01-24 18:01:24 -0800
by
slene
Browse Files
Options
Browse Files
Tag
Download
Plain Diff
Merge pull request #477 from kylemcc/read_or_create
Add a ReadOrCreate method:
2 parents
edf79825
190039b6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
0 deletions
orm/orm.go
orm/orm_test.go
orm/types.go
orm/orm.go
View file @
8296713
...
...
@@ -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 @
8296713
...
...
@@ -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 @
8296713
...
...
@@ -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
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