Skip to content

Commit 0d11c9b

Browse files
committed
Move SQLCipher support into separate file
1 parent 0027f0b commit 0d11c9b

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,10 @@ namespace sqlite {
299299
};
300300

301301
struct sqlite_config {
302-
std::string key{};
303302
};
304303

305304
class database {
306-
private:
305+
protected:
307306
std::shared_ptr<sqlite3> _db;
308307

309308
public:
@@ -327,12 +326,7 @@ namespace sqlite {
327326
database(std::u16string(db_name.begin(), db_name.end()), config) {}
328327

329328
database(const std::u16string &db_name, const sqlite_config &config): database(db_name) {
330-
#ifdef ENABLE_SQLCIPHER
331-
if(!config.key.empty()) set_key(config.key);
332-
#else
333-
assert(config.key.empty() && "Encryption supported is disabled.");
334329
(void)config; // Suppress unused warning
335-
#endif
336330
}
337331

338332
database_binder operator<<(const std::string& sql) {
@@ -356,28 +350,6 @@ namespace sqlite {
356350
sqlite3_int64 last_insert_rowid() const {
357351
return sqlite3_last_insert_rowid(_db.get());
358352
}
359-
360-
#ifdef ENABLE_SQLCIPHER
361-
void set_key(const std::string &key) {
362-
if(auto ret = sqlite3_key(_db.get(), key.data(), key.size()))
363-
exceptions::throw_sqlite_error(ret);
364-
}
365-
366-
void set_key(const std::string &key, const std::string &db_name) {
367-
if(auto ret = sqlite3_key_v2(_db.get(), db_name.c_str(), key.data(), key.size()))
368-
exceptions::throw_sqlite_error(ret);
369-
}
370-
371-
void rekey(const std::string &new_key) {
372-
if(auto ret = sqlite3_rekey(_db.get(), new_key.data(), new_key.size()))
373-
exceptions::throw_sqlite_error(ret);
374-
}
375-
376-
void rekey(const std::string &new_key, const std::string &db_name) {
377-
if(auto ret = sqlite3_rekey_v2(_db.get(), db_name.c_str(), new_key.data(), new_key.size()))
378-
exceptions::throw_sqlite_error(ret);
379-
}
380-
#endif
381353
};
382354

383355
template<std::size_t Count>

hdr/sqlite_modern_cpp/sqlcipher.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#ifndef SQLITE_HAS_CODEC
4+
#define SQLITE_HAS_CODEC
5+
#endif
6+
7+
#include "../sqlite_modern_cpp.h"
8+
9+
namespace sqlite {
10+
struct sqlcipher_config : public sqlite_config {
11+
std::string key;
12+
};
13+
14+
class sqlcipher_database : public database {
15+
public:
16+
sqlcipher_database(std::string db, const sqlcipher_config &config): database(db, config) {
17+
set_key(config.key);
18+
}
19+
20+
sqlcipher_database(std::u16string db, const sqlcipher_config &config): database(db, config) {
21+
set_key(config.key);
22+
}
23+
24+
void set_key(const std::string &key) {
25+
if(auto ret = sqlite3_key(_db.get(), key.data(), key.size()))
26+
exceptions::throw_sqlite_error(ret);
27+
}
28+
29+
void set_key(const std::string &key, const std::string &db_name) {
30+
if(auto ret = sqlite3_key_v2(_db.get(), db_name.c_str(), key.data(), key.size()))
31+
exceptions::throw_sqlite_error(ret);
32+
}
33+
34+
void rekey(const std::string &new_key) {
35+
if(auto ret = sqlite3_rekey(_db.get(), new_key.data(), new_key.size()))
36+
exceptions::throw_sqlite_error(ret);
37+
}
38+
39+
void rekey(const std::string &new_key, const std::string &db_name) {
40+
if(auto ret = sqlite3_rekey_v2(_db.get(), db_name.c_str(), new_key.data(), new_key.size()))
41+
exceptions::throw_sqlite_error(ret);
42+
}
43+
};
44+
}

0 commit comments

Comments
 (0)