diff options
-rw-r--r-- | portato/db/__init__.py | 25 | ||||
-rw-r--r-- | portato/db/dict.py | 3 | ||||
-rw-r--r-- | portato/db/sql.py | 21 |
3 files changed, 35 insertions, 14 deletions
diff --git a/portato/db/__init__.py b/portato/db/__init__.py index 122940a..40bf9ee 100644 --- a/portato/db/__init__.py +++ b/portato/db/__init__.py @@ -12,14 +12,23 @@ from __future__ import absolute_import +from ..session import Session, SectionDict from ..constants import USE_SQL from ..helper import debug -if USE_SQL: - debug("Using SQLDatabase") - from .sql import SQLDatabase - Database = SQLDatabase -else: - debug("Using DictDatabase") - from .dict import DictDatabase - Database = DictDatabase +_SESSION = None + +def Database(): + global _SESSION + + if _SESSION is None: + _SESSION = Session("db.cfg", name = "DB") + + if USE_SQL: + debug("Using SQLDatabase") + from .sql import SQLDatabase + return SQLDatabase(SectionDict(_SESSION, "SQL")) + else: + debug("Using DictDatabase") + from .dict import DictDatabase + return DictDatabase(SectionDict(_SESSION, "dict")) diff --git a/portato/db/dict.py b/portato/db/dict.py index 5c5ca49..d230821 100644 --- a/portato/db/dict.py +++ b/portato/db/dict.py @@ -24,9 +24,10 @@ class DictDatabase (Database): lock = Database.lock - def __init__ (self): + def __init__ (self, session): """Constructor.""" Database.__init__(self) + self.session = session self.__initialize() self.populate() diff --git a/portato/db/sql.py b/portato/db/sql.py index e7be91e..3cffd88 100644 --- a/portato/db/sql.py +++ b/portato/db/sql.py @@ -30,15 +30,22 @@ from .database import Database, PkgData class SQLDatabase (Database): + FORMAT = "1" FORBIDDEN = (".bzr", ".svn", ".git", "CVS", ".hg", "_darcs") lock = Database.lock - def __init__ (self): + def __init__ (self, session): """Constructor.""" Database.__init__(self) self._restrict = "" + self.session = session + updateFormat = False + if "format" not in session or session["format"] != self.FORMAT: + session["format"] = self.FORMAT + updateFormat = True + pkgdb = os.path.join(SESSION_DIR, "package.db") pkgdb_existed = os.path.exists(pkgdb) @@ -49,18 +56,22 @@ class SQLDatabase (Database): pkg_conn = sql.connect(os.path.join(SESSION_DIR, "package.db")) pkg_conn.row_factory = sql.Row + if pkgdb_existed and updateFormat: + pkg_conn.execute("DROP TABLE packages") + pkg_conn.execute(""" CREATE TABLE IF NOT EXISTS packages ( name TEXT, cat TEXT, - inst INTEGER + inst INTEGER, + disabled INTEGER )""") pkg_conn.commit() self.was_updated = self.updated() - if self.was_updated or not pkgdb_existed: + if self.was_updated or not pkgdb_existed or updateFormat: info(_("Cleaning database...")) pkg_conn.execute("DELETE FROM packages") # empty db at beginning info(_("Populating database...")) @@ -153,9 +164,9 @@ class SQLDatabase (Database): for p in system.find_packages(key = category, with_version = False): cat, pkg = p.split("/") - yield (cat, pkg, p in inst) + yield (cat, pkg, p in inst, False) - connection.executemany("INSERT INTO packages (cat, name, inst) VALUES (?, ?, ?)", _get()) + connection.executemany("INSERT INTO packages (cat, name, inst, disabled) VALUES (?, ?, ?, ?)", _get()) connection.commit() @lock |