Commit 2d3e90e4 authored by Lisa's avatar Lisa Committed by Daniele Cocca
Browse files

TEMPORARY: translating everything to the new schema and sqlite3 interface. Not completed

parent d2294bd9
......@@ -403,13 +403,13 @@ QUuid Backend::queryPackages(const QString& sql)
QUuid Backend::searchGroups(const QString& token)
{
QString sql = "SELECT * FROM groups WHERE Name LIKE \"%" + token + "%\" OR Description LIKE \"%" + token + "%\"";
QString sql = "SELECT * FROM groups WHERE name LIKE \"%" + token + "%\" OR description LIKE \"%" + token + "%\"";
return queryGroups(sql);
}
QUuid Backend::searchPackages(const QString& token)
{
QString sql = "SELECT * FROM packages WHERE Name LIKE \"%" + token + "%\" OR Description LIKE \"%" + token + "%\"";
QString sql = "SELECT * FROM packages WHERE name LIKE \"%" + token + "%\" OR description LIKE \"%" + token + "%\"";
return queryPackages(sql);
}
......@@ -723,11 +723,11 @@ Group* Backend::loadGroupFromFile(const QString &path)
QStringList split = line.split('=');
if (split.isEmpty())
continue;
if (split.first() == "Name") {
if (split.first() == "name") {
group = d->groupPool->group(split.last());
} else if (split.first() == "Description" && group) {
} else if (split.first() == "description" && group) {
group->d_ptr->desc = split.last();
} else if (split.first() == "Iconname" && group) {
} else if (split.first() == "iconname" && group) {
group->d_ptr->icon = split.last();
}
}
......
......@@ -24,6 +24,10 @@
namespace Akabei {
/*
* I don't think this namespace is necessary anymore
* (now you can provide the column name to retrieve data)
*/
namespace Tables {
namespace Packages {
......
......@@ -16,41 +16,25 @@
#include "akabeihelpers_p.h"
#include "akabeidelta_p.h"
#include <sqlite3.h>
#include "akabeihook.h"
namespace Akabei {
DatabasePrivate::DatabasePrivate(Database *db, const QString &pathToDatabase)
: q_ptr(db)
, pathToDb(pathToDatabase)
, dbHandle(0)
, mutex(new QMutex())
{
}
DatabasePrivate::~DatabasePrivate()
{
sqlite3_close(dbHandle);
}
DatabasePrivate::~DatabasePrivate() {}
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:
// Everything went fine and the handle is ready
break;
default:
// Errors. Set the handle to 0
dbHandle = 0;
break;
try {
dbConnection(pathToDb, false);
valid = true;
populateGroupCache(); /* populate group cache on startup */
}
// Populate group cache on startup
populateGroupCache();
catch (SQLiteException& e) {}
}
QString Database::name() const
......@@ -59,65 +43,43 @@ QString Database::name() const
return d->pathToDb;
}
sqlite3_stmt* DatabasePrivate::query(QString const& query_)
{
unsigned int size((query_.size() + 1) * sizeof(QChar));
sqlite3_stmt* statement(0);
int rc = sqlite3_prepare16_v2(dbHandle, query_.constData(), size,
&statement, 0);
if (rc != SQLITE_OK) {
// TODO: throw an exception maybe!
return 0;
}
return statement;
}
void DatabasePrivate::populateGroupCache()
{
groupCache.clear();
dbConnection.query("SELECT * FROM GROUPS");
sqlite3_stmt* statement(query("SELECT * FROM groups"));
if (!statement) {
// Errors. Take action here
return;
}
while (sqlite3_step(statement) == SQLITE_ROW) {
Group * group = groupFromRow(statement);
for (int i = 0; i < dbConnection.getRowsNumber(); i++)
{
Group *group = groupFromRow(i);
groupCache[group->name()] = group;
}
sqlite3_finalize(statement);
}
Group *DatabasePrivate::groupFromRow(sqlite3_stmt *stmt)
Group *DatabasePrivate::groupFromRow(int row)
{
using namespace Tables::Groups;
QString name = QSTRING_FROM_DB(stmt, NAME);
QString name = dbConnection.getDataAt(row, "name").toString();
// Is our group still not into the pool?
if (!Backend::instance()->d_func()->groupPool->contains(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);
group->d_ptr->desc = dbConnection.getDataAt(row, "description");
group->d_ptr->icon = dbConnection.getDataAt(row, "icon");
}
// Return the group from the pool
return Backend::instance()->d_func()->groupPool->group(name);
}
Package *DatabasePrivate::packageFromRow(sqlite3_stmt *stmt)
Package *DatabasePrivate::packageFromRow(int row)
{
Q_Q(Database);
int id = sqlite3_column_int(stmt, Tables::Packages::ID);
int id = dbConnection.getDataAt(row, "id").toInt();
int installreason = dbConnection.getDataAt(row, "installreason").toInt();
// Does the cache contain our package?
if (packageCache.contains(id)) {
......@@ -129,66 +91,86 @@ Package *DatabasePrivate::packageFromRow(sqlite3_stmt *stmt)
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);
package->d_func()->buildDate = QDateTime::fromTime_t(sqlite3_column_int(stmt, BUILDDATE));
package->d_func()->installDate = QDateTime::fromTime_t(sqlite3_column_int(stmt, INSTALLDATE));
package->d_func()->desc = QSTRING_FROM_DB(stmt, DESCRIPTION);
package->d_func()->filename = QSTRING_FROM_DB(stmt, FILENAME);
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()->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));
package->d_func()->version = QSTRING_FROM_DB(stmt, VERSION).toUtf8();
package->d_func()->screenshot = QUrl(QSTRING_FROM_DB(stmt, SCREENSHOTURL));
package->d_func()->backup = Helpers::stringlistFromDb(stmt, BACKUP);
package->d_func()->license = QSTRING_FROM_DB(stmt, LICENSE);
Package *package = new Package(q, id, dbConnection.getDataAt(row, "name").toString());
package->d_func()->arch = dbConnection.getDataAt(row, "arch").toString();
package->d_func()->buildDate = QDateTime::fromTime_t(dbConnection.getDataAt(row, "builddate").toInt());
package->d_func()->installDate = QDateTime::fromTime_t(dbConnection.getDataAt(row, "installdate").toInt());
package->d_func()->desc = dbConnection.getDataAt(row, "description").toString();
package->d_func()->filename = dbConnection.getDataAt(row, "filename").toString();
package->d_func()->md5sum = dbConnection.getDataAt(row, "md5sum").toString().toUtf8();
package->d_func()->mimetypes = Helpers::stringlistFromDb(dbConnection.getDataAt(row, "providesmimetypes").toString());
package->d_func()->packager = dbConnection.getDataAt(row, "packager").toString();
package->d_func()->size = dbConnection.getDataAt(row, "size").toInt();
package->d_func()->isize = dbConnection.getDataAt(row, "installedsize").toInt();
package->d_func()->url = QUrl(dbConnection.getDataAt(row, "url").toString());
package->d_func()->version = dbConnection.getDataAt(row, "version").toString().toUtf8();
package->d_func()->screenshot = QUrl(dbConnection.getDataAt(row, "screenshoturl").toString());
package->d_func()->backup = Helpers::stringlistFromDb(dbConnection.getDataAt(row, "backup").toString());
package->d_func()->license = dbConnection.getDataAt(row, "license").toString();
// Fetch the provided package names
QStringList provides;
sqlite3_stmt* prov_stmt(query("SELECT provides FROM provides WHERE package=" + id));
while (sqlite3_step(prov_stmt) == SQLITE_ROW) {
provides << QSTRING_FROM_DB(prov_stmt, 0);
dbConnection.query("SELECT provides FROM provides WHERE package=" + id);
foreach (QVariant v, dbConnection.getColumn("provides"))
{
provides << v.toString();
}
package->d_func()->providers = provides;
// Fetch the conflicting package names
QStringList conflicts;
sqlite3_stmt* conf_stmt(query("SELECT conflicts FROM conflicts WHERE package=" + id));
while (sqlite3_step(conf_stmt) == SQLITE_ROW) {
conflicts << QSTRING_FROM_DB(conf_stmt, 0);
dbConnection.query("SELECT conflict FROM conflicts WHERE package=" + id);
foreach (QVariant v, dbConnection.getColumn("conflict"))
{
conflicts << v.toString();
}
package->d_func()->conflicts = conflicts;
// Fetch the package names to which we depend on
QStringList depends;
sqlite3_stmt* deps_stmt(query("SELECT dependency FROM depends WHERE package=" + id));
while (sqlite3_step(deps_stmt) == SQLITE_ROW) {
depends << QSTRING_FROM_DB(deps_stmt, 0);
dbConnection.query("SELECT dependency FROM depends WHERE package=" + id);
foreach (QVariant v, dbConnection.getColumn("dependency"))
{
depends << v.toString();
}
package->d_func()->deps = depends;
// Fetch the package names to which we optionally dependend on
QStringList optional;
sqlite3_stmt* optn_stmt(query("SELECT dependency FROM optional WHERE package=" + id));
while (sqlite3_step(optn_stmt) == SQLITE_ROW) {
optional << QSTRING_FROM_DB(optn_stmt, 0);
dbConnection.query("SELECT dependency FROM optional WHERE package=" + id);
foreach (QVariant v, dbConnection.getColumn("dependency"))
{
optional << v.toString();
}
package->d_func()->optdepends = optional;
// Fetch the packages names that we replace
QStringList replaces;
sqlite3_stmt* repl_stmt(query("SELECT replaces FROM replaces WHERE package=" + id));
while (sqlite3_step(repl_stmt) == SQLITE_ROW) {
replaces << QSTRING_FROM_DB(repl_stmt, 0);
dbConnection.query("SELECT replaces FROM replaces WHERE package=" + id);
foreach (QVariant v, dbConnection.getColumn("replaces"))
{
replaces << v.toString();
}
package->d_func()->replaces = replaces;
/* Groups */
dbConnection.query("SELECT name FROM groups WHERE package=" + id);
foreach (QVariant v, dbConnection.getColumn("name"))
{
QString group = v.toString();
if (group.isEmpty())
{
continue;
}
if (groupCache.contains(group))
{
package->d_func()->groups << groupCache[group];
}
}
// Install Reason (if any)
switch (sqlite3_column_int(stmt, INSTALLREASON)) {
switch (installreason) {
case 1:
// Explicit
package->d_func()->reason = Package::ExplicitlyInstalledReason;
......@@ -203,116 +185,86 @@ Package *DatabasePrivate::packageFromRow(sqlite3_stmt *stmt)
break;
}
// Groups
foreach (const QString &group, QSTRING_FROM_DB(stmt, GROUPS).split(';')) {
if (group.isEmpty())
continue;
if (groupCache.contains(group)) {
package->d_func()->groups << groupCache[group];
}
}
// Check if it has a scriptlet
package->d_func()->hasScriptlet = false;
QString sql = QString("SELECT * FROM scriptlets WHERE package=%1")
.arg(id);
sqlite3_stmt *statement = 0;
int result = sqlite3_prepare16_v2(dbHandle, sql.constData(), (sql.size() + 1) * sizeof(QChar),
&statement, 0);
switch (result) {
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
break;
}
dbConnection.query(sql);
if (sqlite3_step(statement) == SQLITE_ROW) {
// There is one
if (dbConnection.getRowsNumber() > 0)
{
package->d_func()->hasScriptlet = true;
}
sqlite3_finalize(statement);
// Check if it has any hooks
package->d_func()->hasHooks = false;
sql = QString("SELECT * FROM hooks WHERE packages LIKE \"%%;%1;%%\"")
.arg(id);
statement = 0;
result = sqlite3_prepare16_v2(dbHandle, sql.constData(), (sql.size() + 1) * sizeof(QChar),
&statement, 0);
switch (result) {
case SQLITE_OK:
// Everything went fine and the handle is ready
break;
default:
break;
}
dbConnection.query(sql);
if (sqlite3_step(statement) == SQLITE_ROW) {
if (dbConnection.getRowsNumber() > 0)
{
// 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;
}
Delta *DatabasePrivate::deltaFromRow(sqlite3_stmt *stmt)
Delta *DatabasePrivate::deltaFromRow(int row)
{
const QString filename = QSTRING_FROM_DB(stmt, Tables::Deltas::FILENAME);
Q_Q(Database);
const QString filename = dbConnection.getDataAt(row, "filename").toString();
//Check if the deltaCache contains the dle
if (deltaCache.contains(filename))
return deltaCache[filename];
using namespace Tables::Deltas;
int packageId = sqlite3_column_int(stmt, PACKAGE);
int packageId = dbConnection.getDataAt(row, "id").toInt();
QString targetName;
if (packageCache[packageId])
targetName = packageCache[packageId]->name();
// Otherwise go ahead, create the delta, add it to the cache, and return it.
Delta *delta = new Delta(targetName, QBYTEARRAY_FROM_DB(stmt, VERSIONFROM), QBYTEARRAY_FROM_DB(stmt, VERSIONTO));
delta->d_func()->filename = QSTRING_FROM_DB(stmt, FILENAME);
delta->d_func()->md5sum = QBYTEARRAY_FROM_DB(stmt, MD5SUM);
deltaCache[filename] = delta;
Delta *delta = new Delta(targetName, dbConnection.getDataAt(row, "versionfrom").toByteArray(), dbConnection.getDataAt(row, "versionto").toByteArray());
delta->d_func()->filename = dbConnection.getDataAt(row, "filename").toString();
delta->d_func()->md5sum = dbConnection.getDataAt(row, "md5sum").toByteArray();
deltaCache[filename] = delta;
return delta;
}
Hook *DatabasePrivate::hookFromRow(sqlite3_stmt *stmt)
{
const QString name = QSTRING_FROM_DB(stmt, Tables::Hooks::NAME);
//Check if the deltaCache contains the dle
if (hookCache.contains(name))
return hookCache[name];
using namespace Tables::Hooks;
QStringList pkgs = Helpers::stringlistFromDb(stmt, PACKAGES);
QList<Akabei::Package*> aPkgs;
foreach (const QString &p, pkgs)
aPkgs << packageCache[p.toInt()];
const QString &h = QSTRING_FROM_DB(stmt, HOOK);
// Otherwise go ahead, create the delta, add it to the cache, and return it.
Hook *hook = new Hook(name, h, aPkgs);
hookCache[name] = hook;
return hook;
......@@ -333,13 +285,19 @@ Database::~Database()
void Database::reinit()
{
Q_D(Database);
d->init();
try {
d->populateGroupCache();
} catch (SQLiteException& e) {}
}
/*
* TODO: this and d->valid shouldn't be necessary
*/
bool Database::isValid() const
{
Q_D(const Database);
return d->dbHandle != 0;
return d->valid;
}
QList<Package*> Database::packages()
......@@ -349,7 +307,7 @@ QList<Package*> Database::packages()
QList<Package*> Database::searchPackages(const QString &token)
{
QString sql = QString("SELECT * FROM packages WHERE Name LIKE \"%%%1%%\" OR Description LIKE \"%%%1%%\"")
QString sql = QString("SELECT * FROM packages WHERE name LIKE \"%%%1%%\" OR description LIKE \"%%%1%%\"")
.arg(token);
return queryPackages(sql);
......@@ -361,30 +319,21 @@ QList<Package*> Database::queryPackages(const QString &sql)
// 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),
&statement, 0);
switch (result) {
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;
try
{
d->dbConnection.query(sql);
for (int i = 0; i < d->dbConnection.getRowsNumber(); i++)
{
retlist << d->packageFromRow(i);
}
}
QList<Package*> retlist;
while (sqlite3_step(statement) == SQLITE_ROW) {
retlist << d->packageFromRow(statement);
catch (SQLiteException& e)
{
qDebug() << "Error while querying database for packages: " << e.what();
}
sqlite3_finalize(statement);
return retlist;
}
......@@ -407,28 +356,20 @@ QList< Group* > Database::queryGroups(const QString& sql)
// 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),
&statement, 0);
switch (result) {
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;
while (sqlite3_step(statement) == SQLITE_ROW) {
retlist << d->groupFromRow(statement);
try
{
d->dbConnection.query(sql);
for (int i = 0; i < d->dbConnection.getRowsNumber(); i++)
{
retlist << d->groupFromRow(i);
}
}
catch (SQLiteException& e)
{
qDebug() << "Error while querying database for groups: " << e.what();
}
sqlite3_finalize(statement);
return retlist;
}
......@@ -438,57 +379,19 @@ QList<Delta*> Database::queryDeltas(const QString &sql)
Q_D(Database);
QMutexLocker locker(d->mutex);
sqlite3_stmt *statement = 0;
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<Delta*>();
break;
}
QList<Delta*> retlist;
while (sqlite3_step(statement) == SQLITE_ROW) {
retlist << d->deltaFromRow(statement);
}
sqlite3_finalize(statement);
return retlist;
}
QList<Hook*> Database::queryHooks(const QString &sql)
{
Q_D(Database);
QMutexLocker locker(d->mutex);
sqlite3_stmt *statement = 0;
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<Hook*>();
break;
try
{
d->dbConnection.query(sql);
for (int i = 0; i < d->dbConnection.getRowsNumber(); i++)
{
retlist << d->deltaFromRow(i);
}
}
QList<Hook*> retlist;