Documentation
¶
Index ¶
- func ExportRowsToXlsx(ctx context.Context, conn *sql.Conn, selectSQL string, w io.Writer) error
- func GetObjectSource(ctx context.Context, conn *sql.Conn, driver asset_entity.DatabaseDriver, ...) (string, error)
- func QuoteIdent(name string, driver asset_entity.DatabaseDriver) string
- func QuoteTableRef(database, table string, driver asset_entity.DatabaseDriver) string
- func SQLQuote(value string) string
- type ColumnMeta
- type DatabaseObjects
- type ForeignKeyMeta
- type IndexMeta
- type ObjectMeta
- type OpenTableResult
- type ParsedXlsxTable
- type SQLSession
- type SQLTx
- type TableColumnRule
- type TableImportBatchError
- type TableImportBatchRequest
- type TableImportBatchResult
- type TableMetadata
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExportRowsToXlsx ¶ added in v1.10.0
ExportRowsToXlsx runs selectSQL on conn and streams the result set into an XLSX workbook written to w. It uses excelize's StreamWriter so rows are flushed incrementally rather than held in memory, keeping large exports bounded. The first sheet row holds the column names.
func GetObjectSource ¶ added in v1.10.0
func GetObjectSource(ctx context.Context, conn *sql.Conn, driver asset_entity.DatabaseDriver, database, objType, name, table string) (string, error)
GetObjectSource returns the source / definition of a view / routine / trigger for read-only display. SQLite reads sqlite_master.sql; the others use their catalog definition function. table is only consulted for PostgreSQL triggers (see ObjectMeta.Table); other drivers and object types ignore it.
func QuoteIdent ¶ added in v1.6.0
func QuoteIdent(name string, driver asset_entity.DatabaseDriver) string
QuoteIdent 对单个 SQL 标识符按 driver 加引号。 MySQL 反引号;PostgreSQL/SQLite 双引号;MSSQL 方括号。 标识符里同名转义字符按各方言规则成对转义。
行为与前端 frontend/src/lib/tableSql.ts:quoteIdent 等价,移到后端是为了 OpenTable 等服务端拼装 SQL 时复用,不再依赖前端传 SQL 字符串。
func QuoteTableRef ¶ added in v1.6.0
func QuoteTableRef(database, table string, driver asset_entity.DatabaseDriver) string
QuoteTableRef 把 db + table 拼成限定表引用。 MySQL: `db`.`table`。 PostgreSQL: 忽略 database 参数,table 可以是裸表名或 "schema.table" 形式。 SQLite: database 表示 schema(main/temp/ATTACH 名称),有值时保留 schema 限定。 MSSQL: 与 PostgreSQL 一致,忽略 database 参数。连接已通过 DSN 的 database= 限定了 catalog,所以这里按 schema.table 加方括号即可(裸表名 → [table]、 "dbo.users" → [dbo].[users])。不能拼成两段式 [db].[table]——T-SQL 会把它 解释为 schema=db、object=table,导致 "Invalid object name"。
Types ¶
type ColumnMeta ¶ added in v1.10.0
type ColumnMeta struct {
Name string `json:"name"`
Type string `json:"type"`
Nullable bool `json:"nullable"`
PrimaryKey bool `json:"primaryKey"`
}
ColumnMeta describes one column for the object browser.
type DatabaseObjects ¶ added in v1.10.0
type DatabaseObjects struct {
Views []ObjectMeta `json:"views"`
Procedures []ObjectMeta `json:"procedures"`
Functions []ObjectMeta `json:"functions"`
Triggers []ObjectMeta `json:"triggers"`
}
DatabaseObjects groups non-table schema objects for the object browser.
func ListDatabaseObjects ¶ added in v1.10.0
func ListDatabaseObjects(ctx context.Context, conn *sql.Conn, driver asset_entity.DatabaseDriver, database string) (*DatabaseObjects, error)
ListDatabaseObjects returns views / procedures / functions / triggers for the database. SQLite has no stored procedures/functions, so those groups stay empty.
type ForeignKeyMeta ¶ added in v1.10.0
type ForeignKeyMeta struct {
Name string `json:"name"`
Column string `json:"column"`
ReferencedTable string `json:"referencedTable"`
ReferencedColumn string `json:"referencedColumn"`
}
ForeignKeyMeta describes one foreign-key column mapping.
type IndexMeta ¶ added in v1.10.0
type IndexMeta struct {
Name string `json:"name"`
Columns []string `json:"columns"`
Unique bool `json:"unique"`
}
IndexMeta describes one index.
type ObjectMeta ¶ added in v1.10.0
type ObjectMeta struct {
Name string `json:"name"`
Type string `json:"type"`
Table string `json:"table,omitempty"`
}
ObjectMeta names a schema object (view / routine / trigger). Table is set only for PostgreSQL triggers, whose names are unique per table (not per schema), so the source lookup needs the owning table to resolve them.
type OpenTableResult ¶ added in v1.6.0
type OpenTableResult struct {
Columns []string `json:"columns"`
ColumnTypes map[string]string `json:"columnTypes"`
ColumnRules []TableColumnRule `json:"columnRules"`
PrimaryKeys []string `json:"primaryKeys"`
TotalCount int64 `json:"totalCount"`
FirstPage []map[string]any `json:"firstPage"`
PageSize int `json:"pageSize"`
}
OpenTableResult 是 OpenTable 一次返回的全部首屏数据。 把原来前端 4 次 ExecuteSQL 合并为一次响应。
func OpenTable ¶ added in v1.6.0
func OpenTable(ctx context.Context, db *sql.DB, driver asset_entity.DatabaseDriver, database, table string, pageSize int) (*OpenTableResult, error)
OpenTable 在同一条 *sql.Conn 上顺序执行 4 条 SQL 并组装结果。 调用方负责传入已通过缓存复用的 *sql.DB,本函数自己 db.Conn(ctx) 一次。
4 条查询:
- primary keys (SHOW KEYS / PG information_schema)
- columns + types + rules (SHOW COLUMNS / PG information_schema)
- SELECT COUNT(*) FROM <table>
- SELECT * FROM <table> LIMIT pageSize OFFSET 0
任一步失败立即返回错误;不做"部分成功"的容错,前端按整次失败处理。
type ParsedXlsxTable ¶ added in v1.10.0
ParsedXlsxTable mirrors the frontend ParsedDelimitedTable shape ({ headers, rows }) so a parsed sheet can flow straight into the existing import preview/build pipeline without a separate frontend parser.
func ParseXlsx ¶ added in v1.10.0
func ParseXlsx(r io.Reader, sheet string) (*ParsedXlsxTable, error)
ParseXlsx reads an XLSX workbook from r and returns the named sheet (or the first sheet when sheet is empty) as a header row plus string rows. Data rows are padded/truncated to the header width so ragged sheets stay rectangular.
type SQLSession ¶
type SQLSession interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
BeginTx(ctx context.Context, opts *sql.TxOptions) (SQLTx, error)
}
SQLSession is a single database session. MySQL FOREIGN_KEY_CHECKS is session-scoped, so imports that toggle it must use one SQL session from start to restore.
func NewSQLSession ¶
func NewSQLSession(conn *sql.Conn) SQLSession
NewSQLSession adapts database/sql's Conn to the import service interface.
type SQLTx ¶
type SQLTx interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
Commit() error
Rollback() error
}
SQLTx is the small transaction surface needed by table imports.
type TableColumnRule ¶ added in v1.6.0
type TableColumnRule struct {
Name string `json:"name"`
Nullable bool `json:"nullable"`
HasDefault bool `json:"hasDefault"`
AutoIncrement bool `json:"autoIncrement"`
}
TableColumnRule 描述一列的约束,用于前端 INSERT 校验。 与 frontend/src/components/query/TableDataTab.tsx 现有结构对齐。
type TableImportBatchError ¶
type TableImportBatchError struct {
Index int `json:"index"`
Statement string `json:"statement"`
Message string `json:"message"`
}
TableImportBatchError captures a failed statement without leaking connection setup failures into row logs.
type TableImportBatchRequest ¶
type TableImportBatchRequest struct {
Statements []string `json:"statements"`
Mode string `json:"mode"`
ContinueOnError bool `json:"continueOnError"`
DisableForeignKeyChecks bool `json:"disableForeignKeyChecks"`
Tables []string `json:"tables,omitempty"` // 仅 MSSQL 需要,已 Quote 过的表引用
}
TableImportBatchRequest describes a prepared table import batch.
type TableImportBatchResult ¶
type TableImportBatchResult struct {
Processed int `json:"processed"`
Added int64 `json:"added"`
Updated int64 `json:"updated"`
Deleted int64 `json:"deleted"`
Error int `json:"error"`
RolledBack bool `json:"rolledBack"`
Errors []TableImportBatchError `json:"errors"`
}
TableImportBatchResult returns aggregate counters for the import summary.
func RunTableImportBatch ¶
func RunTableImportBatch( ctx context.Context, session SQLSession, driver asset_entity.DatabaseDriver, request TableImportBatchRequest, ) (result *TableImportBatchResult, err error)
RunTableImportBatch executes an import batch on one database session.
type TableMetadata ¶ added in v1.10.0
type TableMetadata struct {
Columns []ColumnMeta `json:"columns"`
Indexes []IndexMeta `json:"indexes"`
ForeignKeys []ForeignKeyMeta `json:"foreignKeys"`
}
TableMetadata is the full object-browser detail for a single table.
func GetTableMetadata ¶ added in v1.10.0
func GetTableMetadata(ctx context.Context, conn *sql.Conn, driver asset_entity.DatabaseDriver, database, table string) (*TableMetadata, error)
GetTableMetadata returns columns + indexes + foreign keys for a table. Columns reuse the same per-driver query as OpenTable; indexes / foreign keys are best-effort per driver (SQLite/MySQL are precise; PostgreSQL/MSSQL pull from the catalog with the common cases covered).