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
GitLab 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
55ff7c46
Commit
55ff7c46
authored
Nov 2, 2013
by
Carlos Nieto
Browse files
Options
Downloads
Patches
Plain Diff
Updating database functions.
parent
0afe7c12
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
mongo/database.go
+68
-84
68 additions, 84 deletions
mongo/database.go
with
68 additions
and
84 deletions
mongo/database.go
+
68
−
84
View file @
55ff7c46
/*
/*
Copyright (c) 2012-2013 José Carlos Nieto, http://
xiam.
menteslibres.
org/
Copyright (c) 2012-2013 José Carlos Nieto, http
s
://menteslibres.
net/xiam
Permission is hereby granted, free of charge, to any person obtaining
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
a copy of this software and associated documentation files (the
...
@@ -26,117 +26,58 @@ package mongo
...
@@ -26,117 +26,58 @@ package mongo
import
(
import
(
"fmt"
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo"
"upper.io/db"
"net/url"
"net/url"
"time"
"time"
"upper.io/db"
)
)
var
Debug
=
false
var
Debug
=
false
const
driverName
=
`mongo`
// Registers this driver.
// Registers this driver.
func
init
()
{
func
init
()
{
db
.
Register
(
"mongo"
,
&
Source
{})
db
.
Register
(
driverName
,
&
Source
{})
}
}
// Mongodb datasource.
type
Source
struct
{
type
Source
struct
{
name
string
name
string
config
db
.
DataSource
config
db
.
Settings
session
*
mgo
.
Session
session
*
mgo
.
Session
database
*
mgo
.
Database
database
*
mgo
.
Database
}
}
// Returns database
name
.
// Returns
the string name of the
database.
func
(
self
*
Source
)
Name
()
string
{
func
(
self
*
Source
)
Name
()
string
{
return
self
.
name
return
self
.
name
}
}
// Returns a datasource session that is not yet connected to the database.
// Stores database settings.
func
Session
(
config
db
.
DataSource
)
db
.
Database
{
func
(
self
*
Source
)
Setup
(
config
db
.
Settings
)
error
{
self
:=
&
Source
{}
self
.
config
=
config
return
self
}
// Opens a connection.
func
(
self
*
Source
)
Setup
(
config
db
.
DataSource
)
error
{
self
.
config
=
config
self
.
config
=
config
return
self
.
Open
()
return
self
.
Open
()
}
}
// Sets the active database.
// Returns the underlying *mgo.Session instance.
func
(
self
*
Source
)
Use
(
database
string
)
error
{
self
.
config
.
Database
=
database
self
.
name
=
database
self
.
database
=
self
.
session
.
DB
(
self
.
config
.
Database
)
return
nil
}
/*
Starts a transaction block.
*/
func
(
self
*
Source
)
Begin
()
error
{
// TODO:
// MongoDB does not supports something like BEGIN and END statements.
return
nil
}
/*
Ends a transaction block.
*/
func
(
self
*
Source
)
End
()
error
{
// TODO:
// MongoDB does not supports something like BEGIN and END statements.
return
nil
}
// Returns a collection from the current database.
func
(
self
*
Source
)
Collection
(
name
string
)
(
db
.
Collection
,
error
)
{
var
err
error
col
:=
&
SourceCollection
{}
col
.
parent
=
self
col
.
collection
=
self
.
database
.
C
(
name
)
col
.
DB
=
self
col
.
SetName
=
name
if
col
.
Exists
()
==
false
{
err
=
db
.
ErrCollectionDoesNotExists
}
return
col
,
err
}
// Returns a collection from the current database. Panics if the collection does not exists.
func
(
self
*
Source
)
ExistentCollection
(
name
string
)
db
.
Collection
{
col
,
err
:=
self
.
Collection
(
name
)
if
err
!=
nil
{
panic
(
err
.
Error
())
}
return
col
}
// Returns the underlying driver (*mgo.Session).
func
(
self
*
Source
)
Driver
()
interface
{}
{
func
(
self
*
Source
)
Driver
()
interface
{}
{
return
self
.
session
return
self
.
session
}
}
//
Opens a
connect
ion
to
the
data
source. See Session()
.
//
Attempts to
connect to
a
data
base using the stored settings
.
func
(
self
*
Source
)
Open
()
error
{
func
(
self
*
Source
)
Open
()
error
{
var
err
error
var
err
error
connURL
:=
&
url
.
URL
{
Scheme
:
"
mongodb
"
}
connURL
:=
&
url
.
URL
{
Scheme
:
`
mongodb
`
}
if
self
.
config
.
Port
==
0
{
if
self
.
config
.
Port
==
0
{
self
.
config
.
Port
=
27017
self
.
config
.
Port
=
27017
}
}
if
self
.
config
.
Host
==
""
{
if
self
.
config
.
Host
==
""
{
self
.
config
.
Host
=
"
127.0.0.1
"
self
.
config
.
Host
=
`
127.0.0.1
`
}
}
connURL
.
Host
=
fmt
.
Sprintf
(
"
%s:%d
"
,
self
.
config
.
Host
,
self
.
config
.
Port
)
connURL
.
Host
=
fmt
.
Sprintf
(
`
%s:%d
`
,
self
.
config
.
Host
,
self
.
config
.
Port
)
if
self
.
config
.
User
!=
""
{
if
self
.
config
.
User
!=
""
{
connURL
.
User
=
url
.
UserPassword
(
self
.
config
.
User
,
self
.
config
.
Password
)
connURL
.
User
=
url
.
UserPassword
(
self
.
config
.
User
,
self
.
config
.
Password
)
...
@@ -159,13 +100,7 @@ func (self *Source) Open() error {
...
@@ -159,13 +100,7 @@ func (self *Source) Open() error {
return
nil
return
nil
}
}
// Drops the active database and all its collections.
// Closes the current database session.
func
(
self
*
Source
)
Drop
()
error
{
err
:=
self
.
database
.
DropDatabase
()
return
err
}
// Closes the connection to the database.
func
(
self
*
Source
)
Close
()
error
{
func
(
self
*
Source
)
Close
()
error
{
if
self
.
session
!=
nil
{
if
self
.
session
!=
nil
{
self
.
session
.
Close
()
self
.
session
.
Close
()
...
@@ -173,14 +108,63 @@ func (self *Source) Close() error {
...
@@ -173,14 +108,63 @@ func (self *Source) Close() error {
return
nil
return
nil
}
}
// Returns the names of all collection in the current database.
// Changes the active database.
func
(
self
*
Source
)
Collections
()
[]
string
{
func
(
self
*
Source
)
Use
(
database
string
)
error
{
self
.
config
.
Database
=
database
self
.
name
=
database
self
.
database
=
self
.
session
.
DB
(
self
.
config
.
Database
)
return
nil
}
// Starts a transaction block.
func
(
self
*
Source
)
Begin
()
error
{
// TODO:
// MongoDB does not supports something like BEGIN and END statements.
return
nil
}
// Ends a transaction block.
func
(
self
*
Source
)
End
()
error
{
// TODO:
// MongoDB does not supports something like BEGIN and END statements.
return
nil
}
// Drops the currently active database.
func
(
self
*
Source
)
Drop
()
error
{
err
:=
self
.
database
.
DropDatabase
()
return
err
}
// Returns a list of all tables within the currently active database.
func
(
self
*
Source
)
Collections
()
([]
string
,
error
)
{
cols
:=
[]
string
{}
cols
:=
[]
string
{}
rawcols
,
_
:=
self
.
database
.
CollectionNames
()
rawcols
,
err
:=
self
.
database
.
CollectionNames
()
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
col
:=
range
rawcols
{
for
_
,
col
:=
range
rawcols
{
if
col
!=
"system.indexes"
{
if
col
!=
"system.indexes"
{
cols
=
append
(
cols
,
col
)
cols
=
append
(
cols
,
col
)
}
}
}
}
return
cols
return
cols
,
nil
}
// Returns a collection instance by name.
func
(
self
*
Source
)
Collection
(
name
string
)
(
db
.
Collection
,
error
)
{
var
err
error
col
:=
&
SourceCollection
{}
col
.
parent
=
self
col
.
collection
=
self
.
database
.
C
(
name
)
col
.
DB
=
self
col
.
SetName
=
name
if
col
.
Exists
()
==
false
{
err
=
db
.
ErrCollectionDoesNotExists
}
return
col
,
err
}
}
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