diff database/sqlite/src/driver.cpp @ 46:b0593a3e2ca8

Server: use Boost.Asio and add basic authentication support
author David Demelier <markand@malikania.fr>
date Sun, 04 Dec 2016 21:26:18 +0100
parents 1e206fdc7021
children
line wrap: on
line diff
--- a/database/sqlite/src/driver.cpp	Wed Nov 30 21:16:34 2016 +0100
+++ b/database/sqlite/src/driver.cpp	Sun Dec 04 21:26:18 2016 +0100
@@ -5,6 +5,11 @@
 
 #include "driver.hpp"
 
+/*
+ * Global shared stuff for the driver
+ * ------------------------------------------------------------------
+ */
+
 namespace sqlite {
 
 std::unique_ptr<sqlite3, int (*)(sqlite3 *)> database{nullptr, nullptr};
@@ -22,6 +27,38 @@
 
 } // !sqlite
 
+/*
+ * Local function to this file.
+ * ------------------------------------------------------------------
+ */
+
+namespace {
+
+const std::string verify_query(
+    "select name "
+    "from sqlite_master "
+    "where type='table' "
+    "and name='mk_info'"
+);
+
+bool is_initialized()
+{
+    auto stmt = sqlite::prepare(verify_query);
+
+    if (sqlite3_step(stmt.get()) != SQLITE_ROW) {
+        return false;
+    }
+
+    return strcmp((char*)sqlite3_column_text(stmt.get(), 0), "mk_info") == 0;
+}
+
+} // !namespace
+
+/*
+ * Driver API
+ * ------------------------------------------------------------------
+ */
+
 extern "C" {
 
 BOOST_SYMBOL_EXPORT void malikania_driver_load(const std::unordered_map<std::string, std::string>& params)
@@ -40,12 +77,9 @@
 
     sqlite::database = {db, &sqlite3_close};
 
-#if 0
-    // Not needed with SQLITE_DEFAULT_FOREIGN_KEYS
-    if (sqlite3_exec(db, "PRAGMA foreign_keys = ON", nullptr, nullptr, nullptr) != SQLITE_OK) {
-        throw std::runtime_error(sqlite3_errmsg(db));
+    if (!is_initialized()) {
+        throw std::runtime_error("database not initialized");
     }
-#endif
 }
 
 BOOST_SYMBOL_EXPORT void malikania_driver_unload()