diff --git a/main.go b/main.go index c5e4a93..f1b8601 100644 --- a/main.go +++ b/main.go @@ -26,9 +26,11 @@ func GetMagic() int64 { } //export Bolt_Open -func Bolt_Open(path string) (ObjectReference, *C.char, int) { +func Bolt_Open(readOnly bool, path string) (ObjectReference, *C.char, int) { opts := *bolt.DefaultOptions opts.Timeout = 10 * time.Second + opts.ReadOnly = readOnly + ptrDB, err := bolt.Open(path, os.FileMode(0644), &opts) if err != nil { errMsg := err.Error() diff --git a/qbolt/boltdb.cpp b/qbolt/boltdb.cpp index 85add74..54f113b 100644 --- a/qbolt/boltdb.cpp +++ b/qbolt/boltdb.cpp @@ -7,12 +7,12 @@ BoltDB::BoltDB() } -BoltDB* BoltDB::createFrom(QString filePath, QString &errorOut) +BoltDB* BoltDB::createFrom(QString filePath, bool readOnly, QString &errorOut) { QByteArray filePathBytes(filePath.toUtf8()); GoString filePathGS = Interop::toGoString_WeakRef(&filePathBytes); - auto open_ret = ::Bolt_Open(filePathGS); + auto open_ret = ::Bolt_Open(readOnly, filePathGS); if (open_ret.r2 != 0) { errorOut = QString::fromUtf8(open_ret.r1, open_ret.r2); free(open_ret.r1); diff --git a/qbolt/boltdb.h b/qbolt/boltdb.h index e9e7602..1986a42 100644 --- a/qbolt/boltdb.h +++ b/qbolt/boltdb.h @@ -14,7 +14,7 @@ protected: GoInt64 gmsDbRef; public: - static BoltDB* createFrom(QString filePath, QString &errorOut); + static BoltDB* createFrom(QString filePath, bool readOnly, QString &errorOut); bool listBucketsAtRoot(QString& errorOut, NameReciever cb); diff --git a/qbolt/mainwindow.cpp b/qbolt/mainwindow.cpp index 1c928f5..4d1117f 100644 --- a/qbolt/mainwindow.cpp +++ b/qbolt/mainwindow.cpp @@ -42,7 +42,7 @@ void MainWindow::on_actionNew_database_triggered() { QString file = QFileDialog::getSaveFileName(this, tr("Save new bolt database as...")); if (file.length()) { - openDatabase(file); + openDatabase(file, false); } } @@ -50,15 +50,23 @@ void MainWindow::on_actionOpen_database_triggered() { QString file = QFileDialog::getOpenFileName(this, tr("Select bolt database...")); if (file.length()) { - openDatabase(file); + openDatabase(file, false); } } -void MainWindow::openDatabase(QString file) +void MainWindow::on_actionOpen_database_as_read_only_triggered() +{ + QString file = QFileDialog::getOpenFileName(this, tr("Select bolt database...")); + if (file.length()) { + openDatabase(file, true); + } +} + +void MainWindow::openDatabase(QString file, bool readOnly) { // Open QString error; - auto *bdb = BoltDB::createFrom(file, error); + auto *bdb = BoltDB::createFrom(file, readOnly, error); if (bdb == nullptr) { QMessageBox qmb; qmb.setText(tr("Error opening database: %1").arg(error)); diff --git a/qbolt/mainwindow.h b/qbolt/mainwindow.h index cf44f55..6e20805 100644 --- a/qbolt/mainwindow.h +++ b/qbolt/mainwindow.h @@ -51,8 +51,10 @@ private slots: void on_bucketData_itemSelectionChanged(); + void on_actionOpen_database_as_read_only_triggered(); + protected: - void openDatabase(QString file); + void openDatabase(QString file, bool readOnly); void refreshBucketTree(QTreeWidgetItem* top); void refreshData(BoltDB *bdb, QStringList browse); void openEditor(BoltDB *bdb, QStringList saveAs, QByteArray saveAsKey, QByteArray currentContent); diff --git a/qbolt/mainwindow.ui b/qbolt/mainwindow.ui index abd73c6..32e77b4 100644 --- a/qbolt/mainwindow.ui +++ b/qbolt/mainwindow.ui @@ -278,6 +278,7 @@ + @@ -395,6 +396,11 @@ Add bucket... + + + Open database as read-only... + +