Commit af074407 authored by Lisa's avatar Lisa
Browse files

New association table between hooks and packages

parent 133778f8
......@@ -40,8 +40,7 @@ CREATE TABLE IF NOT EXISTS `scriptlets` (
CREATE TABLE IF NOT EXISTS `hooks` (
`name` TEXT PRIMARY KEY ,
`hook` TEXT NOT NULL ,
`packages` TEXT NOT NULL);
`hook` TEXT NOT NULL );
CREATE TABLE IF NOT EXISTS `groups` (
`id` INTEGER PRIMARY KEY ASC ,
......@@ -103,9 +102,15 @@ CREATE TABLE IF NOT EXISTS `replaces` (
REFERENCES `packages` (`id` ) );
CREATE TABLE IF NOT EXISTS `belongs` (
`id` INTEGER PRIMARY KEY ASC ,
`package` INTEGER NOT NULL ,
`group` TEXT NOT NULL ,
CONSTRAINT `fk_belongs_packages`
FOREIGN KEY (`package` )
REFERENCES `packages` (`id` ) );
CREATE TABLE IF NOT EXISTS `belongshook` (
`package` INTEGER NOT NULL ,
`hookname` TEXT NOT NULL ,
CONSTRAINT `fk_hooks_packages`
FOREIGN KEY (`package` )
REFERENCES `packages` (`id` ) );
\ No newline at end of file
......@@ -245,9 +245,21 @@ Delta *DatabasePrivate::deltaFromRow(SQLiteResource& table, int row)
return delta;
}
QList<Akabei::Package*> DatabasePrivate::packagesFromHook(QString& hook)
{
QList<Akabei::Package*> pkgs;
SQLiteResource table = dbConnection.query("SELECT * FROM belongshook JOIN packages ON hookname='" + hook + "' AND belongshook.package=packages.id");
for (int row = 0; row < table.getRowsCount(); row++) {
pkgs << packageFromRow(table, row);
}
return pkgs;
}
Hook* DatabasePrivate::hookFromRow(SQLiteResource& table, int row)
{
const QString name = table.getDataAt(row, "name").toString();
QString name = table.getDataAt(row, "name").toString();
//Check if the deltaCache contains the dle
if (hookCache.contains(name))
......@@ -255,16 +267,11 @@ Hook* DatabasePrivate::hookFromRow(SQLiteResource& table, int row)
using namespace Tables::Hooks;
QStringList pkgs = Helpers::stringlistFromDb(table.getDataAt(row, "packages").toString());
QList<Akabei::Package*> aPkgs;
foreach (const QString &p, pkgs)
aPkgs << packageCache[p.toInt()];
const QString &h = table.getDataAt(row, "hook").toString();
QList<Akabei::Package*> packages = packagesFromHook(name);
// Otherwise go ahead, create the delta, add it to the cache, and return it.
Hook *hook = new Hook(name, h, aPkgs);
Hook *hook = new Hook(name, h, QList<Akabei::Package*>());
hookCache[name] = hook;
return hook;
......
......@@ -38,6 +38,8 @@ public:
Group *groupFromRow(SQLiteResource&, int);
Delta *deltaFromRow(SQLiteResource&, int);
Hook* hookFromRow(SQLiteResource&, int);
QList<Akabei::Package *> packagesFromHook(QString &);
QString pathToDb;
SQLiteConnection dbConnection;
......
......@@ -1075,16 +1075,17 @@ int QueryPerformer::insertHook(Database *db, Akabei::Hook const* hook)
int QueryPerformer::insertHook(SQLiteConnection &dbConnection, Akabei::Hook const* hook)
{
QStringList pkgs;
foreach (Package* p, hook->packages()) {
pkgs << QString::number(p->databaseId());
}
try {
dbConnection.bind(":Name", hook->name());
dbConnection.bind(":Hook", hook->content());
dbConnection.bind(":Packages", stringFromStringList(pkgs));
dbConnection.query("INSERT INTO hooks (`name`, `hook`, `packages`) VALUES (:Name, :Hook, :Packages)");
dbConnection.query("INSERT INTO hooks (`name`, `hook`) VALUES (:Name, :Hook)");
foreach (Package *p, hook->packages()) {
dbConnection.bind(":Name", hook->name());
dbConnection.bind(":Package", p->databaseId());
dbConnection.query("INSERT INTO belongshook ('package', 'hookname') VALUES (:Package, :Name)");
}
} catch (SQLiteException& ex) {
return SQLITE_ABORT;
}
......
......@@ -412,7 +412,8 @@ QList< Hook* > Package::retrieveHooks()
return QList<Hook*>();
if (d->hookCache.isEmpty()) {
d->hookCache = d->database->queryHooks("SELECT * FROM hooks WHERE Packages LIKE \"%;" + QString::number(d->databaseId) + ";%\"");
QString sql = "SELECT * FROM belongshook JOIN hook ON package=" + QString::number(d->databaseId) + " AND belongshook.hookname=hooks.name;";
d->hookCache = d->database->queryHooks(sql);
foreach (Hook * hook, d->hookCache)
d->hooks << hook->name();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment