option to open database as read-only

This commit is contained in:
mappu 2017-05-25 19:59:53 +12:00
parent 142f3f6bf4
commit 7441e0c15b
6 changed files with 27 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -278,6 +278,7 @@
</property>
<addaction name="actionNew_database"/>
<addaction name="actionOpen_database"/>
<addaction name="actionOpen_database_as_read_only"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
</widget>
@ -395,6 +396,11 @@
<string>Add bucket...</string>
</property>
</action>
<action name="actionOpen_database_as_read_only">
<property name="text">
<string>Open database as read-only...</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>