Documentation ¶
Index ¶
- Constants
- func Drivers() []string
- func In(query string, args ...interface{}) (string, []interface{}, error)
- func Rebind(bindType BindVarType, query string) string
- func Register(name string, driver Driver)
- func RegisterDialect(dia Dialect)
- func RegisterModel(m interface{}) error
- type BindVarType
- type Column
- type Conn
- type Cursor
- type D
- type DB
- type DeleteOptions
- type Dialect
- type Driver
- type DropTableOptions
- type E
- type Executor
- type FieldType
- type Filter
- type FindOptions
- type Hook
- type Index
- type IndexKey
- type InsertOptions
- type M
- type OpenOptions
- type Order
- type OrderType
- type ReadPreference
- type Result
- type Scanner
- type SingleResult
- type Tx
- type TxOptions
- type UpdateOptions
Constants ¶
const ( OrderAny OrderType = iota OrderAsc = 0 // 默认升序 OrderDesc = 1 )
Variables ¶
This section is empty.
Functions ¶
func Drivers ¶
func Drivers() []string
Drivers returns a sorted list of the names of the registered drivers.
func In ¶
In expands slice values in args, returning the modified query string and a new arg list that can be executed by a database. The `query` should use the `?` bindVar. The return value uses the `?` bindVar.
func Rebind ¶
func Rebind(bindType BindVarType, query string) string
Rebind a query from the default bindtype (QUESTION) to the target bindtype.
Types ¶
type BindVarType ¶
type BindVarType uint8
Bindvar types supported by Rebind, BindMap and BindStruct.
const ( BindVarUnknown BindVarType = iota // BindVarQuestion // such as mysql, use ? BindVarDollar // such as postgres, $1 $2 BindVarNamed // such as oracle, :named, :arg1,:arg2 BindVarAt // such as sqlserver, @p1, @p2 )
type Column ¶
type Column struct { Name string // 字段名 Type string // 类型 Default string // 默认值, 不需要双引号 NotNull bool // 是否非空 AutoIncrement bool // 是否自增 PrimaryKey bool // 是否是主键 Size int // 类型大小 }
列信息,用于CreateTable
type Conn ¶
type Conn interface { CreateDB(ctx context.Context, name string) error DropDB(ctx context.Context, name string) error Database(ctx context.Context, name string) (DB, error) // ListDatabases(ctx context.Context) ([]string, error) Ping(ctx context.Context) error }
Conn 代表客户端连接
func Open ¶
func Open(driverName, dataSourceName string, opts ...*OpenOptions) (Conn, error)
Open opens a database specified by its database driver name and a driver-specific data source name, usually consisting of at least a database name and connection information.
Most users will open a database via a driver-specific connection helper function that returns a *DB. No database drivers are included in the Go standard library. See https://golang.org/s/sqldrivers for a list of third-party drivers.
Open may just validate its arguments without creating a connection to the database. To verify that the data source name is valid, call Ping.
The returned DB is safe for concurrent use by multiple goroutines and maintains its own pool of idle connections. Thus, the Open function should be called just once. It is rarely necessary to close a DB.
type Cursor ¶
type Cursor interface { Next() bool Decode(out interface{}) error All(results interface{}) error Close() error }
Cursor 查询结果 Scan和Decode的区别是,Scan对应原生sql中的Scan,而Decode则是反射解析到struct中
type D ¶
type D []E
D is an ordered representation of a BSON document. This type should be used when the order of the elements matters, such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.
Example usage:
bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
type DB ¶
type DB interface { Name() string Close() error Indexes(ctx context.Context, table string) ([]*Index, error) CreateIndex(ctx context.Context, table string, index *Index) error DropIndex(ctx context.Context, table string, name string) error CreateTable(ctx context.Context, table string, columns []*Column) error DropTable(ctx context.Context, table string, opts ...*DropTableOptions) error Begin(ctx context.Context, opts *TxOptions) (Tx, error) // CRUD接口,Options仅支持1个 Insert(ctx context.Context, table string, datas []interface{}, opts ...*InsertOptions) (Result, error) InsertOne(ctx context.Context, table string, data interface{}, opts ...*InsertOptions) (Result, error) Update(ctx context.Context, table string, filter interface{}, data interface{}, opts ...*UpdateOptions) (Result, error) UpdateOne(ctx context.Context, table string, filter interface{}, data interface{}, opts ...*UpdateOptions) (Result, error) Delete(ctx context.Context, table string, filter interface{}, opts ...*DeleteOptions) (Result, error) DeleteOne(ctx context.Context, table string, filter interface{}, opts ...*DeleteOptions) (Result, error) Find(ctx context.Context, table string, filter interface{}, opts ...*FindOptions) (Cursor, error) FindOne(ctx context.Context, table string, filter interface{}, opts ...*FindOptions) SingleResult // 原生sql Query(ctx context.Context, query string, args ...interface{}) (Cursor, error) Exec(ctx context.Context, query string, args ...interface{}) (Result, error) }
DB db相关接口,filter使用interface{},可以是bson.M或者Filter
type DeleteOptions ¶
type DeleteOptions struct { }
type Dialect ¶
type Dialect interface { Name() string // Drivers 返回已知的Driver名 Drivers() []string // 返回bind variables类型 BindVar() BindVarType // Indexes(ctx context.Context, db Executor, table string) ([]*Index, error) CreateIndex(ctx context.Context, db Executor, table string, index *Index) error DropIndex(ctx context.Context, db Executor, table string, name string) error CreateTable(ctx context.Context, db Executor, table string, columns []*Column) error DropTable(ctx context.Context, db Executor, table string, opts ...*DropTableOptions) error // crud Insert(ctx context.Context, db Executor, bindType BindVarType, table string, datas []interface{}, opts *InsertOptions) (Result, error) InsertOne(ctx context.Context, db Executor, bindType BindVarType, table string, data interface{}, opts *InsertOptions) (Result, error) Update(ctx context.Context, db Executor, bindType BindVarType, table string, filter interface{}, data interface{}, opts *UpdateOptions) (Result, error) UpdateOne(ctx context.Context, db Executor, bindType BindVarType, table string, filter interface{}, data interface{}, opts *UpdateOptions) (Result, error) Delete(ctx context.Context, db Executor, bindType BindVarType, table string, filter interface{}, opts *DeleteOptions) (Result, error) DeleteOne(ctx context.Context, db Executor, bindType BindVarType, table string, filter interface{}, opts *DeleteOptions) (Result, error) Find(ctx context.Context, db Executor, bindType BindVarType, table string, filter interface{}, opts *FindOptions) (Cursor, error) FindOne(ctx context.Context, db Executor, bindType BindVarType, table string, filter interface{}, opts ...*FindOptions) SingleResult // direct Query(ctx context.Context, db Executor, query string, args ...interface{}) (Cursor, error) Exec(ctx context.Context, db Executor, query string, args ...interface{}) (Result, error) }
Dialect 用于实现不同sql之间差异,需要无状态 [SQL Drivers](https://github.com/golang/go/wiki/SQLDrivers) [Go database/sql tutorial](http://go-database-sql.org/index.html)
type Driver ¶
type Driver interface {
Open(dataSourceName string, opts *OpenOptions) (Conn, error)
}
Driver .
type DropTableOptions ¶
type DropTableOptions struct { }
type E ¶
type E struct { Key string Value interface{} }
E represents a BSON element for a D. It is usually used inside a D.
type Executor ¶
type Executor interface { ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row }
Executor 标准sql接口
type FindOptions ¶
type FindOptions struct { ReadPref ReadPreference // Sort []Order Offset int64 Limit int64 }
type Index ¶
TODO:Partial Index https://blog.huoding.com/2016/04/28/510
type InsertOptions ¶
type InsertOptions struct { }
type M ¶
type M map[string]interface{}
M is an unordered representation of a BSON document. This type should be used when the order of the elements does not matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be serialized in an undefined, random order. If the order of the elements matters, a D should be used instead.
Example usage:
bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}.
type OpenOptions ¶
type ReadPreference ¶
type ReadPreference uint8
ReadPreference see: https://docs.mongodb.com/manual/core/read-preference/
const ( // ReadPrefPrimary indicates that only a primary is // considered for reading. This is the default // mode. ReadPrefPrimary ReadPreference // ReadPrefPrimaryPreferred indicates that if a primary // is available, use it; otherwise, eligible // secondaries will be considered. ReadPrefPrimaryPreferred // ReadPrefSecondary indicates that only secondaries // should be considered. ReadPrefSecondary // ReadPrefSecondaryPreferred indicates that only secondaries // should be considered when one is available. If none // are available, then a primary will be considered. ReadPrefSecondaryPreferred // ReadPrefNearest indicates that all primaries and secondaries // will be considered. ReadPrefNearest )
type Scanner ¶
Scanner 区别于Find,Find用于单次查询,而Scanner用于分页遍历全部符合条件的数据并逐条处理
func NewScanner ¶
func NewScanner(ctx context.Context, out interface{}, db DB, table string, filter interface{}, limit int64, opts *FindOptions) (Scanner, error)
NewScanner .
type SingleResult ¶
SingleResult represents a single document returned from an operation. If the operation resulted in an error, all SingleResult methods will return that error. If the operation did not return any documents, all SingleResult methods will return ErrNoDocuments.
type Tx ¶
type Tx interface { Commit() error Rollback() error // 通用CRUD Insert(ctx context.Context, table string, datas []interface{}, opts ...*InsertOptions) (Result, error) InsertOne(ctx context.Context, table string, data interface{}, opts ...*InsertOptions) (Result, error) Update(ctx context.Context, table string, filter interface{}, data interface{}, opts ...*UpdateOptions) (Result, error) UpdateOne(ctx context.Context, table string, filter interface{}, data interface{}, opts ...*UpdateOptions) (Result, error) Delete(ctx context.Context, table string, filter interface{}, opts ...*DeleteOptions) (Result, error) DeleteOne(ctx context.Context, table string, filter interface{}, opts ...*DeleteOptions) (Result, error) Find(ctx context.Context, table string, filter interface{}, opts ...*FindOptions) (Cursor, error) FindOne(ctx context.Context, table string, filter interface{}, opts ...*FindOptions) SingleResult // 原生sql Query(ctx context.Context, query string, args ...interface{}) (Cursor, error) Exec(ctx context.Context, query string, args ...interface{}) (Result, error) }
Tx 事务
type UpdateOptions ¶
type UpdateOptions struct {
IgnoreZero bool // 是否忽略空值
}