package main import ( "fmt" "strings" "unsafe" "github.com/pkg/browser" "github.com/ying32/govcl/vcl" "github.com/ying32/govcl/vcl/types" "github.com/ying32/govcl/vcl/types/colors" ) type TMainForm struct { *vcl.TForm ImageList *vcl.TImageList Menu *vcl.TMainMenu StatusBar *vcl.TStatusBar Buckets *vcl.TTreeView Tabs *vcl.TPageControl propertiesBox *vcl.TMemo contentBox *vcl.TListView queryInput *vcl.TMemo queryResult *vcl.TListView none *noLoadedDatabase dbs []loadedDatabase } var ( mainForm *TMainForm ) func main() { vcl.RunApp(&mainForm) } func (f *TMainForm) OnFormCreate(sender vcl.IObject) { f.ImageList = loadImages(f) f.SetCaption("yvbolt") f.ScreenCenter() f.ImageList.GetIcon(imgDatabaseLightning, f.Icon()) mnuFile := vcl.NewMenuItem(f) mnuFile.SetCaption("File") mnuFileBadger := vcl.NewMenuItem(mnuFile) mnuFileBadger.SetCaption("Badger") mnuFileBadger.SetImageIndex(imgVendorDgraph) mnuFile.Add(mnuFileBadger) mnuFileBadgerOpen := vcl.NewMenuItem(mnuFileBadger) mnuFileBadgerOpen.SetCaption("Open database...") mnuFileBadgerOpen.SetImageIndex(imgDatabaseAdd) mnuFileBadgerOpen.SetOnClick(f.OnMnuFileBadgerOpenClick) mnuFileBadger.Add(mnuFileBadgerOpen) mnuFileBadgerMemory := vcl.NewMenuItem(mnuFileBadger) mnuFileBadgerMemory.SetCaption("New in-memory database") mnuFileBadgerMemory.SetImageIndex(imgDatabaseAdd) mnuFileBadgerMemory.SetOnClick(f.OnMnuFileBadgerMemoryClick) mnuFileBadger.Add(mnuFileBadgerMemory) // mnuFileBolt := vcl.NewMenuItem(mnuFile) mnuFileBolt.SetCaption("Bolt") mnuFileBolt.SetImageIndex(imgVendorGithub) mnuFile.Add(mnuFileBolt) mnuFileBoltOpen := vcl.NewMenuItem(mnuFileBolt) mnuFileBoltOpen.SetCaption("Open database...") mnuFileBoltOpen.SetImageIndex(imgDatabaseAdd) mnuFileBoltOpen.SetShortCutFromString("Ctrl+O") mnuFileBoltOpen.SetOnClick(f.OnMnuFileOpenClick) mnuFileBolt.Add(mnuFileBoltOpen) // mnuFileRedis := vcl.NewMenuItem(mnuFile) mnuFileRedis.SetCaption("Redis") mnuFileRedis.SetImageIndex(imgVendorRedis) mnuFile.Add(mnuFileRedis) mnuFileRedisConnect := vcl.NewMenuItem(mnuFileRedis) mnuFileRedisConnect.SetCaption("Connect...") mnuFileRedisConnect.SetImageIndex(imgDatabaseAdd) mnuFileRedisConnect.SetOnClick(f.OnMnuFileRedisConnectClick) mnuFileRedis.Add(mnuFileRedisConnect) // mnuFileSqlite := vcl.NewMenuItem(mnuFile) mnuFileSqlite.SetCaption("SQLite") mnuFileSqlite.SetImageIndex(imgVendorSqlite) mnuFile.Add(mnuFileSqlite) mnuFileSqliteOpen := vcl.NewMenuItem(mnuFileSqlite) mnuFileSqliteOpen.SetCaption("Open database...") mnuFileSqliteOpen.SetImageIndex(imgDatabaseAdd) mnuFileSqliteOpen.SetOnClick(f.OnMnuFileSqliteOpenClick) mnuFileSqlite.Add(mnuFileSqliteOpen) mnuFileSqliteMemory := vcl.NewMenuItem(mnuFileSqlite) mnuFileSqliteMemory.SetCaption("New in-memory database") mnuFileSqliteMemory.SetImageIndex(imgDatabaseAdd) mnuFileSqliteMemory.SetOnClick(f.OnMnuFileSqliteMemoryClick) mnuFileSqlite.Add(mnuFileSqliteMemory) // mnuSep := vcl.NewMenuItem(mnuFile) mnuSep.SetCaption("-") // Creates separator mnuFile.Add(mnuSep) mnuFileExit := vcl.NewMenuItem(mnuFile) mnuFileExit.SetCaption("Exit") mnuFileExit.SetOnClick(f.OnMnuFileExitClick) mnuFile.Add(mnuFileExit) mnuQuery := vcl.NewMenuItem(f) mnuQuery.SetCaption("Query") mnuQueryExecute := vcl.NewMenuItem(mnuQuery) mnuQueryExecute.SetCaption("Execute") mnuQueryExecute.SetShortCutFromString("F5") mnuQueryExecute.SetOnClick(f.OnQueryExecute) mnuQueryExecute.SetImageIndex(imgLightning) mnuQuery.Add(mnuQueryExecute) mnuHelp := vcl.NewMenuItem(f) mnuHelp.SetCaption("Help") mnuHelpHomepage := vcl.NewMenuItem(mnuHelp) mnuHelpHomepage.SetCaption("About yvbolt") mnuHelpHomepage.SetShortCutFromString("F1") mnuHelpHomepage.SetOnClick(f.OnMnuHelpHomepage) mnuHelp.Add(mnuHelpHomepage) f.Menu = vcl.NewMainMenu(f) f.Menu.SetImages(f.ImageList) f.Menu.Items().Add(mnuFile) f.Menu.Items().Add(mnuQuery) f.Menu.Items().Add(mnuHelp) // f.StatusBar = vcl.NewStatusBar(f) f.StatusBar.SetParent(f) f.StatusBar.SetSimpleText("") // f.Buckets = vcl.NewTreeView(f) f.Buckets.SetParent(f) f.Buckets.SetImages(f.ImageList) f.Buckets.SetAlign(types.AlLeft) f.Buckets.SetWidth(MY_WIDTH) f.Buckets.SetReadOnly(true) // prevent click to rename on nodes f.Buckets.SetOnExpanding(f.OnNavExpanding) f.Buckets.SetOnChange(f.OnNavChange) f.Buckets.SetOnContextPopup(f.OnNavContextPopup) hsplit := vcl.NewSplitter(f) hsplit.SetParent(f) hsplit.SetAlign(types.AlLeft) hsplit.SetLeft(1) // Just needs to be further "over" than f.Buckets for auto-alignment f.Tabs = vcl.NewPageControl(f) f.Tabs.SetParent(f) f.Tabs.SetAlign(types.AlClient) // fill remaining space f.Tabs.SetImages(f.ImageList) propertiesTab := vcl.NewTabSheet(f.Tabs) propertiesTab.SetParent(f.Tabs) propertiesTab.SetCaption("Properties") propertiesTab.SetImageIndex(imgChartBar) f.propertiesBox = vcl.NewMemo(propertiesTab) f.propertiesBox.SetParent(propertiesTab) f.propertiesBox.BorderSpacing().SetAround(MY_SPACING) f.propertiesBox.SetAlign(types.AlClient) // fill remaining space f.propertiesBox.SetReadOnly(true) f.propertiesBox.SetEnabled(true) // Need to leave it enabled so scrolling works f.propertiesBox.SetColor(colors.ClForm) // 0x00f0f0f0 f.propertiesBox.SetBorderStyle(types.BsNone) f.propertiesBox.SetScrollBars(types.SsAutoVertical) dataTab := vcl.NewTabSheet(f.Tabs) dataTab.SetParent(f.Tabs) dataTab.SetCaption("Data") dataTab.SetImageIndex(imgTable) f.contentBox = vcl.NewListView(dataTab) f.contentBox.SetParent(dataTab) f.contentBox.BorderSpacing().SetAround(MY_SPACING) f.contentBox.SetAlign(types.AlClient) // fill remaining space f.contentBox.SetViewStyle(types.VsReport) // "Report style" i.e. has columns f.contentBox.SetAutoWidthLastColumn(true) f.contentBox.SetReadOnly(true) f.contentBox.Columns().Clear() queryTab := vcl.NewTabSheet(f.Tabs) queryTab.SetParent(f.Tabs) queryTab.SetCaption("Query") queryTab.SetImageIndex(imgLightning) queryButtonBar := vcl.NewToolBar(queryTab) queryButtonBar.SetParent(queryTab) queryButtonBar.SetAlign(types.AlTop) queryButtonBar.BorderSpacing().SetLeft(MY_SPACING) queryButtonBar.BorderSpacing().SetTop(MY_SPACING) queryButtonBar.BorderSpacing().SetBottom(0) queryButtonBar.BorderSpacing().SetRight(MY_SPACING) queryButtonBar.SetImages(f.ImageList) queryButtonBar.SetShowCaptions(true) queryExecBtn := vcl.NewToolButton(queryButtonBar) queryExecBtn.SetParent(queryButtonBar) queryExecBtn.SetCaption("Execute") // queryExecBtn.SetImageIndex(imgLightning) queryExecBtn.SetOnClick(f.OnQueryExecute) f.queryInput = vcl.NewMemo(queryTab) f.queryInput.SetParent(queryTab) f.queryInput.SetHeight(MY_HEIGHT) f.queryInput.SetAlign(types.AlTop) f.queryInput.SetTop(1) f.queryInput.Font().SetName("monospace") f.queryInput.BorderSpacing().SetLeft(MY_SPACING) f.queryInput.BorderSpacing().SetTop(0) f.queryInput.BorderSpacing().SetRight(MY_SPACING) vsplit := vcl.NewSplitter(queryTab) vsplit.SetParent(queryTab) vsplit.SetAlign(types.AlTop) vsplit.SetTop(2) f.queryResult = vcl.NewListView(queryTab) f.queryResult.SetParent(queryTab) f.queryResult.SetAlign(types.AlClient) // fill remaining space f.queryResult.SetViewStyle(types.VsReport) // "Report style" i.e. has columns f.queryResult.SetAutoWidthLastColumn(true) f.queryResult.SetReadOnly(true) f.queryResult.Columns().Clear() f.queryResult.BorderSpacing().SetLeft(MY_SPACING) f.queryResult.BorderSpacing().SetRight(MY_SPACING) f.queryResult.BorderSpacing().SetBottom(MY_SPACING) f.none = &noLoadedDatabase{} f.OnNavChange(f, nil) // calls f.none.RenderForNav and sets up status bar content } func (f *TMainForm) OnMnuFileOpenClick(sender vcl.IObject) { dlg := vcl.NewOpenDialog(f) dlg.SetTitle("Select a database file...") dlg.SetFilter("Bolt database|*.db|All files|*.*") ret := dlg.Execute() // Fake blocking if ret { f.boltAddDatabaseFromFile(dlg.FileName()) } } func (f *TMainForm) OnMnuFileSqliteOpenClick(sender vcl.IObject) { dlg := vcl.NewOpenDialog(f) dlg.SetTitle("Select a database file...") dlg.SetFilter("SQLite database|*.db;*.db3;*.sqlite;*.sqlite3|All files|*.*") ret := dlg.Execute() // Fake blocking if ret { f.sqliteAddDatabaseFromFile(dlg.FileName()) } } func (f *TMainForm) OnMnuFileBadgerOpenClick(sender vcl.IObject) { dlg := vcl.NewSelectDirectoryDialog(f) dlg.SetTitle("Select a database directory...") ret := dlg.Execute() // Fake blocking if ret { f.badgerAddDatabaseFromDirectory(dlg.FileName()) } } func (f *TMainForm) OnMnuFileBadgerMemoryClick(sender vcl.IObject) { f.badgerAddDatabaseFromMemory() } func (f *TMainForm) OnMnuFileSqliteMemoryClick(sender vcl.IObject) { f.sqliteAddDatabaseFromFile(`:memory:`) } func (f *TMainForm) OnMnuFileRedisConnectClick(sender vcl.IObject) { var child *TRedisConnectionDialog vcl.Application.CreateForm(&child) defer child.Free() var ret TRedisConnectionDialogResult child.CustomReturn = &ret mr := child.ShowModal() // Fake blocking if mr != types.MrOk || ret == nil { return // Cancelled } // Connect f.redisConnect(ret) } func (f *TMainForm) OnMnuFileExitClick(sender vcl.IObject) { f.Close() } func (f *TMainForm) OnMnuHelpHomepage(sender vcl.IObject) { err := browser.OpenURL("https://code.ivysaur.me/yvbolt") if err != nil { vcl.ShowMessage("Opening browser: " + err.Error()) } } func (f *TMainForm) OnNavContextPopup(sender vcl.IObject, mousePos types.TPoint, handled *bool) { *handled = true curItem := f.Buckets.Selected() if curItem == nil { // Nothing is selected at all return } mnu := vcl.NewPopupMenu(f.Buckets) mnuRefresh := vcl.NewMenuItem(mnu) mnuRefresh.SetCaption("Refresh") mnuRefresh.SetOnClick(f.OnNavContextRefresh) mnu.Items().Add(mnuRefresh) // Check what custom actions the ndata->db itself wants to add // ... if curItem.Parent() == nil { // Top-level item (database connection). Allow closing by right-click. mnuSep := vcl.NewMenuItem(mnu) mnuSep.SetCaption("-") mnu.Items().Add(mnuSep) mnuClose := vcl.NewMenuItem(mnu) mnuClose.SetCaption("Close") mnuClose.SetOnClick(f.OnNavContextClose) mnu.Items().Add(mnuClose) } // Show popup mnu.Popup2() } func (f *TMainForm) OnNavContextRefresh(sender vcl.IObject) { vcl.ShowMessage("TODO") // TODO } func (f *TMainForm) OnNavContextClose(sender vcl.IObject) { curItem := f.Buckets.Selected() if curItem == nil { return // Nothing selected (shouldn't happen) } if curItem.Parent() != nil { return // Selection is not top-level DB connection (shouldn't happen) } ndata := (*navData)(curItem.Data()) ndata.ld.Close() curItem.Delete() // n.b. This triggers OnNavChange, which will then re-render from noLoadedDatabase{} } func (f *TMainForm) OnQueryExecute(sender vcl.IObject) { // If query tab is not selected, switch to it, but do not exec if f.Tabs.ActivePageIndex() != 2 { f.Tabs.SetActivePageIndex(2) return } queryString := f.queryInput.Text() if f.queryInput.SelLength() > 0 { queryString = f.queryInput.SelText() // Just the selected text } // Execute node := f.Buckets.Selected() if node == nil { vcl.ShowMessage("No database selected") return } ndata := (*navData)(node.Data()) ndata.ld.ExecQuery(queryString, f.queryResult) } func (f *TMainForm) OnNavChange(sender vcl.IObject, node *vcl.TTreeNode) { var ld loadedDatabase = f.none var ndata *navData = nil if node != nil && node.Data() != nil { ndata = (*navData)(node.Data()) ld = ndata.ld } ld.RenderForNav(f, ndata) // Handover to the database type's own renderer function // We're in charge of common status bar text updates f.StatusBar.SetSimpleText(ld.DisplayName() + " | " + ld.DriverName()) } func (f *TMainForm) OnNavExpanding(sender vcl.IObject, node *vcl.TTreeNode, allowExpansion *bool) { if node.Data() == nil { vcl.ShowMessage("unexpected nil data") *allowExpansion = false return } ndata := (*navData)(node.Data()) err := f.NavLoadChildren(node, ndata) if err != nil { vcl.ShowMessage(err.Error()) *allowExpansion = false // Permanently block return } *allowExpansion = node.HasChildren() // While we're here - preload one single level deep (not any deeper) if node.HasChildren() { // This node has children that we haven't processed. Process them now cc := node.GetFirstChild() for { ndataChild := (*navData)(cc.Data()) if ndataChild.childrenLoaded { break // We always do them together, so if one's done, no need to keep looking } f.NavLoadChildren(cc, ndataChild) cc = cc.GetNextSibling() if cc == nil { break } } } } func (f *TMainForm) NavLoadChildren(node *vcl.TTreeNode, ndata *navData) error { if ndata.childrenLoaded { return nil // Nothing to do } // Find the child buckets from this point under the element nextBucketNames, err := ndata.ld.NavChildren(ndata) if err != nil { return fmt.Errorf("Failed to find child buckets under %q: %w", strings.Join(ndata.bucketPath, `/`), err) } ndata.childrenLoaded = true // don't repeat this work if len(nextBucketNames) == 0 { node.SetHasChildren(false) } else { node.SetHasChildren(true) // Populate LCL child nodes for _, bucketName := range nextBucketNames { node := f.Buckets.Items().AddChild(node, formatUtf8([]byte(bucketName))) node.SetHasChildren(true) // dynamically populate in OnNavExpanding node.SetImageIndex(imgTable) node.SetSelectedIndex(imgTable) navData := &navData{ ld: ndata.ld, childrenLoaded: false, // will be loaded dynamically bucketPath: []string{}, // empty = root } navData.bucketPath = append(navData.bucketPath, ndata.bucketPath...) navData.bucketPath = append(navData.bucketPath, bucketName) node.SetData(unsafe.Pointer(navData)) ndata.ld.Keepalive(navData) } } return nil }