From 7573cf04538b1a0738aa80c2d37dda1bd583b8b8 Mon Sep 17 00:00:00 2001 From: mappu Date: Thu, 18 Jul 2024 17:09:32 +1200 Subject: [PATCH] app: upcast loadedDatabase to more specific interfaces --- db_badger.go | 8 -------- db_bolt.go | 4 ---- db_debconf.go | 8 -------- db_none.go | 8 -------- db_pebble.go | 8 -------- db_redis.go | 4 ---- db_sqlite.go | 4 ---- loadedDatabase.go | 11 ++++++++--- main.go | 18 ++++++++++++++++-- 9 files changed, 24 insertions(+), 49 deletions(-) diff --git a/db_badger.go b/db_badger.go index bbefafc..d4c6b0c 100644 --- a/db_badger.go +++ b/db_badger.go @@ -83,10 +83,6 @@ func (ld *badgerLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) error return nil } -func (n *badgerLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error { - return ErrNotSupported -} - func (ld *badgerLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { // In the Badger implementation, there is only one child: "Data" 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 } -func (ld *badgerLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error { - return ErrNotSupported -} - func (ld *badgerLoadedDatabase) Close() { _ = ld.db.Close() ld.arena = nil diff --git a/db_bolt.go b/db_bolt.go index 2adc294..4262e8f 100644 --- a/db_bolt.go +++ b/db_bolt.go @@ -192,10 +192,6 @@ func (ld *boltLoadedDatabase) DeleteBucket(sender vcl.IComponent, ndata *navData return nil } -func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error { - return ErrNotSupported -} - func (ld *boltLoadedDatabase) Close() { _ = ld.db.Close() ld.arena = nil diff --git a/db_debconf.go b/db_debconf.go index 09011b1..436d166 100644 --- a/db_debconf.go +++ b/db_debconf.go @@ -70,10 +70,6 @@ func (ld *debconfLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) erro return nil } -func (n *debconfLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error { - return ErrNotSupported -} - func (ld *debconfLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { // In the debconf implementation, there is only one child: "Data" 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 } -func (ld *debconfLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error { - return ErrNotSupported -} - func (ld *debconfLoadedDatabase) Close() { ld.arena = nil } diff --git a/db_none.go b/db_none.go index f844df1..d1ac898 100644 --- a/db_none.go +++ b/db_none.go @@ -23,14 +23,6 @@ func (n *noLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) error { 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) { return []string{}, nil } diff --git a/db_pebble.go b/db_pebble.go index 0bb6a3c..3b26b86 100644 --- a/db_pebble.go +++ b/db_pebble.go @@ -74,10 +74,6 @@ func (ld *pebbleLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) error return nil } -func (n *pebbleLoadedDatabase) ApplyChanges(f *TMainForm, ndata *navData) error { - return ErrNotSupported -} - func (ld *pebbleLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { // In the pebble implementation, there is only one child: "Data" 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 } -func (ld *pebbleLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGrid) error { - return ErrNotSupported -} - func (ld *pebbleLoadedDatabase) Close() { _ = ld.db.Close() ld.arena = nil diff --git a/db_redis.go b/db_redis.go index 0955b2a..a096116 100644 --- a/db_redis.go +++ b/db_redis.go @@ -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) { // ctx := context.Background() diff --git a/db_sqlite.go b/db_sqlite.go index b366aa4..68f3065 100644 --- a/db_sqlite.go +++ b/db_sqlite.go @@ -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) { colr, err := ld.db.Query(`SELECT name FROM pragma_table_info( ? )`, tableName) if err != nil { diff --git a/loadedDatabase.go b/loadedDatabase.go index 384db63..39fa69c 100644 --- a/loadedDatabase.go +++ b/loadedDatabase.go @@ -7,7 +7,6 @@ import ( ) 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 { Name string @@ -20,14 +19,20 @@ type loadedDatabase interface { DriverName() string RootElement() *vcl.TTreeNode RenderForNav(f *TMainForm, ndata *navData) error - ApplyChanges(f *TMainForm, ndata *navData) error - ExecQuery(query string, resultArea *vcl.TStringGrid) error NavChildren(ndata *navData) ([]string, error) NavContext(ndata *navData) ([]contextAction, error) Keepalive(ndata *navData) 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. type navData struct { ld loadedDatabase diff --git a/main.go b/main.go index d49117e..cec432d 100644 --- a/main.go +++ b/main.go @@ -668,7 +668,14 @@ func (f *TMainForm) OnDataCommitClick(sender vcl.IObject) { scrollPos := f.contentBox.TopRow() 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 { vcl.ShowMessage(err.Error()) } @@ -757,7 +764,14 @@ func (f *TMainForm) OnQueryExecute(sender vcl.IObject) { } 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 { vcl.ShowMessage(err.Error()) return