featurebase/sql3/planner/opalterdatabase.go
Travis Turner 126be915a9
Support for setting individual DatabaseOptions (#2231)
* Implement Schemar.SetDatabaseOption(option, value string)

This replaces the temporary `SetDatabaseOptions()` method, which
replaced the entire DatabaseOptions struct, with `SetDatabaseOption`
which takes an option/value pair of strings to set.

* Add SetDatabaseOption to controller http handler and client

This commit also:
- renames some `writeLog` to `writelog`
- updates ApplyDirective to call resource.Unlock() on any resources
  being removed from the local worker

* Add Database related methods to SchemaAPI interface

Currently all implementations of this interface are implemented with
"unimplemented" errors on those methods. Next will be to implement the
necessary methods.

* SQL: CREATE DATABASE and SHOW DATABASES

* SQL: DROP DATABASE

* SQL: Add UNITS option to CREATE DATABASE

* SQL: ALTER DATABASE

* User serverlessStorage.Remove[*]Resource instead of resource.Unlock()

* Add WITH keyword to CREATE/ALTER DATABASE

* fix some WITH logic

* linter fixes

* WITH on CREATE DATABASE is not required
2023-02-06 08:50:28 -06:00

107 lines
2.7 KiB
Go

// Copyright 2021 Molecula Corp. All rights reserved.
package planner
import (
"context"
"fmt"
"log"
"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 = "workers-min"
optValue = e.Value
default:
return nil, sql3.NewErrInvalidDatabaseOption(0, 0, i.option.String())
}
log.Printf("DEEBUG: SetDatabaseOption: %s = %s", optName, optValue)
if err := i.planner.schemaAPI.SetDatabaseOption(ctx, i.database.ID, optName, optValue); err != nil {
return nil, err
}
}
return nil, types.ErrNoMoreRows
}