Documentation
¶
Overview ¶
Package query implements the embedded SQL engine that file mode runs against: loaded frames are registered as tables in an in-memory SQLite database and query results come back as new frames. It also provides SQL autocompletion for the query editor (see complete.go).
Index ¶
Constants ¶
const ( KindColumn = "column" KindTable = "table" KindFunction = "function" KindKeyword = "keyword" )
Suggestion kinds.
Variables ¶
This section is empty.
Functions ¶
func DotTable ¶
DotTable resolves the table a "qualifier." token ending at cursor refers to: the qualifier itself, or the table it aliases in a FROM/JOIN clause. It reports ok only when the cursor sits in a dot completion and the table exists in sc.Tables (returning the schema's canonical spelling), so callers can use it to decide which table's columns to fetch.
Types ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine wraps a single in-memory SQLite database. Frames are registered as tables (Register), queried with plain SQL (Query) and dropped again (Unregister). The zero value is not usable; construct with NewEngine.
func (*Engine) Query ¶
Query runs q against the engine and materializes the result as a frame. Storage classes map back to the canonical cell set: INTEGER -> int64, REAL -> float64, TEXT -> string, BLOB -> string, NULL -> nil; columns declared BOOLEAN map their 0/1 values back to bool. Duplicate result column names are de-duplicated (a, a -> a, a_2) because frame columns are addressed by name.
func (*Engine) Register ¶
Register (re)creates a table named name holding the contents of f. An existing table with the same name is replaced. Any name is accepted, including "_" and names containing spaces or punctuation. Duplicate column names within the frame are de-duplicated (a, a -> a, a_2) so the table can be created.
func (*Engine) TableColumns ¶
TableColumns returns the column names of every registered table, keyed by table name, in table-declaration order (via PRAGMA table_info).
func (*Engine) Unregister ¶
Unregister drops the named table. Dropping a table that was never registered is not an error.
type Schema ¶
type Schema struct {
Tables map[string][]string // table name -> column names (nil = unknown yet)
Current []string // columns of the current frame
}
Schema is everything the completer knows about the queryable world: the catalog of tables (with their columns, which may be nil while a live connection is still being interrogated) and the columns of the frame currently under view.
type Suggestion ¶
Suggestion is one completion candidate. Text is the full replacement for the token being completed (functions carry a trailing "("), Kind is one of "column", "table", "function" or "keyword", and Detail is extra context such as the owning table of a column.
func Suggest ¶
func Suggest(input string, cursor int, sc Schema) []Suggestion
Suggest returns completion candidates for the identifier token ending at cursor (a byte offset) in input, ranked by how relevant each candidate kind is to the token's syntactic position:
- right after FROM/JOIN/INTO/UPDATE/TABLE: table names (also with an empty token, so "from " pops the catalog);
- "name." dot prefix: the columns of that table, resolving FROM/JOIN aliases ("tbl [AS] alias"), also with an empty token;
- after SELECT/WHERE/ON/AND/OR/BY/HAVING/SET or "," or "(": columns first, then functions, then keywords (empty token: columns only);
- anywhere else: columns, then tables, then functions, then keywords; an empty token yields nothing.
Matching is case-insensitive on the token prefix. Keywords and functions follow the typed case (all-lowercase prefix gets lowercase words, any uppercase letter gets uppercase); identifiers are always verbatim.