Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions internal/mycli/client_side_statement_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ type clientSideStatementDescription struct {
Note string
}

// fuzzyCompletionType represents what kind of candidates to provide for argument completion.
type fuzzyCompletionType int

const (
fuzzyCompleteDatabase fuzzyCompletionType = iota + 1
fuzzyCompleteVariable
fuzzyCompleteTable
)

func (t fuzzyCompletionType) String() string {
switch t {
case fuzzyCompleteDatabase:
return "database"
case fuzzyCompleteVariable:
return "variable"
case fuzzyCompleteTable:
return "table"
default:
return fmt.Sprintf("unhandled fuzzyCompletionType: %d", t)
}
}

// fuzzyArgCompletion defines how to detect and complete an argument for a client-side statement.
type fuzzyArgCompletion struct {
// PrefixPattern matches partial input and captures the argument being typed in group 1.
PrefixPattern *regexp.Regexp

// CompletionType specifies what candidates to fetch.
CompletionType fuzzyCompletionType
}

type clientSideStatementDef struct {
// Descriptions represents human-readable descriptions.
// It can be multiple because some clientSideStatementDef represents multiple statements in single pattern.
Expand All @@ -44,6 +75,10 @@ type clientSideStatementDef struct {

// HandleSubmatch holds a handler which converts the result of (*regexp.Regexp).FindStringSubmatch() to Statement.
HandleSubmatch func(matched []string) (Statement, error)

// Completion defines optional fuzzy argument completion for this statement.
// When non-nil, each entry's PrefixPattern is tried against partial input to detect argument completion.
Completion []fuzzyArgCompletion
}

var schemaObjectsReStr = stringsiter.Join("|", hiter.Map(func(s string) string {
Expand Down Expand Up @@ -81,6 +116,10 @@ var clientSideStatementDefs = []*clientSideStatementDef{
HandleSubmatch: func(matched []string) (Statement, error) {
return &UseStatement{Database: unquoteIdentifier(matched[1]), Role: unquoteIdentifier(matched[2])}, nil
},
Completion: []fuzzyArgCompletion{{
PrefixPattern: regexp.MustCompile(`(?i)^\s*USE\s+(\S*)$`),
CompletionType: fuzzyCompleteDatabase,
}},
},
{
Descriptions: []clientSideStatementDescription{
Expand All @@ -107,6 +146,10 @@ var clientSideStatementDefs = []*clientSideStatementDef{
HandleSubmatch: func(matched []string) (Statement, error) {
return &DropDatabaseStatement{DatabaseId: unquoteIdentifier(matched[1])}, nil
},
Completion: []fuzzyArgCompletion{{
PrefixPattern: regexp.MustCompile(`(?i)^\s*DROP\s+DATABASE\s+(\S*)$`),
CompletionType: fuzzyCompleteDatabase,
}},
},
{
Descriptions: []clientSideStatementDescription{
Expand Down Expand Up @@ -160,6 +203,10 @@ var clientSideStatementDefs = []*clientSideStatementDef{
schema, table := extractSchemaAndName(unquoteIdentifier(matched[1]))
return &ShowColumnsStatement{Schema: schema, Table: table}, nil
},
Completion: []fuzzyArgCompletion{{
PrefixPattern: regexp.MustCompile(`(?i)^\s*SHOW\s+COLUMNS\s+FROM\s+(\S*)$`),
CompletionType: fuzzyCompleteTable,
}},
},
{
Descriptions: []clientSideStatementDescription{
Expand All @@ -173,6 +220,10 @@ var clientSideStatementDefs = []*clientSideStatementDef{
schema, table := extractSchemaAndName(unquoteIdentifier(matched[1]))
return &ShowIndexStatement{Schema: schema, Table: table}, nil
},
Completion: []fuzzyArgCompletion{{
PrefixPattern: regexp.MustCompile(`(?i)^\s*SHOW\s+(?:INDEX|INDEXES|KEYS)\s+FROM\s+(\S*)$`),
CompletionType: fuzzyCompleteTable,
}},
},
{
Descriptions: []clientSideStatementDescription{
Expand Down Expand Up @@ -357,6 +408,10 @@ var clientSideStatementDefs = []*clientSideStatementDef{
schema, table := extractSchemaAndName(unquoteIdentifier(matched[1]))
return &TruncateTableStatement{Schema: schema, Table: table}, nil
},
Completion: []fuzzyArgCompletion{{
PrefixPattern: regexp.MustCompile(`(?i)^\s*TRUNCATE\s+TABLE\s+(\S*)$`),
CompletionType: fuzzyCompleteTable,
}},
},
// EXPLAIN & EXPLAIN ANALYZE
{
Expand Down Expand Up @@ -741,6 +796,10 @@ var clientSideStatementDefs = []*clientSideStatementDef{
HandleSubmatch: func(matched []string) (Statement, error) {
return &SetStatement{VarName: matched[1], Value: matched[2]}, nil
},
Completion: []fuzzyArgCompletion{{
PrefixPattern: regexp.MustCompile(`(?i)^\s*SET\s+([^\s=]*)$`),
CompletionType: fuzzyCompleteVariable,
}},
},
{
Descriptions: []clientSideStatementDescription{
Expand Down Expand Up @@ -777,6 +836,10 @@ var clientSideStatementDefs = []*clientSideStatementDef{
HandleSubmatch: func(matched []string) (Statement, error) {
return &ShowVariableStatement{VarName: matched[1]}, nil
},
Completion: []fuzzyArgCompletion{{
PrefixPattern: regexp.MustCompile(`(?i)^\s*SHOW\s+VARIABLE\s+(\S*)$`),
CompletionType: fuzzyCompleteVariable,
}},
},
// Query Parameter
{
Expand Down
Loading