Documentation ¶
Index ¶
- Constants
- Variables
- func QueryResultFieldToBaseTypeArray[T any](res *QueryResult, fieldName string) ([]T, error)
- func QueryResultRowFieldToBaseType[T any](res *QueryResult, fieldName string, rowIndex ...int) (T, error)
- func QueryResultRowToStruct[T any](res *QueryResult, rowIndex ...int) (T, error)
- func QueryResultRowToStructPtr[T any](res *QueryResult, rowIndex ...int) (*T, error)
- func QueryResultToStructArray[T any](res *QueryResult) ([]T, error)
- func QueryResultToStructPtrArray[T any](res *QueryResult) ([]*T, error)
- func SetLog(lg *logrus.Entry)
- func SetLogger(lg *logrus.Logger)
- func SetValueToStruct(obj interface{}, val map[string]interface{}) error
- func UniqueidentifierValue(val string) sql.NullString
- type DriverName
- type ExecResult
- type IDGenerator
- type MSSQL
- func (m *MSSQL) Close()
- func (m *MSSQL) Exec(sql string, args ...interface{}) *ExecResult
- func (c *MSSQL) GetDb() (*sql.DB, error)
- func (m *MSSQL) Prepare(query string) (*sql.Stmt, error)
- func (m *MSSQL) QueryRow(sql string, args ...interface{}) *QueryResult
- func (m *MSSQL) QueryRows(sql string, args ...interface{}) *QueryResult
- func (m *MSSQL) QueryWithPage(sql string, page *PageObj, args ...interface{}) *QueryResult
- func (c *MSSQL) SetConnMaxIdleTime(n time.Duration)
- func (c *MSSQL) SetMaxIdleConns(n int)
- func (c *MSSQL) SetMaxOpenConns(n int)
- func (c *MSSQL) SetSQLDB(dbSQL *sql.DB)
- func (m *MSSQL) Transaction(t TransactionFunc, option ...*sql.TxOptions) error
- type MYSQL
- func (m *MYSQL) Close()
- func (c *MYSQL) GetDb() (*sql.DB, error)
- func (m *MYSQL) QueryRow(sql string, args ...interface{}) *QueryResult
- func (m *MYSQL) QueryWithPage(sql string, page *PageObj, args ...interface{}) *QueryResult
- func (c *MYSQL) SetConnMaxIdleTime(n time.Duration)
- func (c *MYSQL) SetMaxIdleConns(n int)
- func (c *MYSQL) SetMaxOpenConns(n int)
- func (c *MYSQL) SetSQLDB(dbSQL *sql.DB)
- func (m *MYSQL) Transaction(t TransactionFunc, option ...*sql.TxOptions) error
- type PageObj
- type Query
- type QueryResult
- func (r *QueryResult) ColumnTypes() []string
- func (r *QueryResult) Columns() []string
- func (r *QueryResult) Error() error
- func (r *QueryResult) ErrorToLog(log *logrus.Entry, msg ...string) *QueryResult
- func (r *QueryResult) ForEach(f func(map[string]interface{}) bool) *QueryResult
- func (r *QueryResult) Get(columnName string, index ...int) interface{}
- func (r *QueryResult) GetCsvData() [][]string
- func (r *QueryResult) GetMap(index ...int) map[string]interface{}
- func (r *QueryResult) GetStringMap(index ...int) map[string]string
- func (r *QueryResult) IsEmpty() bool
- func (r *QueryResult) Iterator() RowIterator
- func (r *QueryResult) Length() int
- func (r *QueryResult) Rows() [][]interface{}
- func (r *QueryResult) SQLParams() (string, []interface{})
- type RowIterator
- type SQL
- type SQLPrinter
- type TransactionFunc
- type TxSQL
- type TxSQLErr
Constants ¶
const EmptyUniqueidentifier = "00000000-0000-0000-0000-000000000000"
EmptyUniqueidentifier 空的uniqueidentifier值
Variables ¶
Functions ¶
func QueryResultFieldToBaseTypeArray ¶
func QueryResultFieldToBaseTypeArray[T any](res *QueryResult, fieldName string) ([]T, error)
QueryResultFieldToBaseTypeArray 结果某个字段转换成数组
func QueryResultRowFieldToBaseType ¶
func QueryResultRowFieldToBaseType[T any](res *QueryResult, fieldName string, rowIndex ...int) (T, error)
QueryResultRowFieldToBaseType 结果某个字段转换成
func QueryResultRowToStruct ¶
func QueryResultRowToStruct[T any](res *QueryResult, rowIndex ...int) (T, error)
QueryResultRowToStruct 结果转换成结构体
func QueryResultRowToStructPtr ¶
func QueryResultRowToStructPtr[T any](res *QueryResult, rowIndex ...int) (*T, error)
QueryResultRowToStructPtr 结果转换成结构体指针
func QueryResultToStructArray ¶
func QueryResultToStructArray[T any](res *QueryResult) ([]T, error)
QueryResultToStructArray 结果转换成结构体数组
func QueryResultToStructPtrArray ¶
func QueryResultToStructPtrArray[T any](res *QueryResult) ([]*T, error)
QueryResultToStructPtrArray 结果转换成结构体指针数组
func SetValueToStruct ¶
SetValueToStruct 设置值到Struct
func UniqueidentifierValue ¶
func UniqueidentifierValue(val string) sql.NullString
UniqueidentifierValue 创建一个uniqueidentifier值,当可能是null的时候用
Types ¶
type DriverName ¶
type DriverName string
const ( MSSQLDriver DriverName = "mssql" MYSQLDriver DriverName = "mysql" )
type ExecResult ¶
func ErrExecResult ¶
func ErrExecResult(err error, db string, sql string, args []interface{}) *ExecResult
ErrExecResult 查询错误结果
func NewExecResult ¶
func NewExecResult(rs sql.Result, sql string, args []interface{}) *ExecResult
NewExecResult 获取一个操作结果对象
func (*ExecResult) Error ¶
func (r *ExecResult) Error(reportZeroChange ...bool) error
func (*ExecResult) ErrorToLog ¶
func (r *ExecResult) ErrorToLog(log *logrus.Entry, msg ...string) *ExecResult
func (*ExecResult) LastInsertId ¶
func (r *ExecResult) LastInsertId() (int64, error)
LastInsertId returns the integer generated by the database in response to a command. Typically this will be from an "auto increment" column when inserting a new row. Not all databases support this feature, and the syntax of such statements varies.
func (*ExecResult) RowsAffected ¶
func (r *ExecResult) RowsAffected() (int64, error)
RowsAffected returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.
func (*ExecResult) SQLParams ¶
func (r *ExecResult) SQLParams() (string, []interface{})
SQLParams 获取sql及其参数
type IDGenerator ¶
type IDGenerator struct {
// contains filtered or unexported fields
}
IDGenerator SnowFlakeID算法ID生成器
func (*IDGenerator) NextString ¶
func (sfg *IDGenerator) NextString() string
func (*IDGenerator) SetStartTimestamp ¶
func (sfg *IDGenerator) SetStartTimestamp(startTimestamp int64)
type MSSQL ¶
type MSSQL struct {
// contains filtered or unexported fields
}
MSSQL 操作对象
func MSSQLConnect ¶
MSSQLConnect 链接mssql数据库
func (*MSSQL) Exec ¶
func (m *MSSQL) Exec(sql string, args ...interface{}) *ExecResult
func (*MSSQL) QueryRow ¶
func (m *MSSQL) QueryRow(sql string, args ...interface{}) *QueryResult
func (*MSSQL) QueryRows ¶
func (m *MSSQL) QueryRows(sql string, args ...interface{}) *QueryResult
func (*MSSQL) QueryWithPage ¶
func (m *MSSQL) QueryWithPage(sql string, page *PageObj, args ...interface{}) *QueryResult
func (*MSSQL) SetConnMaxIdleTime ¶
SetConnMaxIdleTime 设置最大空闲连接时间,超时后将关闭连接
func (*MSSQL) Transaction ¶
func (m *MSSQL) Transaction(t TransactionFunc, option ...*sql.TxOptions) error
type MYSQL ¶
type MYSQL struct {
// contains filtered or unexported fields
}
MYSQL mysql操作对象
func MYSQLConnect ¶
MYSQLConnect 链接mysql数据库,其中other参数代表链接字符串附加的配置信息 其中other="loc=Local&multiStatements=true"
func (*MYSQL) QueryRow ¶
func (m *MYSQL) QueryRow(sql string, args ...interface{}) *QueryResult
func (*MYSQL) QueryWithPage ¶
func (m *MYSQL) QueryWithPage(sql string, page *PageObj, args ...interface{}) *QueryResult
func (*MYSQL) SetConnMaxIdleTime ¶
SetConnMaxIdleTime 设置最大空闲连接时间,超时后将关闭连接
func (*MYSQL) Transaction ¶
func (m *MYSQL) Transaction(t TransactionFunc, option ...*sql.TxOptions) error
type PageObj ¶
type PageObj struct { Page int `json:"page"` Rows int `json:"rows"` Total int64 `json:"total"` PageSize int `json:"allpage"` }
PageObj 分页数据
type Query ¶
type Query interface { // QueryRows 查询多条数据,结果以[]map[string]interface{}方式返回 // 返回结果,使用本package中的类型函数进行数据解析 // // param sql string SQL // // param args... interface{} SQL参数 QueryRows(sql string, args ...interface{}) *QueryResult // QueryRow 查询多条数据,结果以[]map[string]interface{}方式返回 // 返回结果,使用本package中的类型函数进行数据解析 // // param sql string SQL // // param args... interface{} SQL参数 QueryRow(sql string, args ...interface{}) *QueryResult // Exec 执行一条SQL // // param sql string SQL // // param args... interface{} SQL参数 Exec(sql string, args ...interface{}) *ExecResult // QueryWithPage 分页查询SQL // // 返回结果,使用本package中的类型函数进行数据解析 // // param sql string SQL // // param page *PageObj 分页数据 // // param args... interface{} SQL参数 QueryWithPage(sql string, page *PageObj, args ...interface{}) *QueryResult // Prepare 预处理 Prepare(query string) (*sql.Stmt, error) }
Query 查询操作集合
type QueryResult ¶
type QueryResult struct {
// contains filtered or unexported fields
}
func ErrQueryResult ¶
func ErrQueryResult(err error, db string, sql string, args []interface{}) *QueryResult
ErrQueryResult 返回一个查询错误
func NewRowsResult ¶
func NewRowsResult(rows *sql.Rows, sql string, args []interface{}) *QueryResult
NewRowsResult 读取查询结果
func (*QueryResult) Error ¶
func (r *QueryResult) Error() error
func (*QueryResult) ErrorToLog ¶
func (r *QueryResult) ErrorToLog(log *logrus.Entry, msg ...string) *QueryResult
ErrorToLog 错误保存到日志
func (*QueryResult) ForEach ¶
func (r *QueryResult) ForEach(f func(map[string]interface{}) bool) *QueryResult
ForEach 循环读取所有数据 返回的map中key是数据字段名称,value是值,回调函数中如果返回false则停止循环后续数据
func (*QueryResult) Get ¶
func (r *QueryResult) Get(columnName string, index ...int) interface{}
Get 读取某行的指定字段值.columnName表示字段名称,index表示第几行默认第一行,如果结果不存在返回nil
func (*QueryResult) GetMap ¶
func (r *QueryResult) GetMap(index ...int) map[string]interface{}
GetMap 读取某行的所有数据. index代表第几行默认第一行,返回的map中key是数据字段名称,value是值
func (*QueryResult) GetStringMap ¶
func (r *QueryResult) GetStringMap(index ...int) map[string]string
GetStringMap 获取string类型map
func (*QueryResult) IsEmpty ¶
func (r *QueryResult) IsEmpty() bool
func (*QueryResult) SQLParams ¶
func (r *QueryResult) SQLParams() (string, []interface{})
SQLParams 获取sql及其参数
type RowIterator ¶
type RowIterator interface { // HasNext 是否有下一条记录 HasNext() bool // Reset 重置游标到首位 Reset() // Next 获取下一条数据 Next() (map[string]interface{}, error) // Length 总数据数 Length() int }
RowIterator 行读取游标
type SQL ¶
type SQL interface { Query // DataBaseName 数据库名称 DataBaseName() string // SetMaxOpenConns 设置最大连接数 SetMaxOpenConns(n int) // SetMaxIdleConns 设置最大空闲连接数 SetMaxIdleConns(n int) // SetConnMaxIdleTime 设置最大空闲连接时间,超时后将关闭连接 SetConnMaxIdleTime(n time.Duration) // GetDb 获取数据库对象 GetDb() (*sql.DB, error) // SetQueryTimeOut 设置查询超时时间 SetQueryTimeOut(outTime time.Duration) // Transaction 事务处理 // // param t TransactionFunc 事务处理函数 // // param new bool 是否创建新事物,默认false,如果设置true不管事务是否存在都会创建新事物 Transaction(t TransactionFunc, option ...*sql.TxOptions) error // Close 关闭数据库 Close() }
SQL 数据库操作接口
type SQLPrinter ¶
type SQLPrinter struct { Message []string `description:"输出的内容"` ErrMessage string `description:"输出的错误"` // contains filtered or unexported fields }
SQLPrinter sql服务输出的消息,MSSQL支持 使用方法:
printer := &SQLPrinter{} res := conn.QueryRows(sql,args1,arg2,printer.ReturnMessage()) printer.Parse() fmt.Printf("Print: %v\n",printer.Message) fmt.Printf("Error: %s\n",printer.ErrMessage) fmt.Printf("Rows: %d\n,res.Length())
func (*SQLPrinter) ReturnMessage ¶
func (s *SQLPrinter) ReturnMessage() *sqlexp.ReturnMessage