Zserio C++ runtime library  1.0.2
Built for Zserio 2.14.1
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 <memory>
5 
8 #include "zserio/StringView.h"
9 
10 #include "sqlite3.h"
11 
12 namespace zserio
13 {
14 
21 {
22 public:
27  {
30  };
31 
38  explicit SqliteConnection(
39  sqlite3* connection = nullptr, ConnectionType connectionType = INTERNAL_CONNECTION);
40 
45 
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),
131  m_connectionType(connectionType)
132 {}
133 
135 {
136  reset();
137 }
138 
139 inline void SqliteConnection::reset(sqlite3* connection, ConnectionType connectionType)
140 {
141  // close connection only if it is internal
142  if (m_connectionType == INTERNAL_CONNECTION)
143  {
144  sqlite3_close_v2(m_connection); // sqlite3_close_v2(NULL) is a harmless no-op
145  }
146 
147  m_connection = connection;
148  m_connectionType = connectionType;
149 }
150 
152 {
153  return m_connectionType;
154 }
155 
157 {
158  return m_connection;
159 }
160 
162 {
163  std::unique_ptr<sqlite3_stmt, SqliteFinalizer> statement(prepareStatement(sqlQuery));
164  int result = sqlite3_step(statement.get());
165  if (result != SQLITE_DONE)
166  {
167  throw SqliteException("SqliteConnection::executeUpdate(): sqlite3_step failed: ")
168  << SqliteErrorCode(result);
169  }
170 }
171 
172 inline sqlite3_stmt* SqliteConnection::prepareStatement(StringView sqlQuery)
173 {
174  sqlite3_stmt* statement = nullptr;
175  const int result = sqlite3_prepare_v2(
176  m_connection, sqlQuery.data(), static_cast<int>(sqlQuery.size()), &statement, nullptr);
177  if (result != SQLITE_OK)
178  {
179  throw SqliteException("SqliteConnection::prepareStatement(): sqlite3_prepare_v2() failed: ")
180  << SqliteErrorCode(result);
181  }
182 
183  return statement;
184 }
185 
187 {
188  bool wasTransactionStarted = false;
189  if (sqlite3_get_autocommit(m_connection) != 0)
190  {
191  executeUpdate("BEGIN;");
192  wasTransactionStarted = true;
193  }
194 
195  return wasTransactionStarted;
196 }
197 
198 inline void SqliteConnection::endTransaction(bool wasTransactionStarted)
199 {
200  if (wasTransactionStarted)
201  {
202  executeUpdate("COMMIT;");
203  }
204 }
205 
206 } // namespace zserio
207 
208 #endif // ZSERIO_SQL_CONNECTION_H_INC
constexpr size_type size() const noexcept
Definition: StringView.h:240
constexpr const_pointer data() const noexcept
Definition: StringView.h:230
sqlite3_stmt * prepareStatement(StringView sqlQuery)
SqliteConnection & operator=(SqliteConnection &&)=delete
SqliteConnection(SqliteConnection &&)=delete
SqliteConnection(const SqliteConnection &)=delete
SqliteConnection & operator=(const SqliteConnection &)=delete
SqliteConnection(sqlite3 *connection=nullptr, ConnectionType connectionType=INTERNAL_CONNECTION)
ConnectionType getConnectionType() const
void endTransaction(bool wasTransactionStarted)
void executeUpdate(StringView sqlQuery)
void reset(sqlite3 *connection=nullptr, ConnectionType connectionType=INTERNAL_CONNECTION)