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 SortFuncName
- type SortedSqlFunc
- type SqlContent
- type TraceWord
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)
// GetObjects 枚举指定库/schema 内某类对象的名称列表(与 GetTables 对称的元数据能力)。
// TABLE 类型应使用 GetTables;不支持的对象类型返回错误
GetObjects(cli def.DatabaseClient, dbName string, schema *string, objectType DatabaseObjectType) ([]string, error)
GetReplaceSql(rd def.RowData) (string, error)
}
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 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)