Documentation
¶
Overview ¶
Package db provides the database driver registry and query execution engine.
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 performs a conservative check: deny by default; only allow explicitly documented read-only statements. Uses lexical analysis instead of simple string matching to correctly handle strings and comments. Returns false on parse failure, multiple statements, or presence of forbidden keywords.
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"` // Data type, e.g. varchar(255), bigint
Nullable bool `json:"nullable" yaml:"nullable"` // Whether NULL is allowed
Default string `json:"default,omitempty" yaml:"default,omitempty"` // Default value
Comment string `json:"comment,omitempty" yaml:"comment,omitempty"` // Column comment
PrimaryKey bool `json:"primary_key" yaml:"primary_key"` // Whether this is a primary key
}
Column represents column information.
type ConnOptions ¶
type ConnOptions struct {
DSN string // Raw DSN (highest priority)
Host string
Port int
User string
Password string
Database string
Params map[string]string // Extra parameters
Dialer Dialer // Custom dialer (e.g. SSH tunnel)
// RegisterCloseHook allows drivers to register cleanup callbacks that should run
// when the owning connection is closed or setup fails.
RegisterCloseHook func(fn func())
}
ConnOptions holds common connection parameters (merged from config/CLI/ENV).
type Driver ¶
type Driver interface {
// Open returns a *sql.DB; connection parameter parsing is handled by each driver.
Open(ctx context.Context, opts ConnOptions) (*sql.DB, *errors.XError)
}
Driver is the minimal abstraction for a database driver.
type ForeignKey ¶ added in v0.0.8
type ForeignKey struct {
Name string `json:"name" yaml:"name"` // Foreign key name
Columns []string `json:"columns" yaml:"columns"` // Local columns
ReferencedTable string `json:"referenced_table" yaml:"referenced_table"` // Referenced table
ReferencedColumns []string `json:"referenced_columns" yaml:"referenced_columns"` // Referenced columns
}
ForeignKey represents foreign key information.
type Index ¶ added in v0.0.8
type Index struct {
Name string `json:"name" yaml:"name"` // Index name
Columns []string `json:"columns" yaml:"columns"` // Indexed columns
Unique bool `json:"unique" yaml:"unique"` // Whether this is a unique index
Primary bool `json:"primary" yaml:"primary"` // Whether this is a primary key index
}
Index represents index information.
type QueryOptions ¶
type QueryOptions struct {
UnsafeAllowWrite bool // Allow write operations (bypass read-only protection)
DBType string // Database type: mysql or pg
}
QueryOptions contains options for query execution.
type QueryResult ¶
type QueryResult struct {
Columns []string `json:"columns" yaml:"columns"`
Rows []map[string]any `json:"rows" yaml:"rows"`
}
QueryResult represents a generic query result.
func Query ¶
func Query(ctx context.Context, db *sql.DB, query string, opts QueryOptions) (*QueryResult, *errors.XError)
Query executes a SQL query and returns the result. When opts.UnsafeAllowWrite is false, dual read-only protection is enabled: 1. SQL statement static analysis (client-side) 2. Database transaction-level read-only mode (server-side) When opts.UnsafeAllowWrite is true, all read-only protections are bypassed.
func (*QueryResult) ToTableData ¶ added in v0.0.5
func (r *QueryResult) ToTableData() (columns []string, rows []map[string]any, ok bool)
ToTableData implements the output.TableFormatter interface for table output without JSON encoding/decoding.
type SchemaDriver ¶ added in v0.0.8
type SchemaDriver interface {
Driver
// DumpSchema exports the database schema.
DumpSchema(ctx context.Context, db *sql.DB, opts SchemaOptions) (*SchemaInfo, *errors.XError)
}
SchemaDriver is the schema export interface. A Driver may optionally implement this interface to support schema export.
type SchemaInfo ¶ added in v0.0.8
type SchemaInfo struct {
Database string `json:"database" yaml:"database"`
Tables []Table `json:"tables" yaml:"tables"`
}
SchemaInfo represents database schema information.
func DumpSchema ¶ added in v0.0.8
func DumpSchema(ctx context.Context, driverName string, db *sql.DB, opts SchemaOptions) (*SchemaInfo, *errors.XError)
DumpSchema exports the database schema. It checks whether the driver implements the SchemaDriver interface.
func (*SchemaInfo) ToSchemaData ¶ added in v0.0.8
func (s *SchemaInfo) ToSchemaData() (string, []output.SchemaTable, bool)
ToSchemaData implements the output.SchemaFormatter interface.
type SchemaOptions ¶ added in v0.0.8
type SchemaOptions struct {
TablePattern string // Table name filter (supports wildcards)
IncludeSystem bool // Whether to include system tables
}
SchemaOptions holds options for schema export.
type Table ¶ added in v0.0.8
type Table struct {
Schema string `json:"schema" yaml:"schema"` // PostgreSQL schema; database name for MySQL
Name string `json:"name" yaml:"name"` // Table 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 represents table information.