autocomplete

package
v0.0.0-...-62ccd28 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommonSQLKeywords = []string{
	"SELECT", "FROM", "WHERE", "GROUP BY", "ORDER BY", "HAVING", "LIMIT",
	"JOIN", "LEFT JOIN", "RIGHT JOIN", "INNER JOIN", "FULL JOIN", "CROSS JOIN",
	"ON", "AND", "OR", "NOT", "IN", "EXISTS", "BETWEEN", "LIKE", "IS NULL", "IS NOT NULL",
	"COUNT", "SUM", "AVG", "MIN", "MAX", "DISTINCT", "AS", "WITH", "UNION", "ALL",
	"INSERT", "INTO", "VALUES", "UPDATE", "SET", "DELETE", "CREATE", "TABLE", "VIEW",
	"DROP", "ALTER", "ADD", "COLUMN", "DESC", "ASC", "PARTITION BY", "OVER", "CAST",
	"CASE", "WHEN", "THEN", "ELSE", "END", "COALESCE", "NULLIF", "EXTRACT", "CURRENT_DATE",
	"CURRENT_TIME", "CURRENT_TIMESTAMP", "INTERVAL",
}

Common SQL keywords for autocompletion

Functions

func FetchAndCacheSchema

func FetchAndCacheSchema(profileName string) error

FetchAndCacheSchema fetches schema metadata for the given profile and caches it

func GetContextualSuggestions

func GetContextualSuggestions(query string, cursorPos int, cache *SchemaCache) []string

GetContextualSuggestions returns suggestions based on the SQL query context It analyzes the query and cursor position to provide more relevant suggestions

func StartSchemaCacheUpdater

func StartSchemaCacheUpdater(interval time.Duration, profileName string, logger *zap.Logger) error

StartSchemaCacheUpdater starts a background goroutine that refreshes schema metadata at the specified interval for the given profile.

Types

type AutocompleteHandler

type AutocompleteHandler struct {
	// contains filtered or unexported fields
}

AutocompleteHandler manages SQL autocompletion integration with TUI

func IntegrateWithTUI

func IntegrateWithTUI(app *tview.Application, input *tview.InputField, flex *tview.Flex, profileName string, logger *zap.Logger) (*AutocompleteHandler, error)

IntegrateWithTUI integrates the autocomplete handler with the TUI

func NewAutocompleteHandler

func NewAutocompleteHandler(db *sql.DB, profileName string, app *tview.Application,
	inputField *tview.InputField, logger *zap.Logger) (*AutocompleteHandler, error)

NewAutocompleteHandler creates a new autocomplete handler for the TUI

func (*AutocompleteHandler) HideSuggestions

func (ah *AutocompleteHandler) HideSuggestions()

HideSuggestions hides the suggestion box

func (*AutocompleteHandler) ProcessKey

func (ah *AutocompleteHandler) ProcessKey(event *tcell.EventKey) bool

ProcessKey handles keyboard input for autocompletion

func (*AutocompleteHandler) ShowSuggestions

func (ah *AutocompleteHandler) ShowSuggestions()

ShowSuggestions displays the suggestion box

func (*AutocompleteHandler) Stop

func (ah *AutocompleteHandler) Stop()

Stop should be called when closing the application

func (*AutocompleteHandler) Update

func (ah *AutocompleteHandler) Update(text string, cursorPos int)

Update should be called when the input text changes

type AutocompleteService

type AutocompleteService struct {
	// contains filtered or unexported fields
}

AutocompleteService provides SQL autocompletion functionality

func NewAutocompleteService

func NewAutocompleteService(db *sql.DB, cacheDir string, logger *zap.Logger) (*AutocompleteService, error)

NewAutocompleteService creates a new autocomplete service

func (*AutocompleteService) BoostSuggestion

func (ac *AutocompleteService) BoostSuggestion(suggestion Suggestion)

BoostSuggestion increases the score of a suggestion when it's used

func (*AutocompleteService) GetCompletions

func (ac *AutocompleteService) GetCompletions(sql string, cursorPos int) ([]Suggestion, error)

GetCompletions returns suggestions for the given SQL input and cursor position

func (*AutocompleteService) SetMaxSuggestions

func (ac *AutocompleteService) SetMaxSuggestions(max int)

SetMaxSuggestions sets the maximum number of suggestions to return

func (*AutocompleteService) Start

func (ac *AutocompleteService) Start() error

Start initializes the service and begins background refresh

func (*AutocompleteService) Stop

func (ac *AutocompleteService) Stop()

Stop gracefully shuts down the service

type ColumnMetadata

type ColumnMetadata struct {
	Name     string `json:"name"`
	DataType string `json:"data_type"`
	Table    string `json:"table"`
	Schema   string `json:"schema"`
}

ColumnMetadata represents a column's metadata

type SQLCompletionType

type SQLCompletionType int

SQLCompletion types represent different categories of SQL suggestions

const (
	Keyword SQLCompletionType = iota
	SchemaName
	TableName
	ColumnName
	Function
)

type SchemaCache

type SchemaCache struct {
	// contains filtered or unexported fields
}

SchemaCache manages caching of Trino schema metadata

func NewSchemaCache

func NewSchemaCache(cacheDir string, logger *zap.Logger) (*SchemaCache, error)

NewSchemaCache creates a new schema cache

func (*SchemaCache) BoostWord

func (sc *SchemaCache) BoostWord(word string, boostAmount int) bool

BoostWord increases the score of a word in the trie

func (*SchemaCache) Close

func (sc *SchemaCache) Close() error

Close closes the schema cache and database connection

func (*SchemaCache) GetAllColumns

func (sc *SchemaCache) GetAllColumns() ([]string, error)

GetAllColumns returns all column names from the cache

func (*SchemaCache) GetAllSchemaQualifiedTables

func (sc *SchemaCache) GetAllSchemaQualifiedTables() ([]string, error)

GetAllSchemaQualifiedTables returns all schema-qualified table names (schema.table) from the cache

func (*SchemaCache) GetAllTables

func (sc *SchemaCache) GetAllTables() ([]string, error)

GetAllTables returns all table names from the cache

func (*SchemaCache) GetColumns

func (sc *SchemaCache) GetColumns(schemaName, tableName string) ([]ColumnMetadata, error)

GetColumns returns all column names for a table from the cache

func (*SchemaCache) GetFuzzyMatches

func (sc *SchemaCache) GetFuzzyMatches(prefix string, maxDistance int, limit int) []string

GetFuzzyMatches gets fuzzy-matched suggestions for a prefix

func (*SchemaCache) GetLastRefreshTime

func (sc *SchemaCache) GetLastRefreshTime() time.Time

GetLastRefreshTime returns when the cache was last refreshed

func (*SchemaCache) GetSchemas

func (sc *SchemaCache) GetSchemas() ([]string, error)

GetSchemas returns all schema names from the cache

func (*SchemaCache) GetSuggestions

func (sc *SchemaCache) GetSuggestions(prefix string, limit int) []string

GetSuggestions returns autocomplete suggestions for a given prefix

func (*SchemaCache) GetTables

func (sc *SchemaCache) GetTables(schemaName string) ([]string, error)

GetTables returns all table names for a schema from the cache

func (*SchemaCache) InitializeSQLKeywords

func (sc *SchemaCache) InitializeSQLKeywords() error

Initialize the cache with SQL keywords

func (*SchemaCache) LoadCache

func (sc *SchemaCache) LoadCache() error

LoadCache initializes the trie with data from the cache database

func (*SchemaCache) StoreSchema

func (sc *SchemaCache) StoreSchema(metadata SchemaMetadata) error

StoreSchema stores a schema's metadata in the cache

type SchemaIntrospector

type SchemaIntrospector struct {
	// contains filtered or unexported fields
}

SchemaIntrospector fetches schema metadata from Trino in real-time

func NewSchemaIntrospector

func NewSchemaIntrospector(db *sql.DB, cache *SchemaCache, logger *zap.Logger) *SchemaIntrospector

NewSchemaIntrospector creates a new schema introspector

func (*SchemaIntrospector) GetColumns

func (si *SchemaIntrospector) GetColumns(schemaName, tableName string) ([]ColumnMetadata, error)

GetColumns retrieves all column metadata for a specific table

func (*SchemaIntrospector) GetLastRefreshTime

func (si *SchemaIntrospector) GetLastRefreshTime() time.Time

GetLastRefreshTime returns when the last introspection refresh occurred

func (*SchemaIntrospector) GetSchemas

func (si *SchemaIntrospector) GetSchemas() ([]string, error)

GetSchemas retrieves all schema names from Trino

func (*SchemaIntrospector) GetTables

func (si *SchemaIntrospector) GetTables(schemaName string) ([]string, error)

GetTables retrieves all table names for a specific schema

func (*SchemaIntrospector) RefreshAll

func (si *SchemaIntrospector) RefreshAll() error

RefreshAll refreshes all schema metadata

func (*SchemaIntrospector) RefreshSchema

func (si *SchemaIntrospector) RefreshSchema(schemaName string) error

RefreshSchema refreshes metadata for a specific schema

func (*SchemaIntrospector) SetRefreshInterval

func (si *SchemaIntrospector) SetRefreshInterval(interval time.Duration)

SetRefreshInterval sets how often the background refresh occurs

func (*SchemaIntrospector) StartBackgroundRefresh

func (si *SchemaIntrospector) StartBackgroundRefresh()

StartBackgroundRefresh begins a background goroutine that refreshes schema metadata

func (*SchemaIntrospector) StopBackgroundRefresh

func (si *SchemaIntrospector) StopBackgroundRefresh()

StopBackgroundRefresh stops the background refresh goroutine

type SchemaMetadata

type SchemaMetadata struct {
	Name       string          `json:"name"`
	Tables     []TableMetadata `json:"tables"`
	LastUpdate time.Time       `json:"last_update"`
}

SchemaMetadata represents a complete schema's metadata

type Suggestion

type Suggestion struct {
	Text       string
	Type       SQLCompletionType
	Score      float64 // Higher is better
	Schema     string  // Only for table/column suggestions
	Table      string  // Only for column suggestions
	DetailText string  // Additional context/details
}

Suggestion represents a single autocompletion suggestion

type TableMetadata

type TableMetadata struct {
	Name    string           `json:"name"`
	Schema  string           `json:"schema"`
	Columns []ColumnMetadata `json:"columns"`
}

TableMetadata represents a table's metadata

type Trie

type Trie struct {
	Root *TrieNode
}

Trie is a prefix tree for fast autocompletion lookups

func NewTrie

func NewTrie() *Trie

NewTrie creates a new trie for autocompletion

func (*Trie) BoostWord

func (t *Trie) BoostWord(word string, boostAmount int) bool

BoostWord increases the score of a word when it's used

func (*Trie) GetFuzzyMatches

func (t *Trie) GetFuzzyMatches(prefix string, maxDistance int, limit int) []string

GetFuzzyMatches returns suggestions with fuzzy matching

func (*Trie) GetSuggestions

func (t *Trie) GetSuggestions(prefix string, limit int) []string

GetSuggestions returns a list of suggestions that match the given prefix

func (*Trie) Insert

func (t *Trie) Insert(word string, score int)

Insert adds a word to the trie with an optional score

func (*Trie) Search

func (t *Trie) Search(word string) bool

Search finds exact matches for a word in the trie

type TrieNode

type TrieNode struct {
	Children map[rune]*TrieNode
	IsWord   bool
	Word     string
	Score    int // For ranking suggestions (frequency or recency)
}

TrieNode represents a node in the prefix tree (trie) for autocompletion

Jump to

Keyboard shortcuts

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