driver

package
v0.0.0-...-012d1c6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

定义数据库操作接口

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidIndexName = errors.New("invalid index name")
)
View Source
var (
	ErrNoDocument = errors.New("no document found")
)

Functions

func Decode

func Decode(cursor Cursor, result interface{}) error

反射数据

func Drivers

func Drivers() []string

Drivers returns a sorted list of the names of the registered drivers.

func Register

func Register(name string, driver Driver)

Register makes a database driver available by the provided name. If Register is called twice with the same name or if driver is nil, it panics.

Types

type BinaryCond

type BinaryCond interface {
	Cond
	X() Cond
	Y() Cond
}

type Column

type Column struct {
	Name          string // 字段名
	Type          string // 类型
	Default       string // 默认值, 不需要双引号
	NotNull       bool   // 是否非空
	AutoIncrement bool   // 是否自增
	PrimaryKey    bool   // 是否是主键
	Size          int    // 类型大小
}

列信息,用于CreateTable

func ParseModel

func ParseModel(model interface{}) ([]Column, error)

ParseModel 解析Model,获得Column信息

type Cond

type Cond interface {
	Operator() Token
}

type Cursor

type Cursor interface {
	//ID() int64
	Close() error
	Next() bool
	Scan(dest ...interface{}) error
	Decode(model interface{}) error
}

Scan和Decode的区别是,Scan对应原生sql中的Scan,而Decode则是反射解析到struct中

type Database

type Database interface {
	Indexes(table string) ([]*Index, error)
	CreateIndex(table string, index *Index) error
	DropIndex(table string, name string) error

	CreateTable(table string, columns []Column) error
	DropTable(table string) error

	Insert(table string, doc interface{}, opts *InsertOptions) (*InsertResult, error)
	Delete(table string, filter Cond, opts *DeleteOptions) (*DeleteResult, error)
	Update(table string, filter Cond, update interface{}, opts *UpdateOptions) (*UpdateResult, error)
	Query(table string, filter Cond, opts *QueryOptions) (QueryResult, error)
}

Database 用于对某个特定数据库进行增删改查

type DeleteOptions

type DeleteOptions struct {
	One bool
}

type DeleteResult

type DeleteResult struct {
	DeletedCount int64
}

type Driver

type Driver interface {
	Name() string
	Open(opts *OpenOptions) error
	Close() error
	Ping() error
	Database(name string) (Database, error)
	Drop(name string) error
}

Driver 数据库驱动,支持同时连接多个数据库

func Find

func Find(name string) (Driver, error)

type ExprCond

type ExprCond interface {
	Cond
	Key() string
	Value() interface{}
}

例如,TOK_EQ

type FieldType

type FieldType int
const (
	FTChar FieldType = 0
)

支持的数据类型

type Index

type Index struct {
	Name       string
	Keys       []IndexKey
	Background bool
	Unique     bool
	Sparse     bool
}

TODO:Partial Index https://blog.huoding.com/2016/04/28/510

func (*Index) GenerateName

func (i *Index) GenerateName()

type IndexKey

type IndexKey struct {
	Name  string
	Order Order
}

type InsertOptions

type InsertOptions struct {
	Context context.Context
}

type InsertResult

type InsertResult struct {
	InsertedIDs []interface{}
}

*

  • All Results

type ListCond

type ListCond interface {
	Cond
	List() []Cond
}

type Model

type Model struct {
	Name    string
	Columns []Column
	Indexes []Index
}

简单数据表模型,不支持外键等操作

func (*Model) Parse

func (m *Model) Parse(model interface{}) error

type OpenOptions

type OpenOptions struct {
	URI string
}

type Order

type Order int
const (
	Asc  Order = 0 // 默认升序
	Desc       = 1
)

type QueryOptions

type QueryOptions struct {
	One        bool
	Skip       int64
	Limit      int64
	Sort       map[string]int //1:ascending, -1:descending
	Projection map[string]int //1:include 0:exclude(有些不支持),nil代表全部
	Context    context.Context
}

type QueryResult

type QueryResult interface {
	Cursor() Cursor
	Decode(result interface{}) error
}

查询结果,可以调用Decode自动反射结果,也可以调用Cursor,手动遍历结果

type Token

type Token int

https://docs.mongodb.com/manual/reference/operator/query-comparison/ Query embedded document: $elemMatch vs. Dot Notation

const (
	TOK_NULL Token = iota
	TOK_EQ
	TOK_NE
	TOK_GT
	TOK_GTE
	TOK_LT
	TOK_LTE
	TOK_IN
	TOK_NIN
	// logical Query Operators
	TOK_AND
	TOK_OR
	TOK_NOR
	TOK_NOT
)

type UnaryCond

type UnaryCond interface {
	Cond
	X() Cond
}

type UpdateOptions

type UpdateOptions struct {
	One    bool
	Upsert bool
}

type UpdateResult

type UpdateResult struct {
	// The number of documents that matched the filter.
	MatchedCount int64
	// The number of documents that were modified.
	ModifiedCount int64
	// The number of documents that were upserted.
	UpsertedCount int64
	// The identifier of the inserted document if an upsert took place.
	UpsertedID interface{}
}

UpdateResult is a result of an update operation.

UpsertedID will be a Go type that corresponds to a BSON type.

Jump to

Keyboard shortcuts

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