featurebase/sql3/planner/opalterdatabase.go
Travis Turner 87011e4294
CLI: make it more like psql (#2235)
* 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
2023-02-14 09:23:51 -06:00

105 lines
2.6 KiB
Go

// Copyright 2021 Molecula Corp. All rights reserved.
package planner
import (
"context"
"fmt"
"github.com/featurebasedb/featurebase/v3/dax"
"github.com/featurebasedb/featurebase/v3/sql3"
"github.com/featurebasedb/featurebase/v3/sql3/parser"
"github.com/featurebasedb/featurebase/v3/sql3/planner/types"
)
// PlanOpAlterDatabase plan operator to alter a database.
type PlanOpAlterDatabase struct {
planner *ExecutionPlanner
database *dax.Database
operation alterOperation
option parser.DatabaseOption
warnings []string
}
func NewPlanOpAlterDatabase(p *ExecutionPlanner, db *dax.Database, operation alterOperation, option parser.DatabaseOption) *PlanOpAlterDatabase {
return &PlanOpAlterDatabase{
planner: p,
database: db,
operation: operation,
option: option,
warnings: make([]string, 0),
}
}
func (p *PlanOpAlterDatabase) Plan() map[string]interface{} {
result := make(map[string]interface{})
result["_op"] = fmt.Sprintf("%T", p)
result["databaseName"] = p.database.Name
result["operation"] = p.operation
result["option"] = p.option
return result
}
func (p *PlanOpAlterDatabase) AddWarning(warning string) {
p.warnings = append(p.warnings, warning)
}
func (p *PlanOpAlterDatabase) Warnings() []string {
return p.warnings
}
func (p *PlanOpAlterDatabase) String() string {
return ""
}
func (p *PlanOpAlterDatabase) Schema() types.Schema {
return types.Schema{}
}
func (p *PlanOpAlterDatabase) Children() []types.PlanOperator {
return []types.PlanOperator{}
}
func (p *PlanOpAlterDatabase) Iterator(ctx context.Context, row types.Row) (types.RowIterator, error) {
return &alterDatabaseRowIter{
planner: p.planner,
database: p.database,
operation: p.operation,
option: p.option,
}, nil
}
func (p *PlanOpAlterDatabase) WithChildren(children ...types.PlanOperator) (types.PlanOperator, error) {
return nil, nil
}
type alterDatabaseRowIter struct {
planner *ExecutionPlanner
database *dax.Database
operation alterOperation
option parser.DatabaseOption
}
var _ types.RowIterator = (*alterDatabaseRowIter)(nil)
func (i *alterDatabaseRowIter) Next(ctx context.Context) (types.Row, error) {
switch i.operation {
case alterOpSet:
var optName string
var optValue string
switch v := i.option.(type) {
case *parser.UnitsOption:
e := v.Expr.(*parser.IntegerLit)
optName = dax.DatabaseOptionWorkersMin
optValue = e.Value
default:
return nil, sql3.NewErrInvalidDatabaseOption(0, 0, i.option.String())
}
if err := i.planner.schemaAPI.SetDatabaseOption(ctx, i.database.ID, optName, optValue); err != nil {
return nil, err
}
}
return nil, types.ErrNoMoreRows
}