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
00a410ad
authored
2014-03-10 20:50:54 +0800
by
slene
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
orm add GetDB api #433
1 parent
6ca30386
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
2 deletions
orm/db_alias.go
orm/orm_test.go
orm/db_alias.go
View file @
00a410a
...
...
@@ -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 @
00a410a
...
...
@@ -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
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