db

package
v0.0.0-...-5765f40 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package db is a database mechanism, which helps accessing data more convenient and efficient.

This mechanism provide two interfaces, Database and Factory, and you MUST implement these two interfaces to add your own database engine. Currently, zebra support ONLY MongoDB with driver mgo(http://labix.org/mgo).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(collection string, args ...interface{}) error

func Drop

func Drop(db string, args ...interface{}) error

func Insert

func Insert(collection string, args ...interface{}) error

func Ioctrl

func Ioctrl(cmd string, args ...interface{}) (interface{}, error)

func Query

func Query(collection string, args ...interface{}) error

func Register

func Register(uri string, factory Factory) error

func UnRegister

func UnRegister() error

func Update

func Update(collection string, args ...interface{}) error

func Use

func Use(db string) error

Types

type Database

type Database interface {
	//Use change current active database
	Use(db string) error

	//Drop delete a database or table/collection of database
	Drop(db string, args ...interface{}) error

	//Insert add a data to database
	Insert(collection string, args ...interface{}) error

	//Delete remove a record from database
	Delete(collection string, args ...interface{}) error

	//Update update a record already in database
	Update(collection string, args ...interface{}) error

	//Query retrieve a record from database
	Query(collection string, args ...interface{}) error

	//Count returns total number of records in collection
	Count(collection string) int

	//Ioctrl implement other operations
	Ioctrl(cmd string, args ...interface{}) (interface{}, error)

	//Factory return a Factory interface instance, which can create and destroy a database engine
	Factory() Factory
}

Database is interface which MUST be implemented to use zebra database mechanism

This interface provide several base operations for database access, such as, Insert, Delete, Update, Query , etc, with which you can access the data with unified APIs, and it's very convenient and efficient.

type FC

type FC func(*mgo.Collection) (interface{}, error)

type FD

type FD func(*mgo.Database) (interface{}, error)

type Factory

type Factory interface {
	//Make create a new database engine with URI string, which implemented Database interface.
	//URI string MUST like: mongodb://user:password@192.168.100.2:2014, depends on each implementation of engine
	Make(uri string) Database

	//Destroy destroy and cleanup engine instance if no further use
	Destroy() error
}

Factory is a interface MUST be implemented to use zebra database mechanism

This interface provide two APIs to create and destroy database engine instance

type MongoDB

type MongoDB struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*MongoDB) Count

func (m *MongoDB) Count(collection string) int

Count returns total number of records in collection, otherwise, -1 returned

func (*MongoDB) Delete

func (m *MongoDB) Delete(collection string, args ...interface{}) error

Delete remove a record from database

func (*MongoDB) Destroy

func (m *MongoDB) Destroy() error

func (*MongoDB) Drop

func (m *MongoDB) Drop(db string, args ...interface{}) error

Drop delete a database or table/collection of database

func (*MongoDB) Factory

func (m *MongoDB) Factory() Factory

Factory return a Factory interface instance, which can create and destroy a database engine

func (*MongoDB) Insert

func (m *MongoDB) Insert(collection string, args ...interface{}) error

Insert add a data to database

func (*MongoDB) Ioctrl

func (m *MongoDB) Ioctrl(cmd string, args ...interface{}) (interface{}, error)

Ioctrl implement other operations, cmd support "database" and "collection", if "database" specified, Ioctrl accept a function as second parameter, whose prototype MUST be func(*mgo.Database) (interface{}, error), and if "collection" specified, Ioctrl accept a collection name as second parameter, and a function as third parameter, whose prototype MUST be func(*mgo.Collection) (interface{}, error).

func (*MongoDB) Make

func (m *MongoDB) Make(uri string) Database

func (*MongoDB) Query

func (m *MongoDB) Query(collection string, args ...interface{}) error

Query retrieve a record from database

func (*MongoDB) Update

func (m *MongoDB) Update(collection string, args ...interface{}) error

Update update a record already in database

func (*MongoDB) Use

func (m *MongoDB) Use(db string) error

Use change current active database

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL