app: upcast loadedDatabase to more specific interfaces

This commit is contained in:
mappu 2024-07-18 17:09:32 +12:00
parent 6dd0635c9e
commit 7573cf0453
9 changed files with 24 additions and 49 deletions

View File

@ -83,10 +83,6 @@ func (ld *badgerLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) error
return nil return nil
} }
func (n *badgerLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error {
return ErrNotSupported
}
func (ld *badgerLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { func (ld *badgerLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
// In the Badger implementation, there is only one child: "Data" // In the Badger implementation, there is only one child: "Data"
if len(ndata.bucketPath) == 0 { if len(ndata.bucketPath) == 0 {
@ -102,10 +98,6 @@ func (ld *badgerLoadedDatabase) NavContext(ndata *navData) ([]contextAction, err
return nil, nil // No special actions are supported return nil, nil // No special actions are supported
} }
func (ld *badgerLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return ErrNotSupported
}
func (ld *badgerLoadedDatabase) Close() { func (ld *badgerLoadedDatabase) Close() {
_ = ld.db.Close() _ = ld.db.Close()
ld.arena = nil ld.arena = nil

View File

@ -192,10 +192,6 @@ func (ld *boltLoadedDatabase) DeleteBucket(sender vcl.IComponent, ndata *navData
return nil return nil
} }
func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return ErrNotSupported
}
func (ld *boltLoadedDatabase) Close() { func (ld *boltLoadedDatabase) Close() {
_ = ld.db.Close() _ = ld.db.Close()
ld.arena = nil ld.arena = nil

View File

@ -70,10 +70,6 @@ func (ld *debconfLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) erro
return nil return nil
} }
func (n *debconfLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error {
return ErrNotSupported
}
func (ld *debconfLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { func (ld *debconfLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
// In the debconf implementation, there is only one child: "Data" // In the debconf implementation, there is only one child: "Data"
if len(ndata.bucketPath) == 0 { if len(ndata.bucketPath) == 0 {
@ -89,10 +85,6 @@ func (ld *debconfLoadedDatabase) NavContext(ndata *navData) ([]contextAction, er
return nil, nil // No special actions are supported return nil, nil // No special actions are supported
} }
func (ld *debconfLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return ErrNotSupported
}
func (ld *debconfLoadedDatabase) Close() { func (ld *debconfLoadedDatabase) Close() {
ld.arena = nil ld.arena = nil
} }

View File

@ -23,14 +23,6 @@ func (n *noLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) error {
return nil return nil
} }
func (n *noLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error {
return ErrNotSupported
}
func (n *noLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return ErrNotSupported
}
func (n *noLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { func (n *noLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
return []string{}, nil return []string{}, nil
} }

View File

@ -74,10 +74,6 @@ func (ld *pebbleLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) error
return nil return nil
} }
func (n *pebbleLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error {
return ErrNotSupported
}
func (ld *pebbleLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { func (ld *pebbleLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
// In the pebble implementation, there is only one child: "Data" // In the pebble implementation, there is only one child: "Data"
if len(ndata.bucketPath) == 0 { if len(ndata.bucketPath) == 0 {
@ -93,10 +89,6 @@ func (ld *pebbleLoadedDatabase) NavContext(ndata *navData) ([]contextAction, err
return nil, nil // No special actions are supported return nil, nil // No special actions are supported
} }
func (ld *pebbleLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error {
return ErrNotSupported
}
func (ld *pebbleLoadedDatabase) Close() { func (ld *pebbleLoadedDatabase) Close() {
_ = ld.db.Close() _ = ld.db.Close()
ld.arena = nil ld.arena = nil

View File

@ -132,10 +132,6 @@ func (ld *redisLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) error
} }
} }
func (n *redisLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error {
return ErrNotSupported
}
func (ld *redisLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { func (ld *redisLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
// ctx := context.Background() // ctx := context.Background()

View File

@ -100,10 +100,6 @@ func (ld *sqliteLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) error
} }
func (n *sqliteLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error {
return ErrNotSupported
}
func (ld *sqliteLoadedDatabase) sqliteGetColumnNamesForTable(tableName string) ([]string, error) { func (ld *sqliteLoadedDatabase) sqliteGetColumnNamesForTable(tableName string) ([]string, error) {
colr, err := ld.db.Query(`SELECT name FROM pragma_table_info( ? )`, tableName) colr, err := ld.db.Query(`SELECT name FROM pragma_table_info( ? )`, tableName)
if err != nil { if err != nil {

View File

@ -7,7 +7,6 @@ import (
) )
var ErrNavNotExist error = errors.New("The selected item no longer exists") var ErrNavNotExist error = errors.New("The selected item no longer exists")
var ErrNotSupported error = errors.New("Unsupported action for this database type")
type contextAction struct { type contextAction struct {
Name string Name string
@ -20,14 +19,20 @@ type loadedDatabase interface {
DriverName() string DriverName() string
RootElement() *vcl.TTreeNode RootElement() *vcl.TTreeNode
RenderForNav(f *TMainForm, ndata *navData) error RenderForNav(f *TMainForm, ndata *navData) error
ApplyChanges(f *TMainForm, ndata *navData) error
ExecQuery(query string, resultArea *vcl.TStringGrid) error
NavChildren(ndata *navData) ([]string, error) NavChildren(ndata *navData) ([]string, error)
NavContext(ndata *navData) ([]contextAction, error) NavContext(ndata *navData) ([]contextAction, error)
Keepalive(ndata *navData) Keepalive(ndata *navData)
Close() Close()
} }
type queryableLoadedDatabase interface {
ExecQuery(query string, resultArea *vcl.TStringGrid) error
}
type editableLoadedDatabase interface {
ApplyChanges(f *TMainForm, ndata *navData) error
}
// navData is the .Data() pointer for each TTreeNode in the left-hand tree. // navData is the .Data() pointer for each TTreeNode in the left-hand tree.
type navData struct { type navData struct {
ld loadedDatabase ld loadedDatabase

18
main.go
View File

@ -668,7 +668,14 @@ func (f *TMainForm) OnDataCommitClick(sender vcl.IObject) {
scrollPos := f.contentBox.TopRow() scrollPos := f.contentBox.TopRow()
ndata := (*navData)(node.Data()) ndata := (*navData)(node.Data())
err := ndata.ld.ApplyChanges(f, ndata)
editableLd, ok := ndata.ld.(editableLoadedDatabase)
if !ok {
vcl.ShowMessage("Unsupported action for this database")
return
}
err := editableLd.ApplyChanges(f, ndata)
if err != nil { if err != nil {
vcl.ShowMessage(err.Error()) vcl.ShowMessage(err.Error())
} }
@ -757,7 +764,14 @@ func (f *TMainForm) OnQueryExecute(sender vcl.IObject) {
} }
ndata := (*navData)(node.Data()) ndata := (*navData)(node.Data())
err := ndata.ld.ExecQuery(queryString, f.queryResult)
queryableLd, ok := ndata.ld.(queryableLoadedDatabase)
if !ok {
vcl.ShowMessage("Unsupported action for this database")
return
}
err := queryableLd.ExecQuery(queryString, f.queryResult)
if err != nil { if err != nil {
vcl.ShowMessage(err.Error()) vcl.ShowMessage(err.Error())
return return