mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-08-28 02:44:59 +00:00
* Refactor CLI to mimic psql's meta-commands
This PR adds support for meta-commands (also known as "backslash
commands") like those in psql, Postgres's CLI. Only a few meta-commands
are currently implemented, but this was meant to demonstrate how we
could use something like `\i file.csv` to insert local files into SQL
statements.
* Meta-commands: \file and \include
The initial implementation used `\i` as a streaming file handle.
This commit changes that to `\file`, and then implements `\i` (or
`\include`) as handling multiple sql commands.
* Add meta-command "help" (\?)
This is basically a copy of the psql help output, but includes only
those options we currently support.
* Add support for \o [file], and \timing
The \o meta-command writes query output to a file.
The \timing meta-command turns on/off the timing display sent to stdout.
* Add meta-commands: \l (show databases) and \dt (show tables)
* Add meta-command: \watch [period]
* Update meta-command \connect to take database name instead of ID
* Add support for \echo, \qecho, and \warn
This commit contains an known issue in that the `-n` option will exclude
the line feed, but if the output is the terminal, the readline package
clobbers any content on the current line (i.e. anything without a line
feed). That will need to be addressed at some point.
* Add support for \w [FILE] (write query buffer to file)
* Add SchemaAPI no-op implementation
* Refactor query handler to align with /sql and /databases endpoints
We want to standardize on:
/sql
/databases/{databaseID}/sql
* Add CLI support for expanded, border, tuples_only (and pset)
* Add help text for \pset and \t
119 lines
3 KiB
Go
119 lines
3 KiB
Go
package cli
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// splitter is a line splitter which splits a line into queryParts and
|
|
// metaCommands. It may not be necessary to have this be a separate struct since
|
|
// it contains no members and just has the one `split()` method, but here we
|
|
// are.
|
|
type splitter struct{}
|
|
|
|
func newSplitter() *splitter {
|
|
return &splitter{}
|
|
}
|
|
|
|
// split splits the given line into queryParts and metaCommands.
|
|
// If a metaCommand is found, everything after that is considered either arguments to that
|
|
// metaCommand, or additional metaCommands. In other words, queryParts can not follow
|
|
// metaCommands in the same line.
|
|
//
|
|
// A line can contain any of the following patterns:
|
|
// 1- [queryParts...]: "select * from tbl; select"
|
|
// 2- [metaCommands...]: "\! pwd \q"
|
|
// 3- [queryParts...][metaCommands...]: "select * from \i file.sql"
|
|
func (s *splitter) split(line string) ([]queryPart, []metaCommand, error) {
|
|
// Look for a meta command
|
|
parts := strings.SplitN(line, `\`, 2)
|
|
|
|
switch len(parts) {
|
|
case 1:
|
|
// slice of queryParts (pattern 1)
|
|
if qps, err := s.splitQueryParts(strings.TrimSpace(parts[0])); err != nil {
|
|
return nil, nil, errors.Wrap(err, "splitting query parts")
|
|
} else {
|
|
return qps, nil, nil
|
|
}
|
|
case 2:
|
|
// slice of parts + slice of meta commands (pattern 3)
|
|
// or
|
|
// slice of meta commands (pattern 2)
|
|
qps, err := s.splitQueryParts(strings.TrimSpace(parts[0]))
|
|
if err != nil {
|
|
return nil, nil, errors.Wrap(err, "splitting query parts")
|
|
}
|
|
|
|
mcs, err := s.splitMetaCommands(strings.TrimSpace(parts[1]))
|
|
if err != nil {
|
|
return nil, nil, errors.Wrap(err, "splitting meta commands")
|
|
}
|
|
|
|
return qps, mcs, nil
|
|
}
|
|
|
|
return nil, nil, nil
|
|
}
|
|
|
|
func (s *splitter) splitQueryParts(line string) ([]queryPart, error) {
|
|
if line == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
// Look for a termination character;
|
|
parts := strings.Split(line, terminationChar)
|
|
|
|
if len(parts) == 1 {
|
|
part0 := strings.TrimSpace(parts[0])
|
|
return []queryPart{
|
|
newPartRaw(part0),
|
|
}, nil
|
|
}
|
|
|
|
qps := make([]queryPart, 0)
|
|
for i := range parts {
|
|
part := strings.TrimSpace(parts[i])
|
|
if part == "" {
|
|
// If the line starts with a ";", treat it as a terminator for a
|
|
// previous line.
|
|
if i == 0 {
|
|
qps = append(qps, &partTerminator{})
|
|
}
|
|
continue
|
|
}
|
|
qps = append(qps, newPartRaw(part))
|
|
if i < len(parts)-1 {
|
|
qps = append(qps, &partTerminator{})
|
|
}
|
|
}
|
|
|
|
return qps, nil
|
|
}
|
|
|
|
func (s *splitter) splitMetaCommands(in string) ([]metaCommand, error) {
|
|
parts := strings.Split(in, `\`)
|
|
if len(parts) == 1 {
|
|
mc, err := splitMetaCommand(parts[0])
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "splitting meta command: %s", parts[0])
|
|
}
|
|
return []metaCommand{mc}, nil
|
|
}
|
|
|
|
mcs := make([]metaCommand, 0)
|
|
for i := range parts {
|
|
part := strings.TrimSpace(parts[i])
|
|
if part == "" {
|
|
continue
|
|
}
|
|
mc, err := splitMetaCommand(part)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(err, "splitting meta command: %s", part)
|
|
}
|
|
mcs = append(mcs, mc)
|
|
}
|
|
|
|
return mcs, nil
|
|
}
|