First attempt at a resultset pseudovar
The resultset needs a table set, so rs:new will also assume the current table.
This commit is contained in:
parent
6bf721873b
commit
18ffe85a56
8 changed files with 87 additions and 6 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/lmika/dynamo-browse/internal/dynamo-browse/models/queryexpr"
|
||||
"github.com/lmika/dynamo-browse/internal/dynamo-browse/services/tables"
|
||||
"github.com/pkg/errors"
|
||||
"time"
|
||||
"ucl.lmika.dev/repl"
|
||||
"ucl.lmika.dev/ucl"
|
||||
)
|
||||
|
|
@ -19,11 +20,36 @@ type rsModule struct {
|
|||
|
||||
var rsNewDoc = repl.Doc{
|
||||
Brief: "Creates a new, empty result set",
|
||||
Usage: "[-table NAME]",
|
||||
Detailed: `
|
||||
The result set assumes the details of the current table. If no table is specified,
|
||||
the command will return an error.
|
||||
`,
|
||||
}
|
||||
|
||||
func (rs *rsModule) rsNew(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||
return &ResultSetProxy{
|
||||
RS: &models.ResultSet{},
|
||||
func (rs *rsModule) rsNew(ctx context.Context, args ucl.CallArgs) (_ any, err error) {
|
||||
var tableInfo *models.TableInfo
|
||||
if args.HasSwitch("table") {
|
||||
var tblName string
|
||||
if err := args.BindSwitch("table", &tblName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tableInfo, err = rs.tableService.Describe(ctx, tblName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if currRs := rs.state.ResultSet(); currRs != nil && currRs.TableInfo != nil {
|
||||
tableInfo = currRs.TableInfo
|
||||
} else {
|
||||
return nil, errors.New("no table specified")
|
||||
}
|
||||
|
||||
return ResultSetProxy{
|
||||
RS: &models.ResultSet{
|
||||
TableInfo: tableInfo,
|
||||
Created: time.Now(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -102,7 +128,7 @@ func (rs *rsModule) rsQuery(ctx context.Context, args ucl.CallArgs) (any, error)
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return &ResultSetProxy{
|
||||
return ResultSetProxy{
|
||||
RS: newResultSet,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
31
internal/common/ui/commandctrl/cmdpacks/pvars.go
Normal file
31
internal/common/ui/commandctrl/cmdpacks/pvars.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package cmdpacks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/lmika/dynamo-browse/internal/common/ui/commandctrl"
|
||||
"github.com/lmika/dynamo-browse/internal/dynamo-browse/controllers"
|
||||
"github.com/pkg/errors"
|
||||
"log"
|
||||
)
|
||||
|
||||
type resultSetPVar struct {
|
||||
state *controllers.State
|
||||
readController *controllers.TableReadController
|
||||
}
|
||||
|
||||
func (rs resultSetPVar) Get(ctx context.Context) (any, error) {
|
||||
return ResultSetProxy{rs.state.ResultSet()}, nil
|
||||
}
|
||||
|
||||
func (rs resultSetPVar) Set(ctx context.Context, value any) error {
|
||||
rsVal, ok := value.(ResultSetProxy)
|
||||
if !ok {
|
||||
return errors.New("new value to @resultset is not a result set")
|
||||
}
|
||||
|
||||
log.Printf("type = %T", rsVal.RS)
|
||||
|
||||
msg := rs.readController.SetResultSet(rsVal.RS)
|
||||
commandctrl.PostMsg(ctx, msg)
|
||||
return nil
|
||||
}
|
||||
|
|
@ -384,4 +384,6 @@ func (sc StandardCommands) ConfigureUCL(ucl *ucl.Inst) {
|
|||
ucl.SetBuiltin("noisy-touch", sc.cmdNoisyTouch)
|
||||
ucl.SetBuiltin("rebind", sc.cmdRebind)
|
||||
// set-opt --> alias to opts:set
|
||||
|
||||
ucl.SetPseudoVar("resultset", resultSetPVar{sc.State, sc.ReadController})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,14 @@ func (s *State) Filter() string {
|
|||
return s.filter
|
||||
}
|
||||
|
||||
func (s *State) SetResultSet(resultSet *models.ResultSet) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
s.resultSet = resultSet
|
||||
s.filter = ""
|
||||
}
|
||||
|
||||
func (s *State) withResultSet(rs func(*models.ResultSet)) {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
|
|
|||
|
|
@ -291,6 +291,12 @@ func (c *TableReadController) setResultSetAndFilter(resultSet *models.ResultSet,
|
|||
return c.state.buildNewResultSetMessage("")
|
||||
}
|
||||
|
||||
func (c *TableReadController) SetResultSet(resultSet *models.ResultSet) tea.Msg {
|
||||
c.state.setResultSetAndFilter(resultSet, "")
|
||||
c.eventBus.Fire(newResultSetEvent, resultSet, resultSetUpdateScript)
|
||||
return c.state.buildNewResultSetMessage("")
|
||||
}
|
||||
|
||||
func (c *TableReadController) Mark(op MarkOp, where string) tea.Msg {
|
||||
var (
|
||||
whereExpr *queryexpr.QueryExpr
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue