Documentation
¶
Index ¶
- Constants
- Variables
- func Delete[T any](hook func(sq.DeleteBuilder) sq.DeleteBuilder) bool
- func Get[T any](db pgxscan.Querier, sb sq.SelectBuilder) (*T, error)
- func GetAll[T any](table string, page, size uint64, hook func(sq.SelectBuilder) sq.SelectBuilder) ([]T, int)
- func GetAllByFields[T any](table string, fields []string, sort []string, page, size uint64, ...) ([]T, int)
- func GetAllByFieldsCte[T any](db pgxscan.Querier, fromTable, idField string, sort []string, ...) ([]T, int, bool)
- func GetCount(table string, hook func(sq.SelectBuilder) sq.SelectBuilder) int
- func GetFixedPanicTrace(kb int, hidePath bool) (string, int)
- func GetFormatTrace(r interface{}, kb int, hidePath bool, hideGor bool) string
- func GetOne[T any](tableName string, cols []string, hook func(sq.SelectBuilder) sq.SelectBuilder) (T, bool)
- func GetOne2[T any](tableName string, cols []string, hook func(sq.SelectBuilder) sq.SelectBuilder) (T, bool)
- func GetOneFromStructNameTable[T any](cols []string, hook func(sq.SelectBuilder) sq.SelectBuilder) (T, bool)
- func GetPanicTrace(kb int) (stackStr string, gorouteLen int)
- func GetRawPanicTrace(kb int) (stack []byte, length int)
- func Insert[T any](hook func(sq.InsertBuilder) sq.InsertBuilder) (T, bool)
- func IsZeroValue(value any) bool
- func RegisterModel[T any](tableName string) error
- func RowToStructByName[T any](row pgx.CollectableRow) (T, error)
- func Select[T any](db pgxscan.Querier, sb sq.SelectBuilder) ([]T, error)
- func ToCte[T sq.Sqlizer](alias string, expr T) T
- func Update[T any](hook func(sq.UpdateBuilder) sq.UpdateBuilder) (T, bool)
- func UpdateTx[T any](tx pgx.Tx, hook func(sq.UpdateBuilder) sq.UpdateBuilder) bool
- func WithCte[T sq.Sqlizer](first Cte[T], rest ...Cte[T]) T
- type Client
- func (c *Client) Delete(sb squirrel.DeleteBuilder) (int64, error)
- func (c *Client) Get(sb squirrel.SelectBuilder, result any) error
- func (c *Client) Insert(sb squirrel.InsertBuilder) (int64, error)
- func (c *Client) Select(sb squirrel.SelectBuilder, result any) error
- func (c *Client) ToStruct()
- func (c *Client) Types(types []*pgtype.Type) *Client
- func (c *Client) Update(sb squirrel.UpdateBuilder) (int64, error)
- type Cte
- type FieldSchema
- type TableSchema
Constants ¶
const (
MinPageElements = 10
)
Variables ¶
var ( StatementBuilder = psql DefaultHook = func(b sq.SelectBuilder) sq.SelectBuilder { return b } )
var CustomFieldTypes = map[string]map[string]string{}
Functions ¶
func Delete ¶
func Delete[T any](hook func(sq.DeleteBuilder) sq.DeleteBuilder) bool
Delete 为某表删除记录 返回是否成功
func GetAll ¶
func GetAll[T any](table string, page, size uint64, hook func(sq.SelectBuilder) sq.SelectBuilder) ([]T, int)
GetAll 获取某表全部数据 通过 hook 钩子函数进行拓展
func GetAllByFields ¶
func GetAllByFields[T any](table string, fields []string, sort []string, page, size uint64, hook func(sq.SelectBuilder) sq.SelectBuilder) ([]T, int)
func GetAllByFieldsCte ¶
func GetAllByFieldsCte[T any]( db pgxscan.Querier, fromTable, idField string, sort []string, page, size uint64, buildPreselect func(selectIdFrom sq.SelectBuilder) sq.SelectBuilder, buildPrimary func(selectPrimaryFrom sq.SelectBuilder) sq.SelectBuilder, ) ([]T, int, bool)
GetAllByFieldsCte 使用CTE实现将三个查询合并到同一语句来执行需要对主表JOIN和LIMIT的分页查询, 同时返回主表查询结果和主记录总数(用于分页)。 注意:当主表需要在JOIN的同时通过COUNT(*)来计算主记录的行数或LIMIT来限制主记录的行数(比如分页), 请使用此方法避免COUNT(*)结果偏大(主记录因为JOIN重复),或者JOIN得到的从表记录返回不完整(被LIMIT截断)。
-- 第一个CTE 预选全部ID(GROUP BY用于在预选条件中存在JOIN的情况下去重):
WITH __cte_all_ids AS (SELECT <id> FROM <主表> <预选条件>... GROUP BY <id> <ORDER>...),
-- 第二个CTE 计算总ID数:
__cte_count AS (SELECT COUNT(*) as pagination_total FROM cte_all_ids)
-- 实际查询: SELECT <主表字段>..., __cte_count.pagination_total FROM <主表> JOIN (SELECT <id> FROM __cte_all_ids <LIMIT> <OFFSET>) as __cts_ids ON __cte_ids.<id> = <主表>.<id> CROSS JOIN __cte_count
func GetCount ¶
func GetCount(table string, hook func(sq.SelectBuilder) sq.SelectBuilder) int
GetCount 获取某表数据条数
func GetFixedPanicTrace ¶
GetFixedPanicTrace 按照易读取的方式返回panic的堆栈信息和goroutine数量
func GetFormatTrace ¶
GetFormatTrace 对panic堆栈信息进行格式化返回可进行Log的panic信息,r为recovery()返回的信息,kb为获取的堆栈信息的长度,hidePath为 是否隐藏路径信息,hideGor为是否隐藏goroutine信息
func GetOne ¶
func GetOne[T any](tableName string, cols []string, hook func(sq.SelectBuilder) sq.SelectBuilder) (T, bool)
GetOne 获取某表一条数据 通过 hook 钩子函数进行拓展
使用本机 RowToStructByName,不支持联立 select * row2struct
func GetOne2 ¶
func GetOne2[T any](tableName string, cols []string, hook func(sq.SelectBuilder) sq.SelectBuilder) (T, bool)
GetOne2 获取某表一条数据 通过 hook 钩子函数进行拓展
使用 pgx.RowToStructByName,支持联立 select * row2struct
func GetOneFromStructNameTable ¶
func GetOneFromStructNameTable[T any](cols []string, hook func(sq.SelectBuilder) sq.SelectBuilder) (T, bool)
GetOneFromStructNameTable 获取某表一条数据 通过 hook 钩子函数进行拓展
func GetPanicTrace ¶
GetPanicTrace 获取最大长度为kb的panic堆栈信息,解析为string并搜索堆栈内goroutine数量
func GetRawPanicTrace ¶
GetRawPanicTrace 返回指定KB长度的panic堆栈信息和堆栈信息长度
func Insert ¶
func Insert[T any](hook func(sq.InsertBuilder) sq.InsertBuilder) (T, bool)
Insert 为某表添加记录 返回是否成功
func IsZeroValue ¶
func RowToStructByName ¶
func Update ¶
func Update[T any](hook func(sq.UpdateBuilder) sq.UpdateBuilder) (T, bool)
Update 为某表更新记录 返回是否成功
func UpdateTx ¶
func UpdateTx[T any](tx pgx.Tx, hook func(sq.UpdateBuilder) sq.UpdateBuilder) bool
UpdateTx 为某表更新记录 返回是否成功
Types ¶
type Client ¶
func (*Client) Delete ¶
func (c *Client) Delete(sb squirrel.DeleteBuilder) (int64, error)
Delete 删除数据
func (*Client) Insert ¶
func (c *Client) Insert(sb squirrel.InsertBuilder) (int64, error)
Insert 插入数据
type FieldSchema ¶
type FieldSchema struct {
GoName string // Go字段名
ColumnName string // 数据库列名
Offset uintptr // 字段偏移量
GoType reflect.Type // Go类型
PrimaryKey bool // 是否主键
AutoIncr bool // 是否自增
}
FieldSchema 字段元数据
type TableSchema ¶
type TableSchema struct {
GoType reflect.Type
TableName string
Fields []*FieldSchema
PrimaryKey *FieldSchema
ColumnToField map[string]*FieldSchema
}
TableSchema 表元数据