SQLiteC++  0.5.0
SQLiteC++ is a smart and easy to use C++ SQLite3 wrapper.
 All Classes Namespaces Files Functions Friends Macros
Database.h
Go to the documentation of this file.
1 /**
2  * @file Database.h
3  * @brief Management of a SQLite Database Connection.
4  *
5  * Copyright (c) 2012-2013 Sebastien Rombauts (sebastien.rombauts@gmail.com)
6  *
7  * Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
8  * or copy at http://opensource.org/licenses/MIT)
9  */
10 #pragma once
11 
12 #include <sqlite3.h>
13 #include "Exception.h"
14 #include "Column.h"
15 
16 namespace SQLite
17 {
18 
19 /**
20  * @brief RAII management of a SQLite Database Connection.
21  *
22  * A Database object manage a list of all SQLite Statements associated with the
23  * underlying SQLite 3 database connection.
24  *
25  * Resource Acquisition Is Initialization (RAII) means that the Database Connection
26  * is opened in the constructor and closed in the destructor, so that there is
27  * no need to worry about memory management or the validity of the underlying SQLite Connection.
28  */
29 class Database
30 {
31  friend class Statement; // Give Statement constructor access to the mpSQLite Connection Handle
32 
33 public:
34  /**
35  * @brief Open the provided database UTF-8 filename.
36  *
37  * Uses sqlite3_open_v2() with readonly default flag, which is the opposite behavior
38  * of the old sqlite3_open() function (READWRITE+CREATE).
39  * This makes sense if you want to use it on a readonly filesystem
40  * or to prevent creation of a void file when a required file is missing.
41  *
42  * Exception is thrown in case of error, then the Database object is NOT constructed.
43  *
44  * @param[in] apFilename UTF-8 path/uri to the database file ("filename" sqlite3 parameter)
45  * @param[in] aFlags SQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE...
46  */
47  Database(const char* apFilename, const int aFlags = SQLITE_OPEN_READONLY); // throw(SQLite::Exception);
48 
49  /**
50  * @brief Close the SQLite database connection.
51  *
52  * All SQLite statements must have been finalized before,
53  * so all Statement objects must have been unregistered.
54  */
55  virtual ~Database(void) throw(); // nothrow
56 
57  /**
58  * @brief Shortcut to execute one or multiple statements without results.
59  *
60  * This is useful for any kind of statements other than the Data Query Language (DQL) "SELECT" :
61  * - Data Definition Language (DDL) statements "CREATE", "ALTER" and "DROP"
62  * - Data Manipulation Language (DML) statements "INSERT", "UPDATE" and "DELETE"
63  * - Data Control Language (DCL) statements "GRANT", "REVOKE", "COMMIT" and "ROLLBACK"
64  *
65  * @see Statement::exec() to handle precompiled statements (for better performances) without results
66  * @see Statement::executeStep() to handle "SELECT" queries with results
67  *
68  * @param[in] apQueries one or multiple UTF-8 encoded, semicolon-separate SQL statements
69  *
70  * @return number of rows modified by those SQL statements (INSERT, UPDATE or DELETE)
71  *
72  * @throw SQLite::Exception in case of error
73  */
74  int exec(const char* apQueries); // throw(SQLite::Exception);
75 
76  /**
77  * @brief Shortcut to execute a one step query and fetch the first column of the result.
78  *
79  * This is a shortcut to execute a simple statement with a single result.
80  * This should be used only for non reusable queries (else you should use a Statement with bind()).
81  * This should be used only for queries with expected results (else an exception is fired).
82  *
83  * @warning WARNING: Be very careful with this dangerous method: you have to
84  * make a COPY OF THE result, else it will be destroy before the next line
85  * (when the underlying temporary Statement and Column objects are destroyed)
86  *
87  * @see also Statement class for handling queries with multiple results
88  *
89  * @param[in] apQuery an UTF-8 encoded SQL query
90  *
91  * @return a temporary Column object with the first column of result.
92  *
93  * @throw SQLite::Exception in case of error
94  */
95  Column execAndGet(const char* apQuery); // throw(SQLite::Exception);
96 
97  /**
98  * @brief Shortcut to test if a table exists.
99  *
100  * Table names are case sensitive.
101  *
102  * @param[in] apTableName an UTF-8 encoded case sensitive Table name
103  *
104  * @return true if the table exists.
105  *
106  * @throw SQLite::Exception in case of error
107  */
108  bool tableExists(const char* apTableName); // throw(SQLite::Exception);
109 
110  /**
111  * @brief Set a busy handler that sleeps for a specified amount of time when a table is locked.
112  *
113  * @param[in] aTimeoutMs Amount of milliseconds to wait before returning SQLITE_BUSY
114  */
115  inline int setBusyTimeout(int aTimeoutMs) // throw(); nothrow
116  {
117  return sqlite3_busy_timeout(mpSQLite, aTimeoutMs);
118  }
119 
120  /**
121  * @brief Get the rowid of the most recent successful INSERT into the database from the current connection.
122  *
123  * @return Rowid of the most recent successful INSERT into the database, or 0 if there was none.
124  */
125  inline sqlite3_int64 getLastInsertRowid(void) const // throw(); nothrow
126  {
127  return sqlite3_last_insert_rowid(mpSQLite);
128  }
129 
130  /**
131  * @brief Return the filename used to open the database
132  */
133  inline const std::string& getFilename(void) const
134  {
135  return mFilename;
136  }
137 
138  /**
139  * @brief Return UTF-8 encoded English language explanation of the most recent error.
140  */
141  inline const char* errmsg(void) const
142  {
143  return sqlite3_errmsg(mpSQLite);
144  }
145 
146 private:
147  /// @{ Database must be non-copyable
148  Database(const Database&);
149  Database& operator=(const Database&);
150  /// @}
151 
152  /**
153  * @brief Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
154  */
155  void check(const int aRet) const; // throw(SQLite::Exception);
156 
157 private:
158  sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
159  std::string mFilename; //!< UTF-8 filename used to open the database
160 };
161 
162 
163 } // namespace SQLite