db

package
v1.6.6 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect added in v1.5.0

type Dialect interface {
	// Quote 对表名/字段名添加引号
	// MySQL 使用反引号 `name`
	// PostgreSQL 使用双引号 "name"
	// SQLite 使用双引号或方括号 "name" 或 [name]
	Quote(name string) string

	// QuoteIdentifier 处理单个标识符(去除已有引号,添加正确引号)
	// 输入可能带有反引号或双引号,会先去除再添加正确格式
	QuoteIdentifier(name string) string

	// QuoteChar 返回引号字符
	// MySQL: `
	// PostgreSQL/SQLite: "
	QuoteChar() string

	// Placeholder 生成占位符
	// MySQL/SQLite 使用 ?
	// PostgreSQL 使用 $1, $2, $3...
	Placeholder(index int) string

	// Placeholders 生成多个占位符,用逗号分隔
	Placeholders(count int, startIndex int) string

	// SupportsLastInsertId 是否支持 LastInsertId
	// PostgreSQL 不支持,需要使用 RETURNING
	SupportsLastInsertId() bool

	// ReturningClause 生成 RETURNING 子句(用于 PostgreSQL)
	ReturningClause(column string) string

	// UpsertSQL 生成 Upsert 语句
	// MySQL: INSERT ... ON DUPLICATE KEY UPDATE ...
	// PostgreSQL: INSERT ... ON CONFLICT ... DO UPDATE SET ...
	// SQLite: INSERT OR REPLACE / INSERT ... ON CONFLICT ...
	UpsertSQL(table string, columns []string, uniqueKeys []string, updateColumns []string) string

	// GetName 获取方言名称
	GetName() string
}

Dialect 数据库方言接口 用于处理不同数据库之间的语法差异

type HoTimeDB

type HoTimeDB struct {
	*sql.DB
	ContextBase
	DBName string
	*cache.HoTimeCache
	Log         *logrus.Logger
	Type        string // 数据库类型: mysql, sqlite3, postgres
	Prefix      string
	LastQuery   string
	LastData    []interface{}
	ConnectFunc func(err ...*Error) (*sql.DB, *sql.DB)
	LastErr     *Error

	*sql.Tx         //事务对象
	SlaveDB *sql.DB // 从数据库
	Mode    int     // mode为0生产模式,1为测试模式,2为开发模式

	Dialect Dialect // 数据库方言适配器
	// contains filtered or unexported fields
}

HoTimeDB 数据库操作核心结构体

func (*HoTimeDB) Action

func (that *HoTimeDB) Action(action func(db HoTimeDB) (isSuccess bool)) (isSuccess bool)

Action 事务操作 如果 action 返回 true 则提交事务;返回 false 则回滚

func (*HoTimeDB) Avg added in v1.5.0

func (that *HoTimeDB) Avg(table string, column string, qu ...interface{}) float64

Avg 平均值

func (*HoTimeDB) C added in v1.5.2

func (that *HoTimeDB) C(args ...string) string

C 辅助方法:获取带前缀和引号的 table.column 支持两种调用方式:

  • db.C("order", "name") 返回 "`app_order`.`name`"
  • db.C("order.name") 返回 "`app_order`.`name`"

func (*HoTimeDB) Count

func (that *HoTimeDB) Count(table string, qu ...interface{}) int

Count 计数

func (*HoTimeDB) Delete

func (that *HoTimeDB) Delete(table string, data map[string]interface{}) int64

Delete 删除数据

func (*HoTimeDB) Exec

func (that *HoTimeDB) Exec(query string, args ...interface{}) (sql.Result, *Error)

Exec 执行非查询 SQL

func (*HoTimeDB) Get

func (that *HoTimeDB) Get(table string, qu ...interface{}) Map

Get 获取单条记录

func (*HoTimeDB) GetDialect added in v1.5.0

func (that *HoTimeDB) GetDialect() Dialect

GetDialect 获取当前方言适配器

func (*HoTimeDB) GetPrefix

func (that *HoTimeDB) GetPrefix() string

GetPrefix 获取表前缀

func (*HoTimeDB) GetProcessor added in v1.5.2

func (that *HoTimeDB) GetProcessor() *IdentifierProcessor

GetProcessor 获取标识符处理器 用于处理表名、字段名的前缀添加和引号转换

func (*HoTimeDB) GetType

func (that *HoTimeDB) GetType() string

GetType 获取数据库类型

func (*HoTimeDB) InitDb

func (that *HoTimeDB) InitDb(err ...*Error) *Error

InitDb 初始化数据库连接

func (*HoTimeDB) Insert

func (that *HoTimeDB) Insert(table string, data map[string]interface{}) int64

Insert 插入新数据

func (*HoTimeDB) Inserts added in v1.6.0

func (that *HoTimeDB) Inserts(table string, dataList []Map) int64

Inserts 批量插入数据 table: 表名 dataList: 数据列表,每个元素是一个 Map 返回受影响的行数

示例:

affected := db.Inserts("user", []Map{
    {"name": "张三", "age": 25, "email": "[email protected]"},
    {"name": "李四", "age": 30, "email": "[email protected]"},
    {"name": "王五", "age": 28, "email": "[email protected]"},
})

func (*HoTimeDB) Max added in v1.5.0

func (that *HoTimeDB) Max(table string, column string, qu ...interface{}) float64

Max 最大值

func (*HoTimeDB) Min added in v1.5.0

func (that *HoTimeDB) Min(table string, column string, qu ...interface{}) float64

Min 最小值

func (*HoTimeDB) Page

func (that *HoTimeDB) Page(page, pageRow int) *HoTimeDB

Page 设置分页参数 page: 页码(从1开始) pageRow: 每页数量

func (*HoTimeDB) PageSelect

func (that *HoTimeDB) PageSelect(table string, qu ...interface{}) []Map

PageSelect 分页查询

func (*HoTimeDB) Query

func (that *HoTimeDB) Query(query string, args ...interface{}) []Map

Query 执行查询 SQL

func (*HoTimeDB) Row

func (that *HoTimeDB) Row(resl *sql.Rows) []Map

Row 数据库数据解析

func (*HoTimeDB) Select

func (that *HoTimeDB) Select(table string, qu ...interface{}) []Map

Select 查询多条记录

func (*HoTimeDB) SetConnect

func (that *HoTimeDB) SetConnect(connect func(err ...*Error) (master, slave *sql.DB), err ...*Error)

SetConnect 设置数据库配置连接

func (*HoTimeDB) SetDialect added in v1.5.0

func (that *HoTimeDB) SetDialect(dialect Dialect)

SetDialect 设置方言适配器

func (*HoTimeDB) Sum added in v1.2.80

func (that *HoTimeDB) Sum(table string, column string, qu ...interface{}) float64

Sum 求和

func (*HoTimeDB) T added in v1.5.2

func (that *HoTimeDB) T(table string) string

T 辅助方法:获取带前缀和引号的表名 用于手动构建 SQL 时使用 示例: db.T("order") 返回 "`app_order`" (MySQL) 或 "\"app_order\"" (PostgreSQL)

func (*HoTimeDB) Table added in v1.2.0

func (that *HoTimeDB) Table(table string) *HotimeDBBuilder

Table 创建链式查询构建器

func (*HoTimeDB) Update

func (that *HoTimeDB) Update(table string, data Map, where Map) int64

Update 更新数据

func (*HoTimeDB) Upsert added in v1.5.0

func (that *HoTimeDB) Upsert(table string, data Map, uniqueKeys Slice, updateColumns ...interface{}) int64

Upsert 插入或更新数据 table: 表名 data: 要插入的数据 uniqueKeys: 唯一键字段(用于冲突检测),支持 Slice{"id"} 或 Slice{"col1", "col2"} updateColumns: 冲突时要更新的字段(如果为空,则更新所有非唯一键字段) 返回受影响的行数

示例:

affected := db.Upsert("user",
    Map{"id": 1, "name": "张三", "email": "[email protected]"},
    Slice{"id"},                    // 唯一键
    Slice{"name", "email"},         // 冲突时更新的字段
)

type HotimeDBBuilder added in v1.2.0

type HotimeDBBuilder struct {
	HoTimeDB *HoTimeDB
	// contains filtered or unexported fields
}

HotimeDBBuilder 链式查询构建器

func (*HotimeDBBuilder) And added in v1.2.0

func (that *HotimeDBBuilder) And(qu ...interface{}) *HotimeDBBuilder

And 添加 AND 条件

func (*HotimeDBBuilder) Count added in v1.2.0

func (that *HotimeDBBuilder) Count() int

Count 统计数量

func (*HotimeDBBuilder) Delete added in v1.2.0

func (that *HotimeDBBuilder) Delete() int64

Delete 删除记录

func (*HotimeDBBuilder) From added in v1.2.0

func (that *HotimeDBBuilder) From(table string) *HotimeDBBuilder

From 设置表名

func (*HotimeDBBuilder) FullJoin added in v1.2.0

func (that *HotimeDBBuilder) FullJoin(table, joinStr string) *HotimeDBBuilder

FullJoin 全连接

func (*HotimeDBBuilder) Get added in v1.2.0

func (that *HotimeDBBuilder) Get(qu ...interface{}) Map

Get 获取单条记录

func (*HotimeDBBuilder) Group added in v1.2.0

func (that *HotimeDBBuilder) Group(qu ...interface{}) *HotimeDBBuilder

Group 设置分组

func (*HotimeDBBuilder) Having added in v1.5.0

func (that *HotimeDBBuilder) Having(qu ...interface{}) *HotimeDBBuilder

Having 设置 HAVING 条件

func (*HotimeDBBuilder) InnerJoin added in v1.2.0

func (that *HotimeDBBuilder) InnerJoin(table, joinStr string) *HotimeDBBuilder

InnerJoin 内连接

func (*HotimeDBBuilder) Join added in v1.2.0

func (that *HotimeDBBuilder) Join(qu ...interface{}) *HotimeDBBuilder

Join 通用连接

func (*HotimeDBBuilder) LeftJoin added in v1.2.0

func (that *HotimeDBBuilder) LeftJoin(table, joinStr string) *HotimeDBBuilder

LeftJoin 左连接

func (*HotimeDBBuilder) Limit added in v1.2.0

func (that *HotimeDBBuilder) Limit(qu ...interface{}) *HotimeDBBuilder

Limit 设置限制

func (*HotimeDBBuilder) Offset added in v1.5.0

func (that *HotimeDBBuilder) Offset(offset int) *HotimeDBBuilder

Offset 设置偏移量

func (*HotimeDBBuilder) Or added in v1.2.0

func (that *HotimeDBBuilder) Or(qu ...interface{}) *HotimeDBBuilder

Or 添加 OR 条件

func (*HotimeDBBuilder) Order added in v1.2.0

func (that *HotimeDBBuilder) Order(qu ...interface{}) *HotimeDBBuilder

Order 设置排序

func (*HotimeDBBuilder) Page added in v1.2.0

func (that *HotimeDBBuilder) Page(page, pageRow int) *HotimeDBBuilder

Page 设置分页

func (*HotimeDBBuilder) RightJoin added in v1.2.0

func (that *HotimeDBBuilder) RightJoin(table, joinStr string) *HotimeDBBuilder

RightJoin 右连接

func (*HotimeDBBuilder) Select added in v1.2.0

func (that *HotimeDBBuilder) Select(qu ...interface{}) []Map

Select 查询多条记录

func (*HotimeDBBuilder) Update added in v1.2.0

func (that *HotimeDBBuilder) Update(qu ...interface{}) int64

Update 更新记录

func (*HotimeDBBuilder) Where added in v1.2.0

func (that *HotimeDBBuilder) Where(qu ...interface{}) *HotimeDBBuilder

Where 设置 WHERE 条件

type IdentifierProcessor added in v1.5.2

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

IdentifierProcessor 标识符处理器 用于处理表名、字段名的前缀添加和引号转换

Example

打印测试结果(用于调试)

// MySQL 示例
mysqlProcessor := NewIdentifierProcessor(&MySQLDialect{}, "app_")
fmt.Println("MySQL:")
fmt.Println("  Table:", mysqlProcessor.ProcessTableName("order"))
fmt.Println("  Column:", mysqlProcessor.ProcessColumn("order.name"))
fmt.Println("  Condition:", mysqlProcessor.ProcessConditionString("user.id = order.user_id"))

// PostgreSQL 示例
pgProcessor := NewIdentifierProcessor(&PostgreSQLDialect{}, "app_")
fmt.Println("PostgreSQL:")
fmt.Println("  Table:", pgProcessor.ProcessTableName("order"))
fmt.Println("  Column:", pgProcessor.ProcessColumn("order.name"))
fmt.Println("  Condition:", pgProcessor.ProcessConditionString("user.id = order.user_id"))
Output:

MySQL:
  Table: `app_order`
  Column: `app_order`.`name`
  Condition: `app_user`.`id` = `app_order`.`user_id`
PostgreSQL:
  Table: "app_order"
  Column: "app_order"."name"
  Condition: "app_user"."id" = "app_order"."user_id"

func NewIdentifierProcessor added in v1.5.2

func NewIdentifierProcessor(dialect Dialect, prefix string) *IdentifierProcessor

NewIdentifierProcessor 创建标识符处理器

func (*IdentifierProcessor) GetDialect added in v1.5.2

func (p *IdentifierProcessor) GetDialect() Dialect

GetDialect 获取方言

func (*IdentifierProcessor) GetPrefix added in v1.5.2

func (p *IdentifierProcessor) GetPrefix() string

GetPrefix 获取前缀

func (*IdentifierProcessor) ProcessColumn added in v1.5.2

func (p *IdentifierProcessor) ProcessColumn(name string) string

ProcessColumn 处理 table.column 格式 输入: "name" 或 "order.name" 或 "`order`.name" 或 "`order`.`name`" 输出: "`name`" 或 "`app_order`.`name`" (MySQL)

func (*IdentifierProcessor) ProcessColumnNoPrefix added in v1.5.2

func (p *IdentifierProcessor) ProcessColumnNoPrefix(name string) string

ProcessColumnNoPrefix 处理 table.column 格式(不添加前缀)

func (*IdentifierProcessor) ProcessConditionString added in v1.5.2

func (p *IdentifierProcessor) ProcessConditionString(condition string) string

ProcessConditionString 智能解析条件字符串(如 ON 条件) 输入: "user.id = order.user_id AND order.status = 1" 输出: "`app_user`.`id` = `app_order`.`user_id` AND `app_order`.`status` = 1" (MySQL)

func (*IdentifierProcessor) ProcessFieldList added in v1.5.2

func (p *IdentifierProcessor) ProcessFieldList(fields string) string

ProcessFieldList 处理字段列表字符串 输入: "order.id, user.name AS uname, COUNT(*)" 输出: "`app_order`.`id`, `app_user`.`name` AS uname, COUNT(*)" (MySQL)

func (*IdentifierProcessor) ProcessTableName added in v1.5.2

func (p *IdentifierProcessor) ProcessTableName(name string) string

ProcessTableName 处理表名(添加前缀+引号) 输入: "order" 或 "`order`" 或 "\"order\"" 或 "INFORMATION_SCHEMA.TABLES" 输出: "`app_order`" (MySQL) 或 "\"app_order\"" (PostgreSQL/SQLite) 对于 database.table 格式,会分别处理,系统数据库不添加前缀

func (*IdentifierProcessor) ProcessTableNameNoPrefix added in v1.5.2

func (p *IdentifierProcessor) ProcessTableNameNoPrefix(name string) string

ProcessTableNameNoPrefix 处理表名(只添加引号,不添加前缀) 用于已经包含前缀的情况

type MySQLDialect added in v1.5.0

type MySQLDialect struct{}

MySQLDialect MySQL 方言实现

func (*MySQLDialect) GetName added in v1.5.0

func (d *MySQLDialect) GetName() string

func (*MySQLDialect) Placeholder added in v1.5.0

func (d *MySQLDialect) Placeholder(index int) string

func (*MySQLDialect) Placeholders added in v1.5.0

func (d *MySQLDialect) Placeholders(count int, startIndex int) string

func (*MySQLDialect) Quote added in v1.5.0

func (d *MySQLDialect) Quote(name string) string

func (*MySQLDialect) QuoteChar added in v1.5.2

func (d *MySQLDialect) QuoteChar() string

func (*MySQLDialect) QuoteIdentifier added in v1.5.2

func (d *MySQLDialect) QuoteIdentifier(name string) string

func (*MySQLDialect) ReturningClause added in v1.5.0

func (d *MySQLDialect) ReturningClause(column string) string

func (*MySQLDialect) SupportsLastInsertId added in v1.5.0

func (d *MySQLDialect) SupportsLastInsertId() bool

func (*MySQLDialect) UpsertSQL added in v1.5.0

func (d *MySQLDialect) UpsertSQL(table string, columns []string, uniqueKeys []string, updateColumns []string) string

type PostgreSQLDialect added in v1.5.0

type PostgreSQLDialect struct{}

PostgreSQLDialect PostgreSQL 方言实现

func (*PostgreSQLDialect) GetName added in v1.5.0

func (d *PostgreSQLDialect) GetName() string

func (*PostgreSQLDialect) Placeholder added in v1.5.0

func (d *PostgreSQLDialect) Placeholder(index int) string

func (*PostgreSQLDialect) Placeholders added in v1.5.0

func (d *PostgreSQLDialect) Placeholders(count int, startIndex int) string

func (*PostgreSQLDialect) Quote added in v1.5.0

func (d *PostgreSQLDialect) Quote(name string) string

func (*PostgreSQLDialect) QuoteChar added in v1.5.2

func (d *PostgreSQLDialect) QuoteChar() string

func (*PostgreSQLDialect) QuoteIdentifier added in v1.5.2

func (d *PostgreSQLDialect) QuoteIdentifier(name string) string

func (*PostgreSQLDialect) ReturningClause added in v1.5.0

func (d *PostgreSQLDialect) ReturningClause(column string) string

func (*PostgreSQLDialect) SupportsLastInsertId added in v1.5.0

func (d *PostgreSQLDialect) SupportsLastInsertId() bool

func (*PostgreSQLDialect) UpsertSQL added in v1.5.0

func (d *PostgreSQLDialect) UpsertSQL(table string, columns []string, uniqueKeys []string, updateColumns []string) string

type SQLiteDialect added in v1.5.0

type SQLiteDialect struct{}

SQLiteDialect SQLite 方言实现

func (*SQLiteDialect) GetName added in v1.5.0

func (d *SQLiteDialect) GetName() string

func (*SQLiteDialect) Placeholder added in v1.5.0

func (d *SQLiteDialect) Placeholder(index int) string

func (*SQLiteDialect) Placeholders added in v1.5.0

func (d *SQLiteDialect) Placeholders(count int, startIndex int) string

func (*SQLiteDialect) Quote added in v1.5.0

func (d *SQLiteDialect) Quote(name string) string

func (*SQLiteDialect) QuoteChar added in v1.5.2

func (d *SQLiteDialect) QuoteChar() string

func (*SQLiteDialect) QuoteIdentifier added in v1.5.2

func (d *SQLiteDialect) QuoteIdentifier(name string) string

func (*SQLiteDialect) ReturningClause added in v1.5.0

func (d *SQLiteDialect) ReturningClause(column string) string

func (*SQLiteDialect) SupportsLastInsertId added in v1.5.0

func (d *SQLiteDialect) SupportsLastInsertId() bool

func (*SQLiteDialect) UpsertSQL added in v1.5.0

func (d *SQLiteDialect) UpsertSQL(table string, columns []string, uniqueKeys []string, updateColumns []string) string

Jump to

Keyboard shortcuts

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