Documentation
¶
Overview ¶
Package sqldb contains SQL plugin helpers that are independent of a specific database driver.
Index ¶
- Constants
- func AnyColumnRedacted(columns, patterns []string) bool
- func AtPlaceholder(n int) string
- func AuditParams(in QueryAudit) map[string]string
- func BoolValue(v any, def bool) bool
- func ColonPlaceholder(n int) string
- func ColumnsArrayField(opts ColumnsField) plugin.Field
- func DDLColumn(spec ColumnSpec) (string, error)
- func DefaultRedactColumnPatterns() []string
- func DisplayBytes(column string, raw []byte) string
- func DisplayValue(column string, value any) any
- func DisplayValues(columns []string, values []any) []any
- func DollarPlaceholder(n int) string
- func DurationValue(v any, def time.Duration) time.Duration
- func FirstKeyword(statement string) string
- func FormatBinaryID(column string, raw []byte) (string, bool)
- func IDLikeColumn(column string) bool
- func IdentifierList(raw string, quote func(string) string) ([]string, error)
- func IdentifierListValue(v any, quote func(string) string) ([]string, error)
- func IsDestructiveStatement(statement string) bool
- func IsReadOnlyStatement(statement string) bool
- func NormalizeJSONValue(value any) ([]byte, error)
- func OptionalIdentifier(raw string) (string, error)
- func ParseDDLColumns(value any) ([]string, error)
- func ParsePatterns(raw string, fallback []string) []string
- func Qualified(schema, name string) string
- func QueryHash(query string) string
- func QuestionPlaceholder(int) string
- func QuoteIdent(s string) string
- func RedactColumn(column string, patterns []string) bool
- func RedactRows(columns []string, rows [][]any, patterns []string) [][]any
- func SafeDefault(s string) bool
- func SafeIdentifier(raw string) (string, error)
- func SafeType(s string) bool
- func SplitStatements(sqlText string) []string
- func TLSConfig(opts TLSOptions) (*tls.Config, error)
- func ValidateRowKey(primaryKey []string, key map[string]any) error
- type ColumnSpec
- type ColumnsField
- type CompletionItem
- type Dialect
- func (d Dialect) Delete(table string, key map[string]any) (string, []any, error)
- func (d Dialect) Insert(table string, values map[string]any) (string, []any, error)
- func (d Dialect) SearchClause(textType string, cols []string, term string, start int) (string, []any)
- func (d Dialect) Update(table string, key, values map[string]any) (string, []any, error)
- type ForeignKey
- type GraphEdge
- type GraphField
- type GraphNode
- type GraphPayload
- type Placeholder
- type QueryAudit
- type QueryRequest
- type QueryResult
- type RowMutation
- type StatementResult
- type TLSOptions
- type TableColumn
Constants ¶
const IdentifierPattern = `^[A-Za-z_][A-Za-z0-9_]{0,62}$`
const RedactedValue = "***"
Variables ¶
This section is empty.
Functions ¶
func AnyColumnRedacted ¶
AnyColumnRedacted reports whether any column matches a redaction pattern. Used to refuse exposing a primary key whose own value is sensitive (api_key, token…) — such tables stay read-only rather than leaking the raw key to the browser.
func AtPlaceholder ¶
AtPlaceholder formats @p1, @p2, … (SQL Server).
func AuditParams ¶
func AuditParams(in QueryAudit) map[string]string
func ColonPlaceholder ¶
ColonPlaceholder formats :1, :2, … (Oracle).
func ColumnsArrayField ¶
func ColumnsArrayField(opts ColumnsField) plugin.Field
ColumnsArrayField builds the create-table "columns" field as a repeatable array of column objects. The submitted value is the same []ColumnSpec shape ParseDDLColumns reads, so handlers bind it unchanged.
func DDLColumn ¶
func DDLColumn(spec ColumnSpec) (string, error)
func DefaultRedactColumnPatterns ¶
func DefaultRedactColumnPatterns() []string
func DisplayBytes ¶
func DisplayValue ¶
func DisplayValues ¶
func DollarPlaceholder ¶
DollarPlaceholder formats $1, $2, … (PostgreSQL, CockroachDB).
func FirstKeyword ¶
func IDLikeColumn ¶
func IdentifierList ¶
IdentifierList parses a comma/whitespace separated list of column identifiers, validates each against the safe-identifier rule, and returns them quoted by the supplied quoter (used to build index column lists across dialects).
func IdentifierListValue ¶
IdentifierListValue parses a column list supplied either as a multiselect array (the route-sourced options path) or a comma-separated string (free text), so a handler accepts both without caring how the form rendered the field.
func IsDestructiveStatement ¶
func IsReadOnlyStatement ¶
func NormalizeJSONValue ¶
func OptionalIdentifier ¶
func ParseDDLColumns ¶
func ParsePatterns ¶
func QuestionPlaceholder ¶
QuestionPlaceholder formats ? (MySQL/MariaDB, ClickHouse).
func QuoteIdent ¶
func RedactColumn ¶
func SafeDefault ¶
func SafeIdentifier ¶
func SplitStatements ¶
func ValidateRowKey ¶
ValidateRowKey ensures a client-supplied key is exactly the table's primary key — same columns, no more, no fewer — so a row mutation can only ever target one identified row and a caller cannot turn an arbitrary column into a WHERE clause that sweeps many rows.
Types ¶
type ColumnSpec ¶
type ColumnsField ¶
type ColumnsField struct {
TypePlaceholder string
// TypeSuggestions are the dialect's common column types; the type field
// becomes a free-text autocomplete offering them (custom types stay typable).
TypeSuggestions []string
Default bool
Primary bool
Unique bool
}
ColumnsField describes which ColumnSpec attributes a dialect's DDL builder honours, so a plugin only exposes sub-fields its handler actually applies.
type CompletionItem ¶
type Dialect ¶
type Dialect struct {
QuoteIdent func(string) string
Placeholder Placeholder
}
Dialect builds parameterized single-row DML for one driver's quoting and placeholder style. The table argument is supplied already quoted/qualified by the caller; QuoteIdent quotes a bare column identifier.
func (Dialect) Delete ¶
Delete builds a DELETE matching the key columns. The key must be non-empty so an editing mistake can never wipe a whole table.
func (Dialect) Insert ¶
Insert builds an INSERT for the given column values. Column order is stable (sorted) so the statement is deterministic and testable.
func (Dialect) SearchClause ¶
func (d Dialect) SearchClause(textType string, cols []string, term string, start int) (string, []any)
SearchClause builds a case-insensitive "contains" filter across cols for the term, binding one placeholder formatted at position start. textType is the dialect's CAST target so non-text columns are searchable (e.g. "TEXT", "CHAR", "NVARCHAR(MAX)", "VARCHAR2(4000)", "String"). Returns ("", nil) when term or cols is empty; unsafe identifiers are skipped.
type ForeignKey ¶
type ForeignKey struct {
Constraint string
ChildSchema string
ChildTable string
ChildColumn string
ParentSchema string
ParentTable string
ParentColumn string
}
ForeignKey is one introspected relationship: a child table column that references a parent table column.
func ForeignKeyFromRow ¶
func ForeignKeyFromRow(r map[string]any) ForeignKey
ForeignKeyFromRow reads a foreign key from an introspection row whose columns are aliased to the standard names, so each dialect only writes its own query.
type GraphField ¶
type GraphField struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Key string `json:"key,omitempty"`
}
GraphField is one column rendered inside an ERD table node. Key marks special columns ("fk" for a foreign key) so the panel can badge them.
type GraphNode ¶
type GraphNode struct {
ID string `json:"id"`
Label string `json:"label"`
Group string `json:"group,omitempty"`
Fields []GraphField `json:"fields,omitempty"`
}
type GraphPayload ¶
GraphPayload is the {nodes, edges} document the generic graph panel renders. Relational schemas reuse the same panel the graph databases use; the optional Fields turn a node into an ERD table box, and the edge field anchors let the panel draw a foreign key from the exact child column to the parent column.
func RelationGraph ¶
func RelationGraph(columns []TableColumn, fks []ForeignKey) GraphPayload
RelationGraph builds an ERD: one node per table (listing its columns), one edge per foreign key, anchored from the child column to the parent column. Tables referenced by a foreign key but absent from columns still get a (fieldless) node so no edge dangles. Labels are schema-qualified only when more than one schema is present, to keep the common single-schema diagram clean.
type Placeholder ¶
Placeholder formats a bind placeholder for the 1-based argument position so the row DML builder stays driver-neutral.
type QueryAudit ¶
type QueryRequest ¶
type QueryResult ¶
type QueryResult struct {
Columns []string `json:"columns"`
Rows [][]any `json:"rows"`
RowCount int64 `json:"rowCount,omitempty"`
ElapsedMS int64 `json:"elapsedMs"`
Statement string `json:"statement,omitempty"`
CommandTag string `json:"commandTag,omitempty"`
Statements []StatementResult `json:"statements,omitempty"`
}
type RowMutation ¶
type RowMutation struct {
Key map[string]any `json:"key,omitempty"`
Values map[string]any `json:"values,omitempty"`
}
RowMutation is the uniform request body for the editable data grid's insert/update/delete routes. Insert sends Values, Update sends Key+Values, Delete sends Key. Keys are the table's identifying columns; the renderer reads their values straight from the row it edited.
type StatementResult ¶
type TLSOptions ¶
type TableColumn ¶
TableColumn is one column of a table, used to build ERD table nodes.
func TableColumnFromRow ¶
func TableColumnFromRow(r map[string]any) TableColumn
TableColumnFromRow reads a column from an introspection row aliased to the standard names (table_schema, table_name, column_name, data_type).