Merge pull request #360 from Kacperos155/small_improvements

Small improvements and code cleaning
This commit is contained in:
Sébastien Rombauts 2022-09-18 14:16:54 +02:00 committed by GitHub
commit c7cffad617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 40 additions and 49 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ build
example1 example1
*.a *.a
.vs/
.vscode/ .vscode/
/SQLiteCpp.sln /SQLiteCpp.sln
*.ncb *.ncb

View File

@ -14,6 +14,7 @@
#include <SQLiteCpp/Database.h> #include <SQLiteCpp/Database.h>
#include <string> #include <string>
#include <memory>
// Forward declaration to avoid inclusion of <sqlite3.h> in a header // Forward declaration to avoid inclusion of <sqlite3.h> in a header
struct sqlite3_backup; struct sqlite3_backup;
@ -114,13 +115,12 @@ public:
int executeStep(const int aNumPage = -1); int executeStep(const int aNumPage = -1);
/// Return the number of source pages still to be backed up as of the most recent call to executeStep(). /// Return the number of source pages still to be backed up as of the most recent call to executeStep().
int getRemainingPageCount(); int getRemainingPageCount() const;
/// Return the total number of pages in the source database as of the most recent call to executeStep(). /// Return the total number of pages in the source database as of the most recent call to executeStep().
int getTotalPageCount(); int getTotalPageCount() const;
private: private:
// TODO: use std::unique_ptr with a custom deleter to call sqlite3_backup_finish()
sqlite3_backup* mpSQLiteBackup = nullptr; ///< Pointer to SQLite Database Backup Handle sqlite3_backup* mpSQLiteBackup = nullptr; ///< Pointer to SQLite Database Backup Handle
}; };

View File

@ -55,10 +55,6 @@ public:
*/ */
explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex); explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex);
// default destructor: the finalization will be done by the destructor of the last shared pointer
// default copy constructor and assignment operator are perfectly suited :
// they copy the Statement::Ptr which in turn increments the reference counter.
/** /**
* @brief Return a pointer to the named assigned to this result column (potentially aliased) * @brief Return a pointer to the named assigned to this result column (potentially aliased)
* *

View File

@ -92,7 +92,7 @@ extern const int OPEN_NOFOLLOW; // SQLITE_OPEN_NOFOLLOW
extern const int OK; ///< SQLITE_OK (used by check() bellow) extern const int OK; ///< SQLITE_OK (used by check() bellow)
extern const char* VERSION; ///< SQLITE_VERSION string from the sqlite3.h used at compile time extern const char* const VERSION; ///< SQLITE_VERSION string from the sqlite3.h used at compile time
extern const int VERSION_NUMBER; ///< SQLITE_VERSION_NUMBER from the sqlite3.h used at compile time extern const int VERSION_NUMBER; ///< SQLITE_VERSION_NUMBER from the sqlite3.h used at compile time
/// Return SQLite version string using runtime call to the compiled library /// Return SQLite version string using runtime call to the compiled library
@ -342,7 +342,7 @@ public:
* *
* @return the sqlite result code. * @return the sqlite result code.
*/ */
int tryExec(const std::string aQueries) noexcept int tryExec(const std::string& aQueries) noexcept
{ {
return tryExec(aQueries.c_str()); return tryExec(aQueries.c_str());
} }
@ -403,7 +403,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
bool tableExists(const char* apTableName); bool tableExists(const char* apTableName) const;
/** /**
* @brief Shortcut to test if a table exists. * @brief Shortcut to test if a table exists.
@ -416,7 +416,7 @@ public:
* *
* @throw SQLite::Exception in case of error * @throw SQLite::Exception in case of error
*/ */
bool tableExists(const std::string& aTableName) bool tableExists(const std::string& aTableName) const
{ {
return tableExists(aTableName.c_str()); return tableExists(aTableName.c_str());
} }
@ -566,7 +566,7 @@ public:
static Header getHeaderInfo(const std::string& aFilename); static Header getHeaderInfo(const std::string& aFilename);
// Parse SQLite header data from a database file. // Parse SQLite header data from a database file.
Header getHeaderInfo() Header getHeaderInfo() const
{ {
return getHeaderInfo(mFilename); return getHeaderInfo(mFilename);
} }

View File

@ -66,7 +66,7 @@ class Savepoint {
* Exception is thrown in case of error, then the Savepoint is NOT * Exception is thrown in case of error, then the Savepoint is NOT
* initiated. * initiated.
*/ */
Savepoint(Database& aDatabase, std::string name); Savepoint(Database& aDatabase, const std::string& name);
// Savepoint is non-copyable // Savepoint is non-copyable
Savepoint(const Savepoint&) = delete; Savepoint(const Savepoint&) = delete;
@ -90,6 +90,6 @@ class Savepoint {
private: private:
Database& mDatabase; ///< Reference to the SQLite Database Connection Database& mDatabase; ///< Reference to the SQLite Database Connection
std::string msName; ///< Name of the Savepoint std::string msName; ///< Name of the Savepoint
bool mbReleased; ///< True when release has been called bool mbReleased = false; ///< True when release has been called
}; };
} // namespace SQLite } // namespace SQLite

View File

@ -74,18 +74,14 @@ public:
Statement(aDatabase, aQuery.c_str()) Statement(aDatabase, aQuery.c_str())
{} {}
/**
* @brief Move an SQLite statement.
*
* @param[in] aStatement Statement to move
*/
Statement(Statement&& aStatement) noexcept;
Statement& operator=(Statement&& aStatement) noexcept = default;
// Statement is non-copyable // Statement is non-copyable
Statement(const Statement&) = delete; Statement(const Statement&) = delete;
Statement& operator=(const Statement&) = delete; Statement& operator=(const Statement&) = delete;
Statement(Statement&& aStatement) noexcept;
Statement& operator=(Statement&& aStatement) noexcept = default;
// TODO: Change Statement move constructor to default
/// Finalize and unregister the SQL query from the SQLite Database Connection. /// Finalize and unregister the SQL query from the SQLite Database Connection.
/// The finalization will be done by the destructor of the last shared pointer /// The finalization will be done by the destructor of the last shared pointer
~Statement() = default; ~Statement() = default;
@ -702,12 +698,12 @@ private:
std::string mQuery; //!< UTF-8 SQL Query std::string mQuery; //!< UTF-8 SQL Query
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object
int mColumnCount{0}; //!< Number of columns in the result of the prepared statement int mColumnCount = 0; //!< Number of columns in the result of the prepared statement
bool mbHasRow{false}; //!< true when a row has been fetched with executeStep() bool mbHasRow = false; //!< true when a row has been fetched with executeStep()
bool mbDone{false}; //!< true when the last executeStep() had no more row to fetch bool mbDone = false; //!< true when the last executeStep() had no more row to fetch
/// Map of columns index by name (mutable so getColumnIndex can be const) /// Map of columns index by name (mutable so getColumnIndex can be const)
mutable std::map<std::string, int> mColumnNames; mutable std::map<std::string, int, std::less<>> mColumnNames;
}; };

View File

@ -88,7 +88,7 @@ public:
private: private:
Database& mDatabase; ///< Reference to the SQLite Database Connection Database& mDatabase; ///< Reference to the SQLite Database Connection
bool mbCommited; ///< True when commit has been called bool mbCommited = false; ///< True when commit has been called
}; };

View File

@ -69,15 +69,16 @@ int Backup::executeStep(const int aNumPage /* = -1 */)
} }
// Get the number of remaining source pages to be copied in this backup process // Get the number of remaining source pages to be copied in this backup process
int Backup::getRemainingPageCount() int Backup::getRemainingPageCount() const
{ {
return sqlite3_backup_remaining(mpSQLiteBackup); return sqlite3_backup_remaining(mpSQLiteBackup);
} }
// Get the number of total source pages to be copied in this backup process // Get the number of total source pages to be copied in this backup process
int Backup::getTotalPageCount() int Backup::getTotalPageCount() const
{ {
return sqlite3_backup_pagecount(mpSQLiteBackup); return sqlite3_backup_pagecount(mpSQLiteBackup);
} }
} // namespace SQLite } // namespace SQLite

View File

@ -78,7 +78,7 @@ double Column::getDouble() const noexcept
const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept
{ {
auto pText = reinterpret_cast<const char*>(sqlite3_column_text(mStmtPtr.get(), mIndex)); auto pText = reinterpret_cast<const char*>(sqlite3_column_text(mStmtPtr.get(), mIndex));
return (pText?pText:apDefaultValue); return (pText ? pText : apDefaultValue);
} }
// Return a pointer to the blob value (*not* NULL terminated) of the column specified by its index starting at 0 // Return a pointer to the blob value (*not* NULL terminated) of the column specified by its index starting at 0

View File

@ -27,6 +27,7 @@
namespace SQLite namespace SQLite
{ {
const int OK = SQLITE_OK;
const int OPEN_READONLY = SQLITE_OPEN_READONLY; const int OPEN_READONLY = SQLITE_OPEN_READONLY;
const int OPEN_READWRITE = SQLITE_OPEN_READWRITE; const int OPEN_READWRITE = SQLITE_OPEN_READWRITE;
const int OPEN_CREATE = SQLITE_OPEN_CREATE; const int OPEN_CREATE = SQLITE_OPEN_CREATE;
@ -42,9 +43,7 @@ const int OPEN_NOFOLLOW = SQLITE_OPEN_NOFOLLOW;
const int OPEN_NOFOLLOW = 0; const int OPEN_NOFOLLOW = 0;
#endif #endif
const int OK = SQLITE_OK; const char* const VERSION = SQLITE_VERSION;
const char* VERSION = SQLITE_VERSION;
const int VERSION_NUMBER = SQLITE_VERSION_NUMBER; const int VERSION_NUMBER = SQLITE_VERSION_NUMBER;
// Return SQLite version string using runtime call to the compiled library // Return SQLite version string using runtime call to the compiled library
@ -142,7 +141,7 @@ Column Database::execAndGet(const char* apQuery)
} }
// Shortcut to test if a table exists. // Shortcut to test if a table exists.
bool Database::tableExists(const char* apTableName) bool Database::tableExists(const char* apTableName) const
{ {
Statement query(*this, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?"); Statement query(*this, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?");
query.bind(1, apTableName); query.bind(1, apTableName);
@ -439,8 +438,8 @@ void Database::backup(const char* apFilename, BackupType aType)
Database otherDatabase(apFilename, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); Database otherDatabase(apFilename, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
// For a 'Save' operation, data is copied from the current Database to the other. A 'Load' is the reverse. // For a 'Save' operation, data is copied from the current Database to the other. A 'Load' is the reverse.
Database& src = (aType == Save ? *this : otherDatabase); Database& src = (aType == BackupType::Save ? *this : otherDatabase);
Database& dest = (aType == Save ? otherDatabase : *this); Database& dest = (aType == BackupType::Save ? otherDatabase : *this);
// Set up the backup procedure to copy between the "main" databases of each connection // Set up the backup procedure to copy between the "main" databases of each connection
Backup bkp(dest, src); Backup bkp(dest, src);

View File

@ -18,8 +18,8 @@
namespace SQLite { namespace SQLite {
// Begins the SQLite savepoint // Begins the SQLite savepoint
Savepoint::Savepoint(Database& aDatabase, std::string aName) Savepoint::Savepoint(Database& aDatabase, const std::string& aName)
: mDatabase(aDatabase), msName(aName), mbReleased(false) { : mDatabase(aDatabase), msName(aName) {
// workaround because you cannot bind to SAVEPOINT // workaround because you cannot bind to SAVEPOINT
// escape name for use in query // escape name for use in query
Statement stmt(mDatabase, "SELECT quote(?)"); Statement stmt(mDatabase, "SELECT quote(?)");

View File

@ -275,7 +275,7 @@ int Statement::getColumnIndex(const char* apName) const
for (int i = 0; i < mColumnCount; ++i) for (int i = 0; i < mColumnCount; ++i)
{ {
const char* pName = sqlite3_column_name(getPreparedStatement(), i); const char* pName = sqlite3_column_name(getPreparedStatement(), i);
mColumnNames[pName] = i; mColumnNames.emplace(pName, i);
} }
} }
@ -288,7 +288,7 @@ int Statement::getColumnIndex(const char* apName) const
return iIndex->second; return iIndex->second;
} }
const char * Statement::getColumnDeclaredType(const int aIndex) const const char* Statement::getColumnDeclaredType(const int aIndex) const
{ {
checkIndex(aIndex); checkIndex(aIndex);
const char * result = sqlite3_column_decltype(getPreparedStatement(), aIndex); const char * result = sqlite3_column_decltype(getPreparedStatement(), aIndex);

View File

@ -21,8 +21,7 @@ namespace SQLite
// Begins the SQLite transaction // Begins the SQLite transaction
Transaction::Transaction(Database& aDatabase, TransactionBehavior behavior) : Transaction::Transaction(Database& aDatabase, TransactionBehavior behavior) :
mDatabase(aDatabase), mDatabase(aDatabase)
mbCommited(false)
{ {
const char *stmt; const char *stmt;
switch (behavior) { switch (behavior) {
@ -43,8 +42,7 @@ Transaction::Transaction(Database& aDatabase, TransactionBehavior behavior) :
// Begins the SQLite transaction // Begins the SQLite transaction
Transaction::Transaction(Database &aDatabase) : Transaction::Transaction(Database &aDatabase) :
mDatabase(aDatabase), mDatabase(aDatabase)
mbCommited(false)
{ {
mDatabase.exec("BEGIN"); mDatabase.exec("BEGIN");
} }