rawsqlprovider

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

View Source
const DbType_MySQL = "mysql"

DbType_MySQL mysql

View Source
const DbType_Sqlite = "sqlite"

DbType_Sqlite sqlite

View Source
const DsnParsetime = "parseTime=True"

DsnParsetime parseTime=True

View Source
const Utf8Char = "charset=utf8mb4"

Utf8Char UTF8

Variables

View Source
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

func ParseSqlDbType

func ParseSqlDbType(str string) (string, error)

ParseSqlDbType 解析数据库类型 @param str @return string @return error

Types

type KeyValue

type KeyValue struct {
	ObjectKey   []byte `gorm:"size:128;primaryKey;default:''"`
	ObjectValue []byte `gorm:"type:longblob"`
}

KeyValue KV object

func (*KeyValue) GetCountSql

func (kv *KeyValue) GetCountSql() (string, []interface{})

GetCountSql get a count filter by key sql @return string sql @return []interface{} key

func (*KeyValue) GetCreateTableSql

func (kv *KeyValue) GetCreateTableSql(dbType string) string

GetCreateTableSql get a create KV table SQL @param dbType mysql/sqlite/tdsql @return string sql string

func (*KeyValue) GetInsertSql

func (kv *KeyValue) GetInsertSql(dbType string) (string, []interface{})

GetInsertSql get a insert kv sql @param dbType:mysql/sqlite/tdsql @return string sql @return []interface{} key,value

func (*KeyValue) GetSaveSql

func (kv *KeyValue) GetSaveSql(dbType string) (string, []interface{})

GetSaveSql get a save kv sql @param dbType:mysql/sqlite/tdsql @return string sql @return []interface{} key,value

func (*KeyValue) GetTableName

func (kv *KeyValue) GetTableName() string

GetTableName key_values @return string

func (*KeyValue) GetUpdateSql

func (kv *KeyValue) GetUpdateSql() (string, []interface{})

GetUpdateSql get an update sql @return string sql @return []interface{} value,key

type NewSqlDBOptions

type NewSqlDBOptions struct {
	Config    *SqlDbConfig
	Logger    protocol.Logger
	Encryptor crypto.SymmetricKey
	ChainId   string
	DbName    string
}

NewSqlDBOptions Option对象

type SqlDBHandle

type SqlDBHandle struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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

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) Close

func (p *SqlDBHandle) Close() error

Close 关闭数据库连接 @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

func (p *SqlDBHandle) GetDbType() string

GetDbType returns db type

func (*SqlDBHandle) GetKeys

func (p *SqlDBHandle) GetKeys(keys [][]byte) ([][]byte, error)

GetKeys returns the value for the given key

func (*SqlDBHandle) GetSqlDbType

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

func NewSqlDBRow(row *sql.Rows) *SqlDBRow

NewSqlDBRow 根据sql.Rows创建SqlDBRow @param row @return *SqlDBRow

func (*SqlDBRow) Data

func (row *SqlDBRow) Data() (map[string][]byte, error)

Data 读取数据为map对象 @return map[string][]byte key为列名,value为字段值 @return error

func (*SqlDBRow) IsEmpty

func (row *SqlDBRow) IsEmpty() bool

IsEmpty 是否空 @return bool

func (*SqlDBRow) ScanColumns

func (r *SqlDBRow) ScanColumns(dest ...interface{}) error

ScanColumns 读取列 @param dest @return error

type SqlDBRows

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

SqlDBRows 封装多行的sql.Rows

func NewSqlDBRows

func NewSqlDBRows(rows *sql.Rows, close func() error) *SqlDBRows

NewSqlDBRows 新建SqlDBRows对象 @param rows @param close @return *SqlDBRows

func (*SqlDBRows) Close

func (r *SqlDBRows) Close() error

Close 关闭 @return error

func (*SqlDBRows) Data

func (r *SqlDBRows) Data() (map[string][]byte, error)

Data 返回的数据转换为map @return map[string][]byte @return error

func (*SqlDBRows) Next

func (r *SqlDBRows) Next() bool

Next 是否有下一个值 @return bool

func (*SqlDBRows) ScanColumns

func (r *SqlDBRows) ScanColumns(dest ...interface{}) error

ScanColumns 扫描列 @param dest @return error

type SqlDBTx

type SqlDBTx struct {
	sync.Mutex
	// contains filtered or unexported fields
}

SqlDBTx 数据库事务的封装

func NewSqlDBTx

func NewSqlDBTx(name string, dbType string, db *sql.Tx, logger protocol.Logger) *SqlDBTx

NewSqlDBTx 创建一个SqlDBTx事务 @param name @param dbType @param db @param logger @return *SqlDBTx

func (*SqlDBTx) BeginDbSavePoint

func (p *SqlDBTx) BeginDbSavePoint(spName string) error

BeginDbSavePoint 开启保存点 @param spName @return error

func (*SqlDBTx) ChangeContextDb

func (p *SqlDBTx) ChangeContextDb(dbName string) error

ChangeContextDb 修改当前上下文的数据库 @param dbName @return error

func (*SqlDBTx) Commit

func (p *SqlDBTx) Commit() error

Commit 提交事务 @return error

func (*SqlDBTx) ExecSql

func (p *SqlDBTx) ExecSql(sql string, values ...interface{}) (int64, error)

ExecSql 执行SQL语句 @param sql @param values @return int64 @return error

func (*SqlDBTx) QueryMulti

func (p *SqlDBTx) QueryMulti(sql string, values ...interface{}) (protocol.SqlRows, error)

QueryMulti 多行查询 @param sql @param values @return protocol.SqlRows @return error

func (*SqlDBTx) QuerySingle

func (p *SqlDBTx) QuerySingle(sql string, values ...interface{}) (protocol.SqlRow, error)

QuerySingle 单行查询 @param sql @param values @return protocol.SqlRow @return error

func (*SqlDBTx) Rollback

func (p *SqlDBTx) Rollback() error

Rollback 回滚事务 @return error

func (*SqlDBTx) RollbackDbSavePoint

func (p *SqlDBTx) RollbackDbSavePoint(spName string) error

RollbackDbSavePoint 回滚保存点 @param spName @return error

func (*SqlDBTx) Save

func (p *SqlDBTx) Save(val interface{}) (int64, error)

Save 保存一个对象到数据库 @param val @return int64 @return error

func (*SqlDBTx) SaveBatch

func (p *SqlDBTx) SaveBatch(vals []interface{}) (int64, error)

SaveBatch 批量的保存同一种对象的多个实例

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 对象数据在表中增改的接口

type UserDefineSave

type UserDefineSave interface {
	// GetSaveSql 提供一个保存该对象的SQL语句和参数列表
	//  @param dbType
	//  @return string
	//  @return []interface{}
	GetSaveSql(dbType string) (string, []interface{})
}

UserDefineSave 用户自定义了保存一个对象的SQL语句的方法

Jump to

Keyboard shortcuts

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