backends/sqlite3/session.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 Maciej Sobczak, Stephen Hutton, David Courtney
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 
9 #include "soci-sqlite3.h"
10 
11 #include <connection-parameters.h>
12 
13 #include <sstream>
14 #include <string>
15 
16 #ifdef _MSC_VER
17 #pragma warning(disable:4355)
18 #endif
19 
20 using namespace soci;
21 using namespace soci::details;
22 using namespace sqlite_api;
23 
24 namespace // anonymous
25 {
26 
27 // helper function for hardcoded queries
28 void execude_hardcoded(sqlite_api::sqlite3* conn, char const* const query, char const* const errMsg)
29 {
30  char *zErrMsg = 0;
31  int const res = sqlite3_exec(conn, query, 0, 0, &zErrMsg);
32  if (res != SQLITE_OK)
33  {
34  std::ostringstream ss;
35  ss << errMsg << " " << zErrMsg;
36  sqlite3_free(zErrMsg);
37  throw sqlite3_soci_error(ss.str(), res);
38  }
39 }
40 
41 void check_sqlite_err(sqlite_api::sqlite3* conn, int res, char const* const errMsg)
42 {
43  if (SQLITE_OK != res)
44  {
45  const char *zErrMsg = sqlite3_errmsg(conn);
46  std::ostringstream ss;
47  ss << errMsg << zErrMsg;
48  throw sqlite3_soci_error(ss.str(), res);
49  }
50 }
51 
52 } // namespace anonymous
53 
54 
57 {
58  int timeout = 0;
59  int connection_flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
60  std::string synchronous;
61  std::string const & connectString = parameters.get_connect_string();
62  std::string dbname(connectString);
63  std::stringstream ssconn(connectString);
64  while (!ssconn.eof() && ssconn.str().find('=') != std::string::npos)
65  {
66  std::string key, val;
67  std::getline(ssconn, key, '=');
68  std::getline(ssconn, val, ' ');
69 
70  if (val.size()>0 && val[0]=='\"')
71  {
72  std::string quotedVal = val.erase(0, 1);
73 
74  if (quotedVal[quotedVal.size()-1] == '\"')
75  {
76  quotedVal.erase(val.size()-1);
77  }
78  else // space inside value string
79  {
80  std::getline(ssconn, val, '\"');
81  quotedVal = quotedVal + " " + val;
82  std::string keepspace;
83  std::getline(ssconn, keepspace, ' ');
84  }
85 
86  val = quotedVal;
87  }
88 
89  if ("dbname" == key || "db" == key)
90  {
91  dbname = val;
92  }
93  else if ("timeout" == key)
94  {
95  std::istringstream converter(val);
96  converter >> timeout;
97  }
98  else if ("synchronous" == key)
99  {
100  synchronous = val;
101  }
102  else if ("shared_cache" == key && "true" == val)
103  {
104  connection_flags |= SQLITE_OPEN_SHAREDCACHE;
105  }
106  }
107 
108  int res = sqlite3_open_v2(dbname.c_str(), &conn_, connection_flags, NULL);
109  check_sqlite_err(conn_, res, "Cannot establish connection to the database. ");
110 
111  if (!synchronous.empty())
112  {
113  std::string const query("pragma synchronous=" + synchronous);
114  std::string const errMsg("Query failed: " + query);
115  execude_hardcoded(conn_, query.c_str(), errMsg.c_str());
116  }
117 
118  res = sqlite3_busy_timeout(conn_, timeout * 1000);
119  check_sqlite_err(conn_, res, "Failed to set busy timeout for connection. ");
120 
121 }
122 
124 {
125  clean_up();
126 }
127 
129 {
130  execude_hardcoded(conn_, "BEGIN", "Cannot begin transaction.");
131 }
132 
134 {
135  execude_hardcoded(conn_, "COMMIT", "Cannot commit transaction.");
136 }
137 
139 {
140  execude_hardcoded(conn_, "ROLLBACK", "Cannot rollback transaction.");
141 }
142 
144 {
145  sqlite3_close(conn_);
146 }
147 
149 {
150  return new sqlite3_statement_backend(*this);
151 }
152 
154 {
155  return new sqlite3_rowid_backend(*this);
156 }
157 
159 {
160  return new sqlite3_blob_backend(*this);
161 }
virtual sqlite3_statement_backend * make_statement_backend()
virtual sqlite3_blob_backend * make_blob_backend()
SOCI_SQLITE3_DECL sqlite3_backend_factory const sqlite3
std::string const & get_connect_string() const
std::string connectString
Definition: test-db2.cpp:21
std::vector< ISM::CombinatorialTrainerParameters > parameters
sqlite3_session_backend(connection_parameters const &parameters)
virtual sqlite3_rowid_backend * make_rowid_backend()


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:41