Zserio C++ runtime library  1.0.1
Built for Zserio 2.14.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 <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  sqlite3_close_v2(m_connection); // sqlite3_close_v2(NULL) is a harmless no-op
144 
145  m_connection = connection;
146  m_connectionType = connectionType;
147 }
148 
150 {
151  return m_connectionType;
152 }
153 
155 {
156  return m_connection;
157 }
158 
160 {
161  std::unique_ptr<sqlite3_stmt, SqliteFinalizer> statement(prepareStatement(sqlQuery));
162  int result = sqlite3_step(statement.get());
163  if (result != SQLITE_DONE)
164  {
165  throw SqliteException("SqliteConnection::executeUpdate(): sqlite3_step failed: ")
166  << SqliteErrorCode(result);
167  }
168 }
169 
170 inline sqlite3_stmt* SqliteConnection::prepareStatement(StringView sqlQuery)
171 {
172  sqlite3_stmt* statement = nullptr;
173  const int result = sqlite3_prepare_v2(
174  m_connection, sqlQuery.data(), static_cast<int>(sqlQuery.size()), &statement, nullptr);
175  if (result != SQLITE_OK)
176  {
177  throw SqliteException("SqliteConnection::prepareStatement(): sqlite3_prepare_v2() failed: ")
178  << SqliteErrorCode(result);
179  }
180 
181  return statement;
182 }
183 
185 {
186  bool wasTransactionStarted = false;
187  if (sqlite3_get_autocommit(m_connection) != 0)
188  {
189  executeUpdate("BEGIN;");
190  wasTransactionStarted = true;
191  }
192 
193  return wasTransactionStarted;
194 }
195 
196 inline void SqliteConnection::endTransaction(bool wasTransactionStarted)
197 {
198  if (wasTransactionStarted)
199  executeUpdate("COMMIT;");
200 }
201 
202 } // namespace zserio
203 
204 #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)