Documentation
¶
Index ¶
- Variables
- func BaseTypeValueGet[T baseType](val any) T
- func InsertStruct(obj any, db Query) error
- func RegisterSetValueToStruct(fun func(obj any, val Row) error)
- func RegisterStructFieldSetHandler(handler StructFieldSetHandler)
- func SetLogger(l *slog.Logger)
- func SetValueToStruct(obj any, val Row) error
- func UpdateStruct(obj any, db Query) error
- type Connect
- func (c *Connect) Close()
- func (c *Connect) GetDb() (*sql.DB, error)
- func (c *Connect) NewTransaction(t TransactionFunc, newTx func(tx *sql.Tx) TxSQL, option ...*sql.TxOptions) error
- func (c *Connect) SetConnMaxIdleTime(n time.Duration)
- func (c *Connect) SetMaxIdleConns(n int)
- func (c *Connect) SetMaxOpenConns(n int)
- func (c *Connect) SetSQLDB(dbSQL *sql.DB)
- type ConnectQuery
- func (q *ConnectQuery) Connect() error
- func (q *ConnectQuery) DataBaseName() string
- func (q *ConnectQuery) Exec(sql string, args ...any) *ExecResult
- func (q *ConnectQuery) ExecWithContext(ctx context.Context, sql string, args ...any) *ExecResult
- func (q *ConnectQuery) Prepare(query string) (*sql.Stmt, error)
- func (q *ConnectQuery) QueryRows(sql string, args ...any) *QueryResult
- func (q *ConnectQuery) QueryRowsWithContext(ctx context.Context, sql string, args ...any) *QueryResult
- func (q *ConnectQuery) SetDataBaseName(dbname string)
- func (q *ConnectQuery) SetQueryTimeOut(outTime time.Duration)
- func (q *ConnectQuery) Transaction(t TransactionFunc, option ...*sql.TxOptions) error
- type DBTag
- type DBVal
- type DriverName
- type ExecResult
- type FieldData
- type PageObj
- type Query
- type QueryResult
- func (r *QueryResult) ColumnTypes() []string
- func (r *QueryResult) ColumnValues[T baseType](fieldName string) ([]T, error)
- func (r *QueryResult) Columns() []string
- func (r *QueryResult) Error(lg ...*slog.Logger) error
- func (r *QueryResult) ForEach(f func(row Row) bool) *QueryResult
- func (r *QueryResult) Get(columnName string, index ...int) any
- func (r *QueryResult) GetCsvData() [][]string
- func (r *QueryResult) GetRow(index ...int) Row
- func (r *QueryResult) GetStringMap(index ...int) map[string]string
- func (r *QueryResult) IsEmpty() bool
- func (r *QueryResult) Iterator() iter.Seq[Row]
- func (r *QueryResult) IteratorWithIndex() iter.Seq2[int, Row]
- func (r *QueryResult) Length() int
- func (r *QueryResult) Rows() [][]any
- func (r *QueryResult) SQLParams() (string, []any)
- func (r *QueryResult) ToStruct[T any]() ([]T, error)
- type QueryWithContext
- type Row
- func (r Row) GetBool(column string) bool
- func (r Row) GetColumn[T baseType](column string) (T, error)
- func (r Row) GetFloat64(column string) float64
- func (r Row) GetInt64(column string) int64
- func (r Row) GetString(column string) string
- func (r Row) GetTime(column string) (time.Time, error)
- func (r Row) ToStringMap() map[string]string
- func (r Row) ToStruct[T any]() (T, error)
- type SQL
- type SelectSQL
- type StructFieldSetHandler
- type TransactionFunc
- type TxConnectQuery
- type TxSQL
- type TxSQLErr
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func RegisterSetValueToStruct ¶
RegisterSetValueToStruct 注册map转结构体处理函数
func RegisterStructFieldSetHandler ¶
func RegisterStructFieldSetHandler(handler StructFieldSetHandler)
RegisterStructFieldSetHandler 注册结构体字段设置处理函数
Types ¶
type Connect ¶
type Connect struct {
// contains filtered or unexported fields
}
func (*Connect) NewTransaction ¶
func (c *Connect) NewTransaction(t TransactionFunc, newTx func(tx *sql.Tx) TxSQL, option ...*sql.TxOptions) error
NewTransaction 事务处理 @param t TransactionFunc 事务处理函数
func (*Connect) SetConnMaxIdleTime ¶
SetConnMaxIdleTime 设置最大空闲连接时间,超时后将关闭连接
func (*Connect) SetMaxIdleConns ¶
SetMaxIdleConns 设置最大空闲连接数
type ConnectQuery ¶
type ConnectQuery struct {
Conn QueryWithContext //这里提取了sql.DB以及sql.Tx的公共方法使得该结构体可以不区分是DB还是Tx
CheckConn bool
Dbname string
DriverName DriverName
QueryTimeOut time.Duration
}
func (*ConnectQuery) Connect ¶
func (q *ConnectQuery) Connect() error
func (*ConnectQuery) Exec ¶
func (q *ConnectQuery) Exec(sql string, args ...any) *ExecResult
func (*ConnectQuery) ExecWithContext ¶
func (q *ConnectQuery) ExecWithContext(ctx context.Context, sql string, args ...any) *ExecResult
func (*ConnectQuery) QueryRows ¶
func (q *ConnectQuery) QueryRows(sql string, args ...any) *QueryResult
func (*ConnectQuery) QueryRowsWithContext ¶
func (q *ConnectQuery) QueryRowsWithContext(ctx context.Context, sql string, args ...any) *QueryResult
func (*ConnectQuery) SetDataBaseName ¶
func (q *ConnectQuery) SetDataBaseName(dbname string)
SetDataBaseName 设置数据库名称
func (*ConnectQuery) SetQueryTimeOut ¶
func (q *ConnectQuery) SetQueryTimeOut(outTime time.Duration)
SetQueryTimeOut 设置查询超时时间
func (*ConnectQuery) Transaction ¶
func (q *ConnectQuery) Transaction(t TransactionFunc, option ...*sql.TxOptions) error
type DBTag ¶
type DBTag struct {
PKName string `description:"主键字段名称"`
TableName string `description:"数据表名称"`
FieldNames []string `description:"数据字段"`
FieldsDesc map[string]string `description:"数据字段描述"`
FieldsData map[string]FieldData `description:"字段要求"`
FieldsDBName map[string]string `description:"结构体字段对应数据库字段"`
}
DBTag 数据库tag解析
type DriverName ¶
type DriverName string
type ExecResult ¶
func ErrExecResult ¶
func ErrExecResult(err error, db string, sql string, args []any) *ExecResult
ErrExecResult 查询错误结果
func NewExecResult ¶
func NewExecResult(rs sql.Result, sql string, args []any) *ExecResult
NewExecResult 获取一个操作结果对象
func (*ExecResult) Error ¶
func (r *ExecResult) Error(reportZeroChange ...bool) error
func (*ExecResult) ErrorToLog ¶
func (r *ExecResult) ErrorToLog(log *slog.Logger, 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.
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 {
// DataBaseName 数据库名称
DataBaseName() string
// QueryRow 查询一条数据
QueryRow(sql string, args ...any) *QueryResult
// QueryRows 查询多条数据
QueryRows(sql string, args ...any) *QueryResult
// QueryWithPage 分页查询SQL
QueryWithPage(sql string, page *PageObj, args ...any) *QueryResult
// Exec 执行一条SQL
Exec(sql string, args ...any) *ExecResult
// Prepare 预处理
Prepare(query string) (*sql.Stmt, error)
// Transaction 事务处理
// t TransactionFunc 事务处理函数
Transaction(t TransactionFunc, option ...*sql.TxOptions) error
}
Query 查询操作集合
type QueryResult ¶
type QueryResult struct {
// contains filtered or unexported fields
}
func ErrQueryResult ¶
func ErrQueryResult(err error, db string, sql string, args []any) *QueryResult
ErrQueryResult 返回一个查询错误
func NewRowsResult ¶
func NewRowsResult(rows *sql.Rows, sql string, args []any) *QueryResult
NewRowsResult 读取查询结果
func (*QueryResult) ColumnValues ¶
func (r *QueryResult) ColumnValues[T baseType](fieldName string) ([]T, error)
ColumnValues 结果某个字段转换成数组
func (*QueryResult) ForEach ¶
func (r *QueryResult) ForEach(f func(row Row) bool) *QueryResult
ForEach 循环读取所有数据 返回的map中key是数据字段名称,value是值,回调函数中如果返回false则停止循环后续数据
func (*QueryResult) Get ¶
func (r *QueryResult) Get(columnName string, index ...int) any
Get 读取某行的指定字段值.columnName表示字段名称,index表示第几行默认第一行,如果结果不存在返回nil
func (*QueryResult) GetRow ¶
func (r *QueryResult) GetRow(index ...int) Row
GetRow 读取某行的所有数据. 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) IteratorWithIndex ¶
func (r *QueryResult) IteratorWithIndex() iter.Seq2[int, Row]
IteratorWithIndex 返回游标及序号
func (*QueryResult) SQLParams ¶
func (r *QueryResult) SQLParams() (string, []any)
SQLParams 获取sql及其参数
func (*QueryResult) ToStruct ¶
func (r *QueryResult) ToStruct[T any]() ([]T, error)
ToStruct 将结果写入结构体数组
type QueryWithContext ¶
type SQL ¶
type SQL interface {
Query
// SetMaxOpenConns 设置最大连接数
SetMaxOpenConns(n int)
// SetMaxIdleConns 设置最大空闲连接数
SetMaxIdleConns(n int)
// SetConnMaxIdleTime 设置最大空闲连接时间,超时后将关闭连接
SetConnMaxIdleTime(n time.Duration)
// GetDb 获取数据库对象
GetDb() (*sql.DB, error)
// SetQueryTimeOut 设置查询超时时间
SetQueryTimeOut(outTime time.Duration)
// Close 关闭数据库
Close()
}
SQL 数据库操作接口
type SelectSQL ¶
type SelectSQL struct {
Columns string
Table string
Where string
GroupBy string
Having string
OrderBy string
LimitStart int64
LimitLen int64
}
func SelectSQLParser ¶
SelectSQLParser 解析select语句
type StructFieldSetHandler ¶
StructFieldSetHandler 结构题字段设置,field结构体字段,dbName数据库字段名称, val数据的值
type TxConnectQuery ¶
type TxConnectQuery struct {
// contains filtered or unexported fields
}
func NewTxConnectQuery ¶
func NewTxConnectQuery(conn txQuery) *TxConnectQuery
func (*TxConnectQuery) Exec ¶
func (m *TxConnectQuery) Exec(sql string, args ...any) *ExecResult
func (*TxConnectQuery) GetTx ¶
func (m *TxConnectQuery) GetTx() *sql.Tx
func (*TxConnectQuery) Transaction ¶
func (m *TxConnectQuery) Transaction(t TransactionFunc, option ...*sql.TxOptions) error
Transaction 事务中再调事务不再开启新事务,而是沿用原事务