Skip to content

Commit 648ce2d

Browse files
committed
Update SqliteDB class from "upstream"
1 parent 1275a0d commit 648ce2d

2 files changed

Lines changed: 86 additions & 69 deletions

File tree

SqliteDB.cpp

Lines changed: 80 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/////////////////////////////////////////////////////////////////////////////
22
// //
33
// SqliteDB Classes //
4-
// Version 1.1, 27-May-2015 //
4+
// Version 1.1.1, 16-Jun-2019 //
55
// //
6-
// Copyright (c) 2013-2015, Frank Fesevur <http://www.fesevur.com> //
6+
// Copyright (c) 2013-2019, Frank Fesevur <https://www.fesevur.com> //
77
// All rights reserved. //
88
// //
99
// Redistribution and use in source and binary forms, with or without //
@@ -31,10 +31,8 @@
3131
/////////////////////////////////////////////////////////////////////////////
3232

3333
#include <windows.h>
34-
#include <wchar.h>
3534
#include <stdio.h>
3635
#include "SqliteDB.h"
37-
using namespace std;
3836

3937
#ifdef _MSC_VER
4038
#define snprintf _snprintf
@@ -47,20 +45,19 @@ using namespace std;
4745

4846
SqliteDatabase::SqliteDatabase()
4947
{
50-
_dbFile[0] = 0;
51-
_db = NULL;
48+
_db = nullptr;
5249
}
5350

5451
SqliteDatabase::SqliteDatabase(LPCWSTR file)
5552
{
56-
_db = NULL;
53+
_db = nullptr;
5754
SetFilename(file);
5855
Open();
5956
}
6057

6158
SqliteDatabase::~SqliteDatabase()
6259
{
63-
if (_db != NULL)
60+
if (_db != nullptr)
6461
sqlite3_close(_db);
6562
}
6663

@@ -69,54 +66,62 @@ SqliteDatabase::~SqliteDatabase()
6966

7067
void SqliteDatabase::SetFilename(LPCWSTR file)
7168
{
72-
wcsncpy(_dbFile, file, MAX_PATH);
69+
_dbFile = file;
7370
}
7471

75-
/////////////////////////////////////////////////////////////////////////////
76-
// Close the database
72+
/**
73+
* Close the database
74+
*/
7775

7876
void SqliteDatabase::Close()
7977
{
8078
if (sqlite3_close(_db) != SQLITE_OK)
8179
throw SqliteException(sqlite3_errmsg(_db));
8280

83-
_db = NULL;
81+
_db = nullptr;
8482
}
8583

86-
/////////////////////////////////////////////////////////////////////////////
87-
// Open the database
84+
/**
85+
* Open the database
86+
*
87+
* @pre The filename is already set
88+
*/
8889

8990
void SqliteDatabase::Open()
9091
{
91-
if (_db != NULL)
92+
if (_db != nullptr)
9293
throw SqliteException("Database already opened!");
9394

9495
// Is the filename filled?
95-
if (wcslen(_dbFile) == 0)
96+
if (_dbFile.length() == 0)
9697
throw SqliteException("Filename not set!");
9798

9899
// Open the database
99-
if (sqlite3_open16(_dbFile, &_db) != SQLITE_OK)
100+
if (sqlite3_open16(_dbFile.c_str(), &_db) != SQLITE_OK)
100101
throw SqliteException(sqlite3_errmsg(_db));
101102
}
102103

103-
/////////////////////////////////////////////////////////////////////////////
104-
// Open the database
104+
/**
105+
* Open the database
106+
*
107+
* @param file The filename of the database to be opened.
108+
*/
105109

106110
void SqliteDatabase::Open(LPCWSTR file)
107111
{
108112
SetFilename(file);
109113
Open();
110114
}
111115

112-
/////////////////////////////////////////////////////////////////////////////
113-
// Delete the database file
116+
/**
117+
* Delete the database file
118+
*/
114119

115120
void SqliteDatabase::Delete()
116121
{
117-
if (!DeleteFile(_dbFile))
122+
if (!DeleteFile(_dbFile.c_str()))
118123
{
119-
DWORD err = GetLastError();
124+
const DWORD err = GetLastError();
120125
if (err != ERROR_FILE_NOT_FOUND)
121126
throw SqliteException("Unable to delete SqliteDatabase");
122127
}
@@ -145,23 +150,25 @@ void SqliteDatabase::Detach(LPCWSTR alias)
145150
stmt.Finalize();
146151
}
147152

148-
/////////////////////////////////////////////////////////////////////////////
149-
// Compress the database
153+
/**
154+
* Compress the database
155+
*/
150156

151157
void SqliteDatabase::Vacuum()
152158
{
153159
Execute("VACUUM;");
154160
}
155161

156-
/////////////////////////////////////////////////////////////////////////////
157-
// Check if a table exists
162+
/**
163+
* Check if a table exists
164+
*/
158165

159166
bool SqliteDatabase::TableExists(const char* table)
160167
{
161168
SqliteStatement stmt(this, "SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = @name;");
162169
stmt.Bind("@name", table);
163170
stmt.GetNextRecord();
164-
int count = stmt.GetIntColumn(0);
171+
const int count = stmt.GetIntColumn(0);
165172
return (count == 1);
166173
}
167174

@@ -183,12 +190,13 @@ void SqliteDatabase::RollbackTransaction()
183190
Execute("ROLLBACK TRANSACTION;");
184191
}
185192

186-
/////////////////////////////////////////////////////////////////////////////
187-
// Execute an SQL statement without results (UPDATE, INSERT, DELETE, etc)
193+
/**
194+
* Execute an SQL statement without results, like \c UPDATE, \c INSERT, \c DELETE, etc.
195+
*/
188196

189197
void SqliteDatabase::Execute(LPCSTR szSQL)
190198
{
191-
if (sqlite3_exec(_db, szSQL, NULL, NULL, NULL) != SQLITE_OK)
199+
if (sqlite3_exec(_db, szSQL, nullptr, nullptr, nullptr) != SQLITE_OK)
192200
throw sqlite3_errmsg(_db);
193201
}
194202

@@ -198,7 +206,7 @@ void SqliteDatabase::Execute(LPCSTR szSQL)
198206
void SqliteDatabase::SetUserVersion(long version, const char* dbname)
199207
{
200208
char sql[MAX_PATH];
201-
snprintf(sql, MAX_PATH, "PRAGMA %s.user_version = %ld;", dbname == NULL ? "main" : dbname, version);
209+
snprintf(sql, MAX_PATH, "PRAGMA %s.user_version = %ld;", dbname == nullptr ? "main" : dbname, version);
202210
Execute(sql);
203211
}
204212

@@ -207,8 +215,8 @@ void SqliteDatabase::SetUserVersion(long version, const char* dbname)
207215

208216
long SqliteDatabase::GetUserVersion(const char* dbname)
209217
{
210-
string sql = "PRAGMA ";
211-
sql += (dbname == NULL ? "main" : dbname);
218+
std::string sql = "PRAGMA ";
219+
sql += (dbname == nullptr ? "main" : dbname);
212220
sql += ".user_version;";
213221

214222
SqliteStatement stmt(this, sql.c_str());
@@ -221,7 +229,7 @@ long SqliteDatabase::GetUserVersion(const char* dbname)
221229

222230
void SqliteDatabase::EnableForeignKeys(bool on)
223231
{
224-
string sql = "PRAGMA foreign_keys = ";
232+
std::string sql = "PRAGMA foreign_keys = ";
225233
sql += (on ? "ON" : "OFF");
226234
Execute(sql.c_str());
227235
}
@@ -232,19 +240,19 @@ void SqliteDatabase::EnableForeignKeys(bool on)
232240
SqliteStatement::SqliteStatement(SqliteDatabase* db)
233241
{
234242
_db = db->GetDB();
235-
_stmt = NULL;
243+
_stmt = nullptr;
236244
}
237245

238246
SqliteStatement::SqliteStatement(SqliteDatabase* db, const char* sql)
239247
{
240248
_db = db->GetDB();
241-
_stmt = NULL;
249+
_stmt = nullptr;
242250
Prepare(sql);
243251
}
244252

245253
SqliteStatement::~SqliteStatement()
246254
{
247-
if (_stmt != NULL)
255+
if (_stmt != nullptr)
248256
sqlite3_finalize(_stmt);
249257
}
250258

@@ -253,7 +261,7 @@ SqliteStatement::~SqliteStatement()
253261

254262
void SqliteStatement::Prepare(const char* sql)
255263
{
256-
if (sqlite3_prepare_v2(_db, sql, -1, &_stmt, NULL) != SQLITE_OK)
264+
if (sqlite3_prepare_v2(_db, sql, -1, &_stmt, nullptr) != SQLITE_OK)
257265
throw SqliteException(sqlite3_errmsg(_db));
258266
_colNames.clear();
259267
}
@@ -283,7 +291,7 @@ void SqliteStatement::SaveRecord()
283291

284292
bool SqliteStatement::GetNextRecord()
285293
{
286-
int rc = sqlite3_step(_stmt);
294+
const int rc = sqlite3_step(_stmt);
287295

288296
if (rc == SQLITE_ROW)
289297
{
@@ -306,7 +314,7 @@ void SqliteStatement::Finalize()
306314
if (sqlite3_finalize(_stmt) != SQLITE_OK)
307315
throw SqliteException(sqlite3_errmsg(_db));
308316

309-
_stmt = NULL;
317+
_stmt = nullptr;
310318
_colNames.clear();
311319
}
312320

@@ -318,9 +326,12 @@ int SqliteStatement::GetBindParameterIndex(std::string col)
318326
return sqlite3_bind_parameter_index(_stmt, col.c_str());
319327
}
320328

321-
/////////////////////////////////////////////////////////////////////////////
322-
// Binds a wchar string to the given parameter. Empty strings are bound
323-
// as null
329+
/**
330+
* Binds a wchar string to the given parameter. Empty strings are bound as null.
331+
*
332+
* @param param The parameter to bind to
333+
* @param val The value to bind
334+
*/
324335

325336
void SqliteStatement::Bind(const char* param, const WCHAR* val)
326337
{
@@ -330,7 +341,7 @@ void SqliteStatement::Bind(const char* param, const WCHAR* val)
330341
void SqliteStatement::Bind(int col, const WCHAR* val)
331342
{
332343
int res = SQLITE_OK;
333-
if (val == NULL)
344+
if (val == nullptr)
334345
{
335346
res = sqlite3_bind_null(_stmt, col);
336347
}
@@ -347,9 +358,12 @@ void SqliteStatement::Bind(int col, const WCHAR* val)
347358
throw SqliteException(sqlite3_errmsg(_db));
348359
}
349360

350-
/////////////////////////////////////////////////////////////////////////////
351-
// Binds a char string to the given parameter. Empty strings are bound
352-
// as null
361+
/**
362+
* Binds a char string to the given parameter. Empty strings are bound as null.
363+
*
364+
* @param param The parameter to bind to
365+
* @param val The value to bind
366+
*/
353367

354368
void SqliteStatement::Bind(const char* param, const char *val)
355369
{
@@ -359,7 +373,7 @@ void SqliteStatement::Bind(const char* param, const char *val)
359373
void SqliteStatement::Bind(int col, const char *val)
360374
{
361375
int res = SQLITE_OK;
362-
if (val == NULL)
376+
if (val == nullptr)
363377
{
364378
res = sqlite3_bind_null(_stmt, col);
365379
}
@@ -376,9 +390,12 @@ void SqliteStatement::Bind(int col, const char *val)
376390
throw SqliteException(sqlite3_errmsg(_db));
377391
}
378392

379-
/////////////////////////////////////////////////////////////////////////////
380-
// Bind an integer to the given parameter. If optional "bool null" is true,
381-
// null is bound. Use like this: stmt.Bind("col", var, var == 0);
393+
/**
394+
* Bind an integer to the given parameter.
395+
*
396+
* If the optional "bool null" is true, \c NULL is bound.
397+
* It can be used like this: <tt>stmt.Bind("col", var, var == 0);</tt>
398+
*/
382399

383400
void SqliteStatement::Bind(const char* param, int val, bool null)
384401
{
@@ -387,13 +404,14 @@ void SqliteStatement::Bind(const char* param, int val, bool null)
387404

388405
void SqliteStatement::Bind(int col, int val, bool null)
389406
{
390-
int res = (null ? sqlite3_bind_null(_stmt, col) : sqlite3_bind_int(_stmt, col, val));
407+
const int res = (null ? sqlite3_bind_null(_stmt, col) : sqlite3_bind_int(_stmt, col, val));
391408
if (res != SQLITE_OK)
392409
throw SqliteException(sqlite3_errmsg(_db));
393410
}
394411

395-
/////////////////////////////////////////////////////////////////////////////
396-
// Binds 1 for true and 0 for false to the given parameter
412+
/**
413+
* Binds \c 1 for \c true and \c 0 for \c false to the given parameter
414+
*/
397415

398416
void SqliteStatement::Bind(const char* param, bool val)
399417
{
@@ -406,8 +424,9 @@ void SqliteStatement::Bind(int col, bool val)
406424
throw SqliteException(sqlite3_errmsg(_db));
407425
}
408426

409-
/////////////////////////////////////////////////////////////////////////////
410-
// Bind NULL to the given parameter
427+
/**
428+
* Bind \c NULL to the given parameter
429+
*/
411430

412431
void SqliteStatement::Bind(const char* param)
413432
{
@@ -439,8 +458,8 @@ int SqliteStatement::GetColumnCount()
439458

440459
std::string SqliteStatement::GetTextColumn(int col)
441460
{
442-
LPCSTR val = (LPCSTR) sqlite3_column_text(_stmt, col);
443-
return (val == NULL ? "" : val);
461+
const LPCSTR val = (LPCSTR) sqlite3_column_text(_stmt, col);
462+
return (val == nullptr ? "" : val);
444463
}
445464

446465
std::string SqliteStatement::GetTextColumn(std::string col)
@@ -450,8 +469,8 @@ std::string SqliteStatement::GetTextColumn(std::string col)
450469

451470
std::wstring SqliteStatement::GetWTextColumn(int col)
452471
{
453-
LPCWSTR val = (LPCWSTR) sqlite3_column_text16(_stmt, col);
454-
return (val == NULL ? L"" : val);
472+
const LPCWSTR val = (LPCWSTR) sqlite3_column_text16(_stmt, col);
473+
return (val == nullptr ? L"" : val);
455474
}
456475

457476
std::wstring SqliteStatement::GetWTextColumn(std::string col)

0 commit comments

Comments
 (0)