Commit abc614de authored by Lukas Appelhans's avatar Lukas Appelhans

Revert "whitespace; create-db syntax change for now; todo--;"

This reverts commit 8e6b481f.
parent fb951f2d
This diff is collapsed.
......@@ -11,15 +11,12 @@
#ifndef AKABEIBACKEND_H
#define AKABEIBACKEND_H
#include <QObject>
#include <QtCore/QObject>
#include "akabeicore_global.h"
class QUuid;
namespace Akabei {
class Delta;
class OperationRunner;
class Group;
......@@ -27,7 +24,6 @@ class Package;
class Database;
class BackendPrivate;
class AKABEICORESHARED_EXPORT Backend : public QObject
{
Q_OBJECT
......@@ -42,13 +38,12 @@ class AKABEICORESHARED_EXPORT Backend : public QObject
StatusOnTransaction = 4,
StatusBroken = 5
};
static Backend* instance();
static Backend *instance();
virtual ~Backend();
Status status() const;
Database* localDatabase();
Database *localDatabase();
QList<Database*> databases();
QUuid packages();
......@@ -60,11 +55,11 @@ class AKABEICORESHARED_EXPORT Backend : public QObject
QUuid queryPackages(const QString &sql);
QUuid queryGroups(const QString &sql);
OperationRunner* operationRunner();
OperationRunner *operationRunner();
Package* loadPackageFromFile(const QString &path);
Delta* loadDeltaFromFile(const QString &path);
Group* loadGroupFromFile(const QString &path);
Package *loadPackageFromFile(const QString &path);
Delta *loadDeltaFromFile(const QString &path);
Group *loadGroupFromFile(const QString &path);
public Q_SLOTS:
void initialize();
......@@ -77,9 +72,9 @@ class AKABEICORESHARED_EXPORT Backend : public QObject
private:
Backend(QObject* parent = 0);
BackendPrivate* const d_ptr;
BackendPrivate * const d_ptr;
Q_PRIVATE_SLOT(d_func(), void _initializationFinished())
Q_PRIVATE_SLOT(d_func(), void __k__initializationFinished())
friend class DatabasePrivate;
friend class Config;
......
......@@ -11,15 +11,15 @@
#ifndef AKABEIBACKEND_P_H
#define AKABEIBACKEND_P_H
#include <QFileSystemWatcher>
#include <QFutureWatcher>
#include <QUuid>
#include "akabeibackend.h"
#include "akabeioperationrunner.h"
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QFutureWatcher>
#include <QtCore/QUuid>
#include "akabeioperationrunner.h"
namespace Akabei {
namespace Akabei
{
class GroupPool
{
......@@ -41,7 +41,7 @@ class BackendPrivate : public QObject
{
Q_OBJECT
Q_DECLARE_PUBLIC(Backend)
Backend* const q_ptr;
Backend * const q_ptr;
public:
BackendPrivate(Backend *p)
......@@ -57,7 +57,7 @@ class BackendPrivate : public QObject
Backend::Status status;
Database* localDatabase;
Database *localDatabase;
QList<Database*> databases;
QFileSystemWatcher *fsWatcher;
......@@ -69,7 +69,7 @@ class BackendPrivate : public QObject
OperationRunner *runner;
void _initializationFinished();
void __k__initializationFinished();
QHash<QString, Database*> performInitialization();
void setStatus(Backend::Status status);
......
......@@ -8,16 +8,16 @@
version 2 of the License, or (at your option) any later version.
*/
#include <qglobal.h>
#include <QDir>
#include "akabeiconfig.h"
#include "akabeibackend.h"
#include "akabeibackend_p.h"
#include "akabeiconfig.h"
#include <qglobal.h>
#include <QtCore/QDir>
namespace Akabei {
namespace Akabei
{
class Config::Private
{
......@@ -41,8 +41,9 @@ class ConfigHelper
{
public:
ConfigHelper() : q(0) {}
~ConfigHelper() { delete q; }
~ConfigHelper() {
delete q;
}
Config *q;
};
......@@ -50,7 +51,9 @@ Q_GLOBAL_STATIC(ConfigHelper, s_globalConfig)
Config *Config::instance()
{
if (!s_globalConfig()->q) { new Config; }
if (!s_globalConfig()->q) {
new Config;
}
return s_globalConfig()->q;
}
......@@ -61,8 +64,8 @@ Config::Config()
Q_ASSERT(!s_globalConfig()->q);
s_globalConfig()->q = this;
setRoot("/");
setCachePath("/var/cache/akabei");
setDatabasePath("/var/akabei");
setCachePath(Akabei::DEFAULT_CACHE_DIR.toString());
setDatabasePath(Akabei::DEFAULT_DATABASE_DIR.toString());
}
Config::~Config()
......
......@@ -13,17 +13,15 @@
#include <akabeicore_global.h>
class QDir;
class QString;
class QStringList;
class QDir;
namespace Akabei {
class AKABEICORESHARED_EXPORT Config
{
public:
static Config* instance();
static Config *instance();
~Config();
QString root() const;
......
......@@ -11,7 +11,7 @@
#ifndef AKABEICORE_GLOBAL_H
#define AKABEICORE_GLOBAL_H
#include <qglobal.h>
#include <QtCore/qglobal.h>
#include <QUrl>
#if defined(AKABEICORE_LIBRARY)
......@@ -20,47 +20,51 @@
# define AKABEICORESHARED_EXPORT Q_DECL_IMPORT
#endif
// Define column numbers
namespace Akabei {
namespace Tables {
namespace Tables {
namespace Packages {
const int ID = 0;
const int NAME = 1;
const int VERSION = 2;
const int FILENAME = 3;
const int DESCRIPTION = 4;
const int GROUPS = 5;
const int SIZE = 6;
const int INSTALLEDSIZE = 7;
const int MD5SUM = 8;
const int URL = 9;
const int BUILDDATE = 10;
const int ARCH = 11;
const int PACKAGER = 12;
const int PROVIDES = 13;
const int CONFLICTS = 14;
const int DEPENDS = 15;
const int OPTDEPENDS = 16;
const int REPLACES = 17;
const int BACKUP = 18;
const int LICENSE = 19;
const int PROVIDESMIMETYPE = 20;
const int FLAGS = 21;
const int SCREENSHOTURL = 22;
const int INSTALLREASON = 23;
const int INSTALLDATE = 24;
}
namespace Packages {
const int ID = 0;
const int NAME = 1;
const int VERSION = 2;
const int FILENAME = 3;
const int DESCRIPTION = 4;
const int GROUPS = 5;
const int SIZE = 6;
const int INSTALLEDSIZE = 7;
const int MD5SUM = 8;
const int URL = 9;
const int BUILDDATE = 10;
const int ARCH = 11;
const int PACKAGER = 12;
const int PROVIDES = 13;
const int CONFLICTS = 14;
const int DEPENDS = 15;
const int OPTDEPENDS = 16;
const int REPLACES = 17;
const int BACKUP = 18;
const int LICENSE = 19;
const int PROVIDESMIMETYPE = 20;
const int FLAGS = 21;
const int SCREENSHOTURL = 22;
const int INSTALLREASON = 23;
const int INSTALLDATE = 24;
}
namespace Groups {
const int ID = 0;
const int NAME = 1;
const int DESCRIPTION = 2;
const int ICONNAME = 3;
}
namespace Groups {
const int ID = 0;
const int NAME = 1;
const int DESCRIPTION = 2;
const int ICONNAME = 3;
}
}
}
const QUrl DEFAULT_DATABASE_DIR = QUrl("/var/local/akabei");
const QUrl DEFAULT_CACHE_DIR = QUrl("/var/cache/akabei");
}
......
......@@ -8,15 +8,14 @@
version 2 of the License, or (at your option) any later version.
*/
#include <sqlite3.h>
#include "akabeidatabase_p.h"
#include "akabeipackage_p.h"
#include "akabeigroup_p.h"
#include "akabeibackend_p.h"
#include "akabeihelpers_p.h"
#include "akabeidatabase_p.h"
#include <sqlite3.h>
namespace Akabei {
......@@ -37,16 +36,18 @@ void DatabasePrivate::init()
{
if (dbHandle)
sqlite3_close(dbHandle);
// Try opening the db read only
int result = sqlite3_open16(pathToDb.constData(), &dbHandle);
switch (result) {
case SQLITE_OK:
break;
default:
dbHandle = 0;
break;
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
// Errors. Set the handle to 0
dbHandle = 0;
break;
}
// Populate group cache on startup
populateGroupCache();
}
......@@ -62,38 +63,42 @@ void DatabasePrivate::populateGroupCache()
QString sql = "SELECT * FROM groups";
sqlite3_stmt *statement = 0;
int result = sqlite3_prepare16_v2(dbHandle, sql.constData(),
(sql.size() + 1) * sizeof(QChar),
int result = sqlite3_prepare16_v2(dbHandle, sql.constData(), (sql.size() + 1) * sizeof(QChar),
&statement, 0);
switch (result) {
case SQLITE_OK:
break;
default:
return;
break;
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
// Errors. Take action here
return;
break;
}
while (sqlite3_step(statement) == SQLITE_ROW) {
Group *group = groupFromRow(statement);
Group * group = groupFromRow(statement);
groupCache[group->name()] = group;
}
result = sqlite3_finalize(statement);
sqlite3_finalize(statement);
}
Group* DatabasePrivate::groupFromRow(sqlite3_stmt* stmt)
Group *DatabasePrivate::groupFromRow(sqlite3_stmt *stmt)
{
using namespace Tables::Groups;
QString name = QSTRING_FROM_DB(stmt, NAME);
// Is our group still not into the pool?
if (!Backend::instance()->d_func()->groupPool->contains(name)) {
Group* group = Backend::instance()->d_func()->groupPool->group(name);
// In this case, populate its fields
Group *group = Backend::instance()->d_func()->groupPool->group(name);
group->d_ptr->desc = QSTRING_FROM_DB(stmt, DESCRIPTION);
group->d_ptr->icon = QSTRING_FROM_DB(stmt, ICONNAME);
}
// Return the group from the pool
return Backend::instance()->d_func()->groupPool->group(name);
}
......@@ -103,12 +108,16 @@ Package *DatabasePrivate::packageFromRow(sqlite3_stmt *stmt)
int id = sqlite3_column_int(stmt, Tables::Packages::ID);
// Does the cache contain our package?
if (packageCache.contains(id)) {
// Simply return it
return packageCache[id];
}
using namespace Tables::Packages;
// Otherwise go ahead, create the package, add it to the cache, and return it.
using namespace Tables::Packages;
// We suppose the statement is actually valid. So let's create our package.
Package *package = new Package(q, id, QSTRING_FROM_DB(stmt, NAME));
package->d_func()->arch = QSTRING_FROM_DB(stmt, ARCH);
......@@ -119,7 +128,7 @@ Package *DatabasePrivate::packageFromRow(sqlite3_stmt *stmt)
package->d_func()->md5sum = QSTRING_FROM_DB(stmt, MD5SUM).toUtf8();
package->d_func()->mimetypes = Helpers::stringlistFromDb(stmt, PROVIDESMIMETYPE);
package->d_func()->packager = QSTRING_FROM_DB(stmt, PACKAGER);
package->d_func()->provides = Helpers::stringlistFromDb(stmt, PROVIDES);
package->d_func()->providers = Helpers::stringlistFromDb(stmt, PROVIDES);
package->d_func()->size = sqlite3_column_int64(stmt, SIZE);
package->d_func()->isize = sqlite3_column_int64(stmt, INSTALLEDSIZE);
package->d_func()->url = QUrl(QSTRING_FROM_DB(stmt, URL));
......@@ -132,18 +141,23 @@ Package *DatabasePrivate::packageFromRow(sqlite3_stmt *stmt)
package->d_func()->license = QSTRING_FROM_DB(stmt, LICENSE);
package->d_func()->replaces = Helpers::stringlistFromDb(stmt, REPLACES);
// Install Reason (if any)
switch (sqlite3_column_int(stmt, INSTALLREASON)) {
case 1:
// Explicit
package->d_func()->reason = Package::ExplicitlyInstalledReason;
break;
case 2:
// Dependency
package->d_func()->reason = Package::InstalledAsDependencyReason;
break;
default:
// No reason
package->d_func()->reason = Package::NoReason;
break;
}
// Groups
foreach (const QString &group, QSTRING_FROM_DB(stmt, GROUPS).split(';')) {
if (group.isEmpty())
continue;
......@@ -152,44 +166,52 @@ Package *DatabasePrivate::packageFromRow(sqlite3_stmt *stmt)
}
}
// Check if it has a scriptlet
package->d_func()->hasScriptlet = false;
QString sql = "SELECT * FROM scriptlets WHERE Package=" + QString::number(id);
sqlite3_stmt *statement = 0;
int result = sqlite3_prepare16_v2(dbHandle, sql.constData(),
(sql.size() + 1) * sizeof(QChar),
int result = sqlite3_prepare16_v2(dbHandle, sql.constData(), (sql.size() + 1) * sizeof(QChar),
&statement, 0);
switch (result) {
case SQLITE_OK:
break;
default:
break;
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
break;
}
if (sqlite3_step(statement) == SQLITE_ROW) {
// There is one
package->d_func()->hasScriptlet = true;
}
sqlite3_finalize(statement);
// Check if it has any hooks
package->d_func()->hasHooks = false;
sql = "SELECT * FROM hooks WHERE Packages LIKE \"%;" + QString::number(id) + ";%\"";
statement = 0;
result = sqlite3_prepare16_v2(dbHandle, sql.constData(),
(sql.size() + 1) * sizeof(QChar),
result = sqlite3_prepare16_v2(dbHandle, sql.constData(), (sql.size() + 1) * sizeof(QChar),
&statement, 0);
switch (result) {
case SQLITE_OK:
break;
default:
break;
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
break;
}
if (sqlite3_step(statement) == SQLITE_ROW) {
// There is one
package->d_func()->hasHooks = true;
}
sqlite3_finalize(statement);
// Attempt loading the archive from cache, if any
// package->d_func()->attemptLoadFromCache();
// Add the package to the cache
packageCache[id] = package;
return package;
......@@ -237,20 +259,22 @@ QList<Package*> Database::queryPackages(const QString &sql)
{
Q_D(Database);
// Shield us from multithreaded disaster
QMutexLocker locker(d->mutex);
sqlite3_stmt *statement = 0;
int result = sqlite3_prepare16_v2(d->dbHandle, sql.constData(),
(sql.size() + 1) * sizeof(QChar),
int result = sqlite3_prepare16_v2(d->dbHandle, sql.constData(), (sql.size() + 1) * sizeof(QChar),
&statement, 0);
switch (result) {
case SQLITE_OK:
break;
default:
qDebug() << "Error while querying Database for packages" << result << sqlite3_errmsg(d->dbHandle);
return QList<Package*>();
break;
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
qDebug() << "Error while querying Database for packages" << result << sqlite3_errmsg(d->dbHandle);
// Errors. Take action here
return QList<Package*>();
break;
}
QList<Package*> retlist;
......@@ -280,19 +304,21 @@ QList< Group* > Database::queryGroups(const QString& sql)
{
Q_D(Database);
// Shield us from multithreaded disaster
QMutexLocker locker(d->mutex);
sqlite3_stmt *statement = 0;
int result = sqlite3_prepare16_v2(d->dbHandle, sql.constData(),
(sql.size() + 1) * sizeof(QChar),
int result = sqlite3_prepare16_v2(d->dbHandle, sql.constData(), (sql.size() + 1) * sizeof(QChar),
&statement, 0);
switch (result) {
case SQLITE_OK:
break;
default:
qDebug() << "Error while querying database";
return QList<Group*>();
break;
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
qDebug() << "Error while querying database";
// Errors. Take action here
return QList<Group*>();
break;
}
QList<Group*> retlist;
......
......@@ -11,46 +11,45 @@
#ifndef AKABEIDATABASE_H
#define AKABEIDATABASE_H
#include <QObject>
#include "akabeicore_global.h"
#include <QtCore/QObject>
namespace Akabei {
class Group;
class Package;
class DatabasePrivate;
class AKABEICORESHARED_EXPORT Database
{
Q_DISABLE_COPY(Database)
Q_DECLARE_PRIVATE(Database)
public:
virtual ~Database();
void reinit();
bool isValid() const;
QString name() const;
public:
virtual ~Database();
QList<Package*> packages();
QList<Package*> searchPackages(const QString &token);
void reinit();
bool isValid() const;
QString name() const;
QList<Package*> packages();
QList<Package*> searchPackages(const QString &token);
QList<Group*> groups();
QList<Group*> searchGroups(const QString &token);
QList<Group*> groups();
QList<Group*> searchGroups(const QString &token);
QList<Package*> queryPackages(const QString &sql);
QList<Group*> queryGroups(const QString &sql);
QList<Package*> queryPackages(const QString &sql);
QList<Group*> queryGroups(const QString &sql);
private:
Database(const QString &pathToDatabase);
private:
Database(const QString &pathToDatabase);
DatabasePrivate* const d_ptr;
DatabasePrivate * const d_ptr;
friend class BackendPrivate;
friend class QueryHelper;
friend class Package;
friend class BackendPrivate;
friend class QueryHelper;
friend class Package;
};
}
......
......@@ -11,15 +11,13 @@
#ifndef AKABEIDATABASE_P_H
#define AKABEIDATABASE_P_H
#include <sqlite3.h>
#include <QHash>
#include "akabeidatabase.h"
#include <QtCore/QHash>
class QMutex;
#include <sqlite3.h>
class QMutex;
namespace Akabei {
class Group;
......@@ -28,23 +26,23 @@ class Package;
class DatabasePrivate
{
Q_DECLARE_PUBLIC(Database)
Database* const q_ptr;
public:
explicit DatabasePrivate(Database *db, const QString &pathToDatabase);
virtual ~DatabasePrivate();
Database * const q_ptr;
public:
explicit DatabasePrivate(Database *db, const QString &pathToDatabase);
virtual ~DatabasePrivate();
void init();
void populateGroupCache();
void init();
void populateGroupCache();
Package* packageFromRow(sqlite3_stmt *stmt);
Group* groupFromRow(sqlite3_stmt *stmt);
Package *packageFromRow(sqlite3_stmt *stmt);
Group *groupFromRow(sqlite3_stmt *stmt);
QString pathToDb;
sqlite3 *dbHandle;
QHash<int, Package*> packageCache;
QHash<QString, Group*> groupCache;
QString pathToDb;
sqlite3 *dbHandle;
QHash<int, Package*> packageCache;
QHash<QString, Group*> groupCache;
QMutex *mutex;
QMutex *mutex;
};
}
......
......@@ -12,9 +12,8 @@
#include "akabeidelta_p.h"
#include "akabeihelpers_p.h"
<