first version of lockless storage backend (WIP with lots of debug output)
[quassel.git] / src / core / abstractsqlstorage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef ABSTRACTSQLSTORAGE_H
22 #define ABSTRACTSQLSTORAGE_H
23
24 #include "storage.h"
25
26 #include <QSqlDatabase>
27
28 class QSqlQuery;
29
30 class AbstractSqlStorage : public Storage {
31   Q_OBJECT
32
33 public:
34   AbstractSqlStorage(QObject *parent = 0);
35   virtual ~AbstractSqlStorage();
36
37 protected:
38   virtual bool init(const QVariantMap &settings = QVariantMap());
39   virtual void sync();
40   
41   QSqlDatabase logDb();
42   
43   QString queryString(const QString &queryName, int version);
44   inline QString queryString(const QString &queryName) { return queryString(queryName, 0); }
45
46   QSqlQuery &cachedQuery(const QString &queryName, int version);
47   inline QSqlQuery &cachedQuery(const QString &queryName) { return cachedQuery(queryName, 0); }
48
49   QStringList setupQueries();
50   bool setup(const QVariantMap &settings = QVariantMap());
51
52   QStringList upgradeQueries(int ver);
53   bool upgradeDb();
54
55   bool watchQuery(QSqlQuery &query);
56   
57   int schemaVersion();
58   virtual int installedSchemaVersion() { return -1; };
59
60   virtual QString driverName() = 0;
61   inline virtual QString hostName() { return QString(); }
62   virtual QString databaseName() = 0;
63   inline virtual QString userName() { return QString(); }
64   inline virtual QString password() { return QString(); }
65
66 signals:
67   void syncCachedQueries();
68
69 private slots:
70   void connectionDestroyed();
71
72 private:
73   void addConnectionToPool();
74
75   int _schemaVersion;
76
77   int _nextConnectionId;
78   QMutex _connectionPoolMutex;
79   class Connection;
80   QHash<QThread *, Connection *> _connectionPool;
81 };
82
83 // ========================================
84 //  AbstractSqlStorage::Connection
85 // ========================================
86 class AbstractSqlStorage::Connection : public QObject {
87   Q_OBJECT
88
89 public:
90   Connection(const QString &name, AbstractSqlStorage *storage, QObject *parent = 0);
91   ~Connection();
92   
93   inline QLatin1String name() const { return QLatin1String(_name); }
94   QSqlQuery &cachedQuery(const QString &queryName, int version);
95
96 public slots:
97   void syncCachedQueries();
98
99 private:
100   QByteArray _name;
101   QHash<QPair<QString, int>, QSqlQuery *> _queryCache;
102   AbstractSqlStorage *_storageEngine;
103 };
104
105 #endif