Skip to content

Commit c7cffad

Browse files
authored
Merge pull request SRombauts#360 from Kacperos155/small_improvements
Small improvements and code cleaning
2 parents 07b2445 + d9f8be8 commit c7cffad

File tree

13 files changed

+40
-49
lines changed

13 files changed

+40
-49
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ build
44
example1
55
*.a
66

7+
.vs/
78
.vscode/
89
/SQLiteCpp.sln
910
*.ncb

include/SQLiteCpp/Backup.h

Lines changed: 3 additions & 3 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;
@@ -114,13 +115,12 @@ class Backup
114115
int executeStep(const int aNumPage = -1);
115116

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

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

122123
private:
123-
// TODO: use std::unique_ptr with a custom deleter to call sqlite3_backup_finish()
124124
sqlite3_backup* mpSQLiteBackup = nullptr; ///< Pointer to SQLite Database Backup Handle
125125
};
126126

include/SQLiteCpp/Column.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ class Column
5555
*/
5656
explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex);
5757

58-
// default destructor: the finalization will be done by the destructor of the last shared pointer
59-
// default copy constructor and assignment operator are perfectly suited :
60-
// they copy the Statement::Ptr which in turn increments the reference counter.
61-
6258
/**
6359
* @brief Return a pointer to the named assigned to this result column (potentially aliased)
6460
*

include/SQLiteCpp/Database.h

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

9393
extern const int OK; ///< SQLITE_OK (used by check() bellow)
9494

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

9898
/// Return SQLite version string using runtime call to the compiled library
9999
const char* getLibVersion() noexcept;
@@ -342,7 +342,7 @@ class Database
342342
*
343343
* @return the sqlite result code.
344344
*/
345-
int tryExec(const std::string aQueries) noexcept
345+
int tryExec(const std::string& aQueries) noexcept
346346
{
347347
return tryExec(aQueries.c_str());
348348
}
@@ -403,7 +403,7 @@ class Database
403403
*
404404
* @throw SQLite::Exception in case of error
405405
*/
406-
bool tableExists(const char* apTableName);
406+
bool tableExists(const char* apTableName) const;
407407

408408
/**
409409
* @brief Shortcut to test if a table exists.
@@ -416,7 +416,7 @@ class Database
416416
*
417417
* @throw SQLite::Exception in case of error
418418
*/
419-
bool tableExists(const std::string& aTableName)
419+
bool tableExists(const std::string& aTableName) const
420420
{
421421
return tableExists(aTableName.c_str());
422422
}
@@ -566,7 +566,7 @@ class Database
566566
static Header getHeaderInfo(const std::string& aFilename);
567567

568568
// Parse SQLite header data from a database file.
569-
Header getHeaderInfo()
569+
Header getHeaderInfo() const
570570
{
571571
return getHeaderInfo(mFilename);
572572
}

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: 8 additions & 12 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;
@@ -702,12 +698,12 @@ class Statement
702698
std::string mQuery; //!< UTF-8 SQL Query
703699
sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle
704700
TStatementPtr mpPreparedStatement; //!< Shared Pointer to the prepared SQLite Statement Object
705-
int mColumnCount{0}; //!< Number of columns in the result of the prepared statement
706-
bool mbHasRow{false}; //!< true when a row has been fetched with executeStep()
707-
bool mbDone{false}; //!< true when the last executeStep() had no more row to fetch
701+
int mColumnCount = 0; //!< Number of columns in the result of the prepared statement
702+
bool mbHasRow = false; //!< true when a row has been fetched with executeStep()
703+
bool mbDone = false; //!< true when the last executeStep() had no more row to fetch
708704

709705
/// Map of columns index by name (mutable so getColumnIndex can be const)
710-
mutable std::map<std::string, int> mColumnNames;
706+
mutable std::map<std::string, int, std::less<>> mColumnNames;
711707
};
712708

713709

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,16 @@ int Backup::executeStep(const int aNumPage /* = -1 */)
6969
}
7070

7171
// Get the number of remaining source pages to be copied in this backup process
72-
int Backup::getRemainingPageCount()
72+
int Backup::getRemainingPageCount() const
7373
{
7474
return sqlite3_backup_remaining(mpSQLiteBackup);
7575
}
7676

7777
// Get the number of total source pages to be copied in this backup process
78-
int Backup::getTotalPageCount()
78+
int Backup::getTotalPageCount() const
7979
{
8080
return sqlite3_backup_pagecount(mpSQLiteBackup);
8181
}
8282

83+
8384
} // 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);

0 commit comments

Comments
 (0)