Store package groups in the database

Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2014-04-26 14:07:20 +02:00
parent 3720bdf6b2
commit cc3244ea8a
4 changed files with 90 additions and 1 deletions

View file

@ -199,6 +199,24 @@ CREATE TABLE PackageRelations (
) ENGINE = InnoDB; ) ENGINE = InnoDB;
---- ----
17. Create tables to store package groups:
----
CREATE TABLE Groups (
ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
Name VARCHAR(64) NOT NULL,
PRIMARY KEY (ID),
UNIQUE (Name)
) ENGINE = InnoDB;
CREATE TABLE PackageGroups (
PackageID INTEGER UNSIGNED NOT NULL,
GroupID INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (PackageID, GroupID),
FOREIGN KEY (PackageID) REFERENCES Packages(ID) ON DELETE CASCADE,
FOREIGN KEY (GroupID) REFERENCES Groups(ID) ON DELETE CASCADE
) ENGINE = InnoDB;
----
From 2.2.0 to 2.3.0 From 2.2.0 to 2.3.0
------------------- -------------------

View file

@ -133,6 +133,27 @@ CREATE TABLE Packages (
) ENGINE = InnoDB; ) ENGINE = InnoDB;
-- Information about groups
--
CREATE TABLE Groups (
ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
Name VARCHAR(64) NOT NULL,
PRIMARY KEY (ID),
UNIQUE (Name)
) ENGINE = InnoDB;
-- Information about package-group-relations
--
CREATE TABLE PackageGroups (
PackageID INTEGER UNSIGNED NOT NULL,
GroupID INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (PackageID, GroupID),
FOREIGN KEY (PackageID) REFERENCES Packages(ID) ON DELETE CASCADE,
FOREIGN KEY (GroupID) REFERENCES Groups(ID) ON DELETE CASCADE
) ENGINE = InnoDB;
-- Define the package dependency types -- Define the package dependency types
-- --
CREATE TABLE DependencyTypes ( CREATE TABLE DependencyTypes (

View file

@ -151,6 +151,7 @@ if ($uid):
} }
} }
$section_info = array( $section_info = array(
'groups' => array(),
'depends' => array(), 'depends' => array(),
'makedepends' => array(), 'makedepends' => array(),
'checkdepends' => array(), 'checkdepends' => array(),
@ -169,6 +170,7 @@ if ($uid):
case 'license': case 'license':
$section_info[$key] = $value; $section_info[$key] = $value;
break; break;
case 'groups':
case 'source': case 'source':
case 'depends': case 'depends':
case 'makedepends': case 'makedepends':
@ -196,7 +198,7 @@ if ($uid):
if (!isset($pkgbase_info['pkgbase'])) { if (!isset($pkgbase_info['pkgbase'])) {
$pkgbase_info['pkgbase'] = $pkgbase_info['pkgname']; $pkgbase_info['pkgbase'] = $pkgbase_info['pkgname'];
} }
foreach (array('source', 'depends', 'makedepends', 'checkdepends', 'optdepends', 'conflicts', 'provides', 'replaces') as $array_opt) { foreach (array('groups', 'source', 'depends', 'makedepends', 'checkdepends', 'optdepends', 'conflicts', 'provides', 'replaces') as $array_opt) {
if (empty($pkgbase_info[$array_opt])) { if (empty($pkgbase_info[$array_opt])) {
$pkgbase_info[$array_opt] = array(); $pkgbase_info[$array_opt] = array();
} else { } else {
@ -357,6 +359,11 @@ if ($uid):
foreach ($pkginfo as $pi) { foreach ($pkginfo as $pi) {
$pkgid = pkg_create($base_id, $pi['pkgname'], $pi['license'], $pi['full-version'], $pi['pkgdesc'], $pi['url']); $pkgid = pkg_create($base_id, $pi['pkgname'], $pi['license'], $pi['full-version'], $pi['pkgdesc'], $pi['url']);
foreach ($pi['groups'] as $grp) {
$grpid = pkg_create_group($grp);
pkg_add_grp($pkgid, $grpid);
}
foreach (array('depends', 'makedepends', 'checkdepends', 'optdepends') as $deptype) { foreach (array('depends', 'makedepends', 'checkdepends', 'optdepends') as $deptype) {
foreach ($pi[$deptype] as $dep) { foreach ($pi[$deptype] as $dep) {
$deppkgname = preg_replace("/(<|=|>).*/", "", $dep); $deppkgname = preg_replace("/(<|=|>).*/", "", $dep);

View file

@ -805,3 +805,46 @@ function pkg_add_src($pkgid, $pkgsrc) {
$dbh->exec($q); $dbh->exec($q);
} }
/**
* Creates a new group and returns its ID
*
* If the groups already exists, the ID of the already existing group is
* returned.
*
* @param string $name The name of the group to create
*
* @return int The ID of the group
*/
function pkg_create_group($name) {
$dbh = DB::connect();
$q = sprintf("SELECT ID FROM Groups WHERE Name = %s", $dbh->quote($name));
$result = $dbh->query($q);
if ($result) {
$grpid = $result->fetch(PDO::FETCH_COLUMN, 0);
if ($grpid > 0) {
return $grpid;
}
}
$q = sprintf("INSERT INTO Groups (Name) VALUES (%s)", $dbh->quote($name));
$dbh->exec($q);
return $dbh->lastInsertId();
}
/**
* Add a package to a group
*
* @param int $pkgid The package ID of the package to add
* @param int $grpid The group ID of the group to add the package to
*
* @return void
*/
function pkg_add_grp($pkgid, $grpid) {
$dbh = DB::connect();
$q = sprintf("INSERT INTO PackageGroups (PackageID, GroupID) VALUES (%d, %d)",
$pkgid,
$grpid
);
$dbh->exec($q);
}