Zserio C++ runtime library  1.0.0
Built for Zserio 2.13.0
SqliteConnection.h
Go to the documentation of this file.
1 #ifndef ZSERIO_SQL_CONNECTION_H_INC
2 #define ZSERIO_SQL_CONNECTION_H_INC
3 
4 #include "sqlite3.h"
5 
6 #include <memory>
7 
8 #include "zserio/StringView.h"
10 #include "zserio/SqliteFinalizer.h"
11 
12 namespace zserio
13 {
14 
21 {
22 public:
27  {
30  };
31 
38  explicit SqliteConnection(sqlite3* connection = nullptr,
39  ConnectionType connectionType = INTERNAL_CONNECTION);
40 
45 
50  SqliteConnection(const SqliteConnection&) = delete;
52 
63  void reset(sqlite3* connection = nullptr, ConnectionType connectionType = INTERNAL_CONNECTION);
64 
73 
79  sqlite3* getConnection();
80 
86  void executeUpdate(StringView sqlQuery);
87 
97  sqlite3_stmt* prepareStatement(StringView sqlQuery);
98 
104  bool startTransaction();
105 
122  void endTransaction(bool wasTransactionStarted);
123 
124 private:
125  sqlite3* m_connection;
126  ConnectionType m_connectionType;
127 };
128 
129 inline SqliteConnection::SqliteConnection(sqlite3* connection, ConnectionType connectionType)
130 : m_connection(connection), m_connectionType(connectionType)
131 {}
132 
134 {
135  reset();
136 }
137 
138 inline void SqliteConnection::reset(sqlite3* connection, ConnectionType connectionType)
139 {
140  // close connection only if it is internal
141  if (m_connectionType == INTERNAL_CONNECTION)
142  sqlite3_close_v2(m_connection); // sqlite3_close_v2(NULL) is a harmless no-op
143 
144  m_connection = connection;
145  m_connectionType = connectionType;
146 }
147 
149 {
150  return m_connectionType;
151 }
152 
154 {
155  return m_connection;
156 }
157 
159 {
160  std::unique_ptr<sqlite3_stmt, SqliteFinalizer> statement(prepareStatement(sqlQuery));
161  int result = sqlite3_step(statement.get());
162  if (result != SQLITE_DONE)
163  {
164  throw SqliteException("SqliteConnection::executeUpdate(): sqlite3_step failed: ") <<
165  SqliteErrorCode(result);
166  }
167 }
168 
169 inline sqlite3_stmt* SqliteConnection::prepareStatement(StringView sqlQuery)
170 {
171  sqlite3_stmt* statement = nullptr;
172  const int result = sqlite3_prepare_v2(m_connection, sqlQuery.data(), static_cast<int>(sqlQuery.size()),
173  &statement, nullptr);
174  if (result != SQLITE_OK)
175  {
176  throw SqliteException("SqliteConnection::prepareStatement(): sqlite3_prepare_v2() failed: ") <<
177  SqliteErrorCode(result);
178  }
179 
180  return statement;
181 }
182 
184 {
185  bool wasTransactionStarted = false;
186  if (sqlite3_get_autocommit(m_connection) != 0)
187  {
188  executeUpdate("BEGIN;");
189  wasTransactionStarted = true;
190  }
191 
192  return wasTransactionStarted;
193 }
194 
195 inline void SqliteConnection::endTransaction(bool wasTransactionStarted)
196 {
197  if (wasTransactionStarted)
198  executeUpdate("COMMIT;");
199 }
200 
201 } // namespace zserio
202 
203 #endif // ZSERIO_SQL_CONNECTION_H_INC
constexpr size_type size() const noexcept
Definition: StringView.h:237
SqliteConnection & operator=(const SqliteConnection &)=delete
constexpr const_pointer data() const noexcept
Definition: StringView.h:227
ConnectionType getConnectionType() const
sqlite3_stmt * prepareStatement(StringView sqlQuery)
void reset(sqlite3 *connection=nullptr, ConnectionType connectionType=INTERNAL_CONNECTION)
SqliteConnection(sqlite3 *connection=nullptr, ConnectionType connectionType=INTERNAL_CONNECTION)
void executeUpdate(StringView sqlQuery)
void endTransaction(bool wasTransactionStarted)