Documentation
¶
Index ¶
- func EnforceReadOnly(sql string, unsafeAllowWrite bool) *errors.XError
- func IsReadOnlySQL(sql string) (bool, string)
- func Register(name string, d Driver)
- func RegisteredNames() []string
- type Column
- type ConnOptions
- type Dialer
- type Driver
- type ForeignKey
- type Index
- type QueryOptions
- type QueryResult
- type SQLToken
- type SchemaDriver
- type SchemaInfo
- type SchemaOptions
- type Table
- type TokenType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsReadOnlySQL ¶
IsReadOnlySQL 做保守判定:默认拒绝;仅允许文档明确的只读语句。 使用词法分析而非简单字符串匹配,正确处理字符串、注释。 解析失败/多语句/包含禁止关键字时一律返回 false。
func RegisteredNames ¶
func RegisteredNames() []string
Types ¶
type Column ¶ added in v0.0.8
type Column struct {
Name string `json:"name" yaml:"name"`
Type string `json:"type" yaml:"type"` // 数据类型,如 varchar(255)、bigint
Nullable bool `json:"nullable" yaml:"nullable"` // 是否允许 NULL
Default string `json:"default,omitempty" yaml:"default,omitempty"` // 默认值
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"` // 列注释
PrimaryKey bool `json:"primary_key" yaml:"primary_key"` // 是否为主键
}
Column 列信息
type ConnOptions ¶
type ConnOptions struct {
DSN string // 原生 DSN(优先级最高)
Host string
Port int
User string
Password string
Database string
Params map[string]string // 额外参数
Dialer Dialer // 自定义 dialer(如 SSH tunnel)
}
ConnOptions 是通用连接参数(由 config/CLI/ENV 合并而来)。
type Driver ¶
type Driver interface {
// Open 返回 *sql.DB;由具体 driver 实现连接参数解析。
Open(ctx context.Context, opts ConnOptions) (*sql.DB, *errors.XError)
}
Driver 是数据库驱动的最小抽象。
type ForeignKey ¶ added in v0.0.8
type ForeignKey struct {
Name string `json:"name" yaml:"name"` // 外键名
Columns []string `json:"columns" yaml:"columns"` // 本表列
ReferencedTable string `json:"referenced_table" yaml:"referenced_table"` // 引用表
ReferencedColumns []string `json:"referenced_columns" yaml:"referenced_columns"` // 引用列
}
ForeignKey 外键信息
type Index ¶ added in v0.0.8
type Index struct {
Name string `json:"name" yaml:"name"` // 索引名
Columns []string `json:"columns" yaml:"columns"` // 索引列
Unique bool `json:"unique" yaml:"unique"` // 是否唯一索引
Primary bool `json:"primary" yaml:"primary"` // 是否主键索引
}
Index 索引信息
type QueryOptions ¶
type QueryOptions struct {
UnsafeAllowWrite bool // 允许写操作(绕过只读保护)
DBType string // 数据库类型:mysql 或 pg
}
QueryOptions 包含查询执行的选项。
type QueryResult ¶
type QueryResult struct {
Columns []string `json:"columns" yaml:"columns"`
Rows []map[string]any `json:"rows" yaml:"rows"`
}
QueryResult 是通用查询结果。
func Query ¶
func Query(ctx context.Context, db *sql.DB, query string, opts QueryOptions) (*QueryResult, *errors.XError)
Query 执行 SQL 查询并返回结果。 当 opts.UnsafeAllowWrite=false 时,会启用双重只读保护: 1. SQL 语句静态分析(客户端) 2. 数据库事务级只读模式(服务端) 当 opts.UnsafeAllowWrite=true 时,绕过所有只读保护。
func (*QueryResult) ToTableData ¶ added in v0.0.5
func (r *QueryResult) ToTableData() (columns []string, rows []map[string]any, ok bool)
ToTableData 实现 output.TableFormatter 接口,支持无 JSON 编解码的表格输出。
type SchemaDriver ¶ added in v0.0.8
type SchemaDriver interface {
Driver
// DumpSchema 导出数据库结构
DumpSchema(ctx context.Context, db *sql.DB, opts SchemaOptions) (*SchemaInfo, *errors.XError)
}
SchemaDriver schema 导出接口 Driver 可选择实现此接口以支持 schema 导出
type SchemaInfo ¶ added in v0.0.8
type SchemaInfo struct {
Database string `json:"database" yaml:"database"`
Tables []Table `json:"tables" yaml:"tables"`
}
SchemaInfo 数据库 schema 信息
func DumpSchema ¶ added in v0.0.8
func DumpSchema(ctx context.Context, driverName string, db *sql.DB, opts SchemaOptions) (*SchemaInfo, *errors.XError)
DumpSchema 导出数据库结构 会检查 driver 是否实现了 SchemaDriver 接口
func (*SchemaInfo) ToSchemaData ¶ added in v0.0.8
func (s *SchemaInfo) ToSchemaData() (string, []output.SchemaTable, bool)
ToSchemaData 实现 output.SchemaFormatter 接口
type SchemaOptions ¶ added in v0.0.8
SchemaOptions schema 导出选项
type Table ¶ added in v0.0.8
type Table struct {
Schema string `json:"schema" yaml:"schema"` // PostgreSQL schema,MySQL 为数据库名
Name string `json:"name" yaml:"name"` // 表名
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"`
Columns []Column `json:"columns" yaml:"columns"`
Indexes []Index `json:"indexes,omitempty" yaml:"indexes,omitempty"`
ForeignKeys []ForeignKey `json:"foreign_keys,omitempty" yaml:"foreign_keys,omitempty"`
}
Table 表信息