From f9b9844eabe29250298f8120fa32a3b98c718454 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 14 Sep 2015 13:46:09 +0200 Subject: Create empty database in cfg-create --- bpkg/database.cxx | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 bpkg/database.cxx (limited to 'bpkg/database.cxx') diff --git a/bpkg/database.cxx b/bpkg/database.cxx new file mode 100644 index 0000000..18bb5af --- /dev/null +++ b/bpkg/database.cxx @@ -0,0 +1,60 @@ +// file : bpkg/database.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include // unique_ptr +#include // move() + +#include + +#include +#include + +using namespace std; + +namespace bpkg +{ + using namespace odb::sqlite; + + database + open (const dir_path& d, bool create) + { + path f (d / path ("bpkg.sqlite3")); + + try + { + // We don't need the thread pool. + // + unique_ptr cf (new single_connection_factory); + + database db (f.string (), + SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0), + true, // Enable FKs. + "", // Default VFS. + move (cf)); + + // Lock the database for as long as the connection is active. First + // we set locking_mode to EXCLUSIVE which instructs SQLite not to + // release any locks until the connection is closed. Then we force + // SQLite to acquire the write lock by starting exclusive transaction. + // See the locking_mode pragma documentation for details. This will + // also fail if the database is inaccessible (e.g., file does not + // exist, already used by another process, etc). + // + { + db.connection ()->execute ("PRAGMA locking_mode = EXCLUSIVE"); + transaction t (db.begin_exclusive ()); + t.commit (); + } + + return db; + } + catch (const database_exception& e) + { + error << f << ": " << e.message (); + throw failed (); + } + } +} -- cgit v1.1