1 #pragma once
2 
3 #include <QSqlDatabase>
4 
5 #include "util/db/dbconnectionpool.h"
6 
7 
8 namespace mixxx {
9 
10 // Dynamically provides thread-local database connections from
11 // the pool.
12 class DbConnectionPooled final {
13   public:
14     explicit DbConnectionPooled(
15             DbConnectionPoolPtr pDbConnectionPool = DbConnectionPoolPtr())
m_pDbConnectionPool(std::move (pDbConnectionPool))16         : m_pDbConnectionPool(std::move(pDbConnectionPool)) {
17     }
18 
19     // Checks if this instance actually references a connection pool
20     // needed for obtaining thread-local database connections (see below).
21     explicit operator bool() const {
22         return static_cast<bool>(m_pDbConnectionPool);
23     }
24 
25     // Tries to obtain an existing thread-local database connection
26     // from the pool. This might fail if either the reference to the
27     // connection pool is missing or if the pool does not contain a
28     // thread-local connection for this thread (previously created
29     // by some DbConnectionPooler). On failure a non-functional default
30     // constructed database connection is returned.
31     //
32     // The returned connections is not bound to this instance:
33     // QSqlDatabase dbConnection = DbConnectionPooled(...);
34     /*implicit*/ operator QSqlDatabase() const;
35 
36   private:
37     DbConnectionPoolPtr m_pDbConnectionPool;
38 };
39 
40 } // namespace mixxx
41