SQLiteXX  0.1.0
 All Classes Namespaces Files Functions Enumerations Enumerator
DBConnection.cpp
1 #include "DBConnection.h"
2 
3 namespace sqlite
4 {
5  const std::chrono::minutes dbconnection::DEFAULT_TIMEOUT(10);
6 
8  m_handle()
9  {}
10 
11  dbconnection::dbconnection(const dbconnection& other) noexcept :
12  m_handle(other.m_handle)
13  {}
14 
16  m_handle(std::move(other.m_handle))
17  {}
18 
20  {
21  if (this != &other) {
22  m_handle = other.m_handle;
23  }
24 
25  return *this;
26  }
27 
29  {
30  assert(this != &other);
31  m_handle = std::move(other.m_handle);
32  return *this;
33  }
34 
36  const std::string& filename,
37  openmode mode,
38  const std::chrono::milliseconds timeout)
39  {
40  open(filename, mode);
41  sqlite3_busy_timeout(handle(), static_cast<int>(timeout.count()));
42  }
43 
45  const std::string& filename,
46  const std::chrono::milliseconds timeout)
47  {
48  open(filename);
49  sqlite3_busy_timeout(handle(), static_cast<int>(timeout.count()));
50  }
51 
53  const std::u16string& filename,
54  const std::chrono::milliseconds timeout)
55  {
56  open(filename);
57  sqlite3_busy_timeout(handle(), static_cast<int>(timeout.count()));
58  }
59 
61  {
62  return dbconnection(":memory:");
63  }
64 
66  {
67  return dbconnection(u":memory:");
68  }
69 
71  {
72  sqlite3_mutex* mutexPtr = sqlite3_db_mutex(m_handle.get());
73  if (mutexPtr == nullptr) {
74  throw SQLiteXXException("This database connection was not able to create a valid mutex.");
75  }
76  return sqlite::mutex(mutexPtr);
77  }
78 
79  dbconnection::operator bool() const noexcept
80  {
81  return static_cast<bool>(m_handle);
82  }
83 
84  sqlite3* dbconnection::handle() const noexcept
85  {
86  return m_handle.get();
87  }
88 
89  void dbconnection::open(const std::string& filename, openmode mode)
90  {
91  sqlite3 *connection;
92  if (SQLITE_OK != sqlite3_open_v2(filename.c_str(), &connection, static_cast<int>(mode), nullptr)) {
93  const sqlite::exception exception(connection);
94  sqlite3_close(connection);
95  throw exception;
96  }
97 
98  m_handle.reset(connection, sqlite3_close);
99  }
100 
101  void dbconnection::open(const std::u16string& filename)
102  {
103  sqlite3 *connection;
104  if (SQLITE_OK != sqlite3_open16(filename.c_str(), &connection)) {
105  const sqlite::exception exception(connection);
106  sqlite3_close(connection);
107  throw exception;
108  }
109 
110  m_handle.reset(connection, sqlite3_close);
111  }
112 
113  long long dbconnection::row_id() const noexcept
114  {
115  return sqlite3_last_insert_rowid(handle());
116  }
117 }
dbconnection & operator=(const dbconnection &other) noexcept
Copy assignment operator.
sqlite3 * handle() const noexcept
Returns pointer to the underlying "sqlite3" object.
openmode
Different ways to open a dbconnection.
Definition: Open.h:14
Encapsulation of the error code and message from SQLite3, based on std::runtime_error.
Definition: Exception.h:14
dbconnection() noexcept
Default constructor.
Definition: DBConnection.cpp:7
Helps with serializing access to a database connection.
Definition: Mutex.h:19
long long row_id() const noexcept
Returns the rowid of the most recent successful "INSERT" into a rowid table or virtual table on datab...
static dbconnection wide_memory()
Create a purely in memory database with UTF-16 as the native byte order.
sqlite::mutex mutex()
Returns a mutex that serializes access to the database.
static dbconnection memory()
Create a purely in memory database.
Class that represents a connection to a database.
Definition: DBConnection.h:24
void open(const std::string &filename, openmode mode=openmode::read_write|openmode::create)
Open an SQLite database file as specified by the filename argument.