query_svc

package
v1.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2026 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExportRowsToXlsx added in v1.10.0

func ExportRowsToXlsx(ctx context.Context, conn *sql.Conn, selectSQL string, w io.Writer) error

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"。

func SQLQuote added in v1.6.0

func SQLQuote(value string) string

SQLQuote 把字符串包成 SQL 字符串字面量,只做单引号转义(' -> ”)。 主要用于 information_schema 查询里需要字面量比较的字段(如 table_schema、 table_name)。更安全的做法是参数化查询,但当前 information_schema 拼接 路径足够窄、SQL 直接落驱动层,这里用字面量转义即可。注意:只防 SQL 注入, 不做语义校验——调用方仍需保证传入的是预期的标识符。

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 条查询:

  1. primary keys (SHOW KEYS / PG information_schema)
  2. columns + types + rules (SHOW COLUMNS / PG information_schema)
  3. SELECT COUNT(*) FROM <table>
  4. SELECT * FROM <table> LIMIT pageSize OFFSET 0

任一步失败立即返回错误;不做"部分成功"的容错,前端按整次失败处理。

type ParsedXlsxTable added in v1.10.0

type ParsedXlsxTable struct {
	Headers []string   `json:"headers"`
	Rows    [][]string `json:"rows"`
}

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL