Skip to content

Commit a396e8a

Browse files
committed
Fix APool::begin not returning an active transaction
1 parent 4a57b60 commit a396e8a

3 files changed

Lines changed: 89 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: MIT
33

44
cmake_minimum_required(VERSION 3.28)
5-
project(libasql VERSION 0.107.0 LANGUAGES CXX)
5+
project(libasql VERSION 0.108.0 LANGUAGES CXX)
66

77
include(GNUInstallDirs)
88
include(GenerateExportHeader)

src/apool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ AExpectedTransaction APool::begin(QObject *receiver, QStringView poolName)
528528
if (db) {
529529
auto result = co_await db->begin(receiver);
530530
if (result) {
531-
chainData->deliverDirect(ATransaction::fromStarted(db.value()));
531+
chainData->deliverDirect(std::move(*result));
532532
co_return;
533533
}
534534
chainData->deliverDirect(std::unexpected(result.error()));

tests/sqlite_tst.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class TestSqlite : public CoverageObject
2525

2626
private Q_SLOTS:
2727
void testQueries();
28+
void testPoolBeginCommit();
29+
void testPoolBeginRollback();
2830
};
2931

3032
void TestSqlite::initTest()
@@ -277,6 +279,91 @@ void TestSqlite::testQueries()
277279
loop.exec();
278280
}
279281

282+
void TestSqlite::testPoolBeginCommit()
283+
{
284+
QEventLoop loop;
285+
{
286+
auto finished = std::make_shared<QObject>();
287+
connect(finished.get(), &QObject::destroyed, &loop, &QEventLoop::quit);
288+
289+
auto poolBeginCommit = [finished]() -> ACoroTerminator {
290+
auto _ = qScopeGuard(
291+
[finished] { qDebug() << "poolBeginCommit exited" << finished.use_count(); });
292+
293+
auto setupDb = co_await APool::database(nullptr, u"file"_s);
294+
AVERIFY(setupDb);
295+
auto create = co_await setupDb->exec(
296+
u"CREATE TABLE IF NOT EXISTS pool_commit_test (id INTEGER)"_s);
297+
AVERIFY(create);
298+
299+
auto t = co_await APool::begin(nullptr, u"file"_s);
300+
AVERIFY(t);
301+
AVERIFY(t->isActive());
302+
303+
auto insert = co_await t->database().exec(u"INSERT INTO pool_commit_test VALUES (1)"_s);
304+
AVERIFY(insert);
305+
ACOMPARE_EQ(insert->numRowsAffected(), qint64(1));
306+
307+
auto commitResult = co_await t->commit();
308+
AVERIFY(commitResult);
309+
AVERIFY(!commitResult->hasError());
310+
311+
auto verifyDb = co_await APool::database(nullptr, u"file"_s);
312+
AVERIFY(verifyDb);
313+
auto select = co_await verifyDb->exec(u"SELECT COUNT(*) FROM pool_commit_test"_s);
314+
AVERIFY(select);
315+
ACOMPARE_EQ((*select)[0][0].toInt(), 1);
316+
317+
co_await verifyDb->exec(u"DROP TABLE pool_commit_test"_s);
318+
};
319+
poolBeginCommit();
320+
}
321+
loop.exec();
322+
}
323+
324+
void TestSqlite::testPoolBeginRollback()
325+
{
326+
QEventLoop loop;
327+
{
328+
auto finished = std::make_shared<QObject>();
329+
connect(finished.get(), &QObject::destroyed, &loop, &QEventLoop::quit);
330+
331+
auto poolBeginRollback = [finished]() -> ACoroTerminator {
332+
auto _ = qScopeGuard(
333+
[finished] { qDebug() << "poolBeginRollback exited" << finished.use_count(); });
334+
335+
auto setupDb = co_await APool::database(nullptr, u"file"_s);
336+
AVERIFY(setupDb);
337+
auto create = co_await setupDb->exec(
338+
u"CREATE TABLE IF NOT EXISTS pool_rollback_test (id INTEGER)"_s);
339+
AVERIFY(create);
340+
341+
auto t = co_await APool::begin(nullptr, u"file"_s);
342+
AVERIFY(t);
343+
AVERIFY(t->isActive());
344+
345+
auto insert =
346+
co_await t->database().exec(u"INSERT INTO pool_rollback_test VALUES (42)"_s);
347+
AVERIFY(insert);
348+
ACOMPARE_EQ(insert->numRowsAffected(), qint64(1));
349+
350+
auto rollbackResult = co_await t->rollback();
351+
AVERIFY(rollbackResult);
352+
AVERIFY(!rollbackResult->hasError());
353+
354+
auto verifyDb = co_await APool::database(nullptr, u"file"_s);
355+
AVERIFY(verifyDb);
356+
auto select = co_await verifyDb->exec(u"SELECT COUNT(*) FROM pool_rollback_test"_s);
357+
AVERIFY(select);
358+
ACOMPARE_EQ((*select)[0][0].toInt(), 0);
359+
360+
co_await verifyDb->exec(u"DROP TABLE pool_rollback_test"_s);
361+
};
362+
poolBeginRollback();
363+
}
364+
loop.exec();
365+
}
366+
280367
QTEST_MAIN(TestSqlite)
281368
#include "sqlite_tst.moc"
282369

0 commit comments

Comments
 (0)