namespace tizen_base {
-template<std::size_t N>
+template <std::size_t N>
struct num {
static const constexpr auto value = N;
};
for_(func, std::make_index_sequence<N>());
}
-using DbType = std::optional<std::variant<int64_t, double, std::string,
- std::vector<unsigned char>>>;
+using DbType = std::optional<
+ std::variant<int64_t, double, std::string, std::vector<unsigned char>>>;
class DbException : public std::runtime_error {
public:
explicit DbException(const std::string& msg, int code = SQLITE_ERROR)
- : std::runtime_error(msg), code_(code) {
- }
+ : std::runtime_error(msg), code_(code) {}
DbException(const std::string& msg, int code, const std::string& file,
- int line) : std::runtime_error(msg), code_(code) {
+ int line)
+ : std::runtime_error(msg), code_(code) {
msg_ = msg + " at " + file + ":" + std::to_string(line);
}
- int code() const {
- return code_;
- }
+ int code() const { return code_; }
const char* msg() const {
- if (msg_.empty())
- return what();
+ if (msg_.empty()) return what();
return msg_.c_str();
}
explicit AutoDbType(DbType db_type, int real_type)
: db_type_(db_type), real_type_(real_type) {}
- int GetType() const {
- return real_type_;
- }
+ int GetType() const { return real_type_; }
- explicit operator int () {
+ explicit operator int() {
if (!db_type_)
throw DbException("invalid type conversion from nullopt to int");
if (real_type_ != SQLITE_INTEGER)
return std::get<int64_t>(*db_type_);
}
- explicit operator int64_t () {
+ explicit operator int64_t() {
if (!db_type_)
throw DbException("invalid type conversion from nullopt to int64_t");
if (real_type_ != SQLITE_INTEGER)
return std::get<int64_t>(*db_type_);
}
- explicit operator std::string () {
+ explicit operator std::string() {
if (!db_type_) {
- throw DbException(
- "invalid type conversion from nullopt to string");
+ throw DbException("invalid type conversion from nullopt to string");
}
if (real_type_ != SQLITE_TEXT)
return std::get<std::string>(*db_type_);
}
- explicit operator double () {
+ explicit operator double() {
if (!db_type_) {
- throw DbException(
- "invalid type conversion from nullopt to double");
+ throw DbException("invalid type conversion from nullopt to double");
}
if (real_type_ != SQLITE_FLOAT)
return std::get<double>(*db_type_);
}
- explicit operator std::vector<unsigned char> () {
+ explicit operator std::vector<unsigned char>() {
if (!db_type_) {
throw DbException(
"invalid type conversion from nullopt to std::vector<unsigned char>");
}
if (real_type_ != SQLITE_BLOB)
- throw DbException("invalid type conversion to std::vector<unsigned char>");
+ throw DbException(
+ "invalid type conversion to std::vector<unsigned char>");
return std::get<std::vector<unsigned char>>(*db_type_);
}
- operator std::optional<int> () {
- if (!db_type_)
- return std::nullopt;
+ operator std::optional<int>() {
+ if (!db_type_) return std::nullopt;
if (real_type_ != SQLITE_INTEGER)
throw DbException("invalid type conversion to int");
return std::get<int64_t>(*db_type_);
}
- operator std::optional<int64_t> () {
- if (!db_type_)
- return std::nullopt;
+ operator std::optional<int64_t>() {
+ if (!db_type_) return std::nullopt;
if (real_type_ != SQLITE_INTEGER)
throw DbException("invalid type conversion to int64_t");
return std::get<int64_t>(*db_type_);
}
- operator std::optional<std::string> () {
- if (!db_type_)
- return std::nullopt;
+ operator std::optional<std::string>() {
+ if (!db_type_) return std::nullopt;
if (real_type_ != SQLITE_TEXT)
throw DbException("invalid type conversion to string");
return std::get<std::string>(*db_type_);
}
- operator std::optional<double> () {
- if (!db_type_)
- return std::nullopt;
+ operator std::optional<double>() {
+ if (!db_type_) return std::nullopt;
if (real_type_ != SQLITE_FLOAT)
throw DbException("invalid type conversion to double");
return std::get<double>(*db_type_);
}
- operator std::optional<std::vector<unsigned char>> () {
- if (!db_type_)
- return std::nullopt;
+ operator std::optional<std::vector<unsigned char>>() {
+ if (!db_type_) return std::nullopt;
if (real_type_ != SQLITE_BLOB)
- throw DbException("invalid type conversion to std::vector<unsigned char>");
+ throw DbException(
+ "invalid type conversion to std::vector<unsigned char>");
return std::get<std::vector<unsigned char>>(*db_type_);
}
class TransactionGuard {
public:
TransactionGuard(const TransactionGuard&) = delete;
- TransactionGuard& operator = (const TransactionGuard&) = delete;
+ TransactionGuard& operator=(const TransactionGuard&) = delete;
- TransactionGuard(TransactionGuard&& t) noexcept {
- db_ = std::move(t.db_);
- }
+ TransactionGuard(TransactionGuard&& t) noexcept { db_ = std::move(t.db_); }
- TransactionGuard& operator = (TransactionGuard&& t) noexcept {
+ TransactionGuard& operator=(TransactionGuard&& t) noexcept {
if (this != &t) {
if (!db_.expired())
sqlite3_exec(db_.lock().get(), "ROLLBACK", nullptr, nullptr, nullptr);
return *this;
}
- explicit TransactionGuard(const std::shared_ptr<sqlite3>& db)
- : db_(db) {
- int r = sqlite3_exec(db_.lock().get(), "BEGIN DEFERRED",
- nullptr, nullptr, nullptr);
+ explicit TransactionGuard(const std::shared_ptr<sqlite3>& db) : db_(db) {
+ int r = sqlite3_exec(db_.lock().get(), "BEGIN DEFERRED", nullptr, nullptr,
+ nullptr);
if (r != SQLITE_OK) {
throw DbException("begin transaction failed", r);
}
}
int Commit() {
- if (db_.expired())
- return SQLITE_OK;
+ if (db_.expired()) return SQLITE_OK;
auto db = db_.lock();
int ret = sqlite3_exec(db.get(), "COMMIT", nullptr, nullptr, nullptr);
public:
Sql(std::string query) : query_(std::move(query)) {}
- Sql& Bind(const char* val) {
+ Sql(const Sql&) = default;
+ Sql& operator=(const Sql& sql) noexcept = default;
+
+ Sql(Sql&& sql) noexcept = default;
+ Sql& operator=(Sql&& sql) noexcept = default;
+
+ Sql&& Bind(const char* val) {
if (val == nullptr) {
bindings_.push_back(DbType(std::nullopt));
} else {
else
bindings_.push_back(DbType(std::move(str)));
}
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::string val) {
+ Sql&& Bind(std::string val) {
if (empty_string_as_null_ && val.empty())
bindings_.push_back(DbType(std::nullopt));
else
bindings_.push_back(DbType(std::move(val)));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int val) {
+ Sql&& Bind(int val) {
bindings_.push_back(DbType(static_cast<int64_t>(val)));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int64_t val) {
+ Sql&& Bind(int64_t val) {
bindings_.push_back(DbType(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(double val) {
+ Sql&& Bind(double val) {
bindings_.push_back(DbType(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::vector<unsigned char> val) {
+ Sql&& Bind(std::vector<unsigned char> val) {
if (empty_vector_as_null_ && val.empty())
bindings_.push_back(DbType(std::nullopt));
else
bindings_.push_back(DbType(std::move(val)));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(const std::nullopt_t val) {
+ Sql&& Bind(const std::nullopt_t val) {
bindings_.push_back(DbType(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int pos, const char* val) {
+ Sql&& Bind(int pos, const char* val) {
if (val == nullptr) {
binding_map_[pos] = DbType(std::nullopt);
} else {
else
binding_map_[pos] = DbType(std::move(str));
}
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int pos, std::string val) {
+ Sql&& Bind(int pos, std::string val) {
if (empty_string_as_null_ && val.empty())
binding_map_[pos] = DbType(std::nullopt);
else
binding_map_[pos] = DbType(std::move(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int pos, int val) {
+ Sql&& Bind(int pos, int val) {
binding_map_[pos] = DbType(static_cast<int64_t>(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int pos, int64_t val) {
+ Sql&& Bind(int pos, int64_t val) {
binding_map_[pos] = DbType(val);
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int pos, double val) {
+ Sql&& Bind(int pos, double val) {
binding_map_[pos] = DbType(val);
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int pos, std::vector<unsigned char> val) {
+ Sql&& Bind(int pos, std::vector<unsigned char> val) {
if (empty_vector_as_null_ && val.empty())
binding_map_[pos] = DbType(std::nullopt);
else
binding_map_[pos] = DbType(std::move(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(int pos, const std::nullopt_t& val) {
+ Sql&& Bind(int pos, const std::nullopt_t& val) {
binding_map_[pos] = DbType(val);
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::string name, const char* val) {
+ Sql&& Bind(std::string name, const char* val) {
if (val == nullptr) {
binding_name_map_[std::move(name)] = DbType(std::nullopt);
} else {
else
binding_name_map_[std::move(name)] = DbType(std::move(str));
}
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::string name, std::string val) {
+ Sql&& Bind(std::string name, std::string val) {
if (empty_string_as_null_ && val.empty())
binding_name_map_[std::move(name)] = DbType(std::nullopt);
else
binding_name_map_[std::move(name)] = DbType(std::move(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::string name, int val) {
+ Sql&& Bind(std::string name, int val) {
binding_name_map_[std::move(name)] = DbType(static_cast<int64_t>(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::string name, int64_t val) {
+ Sql&& Bind(std::string name, int64_t val) {
binding_name_map_[std::move(name)] = DbType(val);
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::string name, double val) {
+ Sql&& Bind(std::string name, double val) {
binding_name_map_[std::move(name)] = DbType(val);
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::string name, std::vector<unsigned char> val) {
+ Sql&& Bind(std::string name, std::vector<unsigned char> val) {
if (empty_vector_as_null_ && val.empty())
binding_name_map_[std::move(name)] = DbType(std::nullopt);
else
binding_name_map_[std::move(name)] = DbType(std::move(val));
- return *this;
+ return std::move(*this);
}
- Sql& Bind(std::string name, const std::nullopt_t& val) {
+ Sql&& Bind(std::string name, const std::nullopt_t& val) {
binding_name_map_[std::move(name)] = DbType(val);
- return *this;
+ return std::move(*this);
}
- const std::vector<DbType>& GetBindings() const {
- return bindings_;
- }
+ const std::vector<DbType>& GetBindings() const { return bindings_; }
- const std::map<int, DbType>& GetBindingMap() const {
- return binding_map_;
- }
+ const std::map<int, DbType>& GetBindingMap() const { return binding_map_; }
const std::map<std::string, DbType>& GetBindingNameMap() const {
return binding_name_map_;
}
- const std::string& GetQuery() const {
- return query_;
- }
+ const std::string& GetQuery() const { return query_; }
- Sql& SetEmptyStringAsNull(bool as_null) {
+ Sql&& SetEmptyStringAsNull(bool as_null) {
empty_string_as_null_ = as_null;
- return *this;
+ return std::move(*this);
}
- Sql& SetEmptyVectorAsNull(bool as_null) {
+ Sql&& SetEmptyVectorAsNull(bool as_null) {
empty_vector_as_null_ = as_null;
- return *this;
+ return std::move(*this);
}
- Sql& Reset() {
+ Sql&& Reset() {
bindings_.clear();
binding_map_.clear();
binding_name_map_.clear();
empty_string_as_null_ = false;
empty_vector_as_null_ = false;
- return *this;
+ return std::move(*this);
}
private:
public:
Result() = default;
~Result() {
- if (stmt_)
- sqlite3_finalize(stmt_);
+ if (stmt_) sqlite3_finalize(stmt_);
}
Result(const Result&) = delete;
- Result& operator = (const Result&) = delete;
+ Result& operator=(const Result&) = delete;
Result(Result&& r) noexcept {
stmt_ = r.stmt_;
is_done_ = r.is_done_;
}
- Result& operator = (Result&& r) noexcept {
+ Result& operator=(Result&& r) noexcept {
if (this != &r) {
- if (stmt_)
- sqlite3_finalize(stmt_);
+ if (stmt_) sqlite3_finalize(stmt_);
stmt_ = r.stmt_;
r.stmt_ = nullptr;
query_ = std::move(r.query_);
std::optional<std::string> GetString(int pos) const {
sqlite3_stmt* stmt = const_cast<sqlite3_stmt*>(stmt_);
- const char* text = reinterpret_cast<const char*>(
- sqlite3_column_text(stmt, pos));
- if (!text)
- return std::nullopt;
+ const char* text =
+ reinterpret_cast<const char*>(sqlite3_column_text(stmt, pos));
+ if (!text) return std::nullopt;
return text;
}
DbType dbt;
if (type == SQLITE_TEXT) {
- dbt = DbType(reinterpret_cast<const char*>(
- sqlite3_column_text(stmt, pos)));
+ dbt = DbType(
+ reinterpret_cast<const char*>(sqlite3_column_text(stmt, pos)));
} else if (type == SQLITE_INTEGER) {
dbt = DbType(static_cast<int64_t>(sqlite3_column_int64(stmt, pos)));
} else if (type == SQLITE_FLOAT) {
int len = sqlite3_column_bytes(stmt, pos);
if (!val || len < 0) {
- throw DbException("invalid blob");;
+ throw DbException("invalid blob");
+ ;
} else {
dbt = DbType(std::vector<unsigned char>(val, val + len));
}
return AutoDbType(dbt, type);
}
- template <typename ...Types>
+ template <typename... Types>
auto Get() const {
std::tuple<Types...> t;
int pos = 0;
- for_<std::tuple_size_v<std::tuple<Types...>>>([&] (auto i) {
- std::get<i.value>(t) = Get(pos++);
- });
+ for_<std::tuple_size_v<std::tuple<Types...>>>(
+ [&](auto i) { std::get<i.value>(t) = Get(pos++); });
return t;
}
Record operator*() { return Record(stmt_); }
- bool operator != (const Iterator& rhs) const {
- return stmt_ != rhs.stmt_;
- }
+ bool operator!=(const Iterator& rhs) const { return stmt_ != rhs.stmt_; }
- void operator ++() {
+ void operator++() {
int r = sqlite3_step(stmt_);
- if (r != SQLITE_ROW)
- stmt_ = nullptr;
+ if (r != SQLITE_ROW) stmt_ = nullptr;
}
private:
};
Iterator begin() const {
- if (is_done_)
- return Iterator(nullptr);
+ if (is_done_) return Iterator(nullptr);
return Iterator(stmt_);
}
- Iterator end() const {
- return Iterator(nullptr);
- }
+ Iterator end() const { return Iterator(nullptr); }
operator bool() const {
- if (stmt_ == nullptr)
- return false;
+ if (stmt_ == nullptr) return false;
return true;
}
explicit operator int() const {
auto db = db_.lock();
- if (db == nullptr)
- return SQLITE_ERROR;
+ if (db == nullptr) return SQLITE_ERROR;
return sqlite3_errcode(db.get());
}
explicit operator const char*() const {
auto db = db_.lock();
- if (db == nullptr)
- return "";
+ if (db == nullptr) return "";
return sqlite3_errmsg(db.get());
}
template <class T>
std::vector<T> ToVector() {
- if (stmt_ == nullptr)
- return {};
+ if (stmt_ == nullptr) return {};
std::vector<T> vec;
for (const auto& rec : *this) {
template <class T>
std::list<T> ToList() {
- if (stmt_ == nullptr)
- return {};
+ if (stmt_ == nullptr) return {};
std::list<T> l;
for (const auto& rec : *this) {
template <class T>
std::optional<T> GetFirst() {
- if (stmt_ == nullptr)
- return std::nullopt;
+ if (stmt_ == nullptr) return std::nullopt;
for (const auto& rec : *this) {
T t;
t(rec);
}
std::optional<Record> GetFirstRecord() {
- if (stmt_ == nullptr)
- return std::nullopt;
+ if (stmt_ == nullptr) return std::nullopt;
for (const auto& rec : *this) {
return rec;
}
return std::nullopt;
}
- sqlite3_stmt* GetRaw() const {
- return stmt_;
- }
+ sqlite3_stmt* GetRaw() const { return stmt_; }
- const std::string& GetQuery() const {
- return query_;
- }
+ const std::string& GetQuery() const { return query_; }
- void SetDone(bool is_done) {
- is_done_ = is_done;
- }
+ void SetDone(bool is_done) { is_done_ = is_done; }
- size_t GetColumnCount() const {
- return sqlite3_column_count(stmt_);
- }
+ size_t GetColumnCount() const { return sqlite3_column_count(stmt_); }
private:
friend class Database;
- Result(sqlite3_stmt* stmt, const std::shared_ptr<sqlite3>& db, std::string query, bool is_done)
+ Result(sqlite3_stmt* stmt, const std::shared_ptr<sqlite3>& db,
+ std::string query, bool is_done)
: stmt_(stmt), db_(db), query_(std::move(query)), is_done_(is_done) {}
sqlite3_stmt* stmt_ = nullptr;
Database(std::string_view db_path, int flags) {
sqlite3* raw_db = nullptr;
int r = sqlite3_open_v2(db_path.data(), &raw_db, flags, nullptr);
- if (r != SQLITE_OK)
- throw DbException("open failed", r);
+ if (r != SQLITE_OK) throw DbException("open failed", r);
db_.reset(raw_db, sqlite3_close_v2);
}
Database(std::string_view db_path, int flags,
- std::function<bool(int)> busy_handler) {
+ std::function<bool(int)> busy_handler) {
sqlite3* raw_db = nullptr;
int r = sqlite3_open_v2(db_path.data(), &raw_db, flags, nullptr);
- if (r != SQLITE_OK)
- throw DbException("sqlite3_open_v2() failed", r);
+ if (r != SQLITE_OK) throw DbException("sqlite3_open_v2() failed", r);
db_.reset(raw_db, sqlite3_close_v2);
busy_handler_ = std::move(busy_handler);
- r = sqlite3_busy_handler(db_.get(), [](void* data, int count) {
- Database* pDb = static_cast<Database*>(data);
- if (pDb->busy_handler_ && pDb->busy_handler_(count))
- return 1;
- return 0;
- }, this);
-
- if (r != SQLITE_OK)
- throw DbException("sqlite3_busy_handler() failed", r);
+ r = sqlite3_busy_handler(
+ db_.get(),
+ [](void* data, int count) {
+ Database* pDb = static_cast<Database*>(data);
+ if (pDb->busy_handler_ && pDb->busy_handler_(count)) return 1;
+ return 0;
+ },
+ this);
+
+ if (r != SQLITE_OK) throw DbException("sqlite3_busy_handler() failed", r);
}
~Database() = default;
Database() = default;
Database(const Database&) = delete;
- Database& operator = (const Database&) = delete;
+ Database& operator=(const Database&) = delete;
Database(Database&& db) noexcept {
db_ = std::move(db.db_);
db.db_ = nullptr;
db.busy_handler_ = nullptr;
- sqlite3_busy_handler(db_.get(), [](void* data, int count) {
- Database* pDb = static_cast<Database*>(data);
- if (pDb->busy_handler_ && pDb->busy_handler_(count))
- return 1;
- return 0;
- }, this);
+ sqlite3_busy_handler(
+ db_.get(),
+ [](void* data, int count) {
+ Database* pDb = static_cast<Database*>(data);
+ if (pDb->busy_handler_ && pDb->busy_handler_(count)) return 1;
+ return 0;
+ },
+ this);
}
explicit operator bool() const {
- if (db_ == nullptr)
- return false;
+ if (db_ == nullptr) return false;
return true;
}
- Database& operator = (Database&& db) noexcept {
+ Database& operator=(Database&& db) noexcept {
if (this != &db) {
db_ = std::move(db.db_);
busy_handler_ = std::move(db.busy_handler_);
db.db_ = nullptr;
db.busy_handler_ = nullptr;
- sqlite3_busy_handler(db_.get(), [](void* data, int count) {
- Database* pDb = static_cast<Database*>(data);
- if (pDb->busy_handler_ && pDb->busy_handler_(count))
- return 1;
- return 0;
- }, this);
+ sqlite3_busy_handler(
+ db_.get(),
+ [](void* data, int count) {
+ Database* pDb = static_cast<Database*>(data);
+ if (pDb->busy_handler_ && pDb->busy_handler_(count)) return 1;
+ return 0;
+ },
+ this);
}
return *this;
}
Result Prepare(const Sql& sql) const {
- if (!db_)
- throw DbException("Not opened");
+ if (!db_) throw DbException("Not opened");
sqlite3_stmt* stmt = nullptr;
- int r = sqlite3_prepare_v2(db_.get(), sql.GetQuery().c_str(),
- -1, &stmt, nullptr);
- if (r != SQLITE_OK)
- return { nullptr, db_, "", true };
- return { stmt, db_, sql.GetQuery(), false };
+ int r = sqlite3_prepare_v2(db_.get(), sql.GetQuery().c_str(), -1, &stmt,
+ nullptr);
+ if (r != SQLITE_OK) return {nullptr, db_, "", true};
+ return {stmt, db_, sql.GetQuery(), false};
}
Result Exec(const Sql& sql) const {
- if (!db_)
- throw DbException("Not opened");
+ if (!db_) throw DbException("Not opened");
sqlite3_stmt* stmt = nullptr;
- int r = sqlite3_prepare_v2(db_.get(), sql.GetQuery().c_str(),
- -1, &stmt, nullptr);
+ int r = sqlite3_prepare_v2(db_.get(), sql.GetQuery().c_str(), -1, &stmt,
+ nullptr);
if (r != SQLITE_OK) {
- return { nullptr, db_, "", true };
+ return {nullptr, db_, "", true};
}
- std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*> stmt_auto(stmt,
- sqlite3_finalize);
+ std::unique_ptr<sqlite3_stmt, decltype(sqlite3_finalize)*> stmt_auto(
+ stmt, sqlite3_finalize);
int pos = 1;
for (const auto& i : sql.GetBindings()) {
Bind(pos++, i, stmt);
for (const auto& i : sql.GetBindingNameMap()) {
pos = sqlite3_bind_parameter_index(stmt, i.first.c_str());
- if (pos == 0)
- throw DbException("Invalid binding");
+ if (pos == 0) throw DbException("Invalid binding");
Bind(pos, i.second, stmt);
}
r = sqlite3_step(stmt);
if (r != SQLITE_ROW && r != SQLITE_DONE) {
- return { nullptr, db_, "", true };
+ return {nullptr, db_, "", true};
}
- return { stmt_auto.release(), db_, sql.GetQuery(),
- r == SQLITE_DONE ? true : false };
+ return {stmt_auto.release(), db_, sql.GetQuery(),
+ r == SQLITE_DONE ? true : false};
}
bool Exec(const Sql& sql, Result& previous_stmt) const {
throw DbException("Query is different");
sqlite3_stmt* stmt = previous_stmt.GetRaw();
- if (!stmt)
- return false;
+ if (!stmt) return false;
int r = sqlite3_reset(stmt);
- if (r != SQLITE_ROW && r != SQLITE_DONE && r != SQLITE_OK)
- return false;
+ if (r != SQLITE_ROW && r != SQLITE_DONE && r != SQLITE_OK) return false;
int pos = 1;
for (const auto& i : sql.GetBindings()) {
for (const auto& i : sql.GetBindingNameMap()) {
pos = sqlite3_bind_parameter_index(stmt, i.first.c_str());
- if (pos == 0)
- throw DbException("Invalid binding");
+ if (pos == 0) throw DbException("Invalid binding");
Bind(pos, i.second, stmt);
}
r = sqlite3_step(stmt);
- if (r != SQLITE_ROW && r != SQLITE_DONE)
- return false;
+ if (r != SQLITE_ROW && r != SQLITE_DONE) return false;
previous_stmt.SetDone(r == SQLITE_DONE ? true : false);
return true;
}
std::future<Result> ExecAsync(const Sql& sql) const {
- return std::async(std::launch::async, [=]() {
- return Exec(sql);
- });
+ return std::async(std::launch::async, [=]() { return Exec(sql); });
}
void OneStepExec(const Sql& sql) const {
char* errmsg = nullptr;
int ret = sqlite3_exec(db_.get(), sql.GetQuery().c_str(), nullptr, nullptr,
- &errmsg);
+ &errmsg);
if (ret != SQLITE_OK) {
- std::unique_ptr<char, decltype(sqlite3_free)*> errmsg_auto(
- errmsg, sqlite3_free);
+ std::unique_ptr<char, decltype(sqlite3_free)*> errmsg_auto(errmsg,
+ sqlite3_free);
throw DbException(errmsg);
}
}
sqlite3* GetRaw() const {
- if (!db_)
- throw DbException("Not opened");
+ if (!db_) throw DbException("Not opened");
return db_.get();
}
}
if (const std::string* pstr = std::get_if<std::string>(&(*type))) {
- r = sqlite3_bind_text(stmt, pos, (*pstr).c_str(), -1,
- SQLITE_TRANSIENT);
+ r = sqlite3_bind_text(stmt, pos, (*pstr).c_str(), -1, SQLITE_TRANSIENT);
} else if (const int64_t* pint = std::get_if<int64_t>(&(*type))) {
r = sqlite3_bind_int64(stmt, pos, (*pint));
} else if (const double* pdouble = std::get_if<double>(&(*type))) {
r = sqlite3_bind_double(stmt, pos, (*pdouble));
} else if (const std::vector<unsigned char>* pvector =
- std::get_if<std::vector<unsigned char>>(&(*type))) {
- r = sqlite3_bind_blob(stmt, pos, (*pvector).data(),
- (*pvector).size(), nullptr);
+ std::get_if<std::vector<unsigned char>>(&(*type))) {
+ r = sqlite3_bind_blob(stmt, pos, (*pvector).data(), (*pvector).size(),
+ nullptr);
} else {
r = -1;
}