sqlbuilder

package
v0.19.4 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0, Apache-2.0 Imports: 1 Imported by: 0

README

go-toolbox/pkg/sqlbuilder

GitHub go.mod Go version GoDoc License

一个功能丰富、高性能的 Go 语言 GORM 仓储层封装库,提供类型安全的 CRUD 操作、复杂查询构建和便利方法

📖 特性 & 文档导航

特性 说明 文档
🚀 仓储模式 泛型 BaseRepository 和 EnhancedRepository,类型安全的 CRUD 📘 快速开始
🔍 高级查询 FilterGroup 支持复杂 AND/OR 条件组合和无限嵌套 📙 条件组合
🎯 类型安全 完全的泛型支持,编译时类型检查 📗 CRUD 指南
⚡ 字段选择 Query 对象支持字段选择、排除敏感字段 ⚡ Query 对象
🔗 便捷查询 简化的查询 API,支持链式调用 🔥 便捷查询
📊 性能优化 批量操作、游标分页、原子字段更新 📓 增强版仓储
🔐 错误处理 集成 go-toolbox/errorx 的结构化错误管理 📒 错误处理
📝 审计追踪 内置审计字段(created_by, updated_by) 📔 模型定义
🧩 JSON 字段 JSON 辅助函数、types.Slice 数组字段和历史标量兼容读取 🧩 JSON 辅助
🗄️ 数据库迁移 自动迁移、索引创建、表注释 🗄️ 数据库迁移
🔄 上下文支持 超时控制、日志追踪、请求隔离 🔄 Context 使用
🎛️ 复杂条件 Filter 和 FilterGroup 构建复杂 WHERE 条件 📕 条件过滤
🚄 并发统计 多表并发查询、时间分组统计、条件聚合 🚄 并发查询

📖 完整文档:查看 文档中心 了解所有功能和学习路径

📦 安装

go get github.com/HitGameAI/apex-toolbox/pkg/sqlbuilder

🚀 快速开始

import (
    "github.com/HitGameAI/apex-toolbox/pkg/sqlbuilder/db"
    "github.com/HitGameAI/apex-toolbox/pkg/sqlbuilder/repository"
)

// 1. 定义模型
type User struct {
    repository.BaseModel
    Name  string `gorm:"type:varchar(100)"`
    Email string `gorm:"type:varchar(100);uniqueIndex"`
}

// 2. 创建 Repository
handler, _ := db.NewGormHandler(gormDB)
repo := repository.NewBaseRepository[User](handler, logger, "users")

// 3. CRUD 操作
user, err := repo.Create(ctx, &User{Name: "张三"})
user, err = repo.Get(ctx, 1)
user, err = repo.Update(ctx, user)
err = repo.Delete(ctx, 1)

💡 详细教程:查看 📘 快速入门文档 了解完整的安装和使用步骤

🏗️ 核心特性

// 内置模型:BaseModel、AuditModel
type User struct { repository.BaseModel; Name string }

// 配置选项:自动字段、批处理、超时、预加载等
repo := repository.NewBaseRepository[User](handler, logger, "users",
    repository.WithAutoFields[User](),
    repository.WithBatchSize[User](200),
)

📖 详细说明:查看 📔 模型定义 和 📘 快速入门 了解更多配置

🧪 测试

go test ./... -v                    # 运行所有测试
go test ./... -cover                # 测试覆盖率
go test -v -run TestBaseRepository  # 运行特定测试

📚 相关资源

📦 依赖

🤝 贡献

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/amazing-feature)
  3. 提交更改 (git commit -m '✨ feat: Add amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 开启 Pull Request

📋 Git Commit Emoji 规范

点击展开 Emoji 规范表
Emoji 类型 说明
✨ feat 新功能
🐛 fix 修复 bug
📝 docs 文档更新
♻️ refactor 代码重构
⚡ perf 性能优化
✅ test 测试相关
🔧 chore 配置/构建
🚀 deploy 部署发布
🔒 security 安全修复
🔥 remove 删除代码
🗃️ db 数据库相关

示例: git commit -m "✨ feat(db): 新增 Migrator 迁移器"

📄 许可证

MIT License - 详见 LICENSE

👨‍💻 作者

Kamal Yang (@kamalyes)

Documentation

Overview

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-08 15:11:22 * @FilePath: \go-toolbox\pkg\sqlbuilder\types.go * @Description: 类型重导出兼容层 - 保持 sqlbuilder.MapAny 等外部引用不变 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSON

type JSON[T any] = types.JSON[T]

type MapAny

type MapAny = types.MapAny

type Slice

type Slice[T any] = types.Slice[T]

func Map

func Map[T any, R any](s Slice[T], fn func(T) R) Slice[R]

type StringSlice

type StringSlice = types.StringSlice

func ParseStringSlice

func ParseStringSlice(input []string) StringSlice

Directories

Path Synopsis
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-30 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-30 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\constants\database.go * @Description: 数据库类型常量定义 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-30 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-30 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\constants\database.go * @Description: 数据库类型常量定义 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 13:05:48 * @FilePath: \go-toolbox\pkg\sqlbuilder\db\handler.go * @Description: 数据库处理器 - Handler 接口和 GORM 实现 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 13:05:48 * @FilePath: \go-toolbox\pkg\sqlbuilder\db\handler.go * @Description: 数据库处理器 - Handler 接口和 GORM 实现 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-12-13 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-12-13 10:26:53 * @FilePath: \go-toolbox\pkg\sqlbuilder\errors\checker.go * @Description: 数据库错误检测工具函数 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-12-13 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-12-13 10:26:53 * @FilePath: \go-toolbox\pkg\sqlbuilder\errors\checker.go * @Description: 数据库错误检测工具函数 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-07-30 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-07-30 13:26:08 * @FilePath: \go-toolbox\pkg\sqlbuilder\repository\async_batch_writer.go * @Description: 通用异步批量写入器,适用于高频写入低频查询的场景(如 ClickHouse 日志表) * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-07-30 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-07-30 13:26:08 * @FilePath: \go-toolbox\pkg\sqlbuilder\repository\async_batch_writer.go * @Description: 通用异步批量写入器,适用于高频写入低频查询的场景(如 ClickHouse 日志表) * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-07-30 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-07-30 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\compressed.go * @Description: 数据库压缩存储类型——字段级透明 gzip 压缩,多 DB 方言适配 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-07-30 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-07-30 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\compressed.go * @Description: 数据库压缩存储类型——字段级透明 gzip 压缩,多 DB 方言适配 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

Jump to

Keyboard shortcuts

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