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
00a410ad
Commit
00a410ad
authored
Mar 10, 2014
by
slene
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
orm add GetDB api #433
parent
6ca30386
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
2 deletions
+27
-2
db_alias.go
orm/db_alias.go
+18
-2
orm_test.go
orm/orm_test.go
+9
-0
No files found.
orm/db_alias.go
View file @
00a410ad
...
...
@@ -168,7 +168,7 @@ func addAliasWthDB(aliasName, driverName string, db *sql.DB) (*alias, error) {
}
if
dataBaseCache
.
add
(
aliasName
,
al
)
==
false
{
return
nil
,
fmt
.
Errorf
(
"
db
name `%s` already registered, cannot reuse"
,
aliasName
)
return
nil
,
fmt
.
Errorf
(
"
DataBase alias
name `%s` already registered, cannot reuse"
,
aliasName
)
}
return
al
,
nil
...
...
@@ -239,7 +239,7 @@ func SetDataBaseTZ(aliasName string, tz *time.Location) error {
if
al
,
ok
:=
dataBaseCache
.
get
(
aliasName
);
ok
{
al
.
TZ
=
tz
}
else
{
return
fmt
.
Errorf
(
"DataBase name `%s` not registered
\n
"
,
aliasName
)
return
fmt
.
Errorf
(
"DataBase
alias
name `%s` not registered
\n
"
,
aliasName
)
}
return
nil
}
...
...
@@ -260,3 +260,19 @@ func SetMaxOpenConns(aliasName string, maxOpenConns int) {
fun
.
Call
([]
reflect
.
Value
{
reflect
.
ValueOf
(
maxOpenConns
)})
}
}
// Get *sql.DB from registered database by db alias name.
// Use "default" as alias name if you not set.
func
GetDB
(
aliasNames
...
string
)
(
*
sql
.
DB
,
error
)
{
var
name
string
if
len
(
aliasNames
)
>
0
{
name
=
aliasNames
[
0
]
}
else
{
name
=
"default"
}
if
al
,
ok
:=
dataBaseCache
.
get
(
name
);
ok
{
return
al
.
DB
,
nil
}
else
{
return
nil
,
fmt
.
Errorf
(
"DataBase of alias name `%s` not found
\n
"
,
name
)
}
}
orm/orm_test.go
View file @
00a410ad
...
...
@@ -139,6 +139,15 @@ func throwFailNow(t *testing.T, err error, args ...interface{}) {
}
}
func
TestGetDB
(
t
*
testing
.
T
)
{
if
db
,
err
:=
GetDB
();
err
!=
nil
{
throwFailNow
(
t
,
err
)
}
else
{
err
=
db
.
Ping
()
throwFailNow
(
t
,
err
)
}
}
func
TestSyncDb
(
t
*
testing
.
T
)
{
RegisterModel
(
new
(
Data
),
new
(
DataNull
))
RegisterModel
(
new
(
User
))
...
...
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