sqlite: load column headers

This commit is contained in:
mappu 2024-06-08 14:49:57 +12:00
parent 6234f02ea6
commit 91f9c5fc30
3 changed files with 54 additions and 14 deletions

11
bolt.go
View File

@ -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 {

16
main.go
View File

@ -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) {

View File

@ -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