Skip to content

Commit ffe156b

Browse files
authored
Remove APool::database sync/callback API; rename coDatabase to database (#55)
1 parent 1a5cec8 commit ffe156b

8 files changed

Lines changed: 41 additions & 123 deletions

File tree

demos/async1/coroutines.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main(int argc, char *argv[])
4343
auto _ = qScopeGuard([] { qDebug() << "coro exited"; });
4444
qDebug() << "coro started";
4545

46-
auto db = co_await APool::coDatabase();
46+
auto db = co_await APool::database();
4747

4848
auto transaction = co_await db->beginTransaction();
4949
qDebug() << "transaction started";
@@ -125,7 +125,7 @@ int main(int argc, char *argv[])
125125
auto _ = qScopeGuard([] { qDebug() << "coro inner exited"; });
126126
qDebug() << "coro inner started";
127127

128-
auto db = co_await APool::coDatabase();
128+
auto db = co_await APool::database();
129129
if (!db) {
130130
qDebug() << "coro db error" << db.error();
131131
co_return;
@@ -155,7 +155,7 @@ int main(int argc, char *argv[])
155155
auto _ = qScopeGuard([] { qDebug() << "coro pool exited"; });
156156
qDebug() << "coro pool started";
157157

158-
auto db = co_await APool::coDatabase();
158+
auto db = co_await APool::database();
159159
if (db.has_value()) {
160160
qDebug() << "coro pool has value" << db->isOpen();
161161
} else {
@@ -213,7 +213,7 @@ int main(int argc, char *argv[])
213213
});
214214
co_yield obj; // so that this promise is destroyed if this object is destroyed
215215

216-
auto db = co_await APool::coDatabase();
216+
auto db = co_await APool::database();
217217

218218
auto result = co_await db->exec(u8"SELECT now(), pg_sleep(2)", obj);
219219
if (result.has_value()) {
@@ -235,7 +235,7 @@ int main(int argc, char *argv[])
235235
});
236236
co_yield obj; // so that this promise is destroyed if this object is destroyed
237237

238-
auto db = co_await APool::coDatabase();
238+
auto db = co_await APool::database();
239239

240240
auto result = co_await db->exec(u8"SELECT now()", obj);
241241
if (result.has_value()) {
@@ -290,7 +290,7 @@ int main(int argc, char *argv[])
290290
auto _ = qScopeGuard([] { qDebug() << "callPool exited"; });
291291
qDebug() << "callPool started";
292292

293-
auto db = co_await APool::coDatabase(nullptr);
293+
auto db = co_await APool::database(nullptr);
294294
if (!db.has_value()) {
295295
qDebug() << "coro exec get db error" << db.error();
296296
co_return;
@@ -452,15 +452,15 @@ int main(int argc, char *argv[])
452452
if (false) {
453453
auto testPoolSync = []() -> ACoroTerminator {
454454
{
455-
auto db1 = co_await APool::coDatabase(nullptr, u"pool");
455+
auto db1 = co_await APool::database(nullptr, u"pool");
456456
qDebug() << db1->isValid();
457457
qDebug() << APool::currentConnections(u"pool");
458458

459-
auto db2 = co_await APool::coDatabase(nullptr, u"pool");
459+
auto db2 = co_await APool::database(nullptr, u"pool");
460460
qDebug() << db2->isValid();
461461
qDebug() << APool::currentConnections(u"pool");
462462

463-
auto db3 = co_await APool::coDatabase(nullptr, u"pool");
463+
auto db3 = co_await APool::database(nullptr, u"pool");
464464
qDebug() << db3->isValid();
465465
qDebug() << APool::currentConnections(u"pool");
466466
}

demos/sqlite/sqlite.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ int main(int argc, char *argv[])
111111

112112
// TODO database returns a db handle that is not open yet.
113113
// because database() method calls open() and do not wait it.
114-
auto db = co_await APool::coDatabase();
114+
auto db = co_await APool::database();
115115
if (db) {
116116
qDebug() << "coro db isOpen" << db->isOpen();
117117
} else {

src/acache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ ACoroTerminator
126126
break;
127127
case ACachePrivate::DbSource::Pool:
128128
{
129-
auto dbFromPool = co_await APool::coDatabase(q_ptr, poolName);
129+
auto dbFromPool = co_await APool::database(q_ptr, poolName);
130130
if (!dbFromPool) {
131131
qCritical(ASQL_CACHE) << "Failed to get connection from pool" << dbFromPool.error();
132132
if (cb) {

src/adriverpg.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,10 @@ int ADriverPg::doExec(APGQuery &pgQuery)
736736
if (prepared == m_preparedQueries.end()) {
737737
const auto preparedId = preparedQueryStringId(pgQuery.preparedQuery->identification());
738738
ret = PQsendPrepare(m_conn->conn(),
739-
preparedId.constData(),
740-
pgQuery.preparedQuery->query().constData(),
741-
0,
742-
nullptr); // perhaps later use binary results
739+
preparedId.constData(),
740+
pgQuery.preparedQuery->query().constData(),
741+
0,
742+
nullptr); // perhaps later use binary results
743743

744744
if (ret == 1 && pipelineStatus() == ADatabase::PipelineStatus::On) {
745745
// pretend that it was prepared otherwise it can't be used in in the pipeline
@@ -907,10 +907,10 @@ int ADriverPg::doExecParams(APGQuery &pgQuery)
907907
if (prepared == m_preparedQueries.end()) {
908908
const auto preparedId = preparedQueryStringId(pgQuery.preparedQuery->identification());
909909
ret = PQsendPrepare(m_conn->conn(),
910-
preparedId.constData(),
911-
pgQuery.preparedQuery->query().constData(),
912-
params.size(),
913-
paramTypes.get());
910+
preparedId.constData(),
911+
pgQuery.preparedQuery->query().constData(),
912+
params.size(),
913+
paramTypes.get());
914914

915915
if (ret == 1 && pipelineStatus() == ADatabase::PipelineStatus::On) {
916916
// pretend that it was prepared otherwise it can't be used in in the pipeline

src/apool.cpp

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -166,52 +166,6 @@ void APool::pushDatabaseBack(QStringView connectionName, ADriver *driver)
166166
}
167167
}
168168

169-
ADatabase APool::database(QStringView poolName)
170-
{
171-
ADatabase db;
172-
auto it = m_connectionPool.find(poolName);
173-
if (it != m_connectionPool.end()) {
174-
APoolInternal &iPool = it.value();
175-
if (iPool.pool.empty()) {
176-
if (iPool.maximuConnections && iPool.connectionCount >= iPool.maximuConnections) {
177-
qCritical(ASQL_POOL) << "Maximum number of connections reached" << poolName
178-
<< iPool.connectionCount << iPool.connectionQueue.size();
179-
} else {
180-
++iPool.connectionCount;
181-
auto driver = iPool.driverFactory->createRawDriver();
182-
qDebug(ASQL_POOL) << "Creating a database connection for pool" << poolName
183-
<< driver;
184-
db.d = std::shared_ptr<ADriver>(driver, [poolName = iPool.name](ADriver *driver) {
185-
pushDatabaseBack(poolName, driver);
186-
});
187-
}
188-
} else {
189-
qDebug(ASQL_POOL) << "Reusing a database connection from pool" << poolName;
190-
ADriver *driver = iPool.pool.takeLast();
191-
db.d = std::shared_ptr<ADriver>(driver, [poolName = iPool.name](ADriver *driver) {
192-
pushDatabaseBack(poolName, driver);
193-
});
194-
}
195-
196-
if (db.isOpen()) {
197-
if (iPool.reuseCb) {
198-
iPool.reuseCb(db);
199-
}
200-
} else {
201-
db.open(nullptr,
202-
[setupCb = iPool.setupCb, db](bool isOpen, const QString &errorString) {
203-
if (isOpen && setupCb) {
204-
setupCb(db);
205-
}
206-
});
207-
}
208-
} else {
209-
qCritical(ASQL_POOL) << "Database pool NOT FOUND" << poolName;
210-
db.open();
211-
}
212-
return db;
213-
}
214-
215169
int APool::currentConnections(QStringView poolName)
216170
{
217171
auto it = m_connectionPool.find(poolName);
@@ -221,7 +175,7 @@ int APool::currentConnections(QStringView poolName)
221175
return 0;
222176
}
223177

224-
void APool::database(QObject *receiver, ADatabaseFn cb, QStringView poolName)
178+
void APool::databaseCallback(QObject *receiver, ADatabaseFn cb, QStringView poolName)
225179
{
226180
ADatabase db;
227181
auto it = m_connectionPool.find(poolName);
@@ -278,10 +232,10 @@ void APool::database(QObject *receiver, ADatabaseFn cb, QStringView poolName)
278232
}
279233
}
280234

281-
AExpectedDatabase APool::coDatabase(QObject *receiver, QStringView poolName)
235+
AExpectedDatabase APool::database(QObject *receiver, QStringView poolName)
282236
{
283237
AExpectedDatabase coro(receiver);
284-
database(receiver, ADatabaseFn{std::weak_ptr<ACoroDatabase>{coro.m_data}}, poolName);
238+
databaseCallback(receiver, ADatabaseFn{std::weak_ptr<ACoroDatabase>{coro.m_data}}, poolName);
285239
return coro;
286240
}
287241

@@ -345,7 +299,7 @@ AExpectedResult APool::exec(QStringView query, QObject *receiver, QStringView po
345299
auto ref = coro.ref();
346300

347301
[](ACoroDataRef ref, auto query, QObject *receiver, QStringView poolName) -> ACoroTerminator {
348-
auto db = co_await coDatabase(receiver, poolName);
302+
auto db = co_await database(receiver, poolName);
349303
if (db) {
350304
auto result = co_await db->exec(query, receiver);
351305
if (result) {
@@ -370,7 +324,7 @@ AExpectedResult APool::exec(QUtf8StringView query, QObject *receiver, QStringVie
370324
auto ref = coro.ref();
371325

372326
[](ACoroDataRef ref, auto query, QObject *receiver, QStringView poolName) -> ACoroTerminator {
373-
auto db = co_await coDatabase(receiver, poolName);
327+
auto db = co_await database(receiver, poolName);
374328
if (db) {
375329
auto result = co_await db->exec(query, receiver);
376330
if (result) {
@@ -395,7 +349,7 @@ AExpectedMultiResult APool::execMulti(QStringView query, QObject *receiver, QStr
395349
auto ref = coro.ref();
396350

397351
[](ACoroDataRef ref, auto query, QObject *receiver, QStringView poolName) -> ACoroTerminator {
398-
auto db = co_await coDatabase(receiver, poolName);
352+
auto db = co_await database(receiver, poolName);
399353
if (db) {
400354
auto awaiter = db->execMulti(query, receiver);
401355

@@ -428,7 +382,7 @@ AExpectedMultiResult
428382
auto ref = coro.ref();
429383

430384
[](ACoroDataRef ref, auto query, QObject *receiver, QStringView poolName) -> ACoroTerminator {
431-
auto db = co_await coDatabase(receiver, poolName);
385+
auto db = co_await database(receiver, poolName);
432386
if (db) {
433387
auto awaiter = db->execMulti(query, receiver);
434388

@@ -460,7 +414,7 @@ AExpectedResult APool::exec(const APreparedQuery &query, QObject *receiver, QStr
460414
auto ref = coro.ref();
461415

462416
[](ACoroDataRef ref, auto query, QObject *receiver, QStringView poolName) -> ACoroTerminator {
463-
auto db = co_await coDatabase(receiver, poolName);
417+
auto db = co_await database(receiver, poolName);
464418
if (db) {
465419
auto result = co_await db->exec(query, receiver);
466420
if (result) {
@@ -489,7 +443,7 @@ AExpectedResult APool::exec(QStringView query,
489443

490444
[](ACoroDataRef ref, auto query, QVariantList params, QObject *receiver, QStringView poolName)
491445
-> ACoroTerminator {
492-
auto db = co_await coDatabase(receiver, poolName);
446+
auto db = co_await database(receiver, poolName);
493447
if (db) {
494448
auto result = co_await db->exec(query, params, receiver);
495449

@@ -519,7 +473,7 @@ AExpectedResult APool::exec(QUtf8StringView query,
519473

520474
[](ACoroDataRef ref, auto query, QVariantList params, QObject *receiver, QStringView poolName)
521475
-> ACoroTerminator {
522-
auto db = co_await coDatabase(receiver, poolName);
476+
auto db = co_await database(receiver, poolName);
523477
if (db) {
524478
auto result = co_await db->exec(query, params, receiver);
525479
if (result) {
@@ -548,7 +502,7 @@ AExpectedResult APool::exec(const APreparedQuery &query,
548502

549503
[](ACoroDataRef ref, auto query, QVariantList params, QObject *receiver, QStringView poolName)
550504
-> ACoroTerminator {
551-
auto db = co_await coDatabase(receiver, poolName);
505+
auto db = co_await database(receiver, poolName);
552506
if (db) {
553507
auto result = co_await db->exec(query, params, receiver);
554508
if (result) {
@@ -570,7 +524,7 @@ AExpectedTransaction APool::begin(QObject *receiver, QStringView poolName)
570524
{
571525
AExpectedTransaction coro(receiver);
572526
[](auto chainData, QObject *receiver, QStringView poolName) -> ACoroTerminator {
573-
auto db = co_await coDatabase(receiver, poolName);
527+
auto db = co_await database(receiver, poolName);
574528
if (db) {
575529
auto result = co_await db->begin(receiver);
576530
if (result) {

src/apool.h

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,41 +57,15 @@ class ASQL_EXPORT APool
5757
*/
5858
static QStringList pools();
5959

60-
/*!
61-
* \brief database
62-
*
63-
* This method returns a new database object, unless an idle connection
64-
* (one that were previously dereferenced) is available on the pool.
65-
*
66-
* If the pool was not created or has reached it's maximum limit an invalid
67-
* database object is returned.
68-
*
69-
* \param poolName
70-
* \return ADatabase
71-
*/
72-
static ADatabase database(QStringView poolName = defaultPool);
73-
7460
/*!
7561
* \brief currentConnections of the pool
7662
* \param poolName
7763
* \return the number of active connections on this pool
7864
*/
7965
static int currentConnections(QStringView poolName = defaultPool);
8066

81-
/*!
82-
* \brief retrieves a database object
83-
*
84-
* This method is only useful if the pool has a limit of maximum connections allowed,
85-
* when the limit is reached instead of immediately returning a database object it will
86-
* queue the request and once a connection is freed the callback is issued.
87-
*
88-
* \param receiver
89-
* \param connectionName
90-
*/
91-
static void database(QObject *receiver, ADatabaseFn cb, QStringView poolName = defaultPool);
92-
93-
static AExpectedDatabase coDatabase(QObject *receiver = nullptr,
94-
QStringView poolName = defaultPool);
67+
static AExpectedDatabase database(QObject *receiver = nullptr,
68+
QStringView poolName = defaultPool);
9569

9670
/*!
9771
* \brief setMaxIdleConnections maximum number of idle connections of the pool
@@ -202,6 +176,7 @@ class ASQL_EXPORT APool
202176
QStringView poolName = defaultPool);
203177

204178
private:
179+
static void databaseCallback(QObject *receiver, ADatabaseFn cb, QStringView poolName);
205180
inline static void pushDatabaseBack(QStringView connectionName, ADriver *driver);
206181
};
207182

tests/CoverageObject.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,21 @@ void CoverageObject::testPool()
7676
[finished] { qDebug() << "rowsAffected exited" << finished.use_count(); });
7777

7878
{
79-
auto db1 = co_await APool::coDatabase();
79+
auto db1 = co_await APool::database();
8080
AVERIFY(db1);
8181
AVERIFY(db1->isOpen());
8282
ACOMPARE_EQ(APool::currentConnections(), 1);
8383

84-
auto db2 = co_await APool::coDatabase();
84+
auto db2 = co_await APool::database();
8585
AVERIFY(db2);
8686
ACOMPARE_EQ(APool::currentConnections(), 2);
8787
}
8888

89-
auto db3 = co_await APool::coDatabase();
89+
auto db3 = co_await APool::database();
9090
AVERIFY(db3);
9191
ACOMPARE_EQ(APool::currentConnections(), 2);
9292

93-
auto db4 = co_await APool::coDatabase();
93+
auto db4 = co_await APool::database();
9494
AVERIFY(db4);
9595
ACOMPARE_EQ(APool::currentConnections(), 2);
9696
};

tests/sqlite_tst.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void TestSqlite::testQueries()
134134
auto _ = qScopeGuard(
135135
[finished] { qDebug() << "multipleQueries exited" << finished.use_count(); });
136136

137-
auto db = co_await APool::coDatabase();
137+
auto db = co_await APool::database();
138138
AVERIFY(db);
139139

140140
// TODO use APool::begin() once new coro class is ready
@@ -167,17 +167,6 @@ void TestSqlite::testQueries()
167167
};
168168
singleQuery();
169169

170-
auto singleQuery2 = [finished]() -> ACoroTerminator {
171-
auto _ = qScopeGuard(
172-
[finished] { qDebug() << "singleQuery2 exited" << finished.use_count(); });
173-
174-
qDebug() << Q_FUNC_INFO << 1;
175-
std::ignore = APool::database();
176-
qDebug() << Q_FUNC_INFO << 2;
177-
co_return;
178-
};
179-
singleQuery2();
180-
181170
auto queryParams = [finished]() -> ACoroTerminator {
182171
auto _ = qScopeGuard(
183172
[finished] { qDebug() << "queryParams exited" << finished.use_count(); });
@@ -204,7 +193,7 @@ void TestSqlite::testQueries()
204193
auto _ = qScopeGuard(
205194
[finished] { qDebug() << "queryPrepared exited" << finished.use_count(); });
206195

207-
auto db = co_await APool::coDatabase(); // Must be the same db
196+
auto db = co_await APool::database(); // Must be the same db
208197
AVERIFY(db);
209198

210199
for (int i = 0; i < 5; ++i) {
@@ -222,7 +211,7 @@ void TestSqlite::testQueries()
222211
auto _ = qScopeGuard(
223212
[finished] { qDebug() << "rowsAffected exited" << finished.use_count(); });
224213

225-
auto db = co_await APool::coDatabase(); // Must be the same memory db
214+
auto db = co_await APool::database(); // Must be the same memory db
226215
AVERIFY(db);
227216

228217
auto create = co_await db->exec(u"CREATE TABLE temp (name TEXT)");

0 commit comments

Comments
 (0)