Skip to content

Commit

Permalink
Исправлена ошибка незакрытия базы данных.
Browse files Browse the repository at this point in the history
Дополнены тесты
  • Loading branch information
orefkov committed Dec 8, 2023
1 parent 384412f commit 4ed655b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ void SqliteBase::close() {
}

int SqliteBase::exec(stru query) {
return db_ ? sqlite3_exec(db_, lstringa<4096>{query}, nullptr, nullptr, nullptr) : SQLITE_ERROR;
return opened_ ? sqlite3_exec(db_, lstringa<4096>{query}, nullptr, nullptr, nullptr) : SQLITE_ERROR;
}

SqliteQuery SqliteBase::prepare(stru query) {
sqlite3_stmt* stmt = nullptr;
if (db_) {
if (opened_) {
sqlite3_prepare16_v3(db_, (void*)query.str, (int)query.length() * 2, 0, &stmt, nullptr);
}
return SqliteQuery(stmt);
Expand Down
28 changes: 17 additions & 11 deletions src/sqlite.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "core_as/str/sstring.h"
#include "sqlite3.h"
#include <utility>
using namespace core_as::str;

class SqliteQuery;
Expand All @@ -21,13 +22,17 @@ concept QueryResultReceiver = requires(T& t) {
class SqliteBase {
public:
SqliteBase() = default;
~SqliteBase() {
close();
}
SqliteBase(const SqliteBase&) = delete;
SqliteBase(SqliteBase&& other) noexcept : db_(other.db_) {
SqliteBase(SqliteBase&& other) noexcept : db_(other.db_), opened_(other.opened_) {
other.db_ = nullptr;
other.opened_ = false;
}
SqliteBase& operator=(SqliteBase other) noexcept {
this->~SqliteBase();
new (this) SqliteBase(std::move(other));
std::swap(db_, other.db_);
std::swap(opened_, other.opened_);
return *this;
}

Expand All @@ -45,16 +50,16 @@ class SqliteBase {
return stru{ db_ ? (const u16s*)sqlite3_errmsg16(db_) : nullptr};
}
int64_t lastId() const {
return db_ ? sqlite3_last_insert_rowid(db_) : 0;
return opened_ ? sqlite3_last_insert_rowid(db_) : 0;
}
int64_t changes() const {
return db_ ? sqlite3_changes64(db_) : 0;
return opened_ ? sqlite3_changes64(db_) : 0;
}
SqliteQuery prepare(stru query);

protected:
sqlite3* db_{nullptr};
bool opened_{false};
sqlite3* db_{};
bool opened_{};
};

template<typename T>
Expand Down Expand Up @@ -238,14 +243,15 @@ class SqliteQuery {
sqlite3_reset(stmt_);
receiver.setResult(result, sqlite3_db_handle(stmt_));
}
template<QueryResultReceiver Q>
Q exec() {
Q receiver;
template<QueryResultReceiver Q, typename ...Args>
Q exec(Args&&... args) {
Q receiver(std::forward<Args>(args)...);
exec(receiver);
return receiver;
}

protected:
sqlite3_stmt* stmt_{nullptr};
sqlite3_stmt* stmt_{};
};

double calcJulianDate(tm& dt);
Expand Down
24 changes: 24 additions & 0 deletions src/tests/test_sqlite.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../sqlite.h"
#include<gtest/gtest.h>
#include <utility>
#include <vector>

#ifdef _WIN32
Expand All @@ -22,6 +23,29 @@ TEST(Sqlite, CreateBase) {
EXPECT_FALSE(base.isOpen());
}

TEST(Sqlite, Move) {
SqliteBase base;
EXPECT_FALSE(base.isOpen());
EXPECT_EQ(base.lastError(), u"");
EXPECT_EQ(base.changes(), 0ll);
EXPECT_EQ(base.lastId(), 0ll);

EXPECT_TRUE(base.open(u":memory:"));
EXPECT_TRUE(base.isOpen());

sqlite3* db = base;

SqliteBase other(std::move(base));
EXPECT_TRUE(other.isOpen());
EXPECT_FALSE(base.isOpen());
EXPECT_EQ(db, static_cast<sqlite3*>(other));

base = std::move(other);
EXPECT_TRUE(base.isOpen());
EXPECT_FALSE(other.isOpen());
EXPECT_EQ(db, static_cast<sqlite3*>(base));
}

TEST(Sqlite, ErrorCreateBase) {
SqliteBase base;
#ifdef _WIN32
Expand Down
4 changes: 2 additions & 2 deletions src/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#define F_VERSION 1,0,0,5
#define P_VERSION "1.0.0.5"
#define F_VERSION 1,0,0,6
#define P_VERSION "1.0.0.6"

0 comments on commit 4ed655b

Please sign in to comment.