bolt: support open as readonly

This commit is contained in:
mappu 2024-06-28 11:33:43 +12:00
parent 2b309fbda7
commit a14e58297a
2 changed files with 20 additions and 3 deletions

View File

@ -107,9 +107,15 @@ var _ loadedDatabase = &boltLoadedDatabase{} // interface assertion
// //
func (f *TMainForm) boltAddDatabaseFromFile(path string) { func (f *TMainForm) boltAddDatabaseFromFile(path string, readonly bool) {
// TODO load in background thread to stop blocking the UI // TODO load in background thread to stop blocking the UI
db, err := bbolt.Open(path, 0644, &bbolt.Options{Timeout: 1 * time.Second})
opts := bbolt.Options{
Timeout: 1 * time.Second,
ReadOnly: readonly,
}
db, err := bbolt.Open(path, 0644, &opts)
if err != nil { if err != nil {
vcl.ShowMessage(fmt.Sprintf("Failed to load database '%s': %s", path, err.Error())) vcl.ShowMessage(fmt.Sprintf("Failed to load database '%s': %s", path, err.Error()))
return return

13
main.go
View File

@ -65,6 +65,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFile.Add(mnuFileBolt) mnuFile.Add(mnuFileBolt)
vcl_menuitem(mnuFileBolt, "Open database...", imgDatabaseAdd, f.OnMnuFileBoltOpenClick) vcl_menuitem(mnuFileBolt, "Open database...", imgDatabaseAdd, f.OnMnuFileBoltOpenClick)
vcl_menuitem(mnuFileBolt, "Open database (read-only)...", imgDatabaseAdd, f.OnMnuFileBoltOpenReadonlyClick)
// //
@ -251,7 +252,17 @@ func (f *TMainForm) OnMnuFileBoltOpenClick(sender vcl.IObject) {
dlg.SetFilter("Bolt database|*.db|All files|*.*") dlg.SetFilter("Bolt database|*.db|All files|*.*")
ret := dlg.Execute() // Fake blocking ret := dlg.Execute() // Fake blocking
if ret { if ret {
f.boltAddDatabaseFromFile(dlg.FileName()) f.boltAddDatabaseFromFile(dlg.FileName(), false)
}
}
func (f *TMainForm) OnMnuFileBoltOpenReadonlyClick(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(), true)
} }
} }