Newer
Older
José Carlos Nieto
committed
/*
Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam
José Carlos Nieto
committed
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// upper.io/db is a package that deals with saving and retrieving data from
// different databases using a reduced instruction set.
José Carlos Nieto
committed
package db
The db.Cond{} expression is used to define conditions in a query, it can be
Carlos Nieto
committed
viewed as a replacement for the SQL "WHERE" clause.
Carlos Nieto
committed
db.Cond { "age": 18 } // Where age equals 18.
Carlos Nieto
committed
db.Cond { "age >=": 18 } // Where age is greater than or equal to 18.
Carlos Nieto
committed
db.Cond { "age $lt": 18 } // Where age is lower than 18 (MongoDB specific).
/*
The db.Func expression is used to represent database functions.
*/
type Func struct {
Name string
Args interface{}
}
Carlos Nieto
committed
The db.And() expression is used to glue two or more expressions under logical
conjunction, it accepts db.Cond{}, db.Or() and other db.And() expressions.
Carlos Nieto
committed
Examples:
db.And (
db.Cond { "name": "Peter" },
db.Cond { "last_name": "Parker "},
)
db.And (
Carlos Nieto
committed
db.Or (
db.Cond{ "name": "Peter" },
db.Cond{ "name": "Mickey" },
Carlos Nieto
committed
),
José Carlos Nieto
committed
type And []interface{}
Carlos Nieto
committed
The db.Or() expression is used to glue two or more expressions under logical
disjunction, it accepts db.Cond{}, db.And() and other db.Or() expressions.
Example:
db.Or (
db.Cond { "year": 2012 },
db.Cond { "year": 1987 },
)
*/
José Carlos Nieto
committed
type Or []interface{}
// Connection and authentication data.
Carlos Nieto
committed
type Settings struct {
// Database server hostname or IP. Leave blank if using unix sockets.
// Database server port. Leave blank if using unix sockets.
// A path of a UNIX socket file. Leave blank if using host and port.
José Carlos Nieto
committed
}
// Returns the underlying driver the wrapper uses as an interface{}.
José Carlos Nieto
committed
Driver() interface{}
// Attempts to stablish a connection with the database server.
José Carlos Nieto
committed
Open() error
// Clones the current database session.
// Clone() (Database, error)
// Returns error if the database server cannot be reached.
// Ping() error
Carlos Nieto
committed
// Closes the currently active connection to the database.
José Carlos Nieto
committed
Close() error
Carlos Nieto
committed
// Returns a db.Collection struct by name.
Collection(string) (Collection, error)
// Returns the names of all non-system collections within the active
// database.
Carlos Nieto
committed
Collections() ([]string, error)
Carlos Nieto
committed
// Drops the active database.
Carlos Nieto
committed
// Sets database connection settings.
Setup(Settings) error
// Returns the name of the active database.
Carlos Nieto
committed
// Starts a transaction block (if the database supports transactions).
Carlos Nieto
committed
// Ends a transaction block (if the database supports transactions).
José Carlos Nieto
committed
type Collection interface {
Carlos Nieto
committed
// Inserts a new item into the collection. Can work with maps or structs.
Append(interface{}) (interface{}, error)
Carlos Nieto
committed
// Returns true if the collection exists.
Exists() bool
José Carlos Nieto
committed
Carlos Nieto
committed
// Creates a filter with the given conditions and returns a result set.
Find(...interface{}) Result
José Carlos Nieto
committed
Carlos Nieto
committed
// Truncates the collection.
Truncate() error
José Carlos Nieto
committed
}
José Carlos Nieto
committed
// Result methods.
type Result interface {
// Defines the maximum number of results on this set.
Limit(uint) Result
// Skips over the n initial results from a query.
Skip(uint) Result
// Receives fields that define the order in which elements will be returned in
// a query, field names may be prefixed with a minus sign (-) indicating
// descending order; ascending order would be used otherwise.
Sort(...string) Result
// Defines specific fields to be returned on results on this result set.
Select(...string) Result
Carlos Nieto
committed
// Removes all items within the result set.
// Updates all items within the result set. Receives an struct or an interface{}.
Carlos Nieto
committed
// Counts all items within the result set.
// Fetches the next result within the result set and dumps it into the given
// pointer to struct or pointer to map. You must manually call Close() after
// finishing using Next().
José Carlos Nieto
committed
Next(interface{}) error
// Fetches the first result within the result set and dumps it into the given
// pointer to struct or pointer to map. Then it calls Close() to free the
// result set.
// Fetches all results within the result set and dumps them into the given
// pointer to slice of maps or structs. Then it calls Close() to free the
// result set.
All(interface{}) error
José Carlos Nieto
committed
Close() error
}
var (
EnvEnableDebug = `UPPERIO_DB_DEBUG`
)