Help us shape the future of Go! Take the 2026 Go Developer Survey

query

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
const (
	KindColumn   = "column"
	KindTable    = "table"
	KindFunction = "function"
	KindKeyword  = "keyword"
)

Suggestion kinds.

Variables

This section is empty.

Functions

func DotTable

func DotTable(input string, cursor int, sc Schema) (string, bool)

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.

func Keywords

func Keywords() []string

Keywords returns every SQL word known to the completer (keywords and function names, without the "(" suffix), sorted.

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 NewEngine

func NewEngine() (*Engine, error)

NewEngine opens a fresh in-memory database.

func (*Engine) Close

func (e *Engine) Close() error

Close releases the underlying database.

func (*Engine) Query

func (e *Engine) Query(q string) (*data.Frame, error)

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

func (e *Engine) Register(name string, f *data.Frame) error

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

func (e *Engine) TableColumns() (map[string][]string, error)

TableColumns returns the column names of every registered table, keyed by table name, in table-declaration order (via PRAGMA table_info).

func (*Engine) Tables

func (e *Engine) Tables() ([]string, error)

Tables returns the names of all registered tables, sorted.

func (*Engine) Unregister

func (e *Engine) Unregister(name string) error

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

type Suggestion struct {
	Text   string
	Kind   string
	Detail string
}

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.

Jump to

Keyboard shortcuts

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