From 91f9c5fc30ff122f41946b8c2c781e1699a827cd Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 8 Jun 2024 14:49:57 +1200 Subject: [PATCH] sqlite: load column headers --- bolt.go | 11 +++++++++++ main.go | 16 ++++++---------- sqlite.go | 41 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/bolt.go b/bolt.go index 733b77d..9386f56 100644 --- a/bolt.go +++ b/bolt.go @@ -9,6 +9,7 @@ import ( "unsafe" "github.com/ying32/govcl/vcl" + "github.com/ying32/govcl/vcl/types" "go.etcd.io/bbolt" ) @@ -46,6 +47,16 @@ func (ld *boltLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) { f.contentBox.SetEnabled(false) f.contentBox.Clear() + // Bolt always uses Key + Value as the columns + + f.contentBox.Columns().Clear() + colKey := f.contentBox.Columns().Add() + colKey.SetCaption("Key") + colKey.SetWidth(MY_WIDTH) + colKey.SetAlignment(types.TaLeftJustify) + colVal := f.contentBox.Columns().Add() + colVal.SetCaption("Value") + err := ld.db.View(func(tx *bbolt.Tx) error { b := boltTargetBucket(tx, ndata.bucketPath) if b == nil { diff --git a/main.go b/main.go index dd26446..484c0f4 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,11 @@ import ( "github.com/ying32/govcl/vcl/types" ) +const ( + MY_SPACING = 6 + MY_WIDTH = 180 +) + type TMainForm struct { *vcl.TForm @@ -34,9 +39,6 @@ func main() { } func (f *TMainForm) OnFormCreate(sender vcl.IObject) { - const MY_SPACING = 6 - const MY_WIDTH = 180 - f.ImageList = loadImages(f) f.SetCaption("yvbolt") @@ -118,13 +120,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) { f.contentBox.SetViewStyle(types.VsReport) // "Report style" i.e. has columns f.contentBox.SetAutoWidthLastColumn(true) f.contentBox.SetReadOnly(true) - colKey := f.contentBox.Columns().Add() - colKey.SetCaption("Key") - colKey.SetWidth(MY_WIDTH) - colKey.SetAlignment(types.TaLeftJustify) - colVal := f.contentBox.Columns().Add() - colVal.SetCaption("Value") - + f.contentBox.Columns().Clear() } func (f *TMainForm) OnMnuFileOpenClick(sender vcl.IObject) { diff --git a/sqlite.go b/sqlite.go index 62a0fc7..1b60388 100644 --- a/sqlite.go +++ b/sqlite.go @@ -8,6 +8,7 @@ import ( _ "github.com/mattn/go-sqlite3" "github.com/ying32/govcl/vcl" + "github.com/ying32/govcl/vcl/types" ) const ( @@ -64,13 +65,45 @@ func (ld *sqliteLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) { // Display table properties f.propertiesBox.SetText(fmt.Sprintf("Selected table %q\n\nSchema:\n\n%s", tableName, schemaStmt)) - // Load column details - ld.db.Query(`pragma table_info(?)`, tableName) - - // Select * with small limit f.contentBox.SetEnabled(false) f.contentBox.Clear() + // Load column details + // Use SELECT form instead of common PRAGMA table_info so we can just get names + colr, err := ld.db.Query(`SELECT name FROM pragma_table_info(?)`, tableName) + if err != nil { + vcl.ShowMessageFmt("Failed to load columns for table %q: %s", tableName, err.Error()) + return + } + defer colr.Close() + + f.contentBox.Columns().Clear() + for colr.Next() { + + var columnName string + err = colr.Scan(&columnName) + if err != nil { + vcl.ShowMessageFmt("Failed to read column names for table %q: %s", tableName, err.Error()) + return + } + + col := f.contentBox.Columns().Add() + col.SetCaption(columnName) + col.SetWidth(MY_WIDTH) + col.SetAlignment(types.TaLeftJustify) + } + if colr.Err() != nil { + vcl.ShowMessageFmt("Failed to load columns for table %q: %s", tableName, err.Error()) + return + } + colr.Close() // will be double-closed + + // Select * with small limit + // TODO + + // We successfully populated the data grid + f.contentBox.SetEnabled(true) + } else { // ??? unknown