Documentation
¶
Overview ¶
Package cydb provides database operations and SQL parsing utilities.
Index ¶
- Constants
- func GetSupportSqlDialect() []string
- func NewSQLParseError(errorType, message string, lineNum int, lineText string) error
- func ReadSQLFile(r io.Reader, callback FuncSQLStmtCallback, ...) error
- func RegisterSqlDialect(dbtype string, sql any)
- func WithBufferSize(size int) func(*ReadSQLFileOptions)
- func WithFilter(filter []string) func(*ReadSQLFileOptions)
- func WithMigrationMode(mode string) func(*ReadSQLFileOptions)
- func WithPreProcessSqlLine(callback FuncPreProcessSqlLine) func(*ReadSQLFileOptions)
- func WithWordCallback(callback ProcessWordCallback) func(*ReadSQLFileOptions)
- type CRUDSqlFuncName
- type DDLSqlFunc
- type DDLSqlFuncName
- type DatabaseObjectType
- type DatabaseTransformer
- type ExtraData
- type FuncPreProcessSqlLine
- type FuncSQLStmtCallback
- type IndexInfo
- type MigrationDialect
- type ParseContext
- type PreRetType
- type ProcessWordCallback
- type RawBlockMarker
- type ReadSQLFileOptions
- type SQLBlock
- type SQLDialect
- type SQLParseError
- type SQLType
- type SchemaObjects
- type SchemaSummary
- type SortFuncName
- type SortedSqlFunc
- type SqlContent
- type TraceWord
- type WriteCapability
Constants ¶
const ( // MigrationCommentPrefix 迁移注释前缀 MigrationCommentPrefix = "-- +migrate" // MigrationUp 向上迁移标记 MigrationUp = "Up" // MigrationDown 向下迁移标记 MigrationDown = "Down" )
迁移注释标记
const ( PreRetTypeReturn = iota PreRetTypeCallCallback PreRetTypeContinue )
const DefaultBufferSize = 100 * 1024 * 1024
DefaultBufferSize 默认缓冲区大小 (10MB) 可以通过 ReadSQLFileOptions 进行自定义
Variables ¶
This section is empty.
Functions ¶
func GetSupportSqlDialect ¶
func GetSupportSqlDialect() []string
func NewSQLParseError ¶
NewSQLParseError 创建一个新的SQL解析错误 提供详细的上下文信息以便于调试
func ReadSQLFile ¶
func ReadSQLFile(r io.Reader, callback FuncSQLStmtCallback, optionFuncs ...func(*ReadSQLFileOptions)) error
ReadSQLFileWithOptions 使用自定义选项从文件中读取 SQL 语句 支持配置缓冲区大小等选项,提供更灵活的控制 使用可变参数函数列表来设置选项,更符合 With 模式的设计理念
func RegisterSqlDialect ¶
func WithBufferSize ¶
func WithBufferSize(size int) func(*ReadSQLFileOptions)
WithBufferSize 设置缓冲区大小的选项函数
func WithFilter ¶
func WithFilter(filter []string) func(*ReadSQLFileOptions)
WithFilter 设置 SQL 过滤器的选项函数
func WithMigrationMode ¶
func WithMigrationMode(mode string) func(*ReadSQLFileOptions)
WithMigrationMode 设置迁移模式的选项函数 mode 可以是 "Up"、"Down" 或空字符串(表示不过滤)
func WithPreProcessSqlLine ¶
func WithPreProcessSqlLine(callback FuncPreProcessSqlLine) func(*ReadSQLFileOptions)
WithPreProcessSqlLine 设置预处理 SQL 行的选项函数
func WithWordCallback ¶
func WithWordCallback(callback ProcessWordCallback) func(*ReadSQLFileOptions)
WithWordCallback 设置单词回调的选项函数
Types ¶
type CRUDSqlFuncName ¶
type CRUDSqlFuncName string
const ( FuncNameGetInsertSql CRUDSqlFuncName = "GetInsertSql" FuncNameGetSelectSql CRUDSqlFuncName = "GetSelectSql" FuncNameGetUpdateSql CRUDSqlFuncName = "GetUpdateSql" FuncNameGetDeleteSql CRUDSqlFuncName = "GetDeleteSql" FuncNameGetReplaceSql CRUDSqlFuncName = "GetReplaceSql" )
type DDLSqlFunc ¶
type DDLSqlFunc func(cli def.DatabaseClient, name ...string) (*SqlContent, error)
type DDLSqlFuncName ¶
type DDLSqlFuncName string
const ( FuncNameGetCreateTableSql DDLSqlFuncName = "GetCreateTableSql" FuncNameGetCreateViewSql DDLSqlFuncName = "GetCreateViewSql" FuncNameGetCreateProcedureSql DDLSqlFuncName = "GetCreateProcedureSql" FuncNameGetCreateFunctionSql DDLSqlFuncName = "GetCreateFunctionSql" FuncNameGetTableEventSql DDLSqlFuncName = "GetTableEventSql" FuncNameGetBeginSql DDLSqlFuncName = "GetBeginSql" FuncNameGetEndSql DDLSqlFuncName = "GetEndSql" )
type DatabaseObjectType ¶
type DatabaseObjectType string
DatabaseObjectType represents the type of database object
const ( ObjectTypeTable DatabaseObjectType = "TABLE" ObjectTypeView DatabaseObjectType = "VIEW" ObjectTypeProcedure DatabaseObjectType = "PROCEDURE" ObjectTypeFunction DatabaseObjectType = "FUNCTION" ObjectTypeTrigger DatabaseObjectType = "TRIGGER" ObjectTypeSequence DatabaseObjectType = "SEQUENCE" )
type DatabaseTransformer ¶
type DatabaseTransformer interface {
WithSubType(subType string) any
EscapeTableName(tableName string) string
EscapeColumnName(columnName string) string
BuildPagination(limit, offset string) string
SupportsBatch() bool
}
func GetSqlTransformer ¶
func GetSqlTransformer(dbtype string, dbSubType ...string) (DatabaseTransformer, bool)
type FuncPreProcessSqlLine ¶
type FuncPreProcessSqlLine func(ctx *ParseContext, pline *string) (PreRetType, error)
type FuncSQLStmtCallback ¶
type IndexInfo ¶
type IndexInfo struct {
Name string `db:"index_name"`
Columns []string `db:"-"`
IsUnique bool `db:"is_unique"`
IsPrimary bool `db:"is_primary"`
}
IndexInfo 索引信息。
type MigrationDialect ¶
type MigrationDialect interface {
// WithSubType returns a new dialect instance with the specified subtype
WithSubType(subType string) any
// GetTableName returns the properly quoted table name for the database
EscapeTableName(tableName string) string
// GetColumnName returns the properly quoted column name for the database
EscapeColumnName(columnName string) string
// GenerateCreateTableSQL generates CREATE TABLE SQL for the dialect
GenerateCreateTableSQL(tableInfo def.TableInfo) string
// GenerateAddColumnSQL generates ALTER TABLE ADD COLUMN SQL for the dialect
GenerateAddColumnSQL(tableName string, field def.ColumnInfo) string
// GenerateAddIndexSQL generates CREATE INDEX SQL for the dialect
GenerateAddIndexSQL(tableName string, field def.ColumnInfo) string
GenerateDropIndexSQL(tableName string, indexName string) string
GenerateDropConstraintSQL(tableName string, constraintType string, constraintName string) string
// GenerateCompositeForeignKeySQL generates SQL for composite foreign key constraints
GenerateCompositeForeignKeySQL(tableName string, fk def.CompositeForeignKey) string
// GenerateCompositeIndexSQL generates SQL for composite indexes
GenerateCompositeIndexSQL(tableName string, index def.CompositeIndex) string
// GenerateRenameColumnSQL generates SQL to rename a column
// oldColumnName: the current column name in the database
// fieldInfo: the field information containing the new column name (ColumnName) and type
GenerateRenameColumnSQL(tableName, oldColumnName string, fieldInfo def.ColumnInfo) string
// GenerateAlterColumnTypeSQL generates SQL to alter an existing column type
GenerateAlterColumnTypeSQL(tableName string, field def.ColumnInfo) string
// NormalizeColumnType returns the normalized/effective column type for comparison
// This is useful for databases like SQLite where INTEGER represents all integer types
// Default implementation should return the type as-is
NormalizeColumnType(fieldType def.StandardFieldType) def.StandardFieldType
}
MigrationDialect defines the interface for database-specific migration operations
func GetMigrationDialect ¶
func GetMigrationDialect(dbtype string, dbSubType ...string) (MigrationDialect, bool)
type ParseContext ¶
type ParseContext struct {
// CurrentStmt 当前正在构建的 SQL 语句
CurrentStmt strings.Builder
// StartLineNum 当前语句的起始行号
StartLineNum int
// CurrentLineNum 当前处理的行号
CurrentLineNum int
// LineContent 当前行的内容
LineContent string
// HasContent 标记当前是否有内容正在处理
HasContent bool
// BlockIndex 块索引,用于标识 SQL 语句块
BlockIndex int
// CurrentType 当前 SQL 语句的类型
CurrentType SQLType
// TypeWord 类型关键字
TypeWord string
// TraceWord 用于跟踪关键字
TraceWord *TraceWord
// ExpectedStringEnd 期望的字符串结束标记
ExpectedStringEnd string
// IsMultiLineComment 是否在多行注释中
IsMultiLineComment bool
// Filter SQL 过滤器
Filter []string
// Extra 额外的特定于方言的数据
Extra ExtraData
// WordCallback 处理单词的回调函数
WordCallback ProcessWordCallback
// PreProcessSqlLine 预处理 SQL 行的回调函数
PreProcessSqlLine FuncPreProcessSqlLine
}
ParseContext 用于解析 SQL 文件的上下文结构体 存储解析过程中的状态和中间结果
func (*ParseContext) IsBlockEnd ¶
func (pc *ParseContext) IsBlockEnd(line string) bool
func (*ParseContext) IsCurrentTypeNull ¶
func (pc *ParseContext) IsCurrentTypeNull() bool
func (*ParseContext) ReadSQLCallback ¶
func (ctx *ParseContext) ReadSQLCallback(callback FuncSQLStmtCallback) error
ReadSQLCallback 用于处理完整的 SQL 语句 当一个 SQL 语句解析完成后调用此函数处理结果
func (*ParseContext) Reset ¶
func (pc *ParseContext) Reset()
Reset 重置 ParseContext 的所有字段到初始状态 在处理完一个 SQL 语句后调用,准备处理下一个语句
type ProcessWordCallback ¶
type ProcessWordCallback func(ctx *ParseContext, word *cyutil.WordInfo) (bool, error)
ProcessWordCallback 用于处理单词的回调函数类型 在遍历 SQL 语句中的单词时调用
type RawBlockMarker ¶
type RawBlockMarker interface {
InRawBlock() bool
}
RawBlockMarker 为可选接口:方言的 ExtraData 可实现它以声明"当前正处于原样块内", 例如 PostgreSQL 的 dollar-quote($tag$ ... $tag$)。块内容是原样文本,不应按普通 SQL 规则解析字符串和注释——否则块内出现的未配对引号(如注释里的 "shouldn't")会 破坏跨行的字符串状态。processLine 在原样块内会跳过字符串/注释处理。
type ReadSQLFileOptions ¶
type ReadSQLFileOptions struct {
// BufferSize 扫描器的缓冲区大小
BufferSize int
// MigrationMode 迁移模式过滤器 ("Up", "Down" 或空字符串表示不过滤)
MigrationMode string
// Filter SQL 过滤器,用于过滤特定类型的 SQL 语句
Filter []string
// WordCallback 单词回调函数,用于处理 SQL 语句中的单词
WordCallback ProcessWordCallback
// PreProcessSqlLine 预处理 SQL 行的回调函数
PreProcessSqlLine FuncPreProcessSqlLine
}
ReadSQLFileOptions 定义了读取 SQL 文件的选项
func NewReadSQLFileOptions ¶
func NewReadSQLFileOptions(options ...func(*ReadSQLFileOptions)) ReadSQLFileOptions
NewReadSQLFileOptions 创建一个新的 ReadSQLFileOptions 实例并应用选项函数
type SQLDialect ¶
type SQLDialect interface {
WithSubType(subType string) any
// DDL operations
GetDDLSqlFunc(funcName DDLSqlFuncName) (DDLSqlFunc, error)
GetSortedSqlFunc(funcName SortFuncName) (SortedSqlFunc, error)
GetDDLEmbedFiles() (*embed.FS, error)
// Schema operations - using def.DatabaseClient instead of specific type
GetTableInfo(cli def.DatabaseClient, database, tableName string) (def.TableInfo, error)
IsTableExist(cli def.DatabaseClient, tableName string) (bool, error)
ObjectExists(cli def.DatabaseClient, objectName string, objectType DatabaseObjectType) (bool, error)
EnsureDBExists(cli def.DatabaseClient, dbName string) error
ReadSQLFile(r io.Reader, callback FuncSQLStmtCallback, options ...func(*ReadSQLFileOptions)) error
GetConnectStr(dbConn *def.DBConnection) (string, string)
ValidateSQL(cli def.DatabaseClient, sql string) *def.ValidationError
AnalyzeSQL(cli def.DatabaseClient, sql string) (*def.SQLImpact, error)
// ExplainSQL returns the execution plan for the provided SQL
// Dialect implementations should handle database-specific explain semantics
ExplainSQL(cli def.DatabaseClient, sql string) (def.QueryResult, error)
GetTables(cli def.DatabaseClient, dbName string, schema *string, keyword *string) ([]string, error)
// GetIndexes 返回表索引列表。
GetIndexes(cli def.DatabaseClient, dbName, schema, tableName string) ([]IndexInfo, error)
// GetDatabases 返回数据库列表。
GetDatabases(cli def.DatabaseClient) ([]string, error)
// GetSchemas 返回指定库内的 schema/模式列表(PG 系为用户 schema;
// MySQL/SQLite 无 schema 概念返回空;Oracle 返回用户列表)。
GetSchemas(cli def.DatabaseClient, dbName string) ([]string, error)
// GetSchemaSummaries 返回库内用户 schema 列表及每 schema 的表计数(PG 系一次往返;
// 非 PG 系返回空,调用方应回退 GetObjects/GetTables 逐库枚举)。
GetSchemaSummaries(cli def.DatabaseClient, dbName string) ([]SchemaSummary, error)
// GetSchemaObjects 一次往返枚举 schema 内全部对象(表/视图/函数/过程);
// 非 PG 系返回空对象(与 GetSchemaSummaries 口径一致,调用方无需区分)。
GetSchemaObjects(cli def.DatabaseClient, dbName, schema string) (*SchemaObjects, error)
// GetObjects 枚举指定库/schema 内某类对象的名称列表(与 GetTables 对称的元数据能力)。
// TABLE 类型应使用 GetTables;不支持的对象类型返回错误
GetObjects(cli def.DatabaseClient, dbName string, schema *string, objectType DatabaseObjectType) ([]string, error)
GetReplaceSql(rd def.RowData) (string, error)
// SuspendConstraintChecksSQL 返回挂起会话级外键/触发器校验的 SQL(空串表示方言不支持,
// 如 Oracle 需逐约束 DISABLE CONSTRAINT)。会话级语句,调用方须先钉单连接
// (PinSingleConnection)保证开关与后续写入同连接
SuspendConstraintChecksSQL() string
// ResumeConstraintChecksSQL 返回恢复校验的 SQL(空串表示不支持),与 SuspendConstraintChecksSQL 成对
ResumeConstraintChecksSQL() string
// GetWriteCapability 返回该方言的批量写入能力(多行 VALUES 支持、绑定参数上限、冲突列要求),
// 调用方按能力决策语句形态,不感知具体方言
GetWriteCapability() WriteCapability
}
func GetSqlDialect ¶
func GetSqlDialect(dbtype string, dbSubType ...string) (SQLDialect, bool)
type SQLParseError ¶
type SQLParseError struct {
// Message 错误消息
Message string
// LineNum 发生错误的行号
LineNum int
// LineText 发生错误的行文本
LineText string
// ErrorType 错误类型
ErrorType string
}
SQLParseError 定义了SQL解析过程中的错误 提供详细的错误信息,包括行号和错误类型
type SQLType ¶
type SQLType interface {
// AsInt 返回 SQL 类型的整数表示
AsInt() int
// String 返回 SQL 类型的字符串表示
String() string
// IsBlock 判断该 SQL 类型是否为块级语句(如存储过程、函数等)
IsBlock() bool
// IsNull 判断该 SQL 类型是否为空
IsNull() bool
}
SQLType 定义了 SQL 类型的接口 实现此接口的类型可以表示不同的 SQL 语句类型(如 SELECT, INSERT 等)
type SchemaObjects ¶ added in v1.1.0
type SchemaObjects struct {
Tables []string `json:"tables"`
Views []string `json:"views"`
Functions []string `json:"functions"`
Procedures []string `json:"procedures"`
}
SchemaObjects schema 内对象清单(表/视图/函数/过程,一次往返枚举)。
type SchemaSummary ¶ added in v1.1.0
type SchemaSummary struct {
Name string `json:"name"`
TableCount int `json:"tableCount"` // 表数量(视图/函数等不计入)
}
SchemaSummary 库内 schema 概要(名 + 表计数,分级元数据一次往返)。
type SortFuncName ¶
type SortFuncName string
const ( FuncNameSortTables SortFuncName = "SortTables" FuncNameSortFunctions SortFuncName = "SortFunctions" FuncNameSortProcedures SortFuncName = "SortProcedures" )
type SortedSqlFunc ¶
type SortedSqlFunc func(cli def.DatabaseClient, names []string) ([]*SqlContent, error)
type SqlContent ¶
type TraceWord ¶
type TraceWord struct {
// Key 表示当前关键字
Key string
// Skip 表示跳过的字符数
Skip int
// NextKeys 表示可能的下一个关键字及其对应的 SQL 类型
NextKeys map[string]SQLType
}
TraceWord 用于跟踪关键字的结构体 在解析 SQL 语句时用于识别和处理特定的关键字
type WriteCapability ¶ added in v1.2.0
type WriteCapability struct {
// MultiRowInsertValues 报告 INSERT ... VALUES 子句是否支持一次写多行
// (含 REPLACE/upsert 语义的等价多值形式,如 REPLACE INTO t (...) VALUES (...),(...))
MultiRowInsertValues bool
// MaxBindParams 单条语句绑定参数个数上限(0 表示无已知硬限制)。
// 多值语句总参数量为 行数×列数,调用方据此对批量分块
MaxBindParams int
// ReplaceNeedsConflictColumns 报告 REPLACE 语义写入是否需要显式冲突列
// (如 PG ON CONFLICT (...) / Oracle MERGE ON;MySQL REPLACE、SQLite INSERT OR REPLACE
// 依赖唯一键自动判定,无需冲突列)
ReplaceNeedsConflictColumns bool
// UpsertNeedsConflictColumns 报告 UPSERT 语义写入是否需要显式冲突列
// (如 PG/SQLite ON CONFLICT (...) / Oracle MERGE ON;MySQL ON DUPLICATE KEY UPDATE 依赖唯一键自动判定)
UpsertNeedsConflictColumns bool
}
WriteCapability 描述方言对批量写入(INSERT/REPLACE/upsert)的能力边界。 调用方按能力决策语句形态(是否合成多行 VALUES、按绑定参数上限分块、是否需要显式冲突列), 方言自描述、调用方不感知具体方言类型
func DefaultWriteCapability ¶ added in v1.2.0
func DefaultWriteCapability() WriteCapability
DefaultWriteCapability 返回方言未注册时的保守回退:不支持多行、不自动补冲突列(与历史逐行行为一致)