base

package
Version: v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2019 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// Mongo represent driver name for mongodb driver
	Mongo driverName = "mongo"

	// PG represent driver name for PostgreSQL
	PG driverName = "pg"

	// MSSQL represent driver name for Microsoft SQL Server
	MSSQL driverName = "mssql"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder added in v0.2.0

type Builder interface {

	// OrderBy set the order of returning result in following command
	OrderBy(sorts ...Sort) Builder

	// Limit set the limit that determines how many results should be
	// returned in the following fetch command.
	Limit(n int) Builder

	// Skip set the starting offset of the following fetch command
	Skip(n int) Builder

	// Count execute a count command that will return the number records in
	// specified destination table. If the query conditions was empty, it
	// returns number of all records un destination table.
	Count() (int, error)

	// First fetch data of the first record that match with query conditions.
	First() (Scheme, error)

	// All returns results that match with query conditions in RecordDataSet
	// format. If the query conditions was empty it will return all records
	// in specified destination table or error if anything went wrong.
	All() ([]Scheme, error)

	// Update updates records that math with query conditions with `data` and
	// returns number of affected rows and error if anything went wring. If
	// the query condition was empty it'll update all records in destination
	// table.
	Update(data Scheme) (int, error)

	// Delete removes every records in destination table that match with condition
	// query and returns number of affected rows and error if anything went wrong.
	// It will removes all records inside destination table if no condition query
	// was set.
	Delete() (int, error)
}

Builder is a wrapper around QueryBuilder that convert RecordData object to model's related scheme.

type Client

type Client interface {

	// CreateTable creates `tableName` table with field and structure
	// defined in `structure` parameter for each table fields
	CreateTable(tableName string, info TableInfo) error

	// EnsureIndex ensures that `index` is exists on `tableName` table,
	// if not, it tries to create index with specified condition in
	// `index` on `tableName`.
	EnsureIndex(tableName string, index Index) error

	// Insert tries to insert `data` into `tableName` and returns error if
	// anything went wrong. `data` should pass by reference to have exact
	// data on `tableName`, otherwise updated record data isn't accessible.
	Insert(tableName string, data *RecordData) error

	// FindByID searches through `tableName` records to find a row that its
	// ID match with `id` and returns it alongside any possible error.
	FindByID(tableName string, id interface{}) (RecordData, error)

	// UpdateByID finds a record in `tableName` that its ID match with `id`,
	// and updates it with data. It will return error if anything went wrong.
	UpdateByID(tableName string, id interface{}, data RecordData) error

	// DeleteByID finds a record in `tableName` that its ID match with `id`,
	// and remove it entirely. It will return error if anything went wrong.
	DeleteByID(tableName string, id interface{}) error

	// Query generates and returns query object for further operations
	Query(tableName string, conditions ...Condition) QueryBuilder

	// Close disconnect client from database and release the taken memory
	Close()
}

Client is an interface for database clients. Database clients are responsible with connecting and interacting with database instance.

type CollectionInfo

type CollectionInfo struct {
	Info *mgo.CollectionInfo
}

CollectionInfo is a wrapper for mgo.CollectionInfo that make it compatible with TableInfo interface for using in clients.

func (CollectionInfo) GetInfo

func (c CollectionInfo) GetInfo() interface{}

GetInfo returns the base mgo.CollectionInfo object

type Condition

type Condition interface {
	// GetField returns the name of field to for querying
	GetField() string

	// GetValue returns the value to be compared or checked in query
	GetValue() interface{}
}

Condition is an interface for query conditions

type DBConfig

type DBConfig struct {
	Driver   driverName
	Host     string
	Port     string
	Database string
	Username string
	Password string
	Prefix   string
	Options  map[string]string
}

DBConfig is the connection settings and options

func (*DBConfig) AddOption

func (c *DBConfig) AddOption(key string, value string)

AddOption append given key value to connection options map

func (*DBConfig) GetOptions

func (c *DBConfig) GetOptions() (options string)

GetOptions return connection options as a querystring

func (*DBConfig) HasPrefix

func (c *DBConfig) HasPrefix() bool

HasPrefix Check if any table/collection prefix is set

type Enquoter

type Enquoter func(i interface{}) string

Enquoter is function alias for clients enquoting operation

type FieldStructure

type FieldStructure struct {
	Name    string
	Type    string
	Options string
	// contains filtered or unexported fields
}

FieldStructure is representing a field structure in a table

func (FieldStructure) String

func (s FieldStructure) String() string

type Index

type Index struct {
	// Column or columns to be index
	Columns []string

	// Determine the selected column or columns should be treated
	// as an unique index. note that if you set `Columns` with
	// multiple columns, a composite unique key will be created.
	Unique bool
}

Index is a struct for declaring columns to be indexed. Indexes can have multiple columns (composite index) and can be defined as unique index.

type MongoCollection

type MongoCollection interface {
	Bulk() *mgo.Bulk
	Count() (n int, err error)
	Create(info *mgo.CollectionInfo) error
	DropAllIndexes() error
	DropCollection() error
	DropIndex(key ...string) error
	DropIndexName(name string) error
	EnsureIndex(index mgo.Index) error
	EnsureIndexKey(key ...string) error
	Find(query interface{}) *mgo.Query
	FindId(id interface{}) *mgo.Query
	Indexes() (indexes []mgo.Index, err error)
	Insert(docs ...interface{}) error
	NewIter(session *mgo.Session, firstBatch []bson.Raw, cursorID int64, err error) *mgo.Iter
	Pipe(pipeline interface{}) *mgo.Pipe
	Remove(selector interface{}) error
	RemoveAll(selector interface{}) (info *mgo.ChangeInfo, err error)
	RemoveId(id interface{}) error
	Repair() *mgo.Iter
	Update(selector interface{}, update interface{}) error
	UpdateAll(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error)
	UpdateId(id interface{}, update interface{}) error
	Upsert(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error)
	UpsertId(id interface{}, update interface{}) (info *mgo.ChangeInfo, err error)
	Watch(pipeline interface{}, options mgo.ChangeStreamOptions) (*mgo.ChangeStream, error)
	With(s *mgo.Session) *mgo.Collection
}

MongoCollection is an interface for mgo.Collection and used for testing and mocking

type MongoQuery

type MongoQuery interface {
	All(result interface{}) error
	Apply(change mgo.Change, result interface{}) (info *mgo.ChangeInfo, err error)
	Batch(n int) *mgo.Query
	Collation(collation *mgo.Collation) *mgo.Query
	Comment(comment string) *mgo.Query
	Count() (n int, err error)
	Distinct(key string, result interface{}) error
	Explain(result interface{}) error
	For(result interface{}, f func() error) error
	Hint(indexKey ...string) *mgo.Query
	Iter() *mgo.Iter
	Limit(n int) *mgo.Query
	LogReplay() *mgo.Query
	MapReduce(job *mgo.MapReduce, result interface{}) (info *mgo.MapReduceInfo, err error)
	One(result interface{}) (err error)
	Prefetch(p float64) *mgo.Query
	Select(selector interface{}) *mgo.Query
	SetMaxScan(n int) *mgo.Query
	SetMaxTime(d time.Duration) *mgo.Query
	Skip(n int) *mgo.Query
	Snapshot() *mgo.Query
	Sort(fields ...string) *mgo.Query
	Tail(timeout time.Duration) *mgo.Iter
}

MongoQuery is an interface for mgo.Query and used for testing and mocking

type MongoSession

type MongoSession interface {
	BuildInfo() (info mgo.BuildInfo, err error)
	Clone() *mgo.Session
	Close()
	Copy() *mgo.Session
	DB(name string) *mgo.Database
	DatabaseNames() (names []string, err error)
	EnsureSafe(safe *mgo.Safe)
	FindRef(ref *mgo.DBRef) *mgo.Query
	Fsync(async bool) error
	FsyncLock() error
	FsyncUnlock() error
	LiveServers() (addrs []string)
	Login(cred *mgo.Credential) error
	LogoutAll()
	Mode() mgo.Mode
	New() *mgo.Session
	Ping() error
	Refresh()
	ResetIndexCache()
	Run(cmd interface{}, result interface{}) error
	Safe() (safe *mgo.Safe)
	SelectServers(tags ...bson.D)
	SetBatch(n int)
	SetBypassValidation(bypass bool)
	SetCursorTimeout(d time.Duration)
	SetMode(consistency mgo.Mode, refresh bool)
	SetPoolLimit(limit int)
	SetPoolTimeout(timeout time.Duration)
	SetPrefetch(p float64)
	SetSafe(safe *mgo.Safe)
	SetSocketTimeout(d time.Duration)
	SetSyncTimeout(d time.Duration)
}

MongoSession is an interface for mgo.Session and used for testing and mocking

type MsScheme

type MsScheme interface {

	// Extend Scheme interface
	Scheme

	// GetSchema returns name of the table schema
	GetSchema() string
}

MsScheme is the same as Scheme except that it has one more method that is for getting table scheme name in database

type Pruner

type Pruner func(recordMap *RecordMap)

Pruner is a function that can prune data of a record data

type QueryBuilder

type QueryBuilder interface {

	// OrderBy set the order of returning result in following command
	OrderBy(sorts ...Sort) QueryBuilder

	// Limit set the limit that determines how many results should be
	// returned in the following fetch command.
	Limit(n int) QueryBuilder

	// Skip set the starting offset of the following fetch command
	Skip(n int) QueryBuilder

	// Count execute a count command that will return the number records in
	// specified destination table. If the query conditions was empty, it
	// returns number of all records un destination table.
	Count() (int, error)

	// First fetch data of the first record that match with query conditions.
	First() (RecordData, error)

	// All returns results that match with query conditions in RecordDataSet
	// format. If the query conditions was empty it will return all records
	// in specified destination table or error if anything went wrong.
	All() (RecordDataSet, error)

	// Update updates records that math with query conditions with `data` and
	// returns number of affected rows and error if anything went wring. If
	// the query condition was empty it'll update all records in destination
	// table.
	Update(data RecordData) (int, error)

	// Delete removes every records in destination table that match with condition
	// query and returns number of affected rows and error if anything went wrong.
	// It will removes all records inside destination table if no condition query
	// was set.
	Delete() (int, error)
}

QueryBuilder is an object that contains information about query. With QueryBuilder you can fetch, update and delete records from database.

type RecordData

type RecordData struct {
	// contains filtered or unexported fields
}

RecordData is a struct containing a map of string interface which represents data of a record in database and list of columns name used for keep map in order. It could be used for both upserting and fetching data from database. Map key represents the column name and its value represents the column value in database

func NewRecordData

func NewRecordData(keys []string, data RecordMap) *RecordData

NewRecordData instantiate the record data with keys and data

func ZeroRecordData

func ZeroRecordData() *RecordData

ZeroRecordData instantiate the record data with zero values

func (*RecordData) Get

func (d *RecordData) Get(key string) interface{}

Get returns the value sets for `key`

func (*RecordData) GetColumns

func (d *RecordData) GetColumns() []string

GetColumns returns list of columns

func (*RecordData) GetMap

func (d *RecordData) GetMap() *RecordMap

GetMap returns the data map

func (*RecordData) GetValues

func (d *RecordData) GetValues(enquoter Enquoter) []string

GetValues returns list of values enquoted by `enquoter`

func (*RecordData) Length

func (d *RecordData) Length() int

Length returns length of record data map

func (*RecordData) PruneData

func (d *RecordData) PruneData(pruner Pruner)

PruneData prune recordData data by pruner function

func (*RecordData) RenameKey added in v0.1.4

func (d *RecordData) RenameKey(currentName string, newName string) error

RenameKey rename the currentName key to newName

func (*RecordData) Set

func (d *RecordData) Set(key string, value interface{})

Set sets `value` for `key` in record data map. It will replace the key value if it key is already exists.

func (*RecordData) Zero

func (d *RecordData) Zero()

Zero will empty all fields of record data

type RecordDataSet

type RecordDataSet []RecordData

RecordDataSet is slice of RecordData represents results from db

type RecordMap

type RecordMap map[string]interface{}

RecordMap is map of string-interface that represent data on a record

type SQLDatabase

type SQLDatabase interface {
	Begin() (*sql.Tx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
	Close() error
	Conn(ctx context.Context) (*sql.Conn, error)
	Driver() driver.Driver
	Exec(query string, args ...interface{}) (sql.Result, error)
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Ping() error
	PingContext(ctx context.Context) error
	Prepare(query string) (*sql.Stmt, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	SetConnMaxLifetime(d time.Duration)
	SetMaxIdleConns(n int)
	SetMaxOpenConns(n int)
	Stats() sql.DBStats
}

SQLDatabase is an interface for sql.DB and used for testing and mocking

type SQLRows

type SQLRows interface {
	Close() error
	ColumnTypes() ([]*sql.ColumnType, error)
	Columns() ([]string, error)
	Err() error
	Next() bool
	NextResultSet() bool
	Scan(dest ...interface{}) error
}

SQLRows is an interface for sql.Rows and used for testing and mocking

type SQLTag

type SQLTag map[string]string

SQLTag is a map of scheme sql tags key value

type Scheme

type Scheme interface {

	// GetID returns the value of the record identifier
	GetID() interface{}

	// GetKeyName return the name of primary key field name
	GetKeyName() string
}

Scheme is an interface represent a record or document in database

type Sort

type Sort struct {
	// Column is the name of column to order the results by
	Column string

	// Descending determine sort is descending or ascending
	Descending bool
}

Sort is a struct for declaring result sort. It contains Column which is column/field name and Descending which determine the sort of results. result will sort Ascending by default

type TableInfo

type TableInfo interface {
	GetInfo() interface{}
}

TableInfo is an interface used for data of a table or collection. it could be a table structure or collection info.

type TableStructure

type TableStructure []FieldStructure

TableStructure is representing structure of a table fields

func (TableStructure) GetInfo

func (t TableStructure) GetInfo() interface{}

GetInfo convert TableStructure to string value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL