constants

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: 0 Imported by: 0

Documentation

Overview

* @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-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-26 21:55:50 * @FilePath: \go-toolbox\pkg\sqlbuilder\constants\operators.go * @Description: 操作符常量定义 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\constants\pagination.go * @Description: 分页和批处理常量定义 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\constants\sort.go * @Description: 排序方向常量定义 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-23 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2025-11-23 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\constants\sql_templates.go * @Description: SQL模板常量定义 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.

Index

Constants

View Source
const (
	// AggregateFuncCount COUNT 聚合
	AggregateFuncCount = "COUNT"
	// AggregateFuncSum SUM 聚合
	AggregateFuncSum = "SUM"
	// AggregateFuncAvg AVG 聚合
	AggregateFuncAvg = "AVG"
	// AggregateFuncMax MAX 聚合
	AggregateFuncMax = "MAX"
	// AggregateFuncMin MIN 聚合
	AggregateFuncMin = "MIN"
)

聚合函数名称

View Source
const (
	// LogConcurrentQueryFailed 并发查询失败日志模板
	LogConcurrentQueryFailed = "⚠️  并发查询任务失败: %s, 错误: %v"

	// LogConcurrentQueryCancelled 并发查询取消日志模板
	LogConcurrentQueryCancelled = "⚠️  查询任务取消: %s"
)

日志消息模板

View Source
const (
	// SQLWildcard SQL 通配符
	SQLWildcard = "*"

	// SQLPlaceholder SQL 占位符
	SQLPlaceholder = "1=1"
)

SQL 相关常量

View Source
const (
	// AggregateDefaultThenValue CASE WHEN 默认 THEN 值
	AggregateDefaultThenValue = "1"

	// AggregateDefaultGroupAlias 默认时间分组别名
	AggregateDefaultGroupAlias = "time_group"

	// AggregateDefaultOrderDirection 默认排序方向
	AggregateDefaultOrderDirection = "ASC"
)

聚合相关常量

View Source
const (
	// DatabaseTypeMySQL MySQL 数据库类型
	DatabaseTypeMySQL = "mysql"

	// DatabaseTypeSQLite SQLite 数据库类型
	DatabaseTypeSQLite = "sqlite"

	// DatabaseTypePostgreSQL PostgreSQL 数据库类型
	DatabaseTypePostgreSQL = "postgres"

	// DatabaseTypeCockroachDB CockroachDB 数据库类型
	DatabaseTypeCockroachDB = "cockroachdb"

	// DatabaseTypeClickHouse ClickHouse 数据库类型
	DatabaseTypeClickHouse = "clickhouse"
)
View Source
const (
	// DialectorMySQL MySQL 方言
	DialectorMySQL = DatabaseTypeMySQL

	// DialectorSQLite SQLite 方言
	DialectorSQLite = DatabaseTypeSQLite

	// DialectorPostgreSQL PostgreSQL 方言
	DialectorPostgreSQL = DatabaseTypePostgreSQL

	// DialectorCockroachDB CockroachDB 方言
	DialectorCockroachDB = DatabaseTypeCockroachDB

	// DialectorClickHouse ClickHouse 方言
	DialectorClickHouse = DatabaseTypeClickHouse
)
View Source
const (
	OP_EQ             = OpEqual
	OP_NEQ            = OpNotEqual
	OP_GT             = OpGreaterThan
	OP_GTE            = OpGreaterThanOrEqual
	OP_LT             = OpLessThan
	OP_LTE            = OpLessThanOrEqual
	OP_LIKE           = OpLike
	OP_NOT_LIKE       = OpNotLike
	OP_STARTS_WITH    = OpStartsWith
	OP_ENDS_WITH      = OpEndsWith
	OP_CONTAINS       = OpContains
	OP_IN             = OpIn
	OP_NOT_IN         = OpNotIn
	OP_BETWEEN        = OpBetween
	OP_NOT_BETWEEN    = OpNotBetween
	OP_IS_NULL        = OpIsNull
	OP_IS_NOT_NULL    = OpIsNotNull
	OP_FIND_IN_SET    = OpFindInSet
	OP_REGEX          = OpRegex
	OP_REGEXP         = OpRegex
	OP_NOT_REGEX      = OpNotRegex
	OP_NOT_REGEXP     = OpNotRegex
	OP_ILIKE          = OpILike
	OP_NOT_ILIKE      = OpNotILike
	OP_SIMILAR_TO     = OpSimilarTo
	OP_NOT_SIMILAR_TO = OpNotSimilarTo
	OP_RAW            = OpRaw          // 原始 SQL 条件
	OP_JSONB_LIKE     = OpJsonbLike    // jsonb 字段文本搜索
	OP_JSON_CONTAINS  = OpJsonContains // JSON 数组包含查询
	OP_JSON_FIELD_EQ  = OpJsonFieldEq  // JSON 对象字段等值查询
	OP_ALL            = OpAll
	OP_ANY            = OpAny
	OP_SOME           = OpSome
	OP_EXISTS         = OpExists
	OP_NOT_EXISTS     = OpNotExists
	LOGIC_AND         = LogicAnd
	LOGIC_OR          = LogicOr
	LOGIC_NOT         = LogicNot
)
View Source
const (
	// 聚合函数
	FUNC_COUNT        = "COUNT"        // 计数
	FUNC_SUM          = "SUM"          // 求和
	FUNC_AVG          = "AVG"          // 平均值
	FUNC_MIN          = "MIN"          // 最小值
	FUNC_MAX          = "MAX"          // 最大值
	FUNC_GROUP_CONCAT = "GROUP_CONCAT" // MySQL 分组连接
	FUNC_STRING_AGG   = "STRING_AGG"   // PostgreSQL 字符串聚合

	// 窗口函数
	FUNC_ROW_NUMBER = "ROW_NUMBER" // 行号
	FUNC_RANK       = "RANK"       // 排名
	FUNC_DENSE_RANK = "DENSE_RANK" // 密集排名
	FUNC_LAG        = "LAG"        // 滞后
	FUNC_LEAD       = "LEAD"       // 超前
)
View Source
const (
	ORDER_ASC  = "ASC"  // 升序
	ORDER_DESC = "DESC" // 降序
)
View Source
const (
	JOIN_INNER = "INNER JOIN" // 内连接
	JOIN_LEFT  = "LEFT JOIN"  // 左连接
	JOIN_RIGHT = "RIGHT JOIN" // 右连接
	JOIN_FULL  = "FULL JOIN"  // 全外连接
	JOIN_CROSS = "CROSS JOIN" // 交叉连接
)
View Source
const (
	// DefaultPage 默认页码
	DefaultPage = 1

	// DefaultPageSize 默认每页大小
	DefaultPageSize = 20

	// MaxPageSize 最大每页大小
	MaxPageSize = 100

	// MinPageSize 最小每页大小
	MinPageSize = 1
)
View Source
const (
	// Asc 升序
	Asc = "ASC"

	// Desc 降序
	Desc = "DESC"

	// DefaultOrder 默认排序方向
	DefaultOrder = Asc
)
View Source
const (
	// SQL_EQUAL 等于条件模板
	SQL_EQUAL = "%s = ?"

	// SQL_NOT_EQUAL 不等于条件模板
	SQL_NOT_EQUAL = "%s != ?"

	// SQL_GREATER 大于条件模板
	SQL_GREATER = "%s > ?"

	// SQL_GREATER_EQUAL 大于等于条件模板
	SQL_GREATER_EQUAL = "%s >= ?"

	// SQL_LESS 小于条件模板
	SQL_LESS = "%s < ?"

	// SQL_LESS_EQUAL 小于等于条件模板
	SQL_LESS_EQUAL = "%s <= ?"

	// SQL_IN IN条件模板(占位符带括号,切片参数由 gORM 展开为 (v1, v2, ...))
	SQL_IN = "%s IN (?)"

	// SQL_NOT_IN NOT IN条件模板(占位符带括号,切片参数由 gORM 展开为 (v1, v2, ...))
	SQL_NOT_IN = "%s NOT IN (?)"

	// SQL_LIKE LIKE条件模板
	SQL_LIKE = "%s LIKE ?"

	// SQL_NOT_LIKE NOT LIKE条件模板
	SQL_NOT_LIKE = "%s NOT LIKE ?"

	// SQL_ILIKE 大小写不敏感 LIKE 条件模板
	// 跨数据库实现:LOWER(field) LIKE LOWER(?),兼容 PostgreSQL/MySQL/SQLite 等
	SQL_ILIKE = "LOWER(%s) LIKE LOWER(?)"

	// SQL_NOT_ILIKE 大小写不敏感 NOT LIKE 条件模板
	SQL_NOT_ILIKE = "LOWER(%s) NOT LIKE LOWER(?)"

	// SQL_BETWEEN BETWEEN条件模板
	SQL_BETWEEN = "%s BETWEEN ? AND ?"

	// SQL_NOT_BETWEEN NOT BETWEEN条件模板
	SQL_NOT_BETWEEN = "%s NOT BETWEEN ? AND ?"

	// SQL_SIMILAR_TO SIMILAR TO条件模板(PostgreSQL)
	SQL_SIMILAR_TO = "%s SIMILAR TO ?"

	// SQL_NOT_SIMILAR_TO NOT SIMILAR TO条件模板(PostgreSQL)
	SQL_NOT_SIMILAR_TO = "%s NOT SIMILAR TO ?"

	// SQL_JSONB_LIKE jsonb字段文本搜索模板(PostgreSQL)
	SQL_JSONB_LIKE = "%s::text LIKE ?"

	// SQL_IS_NULL IS NULL条件模板
	SQL_IS_NULL = "%s IS NULL"

	// SQL_IS_NOT_NULL IS NOT NULL条件模板
	SQL_IS_NOT_NULL = "%s IS NOT NULL"

	// SQL_FIND_IN_SET FIND_IN_SET条件模板
	SQL_FIND_IN_SET = "FIND_IN_SET(?, %s)"

	// SQL_ORDER_BY 排序模板
	SQL_ORDER_BY = "%s %s"

	// SQL_INCREMENT 字段自增模板
	SQL_INCREMENT = "%s + ?"

	// SQL_DECREMENT 字段自减模板
	SQL_DECREMENT = "%s - ?"

	// SQL通配符常量
	SQL_WILDCARD_ANY    = "%" // 匹配任意字符
	SQL_WILDCARD_SINGLE = "_" // 匹配单个字符
)
View Source
const (
	// MySQLFormatHour 小时格式: 2025-12-11 15:00:00
	MySQLFormatHour = "%Y-%m-%d %H:00:00"
	// MySQLFormatDay 天格式: 2025-12-11
	MySQLFormatDay = "%Y-%m-%d"
	// MySQLFormatWeek 周格式: 2025-50
	MySQLFormatWeek = "%Y-%u"
	// MySQLFormatMonth 月格式: 2025-12
	MySQLFormatMonth = "%Y-%m"
	// MySQLFormatYear 年格式: 2025
	MySQLFormatYear = "%Y"
)

MySQL 时间格式化模板

View Source
const (
	// DefaultBatchSize 默认批处理大小
	DefaultBatchSize = 100
)
View Source
const (
	// DefaultQueryTimeout 默认查询超时时间(秒)
	DefaultQueryTimeout = 30
)
View Source
const DefaultTimeField = "created_at"

DefaultTimeField 默认时间字段名

View Source
const (
	// DefaultWorkerCount 默认工作协程数 (0表示不限制)
	DefaultWorkerCount = 0
)

并发查询默认配置

Variables

View Source
var OperatorTemplateMap = map[Operator]string{
	OP_EQ:          SQL_EQUAL,
	OP_NEQ:         SQL_NOT_EQUAL,
	OP_GT:          SQL_GREATER,
	OP_GTE:         SQL_GREATER_EQUAL,
	OP_LT:          SQL_LESS,
	OP_LTE:         SQL_LESS_EQUAL,
	OP_LIKE:        SQL_LIKE,
	OP_NOT_LIKE:    SQL_NOT_LIKE,
	OP_ILIKE:       SQL_ILIKE,
	OP_NOT_ILIKE:   SQL_NOT_ILIKE,
	OP_IN:          SQL_IN,
	OP_NOT_IN:      SQL_NOT_IN,
	OP_IS_NULL:     SQL_IS_NULL,
	OP_IS_NOT_NULL: SQL_IS_NOT_NULL,
	OP_FIND_IN_SET: SQL_FIND_IN_SET,
}

OperatorTemplateMap 操作符到SQL模板的映射

View Source
var PostgreSQLDialectors = []string{
	DialectorPostgreSQL,
	DialectorCockroachDB,
}

PostgreSQLDialectors PostgreSQL 方言组(PostgreSQL 和 CockroachDB)

SupportedDialectors 支持的数据库方言列表

Functions

func IsClickHouseDialector

func IsClickHouseDialector(dialector string) bool

IsClickHouseDialector 检查是否为 ClickHouse 方言

func IsCockroachDBDialector

func IsCockroachDBDialector(dialector string) bool

IsCockroachDBDialector 检查是否为 CockroachDB 方言

func IsMySQLDialector

func IsMySQLDialector(dialector string) bool

IsMySQLDialector 检查是否为 MySQL 方言

func IsPostgreSQLDialector

func IsPostgreSQLDialector(dialector string) bool

IsPostgreSQLDialector 检查是否为 PostgreSQL 方言

func IsPostgreSQLFamilyDialector

func IsPostgreSQLFamilyDialector(dialector string) bool

IsPostgreSQLFamilyDialector 检查是否为 PostgreSQL 家族方言(PostgreSQL 或 CockroachDB)

func IsSQLiteDialector

func IsSQLiteDialector(dialector string) bool

IsSQLiteDialector 检查是否为 SQLite 方言

func IsSupportedDialector

func IsSupportedDialector(dialector string) bool

IsSupportedDialector 检查是否为支持的数据库方言

Types

type Operator

type Operator string

Operator 操作符类型(统一定义,避免重复)

const (
	OpEqual              Operator = "="  // 等于
	OpNotEqual           Operator = "!=" // 不等于
	OpGreaterThan        Operator = ">"  // 大于
	OpGreaterThanOrEqual Operator = ">=" // 大于等于
	OpLessThan           Operator = "<"  // 小于
	OpLessThanOrEqual    Operator = "<=" // 小于等于
)
const (
	OpLike       Operator = "LIKE"        // 模糊匹配
	OpNotLike    Operator = "NOT LIKE"    // 不匹配
	OpStartsWith Operator = "STARTS_WITH" // 开始于(LIKE 'value%')
	OpEndsWith   Operator = "ENDS_WITH"   // 结束于(LIKE '%value')
	OpContains   Operator = "CONTAINS"    // 包含(LIKE '%value%')
)
const (
	OpIn         Operator = "IN"          // 包含
	OpNotIn      Operator = "NOT IN"      // 不包含
	OpBetween    Operator = "BETWEEN"     // 范围
	OpNotBetween Operator = "NOT BETWEEN" // 不在范围内
	OpAll        Operator = "ALL"         // 所有(与子查询配合)
	OpAny        Operator = "ANY"         // 任意(与子查询配合)
	OpSome       Operator = "SOME"        // 某些(与子查询配合)
	OpExists     Operator = "EXISTS"      // 存在(子查询)
	OpNotExists  Operator = "NOT EXISTS"  // 不存在(子查询)
)
const (
	OpIsNull    Operator = "IS NULL"     // 为空
	OpIsNotNull Operator = "IS NOT NULL" // 不为空
)
const (
	OpFindInSet    Operator = "FIND_IN_SET"    // MySQL FIND_IN_SET
	OpRegex        Operator = "REGEXP"         // 正则匹配
	OpNotRegex     Operator = "NOT REGEXP"     // 正则不匹配
	OpILike        Operator = "ILIKE"          // PostgreSQL 不区分大小写匹配
	OpNotILike     Operator = "NOT ILIKE"      // PostgreSQL 不区分大小写不匹配
	OpSimilarTo    Operator = "SIMILAR TO"     // PostgreSQL SIMILAR TO
	OpNotSimilarTo Operator = "NOT SIMILAR TO" // PostgreSQL NOT SIMILAR TO
	OpRaw          Operator = "RAW"            // 原始 SQL 条件(直接使用 Field 作为条件)
	OpJsonbLike    Operator = "JSONB_LIKE"     // jsonb 字段文本搜索(PostgreSQL: field::text LIKE ?)
	OpJsonContains Operator = "JSON_CONTAINS"  // JSON 数组包含查询(方言感知,WHERE 子句用)
	OpJsonFieldEq  Operator = "JSON_FIELD_EQ"  // JSON 对象字段等值查询(方言感知,Field 格式为 "column:jsonKey")
)
const (
	LogicAnd Operator = "AND" // 逻辑与
	LogicOr  Operator = "OR"  // 逻辑或
	LogicNot Operator = "NOT" // 逻辑非
)

func (Operator) String

func (op Operator) String() string

String 返回操作符的字符串表示

Jump to

Keyboard shortcuts

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