litedb

package module
v0.0.0-...-1816c93 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2018 License: MIT Imports: 9 Imported by: 0

README

LiteDB

一个轻便以及追求SQL性能的MYSQL客户端

Intro

LiteDB 的核心设计目标是提供一个轻量级的SQL封装.
LiteDB 不会对设计范式与Mysql本身做更多的侵入
LiteDB 提供基本的SQL CURD封装
LiteDB 不提供任何形式的SQLBuilder
LiteDB 使用 `database/sql` 和 mysql驱动

Document

https://github.com/weixinhost/litedb/wiki

Configure

Client.Config.Set 请参考https://github.com/go-sql-driver/mysql

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug bool = false

Functions

func CloseDebug

func CloseDebug()

关闭Debug模式

func ListStructToMap

func ListStructToMap(vs interface{}) ([]map[string]string, error)

func OpenDebug

func OpenDebug()

开启Debug模式

func SetConnMaxLifeTime

func SetConnMaxLifeTime(max time.Duration)

* 设置每个连接的最大生存时间。如果小于等于0 则用不过期。 *

func StructToMap

func StructToMap(structV interface{}) (map[string]string, error)

func ToInt64

func ToInt64(value interface{}) (d int64)

ToInt64 interface to int64

func ToStr

func ToStr(value interface{}, args ...int) (s string)

ToStr interface to string

Types

type Client

type Client struct {
	Sql
	Host     string
	Port     uint32
	User     string
	Password string
	Database string
	Protocol string
	Config   *ClientDNSConfigure
	// contains filtered or unexported fields
}

客户端

func NewClient

func NewClient(protocol string, host string, port uint32, user string, password string, database string) *Client

初始化数据库 此时并未打开连接池 只有在真实需要与数据库交互的时候才会进行连接.

func NewTcpClient

func NewTcpClient(host string, port uint32, user string, password string, database string) *Client

初始化一个TCP客户端

func (*Client) Begin

func (this *Client) Begin() (*Transaction, error)

开启事务

func (*Client) Close

func (this *Client) Close() error

关闭数据库

func (*Client) Ping

func (this *Client) Ping() error

ping

func (*Client) SetMaxConn

func (this *Client) SetMaxConn(n int)

func (*Client) SetMaxIdleConn

func (this *Client) SetMaxIdleConn(n int)

type ClientDNSConfigure

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

客户端DNS配置

func NewClientDnsConfigure

func NewClientDnsConfigure() *ClientDNSConfigure

func (*ClientDNSConfigure) Parse

func (this *ClientDNSConfigure) Parse() string

将起解析DNS格式的字符串

func (*ClientDNSConfigure) Remove

func (this *ClientDNSConfigure) Remove(k string) bool

移除设置 Remove("timeout")

func (*ClientDNSConfigure) Set

func (this *ClientDNSConfigure) Set(k, v string) bool

设置一个客户端DNS设置. Set("timeout","5") 详细信息请参考golang mysql DNS语法

type ClientExecResult

type ClientExecResult struct {
	Result sql.Result
	Err    error //db error
}

Client.Exec 的结果

type ClientQueryResult

type ClientQueryResult struct {
	Rows *sql.Rows
	Err  error // db error
}

Client.Query 的结果

func (*ClientQueryResult) FirstToMap

func (this *ClientQueryResult) FirstToMap() (map[string]string, error)

将Rows中的首行解析成一个map[string]string

func (*ClientQueryResult) FirstToStruct

func (this *ClientQueryResult) FirstToStruct(v interface{}) error

将首行解析成一个Struct ,需要传递一个 struct的指针. struct 定义中使用标签 tag 来进行数据库字段映射,比如

struct {
	 Id int `db:"id"`
  Name string `db:"name"`
}

func (*ClientQueryResult) ToMap

func (this *ClientQueryResult) ToMap() ([]map[string]string, error)

ToMap 将结果集转换为Map类型. 这个操作不进行任何类型转换. 因为这里的类型转换需要一次SQL去反射字段类型. 更多的时候会得不偿失.

func (*ClientQueryResult) ToStruct

func (this *ClientQueryResult) ToStruct(containers interface{}) error

将结果集转换成一个struct 数组 var containers []Person

ToStruct(&containers) 对于struct类型,支持以下字段类型: int8

int16

int32

int64

int

uint8

uint16

uint32

uint64

uint

float32

float64

string

[]byte

type EmptyRowsError

type EmptyRowsError struct {
}

EmptyRowsError 未发现行

func (*EmptyRowsError) Error

func (err *EmptyRowsError) Error() string

type MarshalBinary

type MarshalBinary interface {
	MarshalDB() ([]byte, error)
}

支持struct中的字段拥有更复杂的类型. 需要实现该接口才能正确的打包成string插入数据库中

type NetError

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

NetError 网络错误

func (*NetError) Error

func (err *NetError) Error() string

type ReflectError

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

ReflectError 反射阶段错误

func (*ReflectError) Error

func (err *ReflectError) Error() string

type SQLError

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

SQLError 错误

func (*SQLError) Error

func (err *SQLError) Error() string

type Sql

type Sql struct {
	Exec  func(sqlFmt string, sqlValue ...interface{}) *ClientExecResult
	Query func(sqlFmt string, sqlValue ...interface{}) *ClientQueryResult
}

Sql操作集

func (*Sql) BatchInsert

func (this *Sql) BatchInsert(table string, vs interface{}) *ClientExecResult

批量插入 SQL语句为: INSERT INTO `%s` (field,field) VALUES (?,?),(?,?)

func (*Sql) BatchReplace

func (this *Sql) BatchReplace(table string, vs interface{}) *ClientExecResult

批量重置 SQL语句为: REPLACE INTO `%s` (field,field) VALUES (?,?),(?,?)

func (*Sql) Delete

func (this *Sql) Delete(table string, whereFmt string, whereValue ...interface{}) *ClientExecResult

根据Where条件删除数据

func (*Sql) Insert

func (this *Sql) Insert(table string, v interface{}) *ClientExecResult

对Struct类型的支持,使用 db tag 进行数据库字段映射 对Map类型会将value转换为string.请确保map类型中只包含基本数据类型

func (*Sql) InsertOrUpdate

func (this *Sql) InsertOrUpdate(table string, v interface{}) *ClientExecResult

插入或更新行(当主键已存在的时候) SQL语句为: INSERT INTO .... ON DUPLICATE KEY UPDATE .... 全部字段更新

func (*Sql) InsertOrUpdateFields

func (this *Sql) InsertOrUpdateFields(table string, v interface{}, updateFields ...string) *ClientExecResult

map类型无必要使用该方法 插入或更新行(当主键已存在的时候) SQL语句为: INSERT INTO .... ON DUPLICATE KEY UPDATE .... 可以指定更新字段

func (*Sql) Update

func (this *Sql) Update(table string, v interface{}, whereFmt string, whereValue ...interface{}) *ClientExecResult

对Struct类型的支持,使用 db tag 进行数据库字段映射 对Map类型会将value转换为string.请确保map类型中只包含基本数据类型 where 条件写法 id = ?

func (*Sql) UpdateFields

func (this *Sql) UpdateFields(table string, v interface{}, fields []string, whereFmt string, whereValue ...interface{}) *ClientExecResult

map类型无必要使用该方法 部分字段更新 该接口的意义是struct类型为完整的数据库字段映射.但某些时候我们仅仅需要更新部分字段.此时,如果使用完整映射的进行更新操作 则更容易误覆盖. 因此提供了这个接口进行部分字段更新. fields 就是需要更新的数据库字段名称 v,whereFmt,WhereValue 等值意义不变

type StrTo

type StrTo string

StrTo is the target string

func (StrTo) Bool

func (f StrTo) Bool() (bool, error)

Bool string to bool

func (*StrTo) Clear

func (f *StrTo) Clear()

Clear string

func (StrTo) Exist

func (f StrTo) Exist() bool

Exist check string exist

func (StrTo) Float32

func (f StrTo) Float32() (float32, error)

Float32 string to float32

func (StrTo) Float64

func (f StrTo) Float64() (float64, error)

Float64 string to float64

func (StrTo) Int

func (f StrTo) Int() (int, error)

Int string to int

func (StrTo) Int16

func (f StrTo) Int16() (int16, error)

Int16 string to int16

func (StrTo) Int32

func (f StrTo) Int32() (int32, error)

Int32 string to int32

func (StrTo) Int64

func (f StrTo) Int64() (int64, error)

Int64 string to int64

func (StrTo) Int8

func (f StrTo) Int8() (int8, error)

Int8 string to int8

func (*StrTo) Set

func (f *StrTo) Set(v string)

Set string

func (StrTo) String

func (f StrTo) String() string

String string to string

func (StrTo) Uint

func (f StrTo) Uint() (uint, error)

Uint string to uint

func (StrTo) Uint16

func (f StrTo) Uint16() (uint16, error)

Uint16 string to uint16

func (StrTo) Uint32

func (f StrTo) Uint32() (uint32, error)

Uint32 string to uint31

func (StrTo) Uint64

func (f StrTo) Uint64() (uint64, error)

Uint64 string to uint64

func (StrTo) Uint8

func (f StrTo) Uint8() (uint8, error)

Uint8 string to uint8

type Transaction

type Transaction struct {
	Sql
	// contains filtered or unexported fields
}

事务客户端

func (*Transaction) Commit

func (this *Transaction) Commit() error

提交事务

func (*Transaction) Rollback

func (this *Transaction) Rollback() error

回滚事务

func (*Transaction) Roolback

func (this *Transaction) Roolback() error

回滚事务

type UnmarshalBinary

type UnmarshalBinary interface {
	UnmarshalDB(data []byte) error
}

对 MarshalBinary 的反向操作

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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