]>
git.ipfire.org Git - people/stevee/pakfire.git/blob - python/pakfire/repository/database.py
2 ###############################################################################
4 # Pakfire - The IPFire package management system #
5 # Copyright (C) 2011 Pakfire development team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
29 import pakfire
.packages
as packages
31 from pakfire
.constants
import *
33 class Cursor(sqlite3
.Cursor
):
34 def execute(self
, *args
, **kwargs
):
35 # For debugging of SQL queries.
38 return sqlite3
.Cursor
.execute(self
, *args
, **kwargs
)
41 class Database(object):
42 def __init__(self
, pakfire
, filename
):
43 self
.pakfire
= pakfire
44 self
.filename
= filename
58 logging
.debug("Open database %s" % self
.filename
)
60 dirname
= os
.path
.dirname(self
.filename
)
61 if not os
.path
.exists(dirname
):
64 database_exists
= os
.path
.exists(self
.filename
)
66 # Make a connection to the database.
67 self
._db
= sqlite3
.connect(self
.filename
)
68 self
._db
.row_factory
= sqlite3
.Row
70 # Create the database if it was not there, yet.
71 if not database_exists
:
83 return self
._db
.cursor(Cursor
)
85 def executescript(self
, *args
, **kwargs
):
87 return self
._db
.executescript(*args
, **kwargs
)
90 class DatabaseLocal(Database
):
91 def __init__(self
, pakfire
, repo
):
94 # Generate filename for package database
95 filename
= os
.path
.join(pakfire
.path
, PACKAGES_DB
)
97 Database
.__init
__(self
, pakfire
, filename
)
103 c
.execute("SELECT COUNT(*) AS count FROM packages")
113 CREATE TABLE settings(
117 INSERT INTO settings(key, val) VALUES('version', '0');
127 CREATE TABLE packages(
128 id INTEGER PRIMARY KEY,
155 CREATE TABLE scriptlets(
156 id INTEGER PRIMARY KEY,
162 CREATE TABLE triggers(
163 id INTEGER PRIMARY KEY,
169 # XXX add some indexes here
173 def add_package(self
, pkg
, reason
=None):
174 logging
.debug("Adding package to database: %s" % pkg
.friendly_name
)
180 INSERT INTO packages(
205 ) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
212 " ".join(pkg
.groups
),
216 " ".join(pkg
.provides
),
217 " ".join(pkg
.requires
),
218 " ".join(pkg
.conflicts
),
219 " ".join(pkg
.obsoletes
),
236 c
.executemany("INSERT INTO files(name, pkg) VALUES(?, ?)",
237 ((file, pkg_id
) for file in pkg
.filelist
))
247 def rem_package(self
, pkg
):
248 logging
.debug("Removing package from database: %s" % pkg
.friendly_name
)
252 # Get the ID of the package in the database.
254 c
.execute("SELECT id FROM packages WHERE uuid = ? LIMIT 1", (pkg
.uuid
,))
262 # First, delete all files from the database and then delete the pkg itself.
263 c
.execute("DELETE FROM files WHERE pkg = ?", (id,))
264 c
.execute("DELETE FROM packages WHERE id = ?", (id,))
273 c
.execute("SELECT * FROM packages ORDER BY name")
276 yield packages
.DatabasePackage(self
.pakfire
, self
.repo
, self
, row
)
280 def get_package_from_solv(self
, solv_pkg
):
282 c
.execute("SELECT * FROM packages WHERE uuid = ? LIMIT 1", (solv_pkg
.uuid
,))
286 return packages
.DatabasePackage(self
.pakfire
, self
.repo
, self
, row
)