Documentation ¶
Index ¶
- Constants
- Variables
- func ParseSqlDbType(str string) (string, error)
- type KeyValue
- func (kv *KeyValue) GetCountSql() (string, []interface{})
- func (kv *KeyValue) GetCreateTableSql(dbType string) string
- func (kv *KeyValue) GetInsertSql(dbType string) (string, []interface{})
- func (kv *KeyValue) GetSaveSql(dbType string) (string, []interface{})
- func (kv *KeyValue) GetTableName() string
- func (kv *KeyValue) GetUpdateSql() (string, []interface{})
- type NewSqlDBOptions
- type SqlDBHandle
- func (p *SqlDBHandle) BeginDbTransaction(txName string) (protocol.SqlDBTransaction, error)
- func (p *SqlDBHandle) Close() error
- func (p *SqlDBHandle) CommitDbTransaction(txName string) error
- func (p *SqlDBHandle) CompactRange(start, limit []byte) error
- func (p *SqlDBHandle) CreateDatabaseIfNotExist(dbName string) (bool, error)
- func (p *SqlDBHandle) CreateTable(obj TableDDLGenerator) error
- func (p *SqlDBHandle) CreateTableIfNotExist(objI interface{}) error
- func (p *SqlDBHandle) Delete(key []byte) error
- func (p *SqlDBHandle) ExecSql(sql string, values ...interface{}) (int64, error)
- func (p *SqlDBHandle) Get(key []byte) ([]byte, error)
- func (p *SqlDBHandle) GetDbTransaction(txName string) (protocol.SqlDBTransaction, error)
- func (p *SqlDBHandle) GetDbType() string
- func (p *SqlDBHandle) GetKeys(keys [][]byte) ([][]byte, error)
- func (p *SqlDBHandle) GetSqlDbType() string
- func (p *SqlDBHandle) GetWriteBatchSize() uint64
- func (p *SqlDBHandle) Has(key []byte) (bool, error)
- func (p *SqlDBHandle) HasTable(tableName string) (bool, error)
- func (p *SqlDBHandle) NewIteratorWithPrefix(prefix []byte) (protocol.Iterator, error)
- func (p *SqlDBHandle) NewIteratorWithRange(start []byte, limit []byte) (protocol.Iterator, error)
- func (p *SqlDBHandle) Put(key []byte, value []byte) error
- func (p *SqlDBHandle) QueryMulti(sql string, values ...interface{}) (protocol.SqlRows, error)
- func (p *SqlDBHandle) QuerySingle(sql string, values ...interface{}) (protocol.SqlRow, error)
- func (p *SqlDBHandle) RollbackDbTransaction(txName string) error
- func (p *SqlDBHandle) Save(val interface{}) (int64, error)
- func (p *SqlDBHandle) WriteBatch(batch protocol.StoreBatcher, sync bool) error
- type SqlDBRow
- type SqlDBRows
- type SqlDBTx
- func (p *SqlDBTx) BeginDbSavePoint(spName string) error
- func (p *SqlDBTx) ChangeContextDb(dbName string) error
- func (p *SqlDBTx) Commit() error
- func (p *SqlDBTx) ExecSql(sql string, values ...interface{}) (int64, error)
- func (p *SqlDBTx) QueryMulti(sql string, values ...interface{}) (protocol.SqlRows, error)
- func (p *SqlDBTx) QuerySingle(sql string, values ...interface{}) (protocol.SqlRow, error)
- func (p *SqlDBTx) Rollback() error
- func (p *SqlDBTx) RollbackDbSavePoint(spName string) error
- func (p *SqlDBTx) Save(val interface{}) (int64, error)
- func (p *SqlDBTx) SaveBatch(vals []interface{}) (int64, error)
- type SqlDbConfig
- type TableDDLGenerator
- type TableDMLGenerator
- type UserDefineSave
Constants ¶
const DbType_MySQL = "mysql"
DbType_MySQL mysql
const DbType_Sqlite = "sqlite"
DbType_Sqlite sqlite
const DbType_TDSQL = "tdsql"
DbType_TDSQL tdsql
const DsnParsetime = "parseTime=True"
DsnParsetime parseTime=True
const Utf8Char = "charset=utf8mb4"
Utf8Char UTF8
Variables ¶
var ( //ErrSql sql error ErrSql = errors.New("sql error") //ErrSqlQuery sql query error ErrSqlQuery = errors.New("sql query error") //ErrTransaction database transaction error ErrTransaction = errors.New("database transaction error") //ErrConnection database connect error ErrConnection = errors.New("database connect error") //ErrDatabase database operation error ErrDatabase = errors.New("database operation error") //ErrTable table operation error ErrTable = errors.New("table operation error") //ErrRow table row query error ErrRow = errors.New("table row query error") //ErrIO database I/O error ErrIO = errors.New("database I/O error") //ErrTxNotFound transaction not found ErrTxNotFound = errors.New("transaction not found") //ErrTypeConvert type convert error ErrTypeConvert = errors.New("type convert error") //ErrNoRowExist no row exist ErrNoRowExist = errors.New("no row exist") )
Functions ¶
Types ¶
type KeyValue ¶
type KeyValue struct { ObjectKey []byte `gorm:"size:128;primaryKey;default:''"` ObjectValue []byte `gorm:"type:longblob"` }
KeyValue KV object
func (*KeyValue) GetCountSql ¶
GetCountSql get a count filter by key sql @return string sql @return []interface{} key
func (*KeyValue) GetCreateTableSql ¶
GetCreateTableSql get a create KV table SQL @param dbType mysql/sqlite/tdsql @return string sql string
func (*KeyValue) GetInsertSql ¶
GetInsertSql get a insert kv sql @param dbType:mysql/sqlite/tdsql @return string sql @return []interface{} key,value
func (*KeyValue) GetSaveSql ¶ added in v2.2.0
GetSaveSql get a save kv sql @param dbType:mysql/sqlite/tdsql @return string sql @return []interface{} key,value
func (*KeyValue) GetTableName ¶
GetTableName key_values @return string
type NewSqlDBOptions ¶
type NewSqlDBOptions struct { Config *SqlDbConfig Logger protocol.Logger Encryptor crypto.SymmetricKey ChainId string DbName string }
NewSqlDBOptions Option对象
type SqlDBHandle ¶
SqlDBHandle 核心DBHandle实现对象
func NewKVDBHandle ¶
func NewKVDBHandle(input *NewSqlDBOptions) *SqlDBHandle
NewKVDBHandle construct a new SqlDBHandle
func NewMemSqlDBHandle ¶
func NewMemSqlDBHandle(log protocol.Logger) *SqlDBHandle
NewMemSqlDBHandle for unit test
func NewShareMemSqlDBHandle ¶ added in v2.2.0
func NewShareMemSqlDBHandle(log protocol.Logger) *SqlDBHandle
NewShareMemSqlDBHandle share cache
func NewSqlDBHandle ¶
func NewSqlDBHandle(input *NewSqlDBOptions) *SqlDBHandle
NewSqlDBHandle construct a new SqlDBHandle
func (*SqlDBHandle) BeginDbTransaction ¶
func (p *SqlDBHandle) BeginDbTransaction(txName string) (protocol.SqlDBTransaction, error)
BeginDbTransaction 开启一个事务并命名该事务对象 @param txName @return protocol.SqlDBTransaction @return error
func (*SqlDBHandle) CommitDbTransaction ¶
func (p *SqlDBHandle) CommitDbTransaction(txName string) error
CommitDbTransaction 提交指定名字的事务 @param txName @return error
func (*SqlDBHandle) CompactRange ¶
func (p *SqlDBHandle) CompactRange(start, limit []byte) error
CompactRange 压缩,无实现 @param start @param limit @return error
func (*SqlDBHandle) CreateDatabaseIfNotExist ¶
func (p *SqlDBHandle) CreateDatabaseIfNotExist(dbName string) (bool, error)
CreateDatabaseIfNotExist 如果数据库不存在则创建对应的数据库,创建后将当前数据库设置为新数据库,返回是否已存在
func (*SqlDBHandle) CreateTable ¶
func (p *SqlDBHandle) CreateTable(obj TableDDLGenerator) error
CreateTable 创建一个对象对应的表 @param obj @return error
func (*SqlDBHandle) CreateTableIfNotExist ¶
func (p *SqlDBHandle) CreateTableIfNotExist(objI interface{}) error
CreateTableIfNotExist 根据一个对象struct,自动构建对应的sql数据库表
func (*SqlDBHandle) Delete ¶
func (p *SqlDBHandle) Delete(key []byte) error
Delete deletes the given key
func (*SqlDBHandle) ExecSql ¶
func (p *SqlDBHandle) ExecSql(sql string, values ...interface{}) (int64, error)
ExecSql 执行SQL语句
func (*SqlDBHandle) Get ¶
func (p *SqlDBHandle) Get(key []byte) ([]byte, error)
Get returns the value for the given key, or returns nil if none exists
func (*SqlDBHandle) GetDbTransaction ¶
func (p *SqlDBHandle) GetDbTransaction(txName string) (protocol.SqlDBTransaction, error)
GetDbTransaction 根据名字获得之前创建的事务对象 @param txName @return protocol.SqlDBTransaction @return error
func (*SqlDBHandle) GetDbType ¶ added in v2.2.0
func (p *SqlDBHandle) GetDbType() string
GetDbType returns db type
func (*SqlDBHandle) GetKeys ¶ added in v2.3.0
func (p *SqlDBHandle) GetKeys(keys [][]byte) ([][]byte, error)
GetKeys returns the value for the given key
func (*SqlDBHandle) GetSqlDbType ¶ added in v2.2.0
func (p *SqlDBHandle) GetSqlDbType() string
GetSqlDbType 获得对象的数据库类型 @return string
func (*SqlDBHandle) GetWriteBatchSize ¶
func (p *SqlDBHandle) GetWriteBatchSize() uint64
GetWriteBatchSize return write batch size
func (*SqlDBHandle) Has ¶
func (p *SqlDBHandle) Has(key []byte) (bool, error)
Has return true if the given key exist, or return false if none exists
func (*SqlDBHandle) HasTable ¶
func (p *SqlDBHandle) HasTable(tableName string) (bool, error)
HasTable 判断某表是否已经存在 @param tableName @return bool @return error
func (*SqlDBHandle) NewIteratorWithPrefix ¶
func (p *SqlDBHandle) NewIteratorWithPrefix(prefix []byte) (protocol.Iterator, error)
NewIteratorWithPrefix returns an iterator that contains all the key-values with given prefix
func (*SqlDBHandle) NewIteratorWithRange ¶
func (p *SqlDBHandle) NewIteratorWithRange(start []byte, limit []byte) (protocol.Iterator, error)
NewIteratorWithRange returns an iterator that contains all the key-values between given key ranges start is included in the results and limit is excluded.
func (*SqlDBHandle) Put ¶
func (p *SqlDBHandle) Put(key []byte, value []byte) error
Put saves the key-values
func (*SqlDBHandle) QueryMulti ¶
func (p *SqlDBHandle) QueryMulti(sql string, values ...interface{}) (protocol.SqlRows, error)
QueryMulti 查询并返回多行记录 @param sql @param values @return protocol.SqlRows @return error
func (*SqlDBHandle) QuerySingle ¶
func (p *SqlDBHandle) QuerySingle(sql string, values ...interface{}) (protocol.SqlRow, error)
QuerySingle 查询并返回单行记录 @param sql @param values @return protocol.SqlRow @return error
func (*SqlDBHandle) RollbackDbTransaction ¶
func (p *SqlDBHandle) RollbackDbTransaction(txName string) error
RollbackDbTransaction 回滚指定名字的事务 @param txName @return error
func (*SqlDBHandle) Save ¶
func (p *SqlDBHandle) Save(val interface{}) (int64, error)
Save 保存一个对象到数据库 @param val @return int64 @return error
func (*SqlDBHandle) WriteBatch ¶
func (p *SqlDBHandle) WriteBatch(batch protocol.StoreBatcher, sync bool) error
WriteBatch writes a batch in an atomic operation
type SqlDBRow ¶
type SqlDBRow struct {
// contains filtered or unexported fields
}
SqlDBRow 封装sql.Rows
func NewSqlDBRow ¶
NewSqlDBRow 根据sql.Rows创建SqlDBRow @param row @return *SqlDBRow
func (*SqlDBRow) Data ¶
Data 读取数据为map对象 @return map[string][]byte key为列名,value为字段值 @return error
type SqlDBRows ¶
type SqlDBRows struct {
// contains filtered or unexported fields
}
SqlDBRows 封装多行的sql.Rows
func NewSqlDBRows ¶
NewSqlDBRows 新建SqlDBRows对象 @param rows @param close @return *SqlDBRows
func (*SqlDBRows) Data ¶
Data 返回的数据转换为map @return map[string][]byte @return error
type SqlDBTx ¶
SqlDBTx 数据库事务的封装
func NewSqlDBTx ¶
NewSqlDBTx 创建一个SqlDBTx事务 @param name @param dbType @param db @param logger @return *SqlDBTx
func (*SqlDBTx) BeginDbSavePoint ¶
BeginDbSavePoint 开启保存点 @param spName @return error
func (*SqlDBTx) ChangeContextDb ¶
ChangeContextDb 修改当前上下文的数据库 @param dbName @return error
func (*SqlDBTx) ExecSql ¶
ExecSql 执行SQL语句 @param sql @param values @return int64 @return error
func (*SqlDBTx) QueryMulti ¶
QueryMulti 多行查询 @param sql @param values @return protocol.SqlRows @return error
func (*SqlDBTx) QuerySingle ¶
QuerySingle 单行查询 @param sql @param values @return protocol.SqlRow @return error
func (*SqlDBTx) RollbackDbSavePoint ¶
RollbackDbSavePoint 回滚保存点 @param spName @return error
func (*SqlDBTx) Save ¶
Save 保存一个对象到数据库 @param val @return int64 @return error
type SqlDbConfig ¶
type SqlDbConfig struct { //mysql, sqlite, postgres, sqlserver SqlDbType string `mapstructure:"sqldb_type"` Dsn string `mapstructure:"dsn"` MaxIdleConns int `mapstructure:"max_idle_conns"` MaxOpenConns int `mapstructure:"max_open_conns"` ConnMaxLifeTime int `mapstructure:"conn_max_lifetime"` //second SqlLogMode string `mapstructure:"sqllog_mode"` //Silent,Error,Warn,Info SqlVerifier string `mapstructure:"sql_verifier"` //simple,safe DbPrefix string `mapstructure:"db_prefix"` }
SqlDbConfig SqlDB的配置
type TableDDLGenerator ¶
type TableDDLGenerator interface { // GetCreateTableSql 获得创建表的SQL语句 // @param dbType // @return string GetCreateTableSql(dbType string) string // GetTableName 获得表的名字 // @return string GetTableName() string }
TableDDLGenerator 创建数据库表的接口
type TableDMLGenerator ¶
type TableDMLGenerator interface { // GetInsertSql 获得插入一行数据到表的SQL语句和参数 // @param dbType // @return string // @return []interface{} GetInsertSql(dbType string) (string, []interface{}) // GetUpdateSql 获得更新一条数据行对应的SQL语句与参数 // @return string // @return []interface{} GetUpdateSql() (string, []interface{}) // GetCountSql 获得该对象在数据库中存在的行数的SQL和参数 // @return string // @return []interface{} GetCountSql() (string, []interface{}) }
TableDMLGenerator 对象数据在表中增改的接口