Skip to content

Commit be5400c

Browse files
committed
Small improvements & code cleanup
1 parent 9158225 commit be5400c

File tree

10 files changed

+54
-58
lines changed

10 files changed

+54
-58
lines changed

include/SQLiteCpp/Backup.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <SQLiteCpp/Database.h>
1515

1616
#include <string>
17+
#include <memory>
1718

1819
// Forward declaration to avoid inclusion of <sqlite3.h> in a header
1920
struct sqlite3_backup;
@@ -95,9 +96,6 @@ class Backup
9596
Backup(const Backup&) = delete;
9697
Backup& operator=(const Backup&) = delete;
9798

98-
/// Release the SQLite Backup resource.
99-
~Backup();
100-
10199
/**
102100
* @brief Execute a step of backup with a given number of source pages to be copied
103101
*
@@ -114,14 +112,19 @@ class Backup
114112
int executeStep(const int aNumPage = -1);
115113

116114
/// Return the number of source pages still to be backed up as of the most recent call to executeStep().
117-
int getRemainingPageCount();
115+
int getRemainingPageCount() const;
118116

119117
/// Return the total number of pages in the source database as of the most recent call to executeStep().
120-
int getTotalPageCount();
118+
int getTotalPageCount() const;
121119

122120
private:
123-
// TODO: use std::unique_ptr with a custom deleter to call sqlite3_backup_finish()
124-
sqlite3_backup* mpSQLiteBackup = nullptr; ///< Pointer to SQLite Database Backup Handle
121+
// Deleter functor to use with smart pointers to close the SQLite database backup in an RAII fashion.
122+
struct Deleter
123+
{
124+
void operator()(sqlite3_backup* apBackup);
125+
};
126+
127+
std::unique_ptr<sqlite3_backup, Deleter> mpSQLiteBackup{}; ///< Pointer to SQLite Database Backup Handle
125128
};
126129

127130
} // namespace SQLite

include/SQLiteCpp/Database.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ extern const int OPEN_NOFOLLOW; // SQLITE_OPEN_NOFOLLOW
7878

7979
extern const int OK; ///< SQLITE_OK (used by check() bellow)
8080

81-
extern const char* VERSION; ///< SQLITE_VERSION string from the sqlite3.h used at compile time
82-
extern const int VERSION_NUMBER; ///< SQLITE_VERSION_NUMBER from the sqlite3.h used at compile time
81+
extern const char* const VERSION; ///< SQLITE_VERSION string from the sqlite3.h used at compile time
82+
extern const int VERSION_NUMBER; ///< SQLITE_VERSION_NUMBER from the sqlite3.h used at compile time
8383

8484
/// Return SQLite version string using runtime call to the compiled library
8585
const char* getLibVersion() noexcept;
@@ -328,7 +328,7 @@ class Database
328328
*
329329
* @return the sqlite result code.
330330
*/
331-
int tryExec(const std::string aQueries) noexcept
331+
int tryExec(const std::string& aQueries) noexcept
332332
{
333333
return tryExec(aQueries.c_str());
334334
}
@@ -389,7 +389,7 @@ class Database
389389
*
390390
* @throw SQLite::Exception in case of error
391391
*/
392-
bool tableExists(const char* apTableName);
392+
bool tableExists(const char* apTableName) const;
393393

394394
/**
395395
* @brief Shortcut to test if a table exists.
@@ -402,7 +402,7 @@ class Database
402402
*
403403
* @throw SQLite::Exception in case of error
404404
*/
405-
bool tableExists(const std::string& aTableName)
405+
bool tableExists(const std::string& aTableName) const
406406
{
407407
return tableExists(aTableName.c_str());
408408
}
@@ -552,7 +552,7 @@ class Database
552552
static Header getHeaderInfo(const std::string& aFilename);
553553

554554
// Parse SQLite header data from a database file.
555-
Header getHeaderInfo()
555+
Header getHeaderInfo() const
556556
{
557557
return getHeaderInfo(mFilename);
558558
}

include/SQLiteCpp/Savepoint.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Savepoint {
6666
* Exception is thrown in case of error, then the Savepoint is NOT
6767
* initiated.
6868
*/
69-
Savepoint(Database& aDatabase, std::string name);
69+
Savepoint(Database& aDatabase, const std::string& name);
7070

7171
// Savepoint is non-copyable
7272
Savepoint(const Savepoint&) = delete;
@@ -88,8 +88,8 @@ class Savepoint {
8888
void rollback();
8989

9090
private:
91-
Database& mDatabase; ///< Reference to the SQLite Database Connection
92-
std::string msName; ///< Name of the Savepoint
93-
bool mbReleased; ///< True when release has been called
91+
Database& mDatabase; ///< Reference to the SQLite Database Connection
92+
std::string msName; ///< Name of the Savepoint
93+
bool mbReleased{ false }; ///< True when release has been called
9494
};
9595
} // namespace SQLite

include/SQLiteCpp/Statement.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,14 @@ class Statement
7474
Statement(aDatabase, aQuery.c_str())
7575
{}
7676

77-
/**
78-
* @brief Move an SQLite statement.
79-
*
80-
* @param[in] aStatement Statement to move
81-
*/
82-
Statement(Statement&& aStatement) noexcept;
83-
Statement& operator=(Statement&& aStatement) noexcept = default;
84-
8577
// Statement is non-copyable
8678
Statement(const Statement&) = delete;
8779
Statement& operator=(const Statement&) = delete;
8880

81+
Statement(Statement&& aStatement) noexcept;
82+
Statement& operator=(Statement&& aStatement) noexcept = default;
83+
// TODO: Change Statement move constructor to default
84+
8985
/// Finalize and unregister the SQL query from the SQLite Database Connection.
9086
/// The finalization will be done by the destructor of the last shared pointer
9187
~Statement() = default;
@@ -705,7 +701,7 @@ class Statement
705701
int mColumnCount{0}; //!< Number of columns in the result of the prepared statement
706702
bool mbHasRow{false}; //!< true when a row has been fetched with executeStep()
707703
bool mbDone{false}; //!< true when the last executeStep() had no more row to fetch
708-
704+
709705
/// Map of columns index by name (mutable so getColumnIndex can be const)
710706
mutable std::map<std::string, int> mColumnNames{};
711707
};

include/SQLiteCpp/Transaction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ class Transaction
8787
void commit();
8888

8989
private:
90-
Database& mDatabase; ///< Reference to the SQLite Database Connection
91-
bool mbCommited; ///< True when commit has been called
90+
Database& mDatabase; ///< Reference to the SQLite Database Connection
91+
bool mbCommited{ false }; ///< True when commit has been called
9292
};
9393

9494

src/Backup.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ Backup::Backup(Database& aDestDatabase,
2424
Database& aSrcDatabase,
2525
const char* apSrcDatabaseName)
2626
{
27-
mpSQLiteBackup = sqlite3_backup_init(aDestDatabase.getHandle(),
27+
mpSQLiteBackup.reset(sqlite3_backup_init(aDestDatabase.getHandle(),
2828
apDestDatabaseName,
2929
aSrcDatabase.getHandle(),
30-
apSrcDatabaseName);
30+
apSrcDatabaseName));
3131
if (nullptr == mpSQLiteBackup)
3232
{
3333
// If an error occurs, the error code and message are attached to the destination database connection.
@@ -48,19 +48,10 @@ Backup::Backup(Database &aDestDatabase, Database &aSrcDatabase) :
4848
{
4949
}
5050

51-
// Release resource for SQLite database backup
52-
Backup::~Backup()
53-
{
54-
if (mpSQLiteBackup)
55-
{
56-
sqlite3_backup_finish(mpSQLiteBackup);
57-
}
58-
}
59-
6051
// Execute backup step with a given number of source pages to be copied
6152
int Backup::executeStep(const int aNumPage /* = -1 */)
6253
{
63-
const int res = sqlite3_backup_step(mpSQLiteBackup, aNumPage);
54+
const int res = sqlite3_backup_step(mpSQLiteBackup.get(), aNumPage);
6455
if (SQLITE_OK != res && SQLITE_DONE != res && SQLITE_BUSY != res && SQLITE_LOCKED != res)
6556
{
6657
throw SQLite::Exception(sqlite3_errstr(res), res);
@@ -69,15 +60,24 @@ int Backup::executeStep(const int aNumPage /* = -1 */)
6960
}
7061

7162
// Get the number of remaining source pages to be copied in this backup process
72-
int Backup::getRemainingPageCount()
63+
int Backup::getRemainingPageCount() const
7364
{
74-
return sqlite3_backup_remaining(mpSQLiteBackup);
65+
return sqlite3_backup_remaining(mpSQLiteBackup.get());
7566
}
7667

7768
// Get the number of total source pages to be copied in this backup process
78-
int Backup::getTotalPageCount()
69+
int Backup::getTotalPageCount() const
7970
{
80-
return sqlite3_backup_pagecount(mpSQLiteBackup);
71+
return sqlite3_backup_pagecount(mpSQLiteBackup.get());
72+
}
73+
74+
// Release resource for SQLite database backup
75+
void SQLite::Backup::Deleter::operator()(sqlite3_backup* apBackup)
76+
{
77+
if (apBackup)
78+
{
79+
sqlite3_backup_finish(apBackup);
80+
}
8181
}
8282

8383
} // namespace SQLite

src/Column.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ double Column::getDouble() const noexcept
7878
const char* Column::getText(const char* apDefaultValue /* = "" */) const noexcept
7979
{
8080
auto pText = reinterpret_cast<const char*>(sqlite3_column_text(mStmtPtr.get(), mIndex));
81-
return (pText?pText:apDefaultValue);
81+
return (pText ? pText : apDefaultValue);
8282
}
8383

8484
// Return a pointer to the blob value (*not* NULL terminated) of the column specified by its index starting at 0

src/Database.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
namespace SQLite
2828
{
2929

30+
const int OK = SQLITE_OK;
3031
const int OPEN_READONLY = SQLITE_OPEN_READONLY;
3132
const int OPEN_READWRITE = SQLITE_OPEN_READWRITE;
3233
const int OPEN_CREATE = SQLITE_OPEN_CREATE;
@@ -42,10 +43,8 @@ const int OPEN_NOFOLLOW = SQLITE_OPEN_NOFOLLOW;
4243
const int OPEN_NOFOLLOW = 0;
4344
#endif
4445

45-
const int OK = SQLITE_OK;
46-
47-
const char* VERSION = SQLITE_VERSION;
48-
const int VERSION_NUMBER = SQLITE_VERSION_NUMBER;
46+
const char* const VERSION = SQLITE_VERSION;
47+
const int VERSION_NUMBER = SQLITE_VERSION_NUMBER;
4948

5049
// Return SQLite version string using runtime call to the compiled library
5150
const char* getLibVersion() noexcept
@@ -142,7 +141,7 @@ Column Database::execAndGet(const char* apQuery)
142141
}
143142

144143
// Shortcut to test if a table exists.
145-
bool Database::tableExists(const char* apTableName)
144+
bool Database::tableExists(const char* apTableName) const
146145
{
147146
Statement query(*this, "SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?");
148147
query.bind(1, apTableName);
@@ -439,8 +438,8 @@ void Database::backup(const char* apFilename, BackupType aType)
439438
Database otherDatabase(apFilename, SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
440439

441440
// For a 'Save' operation, data is copied from the current Database to the other. A 'Load' is the reverse.
442-
Database& src = (aType == Save ? *this : otherDatabase);
443-
Database& dest = (aType == Save ? otherDatabase : *this);
441+
Database& src = (aType == BackupType::Save ? *this : otherDatabase);
442+
Database& dest = (aType == BackupType::Save ? otherDatabase : *this);
444443

445444
// Set up the backup procedure to copy between the "main" databases of each connection
446445
Backup bkp(dest, src);

src/Savepoint.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
namespace SQLite {
1919

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

src/Transaction.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ namespace SQLite
2121

2222
// Begins the SQLite transaction
2323
Transaction::Transaction(Database& aDatabase, TransactionBehavior behavior) :
24-
mDatabase(aDatabase),
25-
mbCommited(false)
24+
mDatabase(aDatabase)
2625
{
2726
const char *stmt;
2827
switch (behavior) {
@@ -43,8 +42,7 @@ Transaction::Transaction(Database& aDatabase, TransactionBehavior behavior) :
4342

4443
// Begins the SQLite transaction
4544
Transaction::Transaction(Database &aDatabase) :
46-
mDatabase(aDatabase),
47-
mbCommited(false)
45+
mDatabase(aDatabase)
4846
{
4947
mDatabase.exec("BEGIN");
5048
}

0 commit comments

Comments
 (0)