complete DbSourceImpl for packages
authorKlaus Kaempf <kkaempf@suse.de>
Mon, 6 Feb 2006 19:13:52 +0000 (19:13 +0000)
committerKlaus Kaempf <kkaempf@suse.de>
Mon, 6 Feb 2006 19:13:52 +0000 (19:13 +0000)
zmd/backend/dbsource/DbAccess.cc
zmd/backend/dbsource/DbAccess.h
zmd/backend/dbsource/DbPackageImpl.cc [new file with mode: 0644]
zmd/backend/dbsource/DbPackageImpl.h [new file with mode: 0644]
zmd/backend/dbsource/DbSourceImpl.cc
zmd/backend/dbsource/DbSourceImpl.h
zmd/backend/dbsource/DbSources.cc
zmd/backend/dbsource/DbSources.h
zmd/backend/dbsource/Makefile.am

index 5ffaa41..f86d062 100644 (file)
@@ -23,32 +23,40 @@ IMPL_PTR_TYPE(DbAccess);
 using namespace std;
 using namespace zypp;
 
+
+static struct archrc {
+       char *arch;
+       RCArch rc;
+    } archtable[] = {
+       { "noarch",     RC_ARCH_NOARCH },
+       { "i386",       RC_ARCH_I386 },
+       { "i486",       RC_ARCH_I486 },
+       { "i586",       RC_ARCH_I586 },
+       { "i686",       RC_ARCH_I686 },
+       { "x86_64",     RC_ARCH_X86_64 },
+       { "ia32e",      RC_ARCH_IA32E },
+       { "athlon",     RC_ARCH_ATHLON },
+       { "ppc",        RC_ARCH_PPC },
+       { "ppc64",      RC_ARCH_PPC64 },
+       { "s390",       RC_ARCH_S390 },
+       { "s390x",      RC_ARCH_S390X },
+       { "ia64",       RC_ARCH_IA64 },
+       { "sparc",      RC_ARCH_SPARC },
+       { "sparc64",    RC_ARCH_SPARC64 },
+       { NULL,         RC_ARCH_UNKNOWN }
+    };
+
+
 //----------------------------------------------------------------------------
 
-// Convert ZYPP relation operator to ZMD int
-static int
-Rel2Int (Rel op)
+// Convert ZYPP relation operator to ZMD RCResolvableRelation
+
+RCResolvableRelation
+DbAccess::Rel2Rc (Rel op)
 {
-#define RELATION_ANY 0
-#define RELATION_EQUAL (1 << 0)
-#define RELATION_LESS (1 << 1)
-#define RELATION_GREATER (1 << 2)
-#define RELATION_NONE (1 << 3)
 
    /* This enum is here so that gdb can give us pretty strings */
 
-    typedef enum {
-       RC_RELATION_INVALID            = -1,
-       RC_RELATION_ANY                = RELATION_ANY,
-       RC_RELATION_EQUAL              = RELATION_EQUAL,
-       RC_RELATION_LESS               = RELATION_LESS,
-       RC_RELATION_LESS_EQUAL         = RELATION_LESS | RELATION_EQUAL,
-       RC_RELATION_GREATER            = RELATION_GREATER,
-       RC_RELATION_GREATER_EQUAL      = RELATION_GREATER | RELATION_EQUAL,
-       RC_RELATION_NOT_EQUAL          = RELATION_LESS | RELATION_GREATER,
-       RC_RELATION_NONE               = RELATION_NONE,
-    } RCResolvableRelation;
-
     switch (op.inSwitch()) {
        case Rel::EQ_e:    return RC_RELATION_EQUAL; break;
        case Rel::NE_e:    return RC_RELATION_NOT_EQUAL; break;
@@ -62,63 +70,59 @@ Rel2Int (Rel op)
     return RC_RELATION_INVALID;
 }
 
+
+Rel
+DbAccess::Rc2Rel (RCResolvableRelation rel)
+{
+    switch (rel) {
+       case RC_RELATION_INVALID:       return Rel::NONE; break;
+       case RC_RELATION_ANY:           return Rel::ANY; break;
+       case RC_RELATION_EQUAL:         return Rel::EQ; break;
+       case RC_RELATION_LESS:          return Rel::LT; break;
+       case RC_RELATION_LESS_EQUAL:    return Rel::LE; break;
+       case RC_RELATION_GREATER:       return Rel::GT; break;
+       case RC_RELATION_GREATER_EQUAL: return Rel::GE; break;
+       case RC_RELATION_NOT_EQUAL:     return Rel::NE; break;
+       case RC_RELATION_NONE:          return Rel::NONE; break;
+    }
+    return Rel::NONE;
+}
+
 //----------------------------------------------------------------------------
 
 // convert ZYPP architecture string to ZMD int
 
-typedef enum {
-    RC_ARCH_UNKNOWN = -1,
-    RC_ARCH_NOARCH = 0,
-    RC_ARCH_I386,
-    RC_ARCH_I486,
-    RC_ARCH_I586,
-    RC_ARCH_I686,
-    RC_ARCH_X86_64,
-    RC_ARCH_IA32E,
-    RC_ARCH_ATHLON,
-    RC_ARCH_PPC,
-    RC_ARCH_PPC64,
-    RC_ARCH_S390,
-    RC_ARCH_S390X,
-    RC_ARCH_IA64,
-    RC_ARCH_SPARC,
-    RC_ARCH_SPARC64,
-} RCArch;
-
-static int
-arch2zmd (string str)
+RCArch
+DbAccess::Arch2Rc (const Arch & arch)
 {
-    struct archrc {
-       char *arch;
-       RCArch rc;
-    } archtable[] = {
-       { "noarch",     RC_ARCH_NOARCH },
-       { "i386",       RC_ARCH_I386 },
-       { "i486",       RC_ARCH_I486 },
-       { "i586",       RC_ARCH_I586 },
-       { "i686",       RC_ARCH_I686 },
-       { "x86_64",     RC_ARCH_X86_64 },
-       { "ia32e",      RC_ARCH_IA32E },
-       { "athlon",     RC_ARCH_ATHLON },
-       { "ppc",        RC_ARCH_PPC },
-       { "ppc64",      RC_ARCH_PPC64 },
-       { "s390",       RC_ARCH_S390 },
-       { "s390x",      RC_ARCH_S390X },
-       { "ia64",       RC_ARCH_IA64 },
-       { "sparc",      RC_ARCH_SPARC },
-       { "sparc64",    RC_ARCH_SPARC64 },
-       { NULL,         RC_ARCH_UNKNOWN }
-    };
-
+    string arch_str = arch.asString();
     struct archrc *aptr = archtable;
     while (aptr->arch != NULL) {
-       break;
+       if (arch_str == aptr->arch)
+           break;
        aptr++;
     }
 
     return aptr->rc;
 }
 
+Arch
+DbAccess::Rc2Arch (RCArch rc)
+{
+    if (rc == RC_ARCH_UNKNOWN)
+       return Arch();
+
+    struct archrc *aptr = archtable;
+    while (aptr->arch != NULL) {
+       if (aptr->rc == rc) {
+           return Arch (aptr->arch);
+       }
+       aptr++;
+    }
+    WAR << "DbAccess::Rc2Arch(" << rc << ") unknown" << endl;
+    return Arch ();
+}
+
 
 // convert description Text (list<string>) to a single string
 static string
@@ -369,7 +373,7 @@ DbAccess::writeDependency( sqlite_int64 res_id, RCDependencyType type, const zyp
        }
 
        sqlite3_bind_int (handle, 7, 0);                                // arch
-       sqlite3_bind_int (handle, 8, Rel2Int(iter->op()));
+       sqlite3_bind_int (handle, 8, Rel2Rc( iter->op() ));
 
        rc = sqlite3_step (handle);
        sqlite3_reset (handle);
@@ -448,7 +452,7 @@ DbAccess::writeResObject (ResObject::constPtr obj, bool is_installed)
        sqlite3_bind_int (handle, 4, ed.epoch());
     }
 
-    sqlite3_bind_int (handle, 5, arch2zmd (obj->arch().asString()));
+    sqlite3_bind_int (handle, 5, Arch2Rc (obj->arch()));
     if (pkg != NULL) {
        sqlite3_bind_int64 (handle, 7, pkg->archivesize());
        sqlite3_bind_int64 (handle, 8, pkg->size());
index ef590f3..1af02b8 100644 (file)
 #include "zypp/ResPool.h"
 #include "zypp/CapSet.h"
 #include "zypp/Dep.h"
+#include "zypp/Rel.h"
+#include "zypp/Arch.h"
 
 DEFINE_PTR_TYPE(DbAccess);
 
 typedef std::list<zypp::ResObject::constPtr> ResObjectList;
 
-
-///////////////////////////////////////////////////////////////////
-//
-//     CLASS NAME : DbAccess
-//
-/**
-*/
-
-class DbAccess : public zypp::base::ReferenceCounted, public zypp::base::NonCopyable
-{
-  private:
-    typedef enum {
+#define RELATION_ANY 0
+#define RELATION_EQUAL (1 << 0)
+#define RELATION_LESS (1 << 1)
+#define RELATION_GREATER (1 << 2)
+#define RELATION_NONE (1 << 3)
+
+typedef enum {
+       RC_RELATION_INVALID            = -1,
+       RC_RELATION_ANY                = RELATION_ANY,
+       RC_RELATION_EQUAL              = RELATION_EQUAL,
+       RC_RELATION_LESS               = RELATION_LESS,
+       RC_RELATION_LESS_EQUAL         = RELATION_LESS | RELATION_EQUAL,
+       RC_RELATION_GREATER            = RELATION_GREATER,
+       RC_RELATION_GREATER_EQUAL      = RELATION_GREATER | RELATION_EQUAL,
+       RC_RELATION_NOT_EQUAL          = RELATION_LESS | RELATION_GREATER,
+       RC_RELATION_NONE               = RELATION_NONE,
+} RCResolvableRelation;
+
+
+typedef enum {
+    RC_ARCH_UNKNOWN = -1,
+    RC_ARCH_NOARCH = 0,
+    RC_ARCH_I386,
+    RC_ARCH_I486,
+    RC_ARCH_I586,
+    RC_ARCH_I686,
+    RC_ARCH_X86_64,
+    RC_ARCH_IA32E,
+    RC_ARCH_ATHLON,
+    RC_ARCH_PPC,
+    RC_ARCH_PPC64,
+    RC_ARCH_S390,
+    RC_ARCH_S390X,
+    RC_ARCH_IA64,
+    RC_ARCH_SPARC,
+    RC_ARCH_SPARC64,
+} RCArch;
+
+typedef enum {
        RC_DEP_TYPE_REQUIRE = 0,
        RC_DEP_TYPE_PROVIDE,
        RC_DEP_TYPE_CONFLICT,
@@ -52,8 +81,17 @@ class DbAccess : public zypp::base::ReferenceCounted, public zypp::base::NonCopy
        RC_DEP_TYPE_RECOMMEND,
        RC_DEP_TYPE_SUGGEST,
        RC_DEP_TYPE_ENHANCE
-    } RCDependencyType;
+} RCDependencyType;
 
+///////////////////////////////////////////////////////////////////
+//
+//     CLASS NAME : DbAccess
+//
+/**
+*/
+
+class DbAccess : public zypp::base::ReferenceCounted, public zypp::base::NonCopyable
+{
   private:
     std::string _dbfile;
 
@@ -62,9 +100,6 @@ class DbAccess : public zypp::base::ReferenceCounted, public zypp::base::NonCopy
     sqlite3_stmt *_insert_pkg_handle;
     sqlite3_stmt *_insert_dep_handle;
 
-    bool openDb(bool for_writing);
-    void closeDb(void);
-
     bool prepareWrite(void);
     bool prepareRead(void);
 
@@ -81,6 +116,15 @@ public:
     /** Dtor */
     ~DbAccess( );
 
+    static RCResolvableRelation Rel2Rc (zypp::Rel op);
+    static zypp::Rel Rc2Rel (RCResolvableRelation rel);
+    static RCArch Arch2Rc (const zypp::Arch & arch);
+    static zypp::Arch Rc2Arch (RCArch rc);
+
+    sqlite3 *db() const { return _db; }
+    bool openDb(bool for_writing);
+    void closeDb(void);
+
     void writeStore( const zypp::ResStore & resolvables, bool is_installed );
     void writeResObjects( const ResObjectList & resolvables, bool is_installed );
 
diff --git a/zmd/backend/dbsource/DbPackageImpl.cc b/zmd/backend/dbsource/DbPackageImpl.cc
new file mode 100644 (file)
index 0000000..8ef3fbb
--- /dev/null
@@ -0,0 +1,90 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file zmd/backend/dbsource/DbPackageImpl.h
+ *
+*/
+
+#include "DbPackageImpl.h"
+#include "zypp/source/SourceImpl.h"
+#include "zypp/TranslatedText.h"
+#include "zypp/base/String.h"
+#include "zypp/base/Logger.h"
+
+using namespace std;
+using namespace zypp::detail;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////
+//
+//        CLASS NAME : DbPackageImpl
+//
+///////////////////////////////////////////////////////////////////
+
+/** Default ctor
+*/
+DbPackageImpl::DbPackageImpl (Source_Ref source_r)
+    : _source (source_r)
+    , _install_only(false)
+    , _size_installed(0)
+    , _size_archive(0)
+{
+}
+
+/**
+ * read package specific data from handle
+ * throw() on error
+ */
+
+void
+DbPackageImpl::readHandle( sqlite3_stmt *handle )
+{
+    const char * text = (const char *) sqlite3_column_text (handle, 12);
+    if (text != NULL)
+       _summary = TranslatedText( string( text ) );
+    text = (const char *) sqlite3_column_text (handle, 13);
+    if (text != NULL)
+       _description = TranslatedText( string( text ) );
+    _group = "group";          //FIXME ((const char *) sqlite3_column_text (handle, 10));
+    _size_installed = sqlite3_column_int (handle, 6);
+    _size_archive = sqlite3_column_int (handle, 11);
+    _install_only = (sqlite3_column_int (handle, 15) != 0);
+}
+
+
+Source_Ref
+DbPackageImpl::source() const
+{ return _source; }
+
+/** Package summary */
+TranslatedText DbPackageImpl::summary() const
+{ return _summary; }
+
+/** Package description */
+TranslatedText DbPackageImpl::description() const
+{ return _description; }
+
+PackageGroup DbPackageImpl::group() const
+{ return _group; }
+
+ByteCount DbPackageImpl::size() const
+{ return _size_installed; }
+
+/** */
+ByteCount DbPackageImpl::archivesize() const
+{ return _size_archive; }
+
+bool DbPackageImpl::installOnly() const
+{ return _install_only; }
+
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
diff --git a/zmd/backend/dbsource/DbPackageImpl.h b/zmd/backend/dbsource/DbPackageImpl.h
new file mode 100644 (file)
index 0000000..8d7e704
--- /dev/null
@@ -0,0 +1,67 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file zmd/backend/dbsource/DbPackageImpl.h
+ *
+*/
+#ifndef ZMD_BACKEND_DBSOURCE_DBPACKAGEIMPL_H
+#define ZMD_BACKEND_DBSOURCE_DBPACKAGEIMPL_H
+
+#include "zypp/detail/PackageImpl.h"
+#include "zypp/Source.h"
+#include <sqlite3.h>
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////
+//
+//        CLASS NAME : DbPackageImpl
+//
+/** Class representing a package
+*/
+class DbPackageImpl : public detail::PackageImplIf
+{
+public:
+
+       /** Default ctor
+       */
+       DbPackageImpl( Source_Ref source_r );
+       void readHandle( sqlite3_stmt *handle );
+
+       /** Package summary */
+       virtual TranslatedText summary() const;
+       /** Package description */
+       virtual TranslatedText description() const;
+       virtual ByteCount size() const;
+       /** */
+       virtual PackageGroup group() const;
+       /** */
+       virtual ByteCount archivesize() const;
+       /** */
+       virtual bool installOnly() const;
+       /** */
+       virtual Source_Ref source() const;
+
+protected:
+       Source_Ref _source;
+       TranslatedText _summary;
+       TranslatedText _description;
+       PackageGroup _group;
+       bool _install_only;
+
+       ByteCount _size_installed;
+       ByteCount _size_archive;
+
+
+ };
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZMD_BACKEND_DBSOURCE_DBPACKAGEIMPL_H
index d1993c2..bfcc7fb 100644 (file)
  * 02111-1307, USA.
  */
 
-#include "DbExtract.h"
 #include "DbSourceImpl.h"
+
 #include "DbPackageImpl.h"
+#if 0
 #include "DbScriptImpl.h"
 #include "DbMessageImpl.h"
 #include "DbPatchImpl.h"
 #include "DbPatternImpl.h"
 #include "DbProductImpl.h"
+#endif
 
 #include "zypp/source/SourceImpl.h"
 #include "zypp/base/Logger.h"
 #include "zypp/base/Exception.h"
+#include "zypp/CapFactory.h"
 
 using namespace std;
 using namespace zypp;
 
 //---------------------------------------------------------------------------
 
-DbSourceImpl::DbSourceImpl(media::MediaAccess::Ptr & media_r, const Pathname & path_r, const std::string & alias_r)
-    : SourceImpl (media_r, path_r, alias_r)
-    , _source (Source_Ref::noSource)
-    , _pathname (path_r)
+DbSourceImpl::DbSourceImpl()
+    : _db (NULL)
 {
 }
 
 
 void
-DbSourceImpl::createResolvables(Source_Ref source_r)
+DbSourceImpl::factoryInit()
 {
-    _source = source_r;
-    extractDbFile (_pathname.asString(), this);
+    MIL << "DbSourceImpl::factoryInit()" << endl;
+
+    try {
+       media::MediaManager media_mgr;
+       MIL << "Adding no media verifier" << endl;
+       media_mgr.delVerifier(_media);
+       media_mgr.addVerifier(_media, media::MediaVerifierRef(new media::NoVerifier()));
+    }
+    catch (const Exception & excpt_r)
+    {
+#warning FIXME: If media data is not set, verifier is not set. Should the media be refused instead?
+       ZYPP_CAUGHT(excpt_r);
+       WAR << "Verifier not found" << endl;
+    }
+    return;
+}
+
+void
+DbSourceImpl::factoryCtor( const media::MediaId & media_r, const Pathname & path_r, const std::string & alias_r, const Pathname cache_dir_r)
+{
+    MIL << "DbSourceImpl::factoryCtor(<media>, " << path_r << ", " << alias_r << ", " << cache_dir_r << ")" << endl;
+    _media = media_r;
+    _alias = alias_r;
+    _cache_dir = cache_dir_r;
+}
+
+void
+DbSourceImpl::attachDatabase( sqlite3 *db)
+{
+    _db = db;
 }
 
 //-----------------------------------------------------------------------------
 
-Dependencies
-DbSourceImpl::createDependencies (const DbReader & parsed)
+static sqlite3_stmt *
+create_dependency_handle (sqlite3 *db)
 {
-    Dependencies deps;
+    const char *query;
+    int rc;
+    sqlite3_stmt *handle = NULL;
+
+    query =
+       //      0         1     2        3        4      5     6
+        "SELECT dep_type, name, version, release, epoch, arch, relation "
+        "FROM dependencies "
+        "WHERE resolvable_id = ?";
+
+    rc = sqlite3_prepare (db, query, -1, &handle, NULL);
+    if (rc != SQLITE_OK) {
+       ERR << "Can not prepare dependency selection clause: " << sqlite3_errmsg (db) << endl;
+        sqlite3_finalize (handle);
+        return NULL;
+    }
 
-    deps[Dep::PROVIDES] = parsed.provides;
-    deps[Dep::PREREQUIRES] = parsed.prerequires;
-    deps[Dep::REQUIRES] = parsed.requires;
-    deps[Dep::CONFLICTS] = parsed.conflicts;
-    deps[Dep::OBSOLETES] = parsed.obsoletes;
-    deps[Dep::RECOMMENDS] = parsed.recommends;
-    deps[Dep::SUGGESTS] = parsed.suggests;
-    deps[Dep::FRESHENS] = parsed.freshens;
-    deps[Dep::ENHANCES] = parsed.enhances;
+    return handle;
+}
 
-    return deps;
+static sqlite3_stmt *
+create_package_handle (sqlite3 *db)
+{
+    const char *query;
+    int rc;
+    sqlite3_stmt *handle = NULL;
+
+    query = 
+       //      0   1     2        3        4      5
+        "SELECT id, name, version, release, epoch, arch, "
+       //      6               7
+        "       installed_size, catalog,"
+       //      8          9      10       11
+        "       installed, local, section, file_size,"
+       //      12       13           14
+        "       summary, description, package_filename,"
+       //      15
+        "       install_only "
+        "FROM packages "
+        "WHERE catalog = ?";
+
+    rc = sqlite3_prepare (db, query, -1, &handle, NULL);
+    if (rc != SQLITE_OK) {
+       ERR << "Can not prepare package_details selection clause: " << sqlite3_errmsg (db) << endl;
+        sqlite3_finalize (handle);
+        return NULL;
+    }
+
+    return handle;
 }
 
 
-Package::Ptr
-DbSourceImpl::createPackage (const DbReader & parsed)
+void
+DbSourceImpl::createResolvables(Source_Ref source_r)
 {
-    try
-    {
-       shared_ptr<DbPackageImpl> impl(new DbPackageImpl(_source, parsed));
+    MIL << "DbSourceImpl::createResolvables(" << source_r.id() << ")" << endl;
+    _source = source_r;
+    if (_db == NULL) {
+       ERR << "Must call attachDatabase() first" << endl;
+       return;
+    }
 
-       // Collect basic Resolvable data
-       NVRAD dataCollect( parsed.name,
-                       Edition( parsed.version, parsed.release, parsed.epoch ),
-                       Arch( parsed.arch ),
-                       createDependencies (parsed));
-       Package::Ptr package = detail::makeResolvableFromImpl(dataCollect, impl);
-       return package;
+    _dependency_handle = create_dependency_handle (_db);
+    if (_dependency_handle == NULL) return;
+
+    createPackages();
+
+    return;
+}
+
+
+void
+DbSourceImpl::createPackages(void)
+{
+    sqlite3_stmt *handle = create_package_handle (_db);
+    if (handle == NULL) return;
+
+    sqlite3_bind_text (handle, 1, _source.id().c_str(), -1, SQLITE_STATIC);
+
+    int rc;
+    while ((rc = sqlite3_step (handle)) == SQLITE_ROW) {
+
+       string name;
+
+       try
+       {
+           shared_ptr<DbPackageImpl> impl(new DbPackageImpl( _source ));
+
+           sqlite_int64 id = sqlite3_column_int64( handle, 0 );
+           name = (const char *) sqlite3_column_text (handle, 1);
+           string version ((const char *) sqlite3_column_text (handle, 2));
+           string release ((const char *) sqlite3_column_text (handle, 3));
+           unsigned epoch = sqlite3_column_int (handle, 4);
+           Arch arch (DbAccess::Rc2Arch( (RCArch)(sqlite3_column_int (handle, 5)) ) );
+
+           impl->readHandle( handle );
+
+           // Collect basic Resolvable data
+           NVRAD dataCollect( name,
+                       Edition( version, release, epoch ),
+                       arch,
+                       createDependencies (id));
+
+           Package::Ptr package = detail::makeResolvableFromImpl(dataCollect, impl);
+           _store.insert( package );
+       }
+       catch (const Exception & excpt_r)
+       {
+           ERR << "Cannot create package object '"+name+"' from catalog '"+_source.id()+"'" << endl;
+           sqlite3_reset (handle);
+           ZYPP_RETHROW (excpt_r);
+       }
     }
-    catch (const Exception & excpt_r)
-    {
-       ERR << excpt_r << endl;
-       throw "Cannot create package object";
+
+    sqlite3_reset (handle);
+    return;
+}
+
+//-----------------------------------------------------------------------------
+
+Dependencies
+DbSourceImpl::createDependencies (sqlite_int64 resolvable_id)
+{
+    Dependencies deps;
+    CapFactory factory;
+
+    sqlite3_bind_int64 (_dependency_handle, 1, resolvable_id);
+
+    RCDependencyType dep_type;
+    string name, version, release;
+    unsigned epoch;
+    Arch arch;
+    Rel rel;
+    Capability cap;
+    Resolvable::Kind dkind = ResTraits<Package>::kind;  // default to package
+
+    const char *text;
+    int rc;
+    while ((rc = sqlite3_step( _dependency_handle)) == SQLITE_ROW) {
+       try {
+           dep_type = (RCDependencyType)sqlite3_column_int( _dependency_handle, 0);
+           name = string ( (const char *)sqlite3_column_text( _dependency_handle, 1) );
+           text = (const char *)sqlite3_column_text( _dependency_handle, 2);
+           if (text != NULL)
+               version = text;
+           else
+               version.clear();
+           text = (const char *)sqlite3_column_text( _dependency_handle, 3);
+           if (text != NULL)
+               release = text;
+           else
+               release.clear();
+           epoch = sqlite3_column_int( _dependency_handle, 4 );
+           arch = DbAccess::Rc2Arch( (RCArch) sqlite3_column_int( _dependency_handle, 5 ) );
+           rel = DbAccess::Rc2Rel( (RCResolvableRelation) sqlite3_column_int( _dependency_handle, 6 ) );
+
+           cap = factory.parse( dkind, name, rel, Edition( version, release, epoch ) );
+           switch (dep_type) {
+               case RC_DEP_TYPE_REQUIRE:
+                   deps[Dep::REQUIRES].insert( cap );
+               break;
+               case RC_DEP_TYPE_PROVIDE:
+                   deps[Dep::PROVIDES].insert( cap );
+               break;
+               case RC_DEP_TYPE_CONFLICT:
+                   deps[Dep::CONFLICTS].insert( cap );
+               break;
+               case RC_DEP_TYPE_OBSOLETE:
+                   deps[Dep::OBSOLETES].insert( cap );
+               break;
+               case RC_DEP_TYPE_PREREQUIRE:
+                   deps[Dep::PREREQUIRES].insert( cap );
+               break;
+               case RC_DEP_TYPE_FRESHEN:
+                   deps[Dep::FRESHENS].insert( cap );
+               break;
+               case RC_DEP_TYPE_RECOMMEND:
+                   deps[Dep::RECOMMENDS].insert( cap );
+               break;
+               case RC_DEP_TYPE_SUGGEST:
+                   deps[Dep::SUGGESTS].insert( cap );
+               break;
+               case RC_DEP_TYPE_ENHANCE:
+                   deps[Dep::ENHANCES].insert( cap );
+               break;
+               default:
+                  ERR << "Unhandled dep_type " << dep_type << endl;
+               break;
+           }
+       }
+       catch ( Exception & excpt_r ) {
+           ERR << "Can't parse dependencies for resolvable_id " << resolvable_id << ", name " << name << endl;
+           ZYPP_RETHROW( excpt_r );
+       }
     }
-    return NULL;
+
+    sqlite3_reset (_dependency_handle);
+    return deps;
 }
 
+#if 0
 
 Message::Ptr
 DbSourceImpl::createMessage (const DbReader & parsed)
@@ -255,6 +446,7 @@ DbSourceImpl::parserCallback (const DbReader & parsed)
     ZYPP_CAUGHT (excpt_r);
   }
 }
+#endif
 
 //-----------------------------------------------------------------------------
 
index ae8f914..deb93be 100644 (file)
  * 02111-1307, USA.
  */
 
-#ifndef ZYPP_ZMD_BACKEND_DBSOURCEIMPL_H
-#define ZYPP_ZMD_BACKEND_DBSOURCEIMPL_H
+#ifndef ZMD_BACKEND_DBSOURCEIMPL_H
+#define ZMD_BACKEND_DBSOURCEIMPL_H
 
 #include <iosfwd>
 #include <string>
 
 #include "zypp/source/SourceImpl.h"
+#include "zypp/media/MediaManager.h"
 
-class DbReader;
-//#include "DbReader.h"
+#include "DbAccess.h"
 
 #include "zypp/Package.h"
 #include "zypp/Message.h"
@@ -37,39 +37,58 @@ class DbReader;
 #include "zypp/Product.h"
 #include "zypp/Selection.h"
 #include "zypp/Pattern.h"
-
-namespace zypp {
-        
+       
 ///////////////////////////////////////////////////////////////////
 //
 //     CLASS NAME : DbSourceImpl
 
 class DbSourceImpl : public zypp::source::SourceImpl {
 
-  public:
-
+ public:
     /** Default ctor */
-    DbSourceImpl(media::MediaAccess::Ptr & media_r, const Pathname & path_r = "/", const std::string & alias_r = "");
+    DbSourceImpl();
+
+ private:
+    /** Ctor substitute.
+     * Actually get the metadata.
+     * \throw EXCEPTION on fail
+     */
+    virtual void factoryInit();
+
+    sqlite3 *_db;
+    sqlite3_stmt *_dependency_handle;
+    sqlite3_stmt *_patch_handle;
+    sqlite3_stmt *_pattern_handle;
+    sqlite3_stmt *_product_handle;
+
+    void createPackages(void);
+
+    zypp::Dependencies createDependencies (sqlite_int64 resolvable_id);
+
+#if 0
+    zypp::Message::Ptr createMessage (const DbReader & data);
+    zypp::Script::Ptr  createScript (const DbReader & data);
+    zypp::Patch::Ptr   createPatch (const DbReader & data);
+    zypp::Pattern::Ptr createPattern (const DbReader & data);
+    zypp::Product::Ptr createProduct (const DbReader & data);
+#endif
+
+ public:
+    void factoryCtor( const zypp::media::MediaId & media_r,
+                      const zypp::Pathname & path_r = "/",
+                      const std::string & alias_r = "",
+                      const zypp::Pathname cache_dir_r = "");
+
 
     virtual const bool valid() const
     { return true; }
 
-    Package::Ptr createPackage (const DbReader & data);
-    Message::Ptr createMessage (const DbReader & data);
-    Script::Ptr  createScript (const DbReader & data);
-    Patch::Ptr   createPatch (const DbReader & data);
-    Pattern::Ptr createPattern (const DbReader & data);
-    Product::Ptr createProduct (const DbReader & data);
-
-    Dependencies createDependencies (const DbReader & data);
+    void attachDatabase( sqlite3 *db );
 
   private:
-    Source_Ref _source;
-    const Pathname _pathname;
-    void createResolvables(Source_Ref source_r);
+    zypp::Source_Ref _source;
+    void createResolvables( zypp::Source_Ref source_r );
 };
 
 
-} // namespace zypp
-
-#endif // ZYPP_ZMD_BACKEND_DBSOURCEIMPL_H
+#endif // ZMD_BACKEND_DBSOURCEIMPL_H
index de3947f..9189f3a 100644 (file)
@@ -42,7 +42,8 @@ using namespace zypp;
 
 DbSources::DbSources (sqlite3 *db)
     : _db (db)
-{
+{ 
+    MIL << "DbSources::DbSources(" << db << ")" << endl;
 }
 
 DbSources::~DbSources ()
@@ -54,6 +55,9 @@ DbSources::sources (bool refresh)
 {
     MIL << "DbSources::sources(" << (refresh ? "refresh" : "") << ")" << endl;
 
+    if (_db == NULL)
+       return _sources;
+
     if (!refresh
        && !_sources.empty())
     {
@@ -62,9 +66,6 @@ DbSources::sources (bool refresh)
 
     _sources.clear();
 
-    media::MediaManager mmgr;
-    media::MediaId mediaid = mmgr.open(Url("file://"));
-
     const char *query =
         "SELECT id, name, alias, description, priority, priority_unsubd "
         "FROM catalogs";
@@ -73,9 +74,13 @@ DbSources::sources (bool refresh)
     int rc = sqlite3_prepare (_db, query, -1, &handle, NULL);
     if (rc != SQLITE_OK) {
        ERR << "Can not read 'channels': " << sqlite3_errmsg (_db) << endl;
-       goto cleanup;
+       return _sources;
     }
 
+    media::MediaManager mmgr;
+    media::MediaId mediaid = mmgr.open(Url("file://"));
+    SourceFactory factory;
+
     while ((rc = sqlite3_step (handle)) == SQLITE_ROW) {
        string id ((const char *) sqlite3_column_text (handle, 0));
        string name ((const char *) sqlite3_column_text (handle, 1));
@@ -89,39 +94,35 @@ DbSources::sources (bool refresh)
            << ", alias " << alias
            << ", desc " << desc
            << ", prio " << priority
-           << ", pr. un" << priority_unsub
+           << ", pr. un " << priority_unsub
            << endl;
-#if 0
 
        try {
 
-           Source_Ref::Impl_Ptr impl = new DbSourceImpl (mediaid, pathname, alias);
+           DbSourceImpl *impl = new DbSourceImpl ();
+           impl->factoryCtor (mediaid, Pathname(), alias);
            impl->setId( id );
-           impl->setName( name );
+           impl->setZmdName( name );
+           impl->setZmdDescription ( desc );
            impl->setPriority( priority );
            impl->setPriorityUnsubscribed( priority_unsub );
-           SourceFactory _f;
-           Source_Ref s = _f.createFrom( impl );
+
+           impl->attachDatabase (_db);
+
+           Source_Ref src( factory.createFrom( impl ) );
+           _sources.push_back( src );
        }
        catch (Exception & excpt_r) {
            ZYPP_CAUGHT(excpt_r);
        }
-        if (!strcmp (rc_channel_get_id (ch), "@system")) {
-            rc_channel_set_system (ch);
-            rc_channel_set_hidden (ch);
-        }
-#endif
+
     }
 
     if (rc != SQLITE_DONE) {
        ERR << "Error while reading 'channels': " << sqlite3_errmsg (_db) << endl;
        _sources.clear();
-        goto cleanup;
     }
 
- cleanup:
-    if (handle)
-        sqlite3_finalize (handle);
-
+    MIL << "Read " << _sources.size() << " catalogs" << endl;
     return _sources;
 }
index daa924c..af6f02f 100644 (file)
@@ -19,8 +19,8 @@
  * 02111-1307, USA.
  */
 
-#ifndef ZYPP_ZMD_BACKEND_DBSOURCES_H
-#define ZYPP_ZMD_BACKEND_DBSOURCES_H
+#ifndef ZMD_BACKEND_DBSOURCES_H
+#define ZMD_BACKEND_DBSOURCES_H
 
 #include <iosfwd>
 #include <string>
@@ -29,8 +29,6 @@
 #include <sqlite3.h>
 #include "zypp/Source.h"
 
-namespace zypp {
-
 ///////////////////////////////////////////////////////////////////
 //
 //      CLASS NAME : DbSources
@@ -51,6 +49,4 @@ class DbSources
     const SourcesList & sources (bool refresh = false);
 };
 
-} // namespace zypp
-
-#endif  // ZYPP_ZMD_BACKEND_DBSOURCES_H
+#endif  // ZMD_BACKEND_DBSOURCES_H
index da49367..301543f 100644 (file)
@@ -12,10 +12,13 @@ LDADD =     $(noinst_LTLIBRARIES) $(top_srcdir)/zypp/lib@PACKAGE@.la
 noinst_HEADERS =                               \
        DbSources.h                             \
        DbSourceImpl.h                          \
+       DbPackageImpl.h                         \
        DbAccess.h
        
 noinst_LTLIBRARIES =   lib@PACKAGE@_dbsource.la
 
 lib@PACKAGE@_dbsource_la_SOURCES =             \
+       DbSourceImpl.cc                         \
+       DbPackageImpl.cc                        \
        DbSources.cc                            \
        DbAccess.cc