- Added new class to create database objects (Factory)
[dashstudio.git] / src / dashserver / xdbms / manager.cpp
blobf0e941fad0ab487cc54802b4a1acdd16cf353daa
1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
3 * krawek@gmail.com *
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) any later version. *
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 ***************************************************************************/
22 #include "manager.h"
23 #include "table.h"
24 #include "objectfactory.h"
26 #include <QHash>
27 #include <QDir>
29 #include <dcore/globaldeleter.h>
30 #include <dcore/debug.h>
32 static DCore::GlobalDeleter<XDBMS::Manager> deleter;
34 namespace XDBMS {
36 struct Manager::Private
38 Private() : initialized(false), factory(new ObjectFactory) {}
40 Manager *self;
41 QHash<QString, Table *> tables;
42 QString databasePath;
44 bool initialized;
45 ObjectFactory *factory;
48 Manager::Private *const Manager::d = new Manager::Private;
50 Manager::Manager(QObject *parent)
51 : QObject(parent)
56 Manager::~Manager()
58 delete d->factory;
59 delete d;
62 bool Manager::initialize(const QString &databasePath)
64 if( d->initialized ) return false;
66 QDir dir(databasePath);
68 if( !dir.exists() )
70 bool ok = dir.mkdir(dir.absolutePath());
72 if( ok )
74 d->initialized = true;
75 d->databasePath = dir.absolutePath();
78 return ok;
81 d->databasePath = dir.absolutePath();
82 d->initialized = true;
84 // Read all tables
85 foreach(QFileInfo table, dir.entryInfoList(QStringList() << "*.xdt"))
87 QString name = table.baseName();
89 dDebug() << "=> Loading table: " << name;
91 Table *t = this->table(name);
92 Q_UNUSED(t);
95 return d->initialized;
98 Table *Manager::table(const QString &tableId)
100 Table *table = d->tables.value(tableId);
102 if( !table )
104 table = new Table(tableId);
105 d->tables[tableId] = table;
108 return table;
111 bool Manager::addObject(const QString &tableId, Object *object)
113 Table *table = this->table(tableId);
114 if( table )
116 return table->addObject(object);
119 return false;
122 bool Manager::load(const QString &tableId, Object *object) const
124 Table *table = d->tables.value(tableId);
125 if( table )
127 return table->load(object);
130 return false;
133 Object *Manager::object(const QString &tableId, const QString &id) const
135 Table *table = d->tables.value(tableId);
136 if( table )
138 return table->object(id);
141 return 0;
144 bool Manager::update(const QString &tableId, Object *object)
146 Table *table = d->tables.value(tableId);
147 if( table )
149 return table->update(object);
152 return false;
155 bool Manager::remove(const QString &tableId, Object *object)
157 Table *table = d->tables.value(tableId);
158 if( table )
160 return table->remove(object);
163 return false;
166 QDir Manager::databaseDir() const
168 return QDir(d->databasePath);
171 void Manager::setObjectFactory(ObjectFactory *factory)
173 if(d->factory == factory)
174 return;
176 delete d->factory;
177 d->factory = factory;
180 ObjectFactory *Manager::objectFactory() const
182 return d->factory;
186 Manager *Manager::self()
188 if( d->self )
190 d->self = new Manager;
191 deleter.setObject(d->self);
194 return d->self;