good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
upper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
open
upper
Commits
75d1ac85
Commit
75d1ac85
authored
12 years ago
by
José Carlos Nieto
Browse files
Options
Downloads
Patches
Plain Diff
This will allow to register drivers like database/sql.
parent
2c99deab
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
db.go
+22
-0
22 additions, 0 deletions
db.go
examples/mongo.go
+54
-0
54 additions, 0 deletions
examples/mongo.go
mongo/mongo.go
+9
-0
9 additions, 0 deletions
mongo/mongo.go
with
85 additions
and
0 deletions
db.go
+
22
−
0
View file @
75d1ac85
...
...
@@ -186,6 +186,8 @@ type Database interface {
Use
(
string
)
error
Drop
()
error
Setup
(
DataSource
)
error
}
// Collection methods.
...
...
@@ -323,3 +325,23 @@ func (item Item) GetBool(name string) bool {
return
true
}
var
wrappers
=
make
(
map
[
string
]
Database
)
func
Register
(
name
string
,
driver
Database
)
{
if
name
==
""
{
panic
(
"db: Wrapper name cannot be nil."
)
}
if
_
,
ok
:=
wrappers
[
name
];
ok
!=
false
{
panic
(
"db: Wrapper was already registered."
)
}
wrappers
[
name
]
=
driver
}
func
Open
(
name
string
,
settings
DataSource
)
Database
{
if
_
,
ok
:=
wrappers
[
name
];
ok
==
false
{
panic
(
fmt
.
Sprintf
(
"db: Unknown wrapper: %s."
,
name
))
}
wrappers
[
name
]
.
Setup
(
settings
)
return
wrappers
[
name
]
}
This diff is collapsed.
Click to expand it.
examples/mongo.go
0 → 100644
+
54
−
0
View file @
75d1ac85
package
main
import
(
"fmt"
"github.com/gosexy/db"
_
"github.com/gosexy/db/mongo"
"github.com/gosexy/sugar"
)
func
main
()
{
sess
:=
db
.
Open
(
"mongo"
,
db
.
DataSource
{
Host
:
"127.0.0.1"
,
Database
:
"gosexy-dev"
})
if
sess
==
nil
{
panic
(
"Could not open connection to MongoDB."
)
}
defer
sess
.
Close
()
sess
.
Drop
()
animals
:=
sess
.
Collection
(
"animals"
)
animals
.
Append
(
db
.
Item
{
"animal"
:
"Bird"
,
"young"
:
"Chick"
,
"female"
:
"Hen"
,
"male"
:
"Cock"
,
"group"
:
"flock"
,
})
animals
.
Append
(
db
.
Item
{
"animal"
:
"Bovidae"
,
"young"
:
"Calf"
,
"female"
:
"Cow"
,
"male"
:
"Bull"
,
"group"
:
"Herd"
,
})
animals
.
Append
(
db
.
Item
{
"animal"
:
"Canidae"
,
"young"
:
sugar
.
List
{
"Puppy"
,
"Pup"
},
"female"
:
"Bitch"
,
"male"
:
"Dog"
,
"group"
:
"Pack"
,
})
items
:=
animals
.
FindAll
()
for
_
,
item
:=
range
items
{
fmt
.
Printf
(
"animal: %s, young: %s
\n
"
,
item
[
"animal"
],
item
[
"young"
])
}
}
This diff is collapsed.
Click to expand it.
mongo/mongo.go
+
9
−
0
View file @
75d1ac85
...
...
@@ -36,6 +36,10 @@ import (
"time"
)
func
init
()
{
db
.
Register
(
"mongo"
,
&
MongoDataSource
{})
}
// Session
type
MongoDataSource
struct
{
config
db
.
DataSource
...
...
@@ -506,6 +510,11 @@ func Session(config db.DataSource) db.Database {
return
m
}
func
(
m
*
MongoDataSource
)
Setup
(
config
db
.
DataSource
)
error
{
m
.
config
=
config
return
m
.
Open
()
}
// Sets the active database.
func
(
m
*
MongoDataSource
)
Use
(
database
string
)
error
{
m
.
config
.
Database
=
database
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment