e087c6dff31bf1e8165cbb8aa6ba9f85298b0a93
[quassel.git] / src / core / abstractsqlstorage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel Project                          *
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 State init(const QVariantMap &settings = QVariantMap());
39   inline 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   QStringList setupQueries();
47   bool setup(const QVariantMap &settings = QVariantMap());
48
49   QStringList upgradeQueries(int ver);
50   bool upgradeDb();
51
52   bool watchQuery(QSqlQuery &query);
53   
54   int schemaVersion();
55   virtual int installedSchemaVersion() { return -1; };
56   virtual bool updateSchemaVersion(int newVersion) = 0;
57   virtual bool setupSchemaVersion(int version) = 0;
58
59   virtual void setConnectionProperties(const QVariantMap &properties) = 0;
60   virtual QString driverName() = 0;
61   inline virtual QString hostName() { return QString(); }
62   inline virtual int port() { return -1; }
63   virtual QString databaseName() = 0;
64   inline virtual QString userName() { return QString(); }
65   inline virtual QString password() { return QString(); }
66
67 private slots:
68   void connectionDestroyed();
69
70 private:
71   void addConnectionToPool();
72
73   int _schemaVersion;
74
75   int _nextConnectionId;
76   QMutex _connectionPoolMutex;
77   // we let a Connection Object manage each actual db connection
78   // those objects reside in the thread the connection belongs to
79   // which allows us thread safe termination of a connection
80   class Connection;
81   QHash<QThread *, Connection *> _connectionPool;
82 };
83
84 // ========================================
85 //  AbstractSqlStorage::Connection
86 // ========================================
87 class AbstractSqlStorage::Connection : public QObject {
88   Q_OBJECT
89
90 public:
91   Connection(const QString &name, QObject *parent = 0);
92   ~Connection();
93   
94   inline QLatin1String name() const { return QLatin1String(_name); }
95
96 private:
97   QByteArray _name;
98 };
99
100 #endif