good morning!!!!

Skip to content
Snippets Groups Projects
Commit 55ff7c46 authored by Carlos Nieto's avatar Carlos Nieto
Browse files

Updating database functions.

parent 0afe7c12
Branches
Tags
No related merge requests found
/* /*
Copyright (c) 2012-2013 José Carlos Nieto, http://xiam.menteslibres.org/ Copyright (c) 2012-2013 José Carlos Nieto, https://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 connection to the datasource. See Session(). // Attempts to connect to a database 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
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment