Skip to content

Commit 4ed655b

Browse files
committed
Исправлена ошибка незакрытия базы данных.
Дополнены тесты
1 parent 384412f commit 4ed655b

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

src/sqlite.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ void SqliteBase::close() {
2828
}
2929

3030
int SqliteBase::exec(stru query) {
31-
return db_ ? sqlite3_exec(db_, lstringa<4096>{query}, nullptr, nullptr, nullptr) : SQLITE_ERROR;
31+
return opened_ ? sqlite3_exec(db_, lstringa<4096>{query}, nullptr, nullptr, nullptr) : SQLITE_ERROR;
3232
}
3333

3434
SqliteQuery SqliteBase::prepare(stru query) {
3535
sqlite3_stmt* stmt = nullptr;
36-
if (db_) {
36+
if (opened_) {
3737
sqlite3_prepare16_v3(db_, (void*)query.str, (int)query.length() * 2, 0, &stmt, nullptr);
3838
}
3939
return SqliteQuery(stmt);

src/sqlite.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include "core_as/str/sstring.h"
33
#include "sqlite3.h"
4+
#include <utility>
45
using namespace core_as::str;
56

67
class SqliteQuery;
@@ -21,13 +22,17 @@ concept QueryResultReceiver = requires(T& t) {
2122
class SqliteBase {
2223
public:
2324
SqliteBase() = default;
25+
~SqliteBase() {
26+
close();
27+
}
2428
SqliteBase(const SqliteBase&) = delete;
25-
SqliteBase(SqliteBase&& other) noexcept : db_(other.db_) {
29+
SqliteBase(SqliteBase&& other) noexcept : db_(other.db_), opened_(other.opened_) {
2630
other.db_ = nullptr;
31+
other.opened_ = false;
2732
}
2833
SqliteBase& operator=(SqliteBase other) noexcept {
29-
this->~SqliteBase();
30-
new (this) SqliteBase(std::move(other));
34+
std::swap(db_, other.db_);
35+
std::swap(opened_, other.opened_);
3136
return *this;
3237
}
3338

@@ -45,16 +50,16 @@ class SqliteBase {
4550
return stru{ db_ ? (const u16s*)sqlite3_errmsg16(db_) : nullptr};
4651
}
4752
int64_t lastId() const {
48-
return db_ ? sqlite3_last_insert_rowid(db_) : 0;
53+
return opened_ ? sqlite3_last_insert_rowid(db_) : 0;
4954
}
5055
int64_t changes() const {
51-
return db_ ? sqlite3_changes64(db_) : 0;
56+
return opened_ ? sqlite3_changes64(db_) : 0;
5257
}
5358
SqliteQuery prepare(stru query);
5459

5560
protected:
56-
sqlite3* db_{nullptr};
57-
bool opened_{false};
61+
sqlite3* db_{};
62+
bool opened_{};
5863
};
5964

6065
template<typename T>
@@ -238,14 +243,15 @@ class SqliteQuery {
238243
sqlite3_reset(stmt_);
239244
receiver.setResult(result, sqlite3_db_handle(stmt_));
240245
}
241-
template<QueryResultReceiver Q>
242-
Q exec() {
243-
Q receiver;
246+
template<QueryResultReceiver Q, typename ...Args>
247+
Q exec(Args&&... args) {
248+
Q receiver(std::forward<Args>(args)...);
244249
exec(receiver);
245250
return receiver;
246251
}
252+
247253
protected:
248-
sqlite3_stmt* stmt_{nullptr};
254+
sqlite3_stmt* stmt_{};
249255
};
250256

251257
double calcJulianDate(tm& dt);

src/tests/test_sqlite.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../sqlite.h"
22
#include<gtest/gtest.h>
3+
#include <utility>
34
#include <vector>
45

56
#ifdef _WIN32
@@ -22,6 +23,29 @@ TEST(Sqlite, CreateBase) {
2223
EXPECT_FALSE(base.isOpen());
2324
}
2425

26+
TEST(Sqlite, Move) {
27+
SqliteBase base;
28+
EXPECT_FALSE(base.isOpen());
29+
EXPECT_EQ(base.lastError(), u"");
30+
EXPECT_EQ(base.changes(), 0ll);
31+
EXPECT_EQ(base.lastId(), 0ll);
32+
33+
EXPECT_TRUE(base.open(u":memory:"));
34+
EXPECT_TRUE(base.isOpen());
35+
36+
sqlite3* db = base;
37+
38+
SqliteBase other(std::move(base));
39+
EXPECT_TRUE(other.isOpen());
40+
EXPECT_FALSE(base.isOpen());
41+
EXPECT_EQ(db, static_cast<sqlite3*>(other));
42+
43+
base = std::move(other);
44+
EXPECT_TRUE(base.isOpen());
45+
EXPECT_FALSE(other.isOpen());
46+
EXPECT_EQ(db, static_cast<sqlite3*>(base));
47+
}
48+
2549
TEST(Sqlite, ErrorCreateBase) {
2650
SqliteBase base;
2751
#ifdef _WIN32

src/version.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#define F_VERSION 1,0,0,5
2-
#define P_VERSION "1.0.0.5"
1+
#define F_VERSION 1,0,0,6
2+
#define P_VERSION "1.0.0.6"
33

0 commit comments

Comments
 (0)