db: lift execQuery error handling to parent

This commit is contained in:
mappu 2024-07-06 11:59:55 +12:00
parent aad92d27e9
commit 2f65ffdd70
9 changed files with 25 additions and 21 deletions

View File

@ -103,8 +103,8 @@ func (ld *badgerLoadedDatabase) NavContext(ndata *navData) ([]contextAction, err
return nil, nil // No special actions are supported
}
func (ld *badgerLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) {
vcl.ShowMessage("Badger doesn't support querying")
func (ld *badgerLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return errors.New("Badger doesn't support querying")
}
func (ld *badgerLoadedDatabase) Close() {

View File

@ -185,8 +185,8 @@ func (ld *boltLoadedDatabase) DeleteBucket(ndata *navData) {
}
}
func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) {
vcl.ShowMessage("Bolt doesn't support querying")
func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return errors.New("Bolt doesn't support querying")
}
func (ld *boltLoadedDatabase) Close() {

View File

@ -89,8 +89,8 @@ func (ld *debconfLoadedDatabase) NavContext(ndata *navData) ([]contextAction, er
return nil, nil // No special actions are supported
}
func (ld *debconfLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) {
vcl.ShowMessage("debconf doesn't support querying")
func (ld *debconfLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return errors.New("debconf doesn't support querying")
}
func (ld *debconfLoadedDatabase) Close() {

View File

@ -28,7 +28,8 @@ func (n *noLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error {
return errors.New("Editing is not supported")
}
func (n *noLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) {
func (n *noLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return nil
}
func (n *noLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {

View File

@ -94,8 +94,8 @@ func (ld *pebbleLoadedDatabase) NavContext(ndata *navData) ([]contextAction, err
return nil, nil // No special actions are supported
}
func (ld *pebbleLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) {
vcl.ShowMessage("pebble doesn't support querying")
func (ld *pebbleLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return errors.New("pebble doesn't support querying")
}
func (ld *pebbleLoadedDatabase) Close() {

View File

@ -168,22 +168,20 @@ func (ld *redisLoadedDatabase) NavContext(ndata *navData) ([]contextAction, erro
return nil, nil // No special actions are supported
}
func (ld *redisLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) {
func (ld *redisLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
ctx := context.Background()
// Need to parse the query into separate string+args fields for the protocol
fields, err := lexer.Fields(query)
if err != nil {
vcl.ShowMessage(fmt.Sprintf("Parsing the query: %v", err))
return
return fmt.Errorf("Parsing the query: %w", err)
}
fields_boxed := box_interface(fields)
ret, err := ld.db.Do(ctx, fields_boxed...).Result()
if err != nil {
vcl.ShowMessage(fmt.Sprintf("The redis query returned an error: %v", err))
return
return fmt.Errorf("The redis query returned an error: %w", err)
}
vcl_stringgrid_clear(resultArea)
@ -212,6 +210,8 @@ func (ld *redisLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGr
vcl_stringgrid_columnwidths(resultArea)
resultArea.SetEnabled(true)
return nil
}
func (ld *redisLoadedDatabase) Close() {

View File

@ -164,11 +164,10 @@ func populateRows(rr *sql.Rows, dest *vcl.TStringGrid) {
}
}
func (ld *sqliteLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) {
func (ld *sqliteLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
rr, err := ld.db.Query(query)
if err != nil {
vcl.ShowMessage(err.Error())
return
return err
}
defer rr.Close()
@ -177,8 +176,7 @@ func (ld *sqliteLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringG
columns, err := rr.Columns()
if err != nil {
vcl.ShowMessage(err.Error())
return
return err
}
populateColumns(columns, resultArea)
@ -187,6 +185,7 @@ func (ld *sqliteLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringG
vcl_stringgrid_columnwidths(resultArea)
resultArea.SetEnabled(true)
return nil
}
func (ld *sqliteLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {

View File

@ -20,7 +20,7 @@ type loadedDatabase interface {
RootElement() *vcl.TTreeNode
RenderForNav(f *TMainForm, ndata *navData)
ApplyChanges(f *TMainForm, ndata *navData) error
ExecQuery(query string, resultArea *vcl.TStringGrid)
ExecQuery(query string, resultArea *vcl.TStringGrid) error
NavChildren(ndata *navData) ([]string, error)
NavContext(ndata *navData) ([]contextAction, error)
Keepalive(ndata *navData)

View File

@ -712,7 +712,11 @@ func (f *TMainForm) OnQueryExecute(sender vcl.IObject) {
}
ndata := (*navData)(node.Data())
ndata.ld.ExecQuery(queryString, f.queryResult)
err := ndata.ld.ExecQuery(queryString, f.queryResult)
if err != nil {
vcl.ShowMessage(err.Error())
return
}
}
func (f *TMainForm) OnNavChange(sender vcl.IObject, node *vcl.TTreeNode) {