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 9cef37d
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 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 @@ -213,7 +214,7 @@ namespace sqlite {
std::is_floating_point<Type>::value
|| std::is_integral<Type>::value
|| std::is_same<sqlite_int64, Type>::value
> { };
> { };


template<typename T> friend database_binder& operator <<(database_binder& db, const T& val);
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 9cef37d

Please sign in to comment.