1 /***************************************************************************
2 * Copyright (C) 2007 by David Cuadrado *
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. *
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. *
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 ***************************************************************************/
24 #include "objectfactory.h"
29 #include <dcore/globaldeleter.h>
30 #include <dcore/debug.h>
32 static DCore::GlobalDeleter
<XDBMS::Manager
> deleter
;
36 struct Manager::Private
38 Private() : initialized(false), factory(new ObjectFactory
) {}
41 QHash
<QString
, Table
*> tables
;
45 ObjectFactory
*factory
;
48 Manager::Private
*const Manager::d
= new Manager::Private
;
50 Manager::Manager(QObject
*parent
)
62 bool Manager::initialize(const QString
&databasePath
)
64 if( d
->initialized
) return false;
66 QDir
dir(databasePath
);
70 bool ok
= dir
.mkdir(dir
.absolutePath());
74 d
->initialized
= true;
75 d
->databasePath
= dir
.absolutePath();
81 d
->databasePath
= dir
.absolutePath();
82 d
->initialized
= true;
85 foreach(QFileInfo table
, dir
.entryInfoList(QStringList() << "*.xdt"))
87 QString name
= table
.baseName();
89 dDebug() << "=> Loading table: " << name
;
91 Table
*t
= this->table(name
);
95 return d
->initialized
;
98 Table
*Manager::table(const QString
&tableId
)
100 Table
*table
= d
->tables
.value(tableId
);
104 table
= new Table(tableId
);
105 d
->tables
[tableId
] = table
;
111 bool Manager::addObject(const QString
&tableId
, Object
*object
)
113 Table
*table
= this->table(tableId
);
116 return table
->addObject(object
);
122 bool Manager::load(const QString
&tableId
, Object
*object
) const
124 Table
*table
= d
->tables
.value(tableId
);
127 return table
->load(object
);
133 Object
*Manager::object(const QString
&tableId
, const QString
&id
) const
135 Table
*table
= d
->tables
.value(tableId
);
138 return table
->object(id
);
144 bool Manager::update(const QString
&tableId
, Object
*object
)
146 Table
*table
= d
->tables
.value(tableId
);
149 return table
->update(object
);
155 bool Manager::remove(const QString
&tableId
, Object
*object
)
157 Table
*table
= d
->tables
.value(tableId
);
160 return table
->remove(object
);
166 QDir
Manager::databaseDir() const
168 return QDir(d
->databasePath
);
171 void Manager::setObjectFactory(ObjectFactory
*factory
)
173 if(d
->factory
== factory
)
177 d
->factory
= factory
;
180 ObjectFactory
*Manager::objectFactory() const
186 Manager
*Manager::self()
190 d
->self
= new Manager
;
191 deleter
.setObject(d
->self
);