comparison 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
comparison
equal deleted inserted replaced
45:719b5457ea92 46:b0593a3e2ca8
2 2
3 #include <unordered_map> 3 #include <unordered_map>
4 #include <string> 4 #include <string>
5 5
6 #include "driver.hpp" 6 #include "driver.hpp"
7
8 /*
9 * Global shared stuff for the driver
10 * ------------------------------------------------------------------
11 */
7 12
8 namespace sqlite { 13 namespace sqlite {
9 14
10 std::unique_ptr<sqlite3, int (*)(sqlite3 *)> database{nullptr, nullptr}; 15 std::unique_ptr<sqlite3, int (*)(sqlite3 *)> database{nullptr, nullptr};
11 16
19 24
20 return {stmt, &sqlite3_finalize}; 25 return {stmt, &sqlite3_finalize};
21 } 26 }
22 27
23 } // !sqlite 28 } // !sqlite
29
30 /*
31 * Local function to this file.
32 * ------------------------------------------------------------------
33 */
34
35 namespace {
36
37 const std::string verify_query(
38 "select name "
39 "from sqlite_master "
40 "where type='table' "
41 "and name='mk_info'"
42 );
43
44 bool is_initialized()
45 {
46 auto stmt = sqlite::prepare(verify_query);
47
48 if (sqlite3_step(stmt.get()) != SQLITE_ROW) {
49 return false;
50 }
51
52 return strcmp((char*)sqlite3_column_text(stmt.get(), 0), "mk_info") == 0;
53 }
54
55 } // !namespace
56
57 /*
58 * Driver API
59 * ------------------------------------------------------------------
60 */
24 61
25 extern "C" { 62 extern "C" {
26 63
27 BOOST_SYMBOL_EXPORT void malikania_driver_load(const std::unordered_map<std::string, std::string>& params) 64 BOOST_SYMBOL_EXPORT void malikania_driver_load(const std::unordered_map<std::string, std::string>& params)
28 { 65 {
38 throw std::runtime_error(sqlite3_errmsg(db)); 75 throw std::runtime_error(sqlite3_errmsg(db));
39 } 76 }
40 77
41 sqlite::database = {db, &sqlite3_close}; 78 sqlite::database = {db, &sqlite3_close};
42 79
43 #if 0 80 if (!is_initialized()) {
44 // Not needed with SQLITE_DEFAULT_FOREIGN_KEYS 81 throw std::runtime_error("database not initialized");
45 if (sqlite3_exec(db, "PRAGMA foreign_keys = ON", nullptr, nullptr, nullptr) != SQLITE_OK) {
46 throw std::runtime_error(sqlite3_errmsg(db));
47 } 82 }
48 #endif
49 } 83 }
50 84
51 BOOST_SYMBOL_EXPORT void malikania_driver_unload() 85 BOOST_SYMBOL_EXPORT void malikania_driver_unload()
52 { 86 {
53 // Explicit destruction, optional. 87 // Explicit destruction, optional.