Skip to content

Commit

Permalink
Move additional constructor arguments into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
zauguin committed Feb 1, 2017
1 parent 2c9cbf6 commit 61bd9ed
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cassert>
#include <string>
#include <functional>
#include <stdexcept>
Expand Down Expand Up @@ -297,6 +298,10 @@ namespace sqlite {
}
};

struct sqlite_config {
std::string key{};
};

class database {
private:
std::shared_ptr<sqlite3> _db;
Expand All @@ -318,6 +323,18 @@ namespace sqlite {
database(std::shared_ptr<sqlite3> db):
_db(db) {}

database(const std::string &db_name, const sqlite_config &config):
database(std::u16string(db_name.begin(), db_name.end()), config) {}

database(const std::u16string &db_name, const sqlite_config &config): database(db_name) {
#ifdef SQLITE_HAS_CODEC
if(!config.key.empty()) set_key(config.key);
#else
assert(config.key.empty() && "Encryption supported is disabled.");
(void)config; // Suppress unused warning
#endif
}

database_binder operator<<(const std::string& sql) {
return database_binder(_db, sql);
}
Expand All @@ -341,13 +358,6 @@ namespace sqlite {
}

#ifdef SQLITE_HAS_CODEC
database(const std::string &db_name, const std::string &key): database(db_name) {
set_key(key);
}
database(const std::u16string &db_name, const std::string &key): database(db_name) {
set_key(key);
}

void set_key(const std::string &key) {
if(auto ret = sqlite3_key(_db.get(), key.data(), key.size()))
exceptions::throw_sqlite_error(ret);
Expand Down

0 comments on commit 61bd9ed

Please sign in to comment.