]> git.ipfire.org Git - pakfire.git/commitdiff
db: Import dependencies
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Feb 2021 17:05:03 +0000 (17:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Feb 2021 17:05:03 +0000 (17:05 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/db.c

index ff72704f6688676a0f48816ee23f02bee6cb61e9..f4769c3dd0d7d8c8de39066f252916db224ed5f8 100644 (file)
@@ -30,6 +30,7 @@
 #include <pakfire/package.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
+#include <pakfire/relationlist.h>
 #include <pakfire/repo.h>
 #include <pakfire/types.h>
 #include <pakfire/util.h>
@@ -625,6 +626,103 @@ PAKFIRE_EXPORT int pakfire_db_check(struct pakfire_db* db) {
        return 0;
 }
 
+static int pakfire_db_add_dependencies(struct pakfire_db* db, unsigned long id, PakfirePackage pkg) {
+       sqlite3_stmt* stmt = NULL;
+       int r = 1;
+
+       const char* sql = "INSERT INTO dependencies(pkg, type, dependency) VALUES(?, ?, ?)";
+
+       // Prepare the statement
+       r = sqlite3_prepare_v2(db->handle, sql, -1, &stmt, NULL);
+       if (r) {
+               ERROR(db->pakfire, "Could not prepare SQL statement: %s: %s\n",
+                       sql, sqlite3_errmsg(db->handle));
+               goto END;
+       }
+
+       const struct __relation {
+               const char* type;
+               PakfireRelationList (*func)(PakfirePackage);
+       } relations[] = {
+               { "provides", pakfire_package_get_provides },
+               { "prerequires", pakfire_package_get_prerequires },
+               { "requires", pakfire_package_get_requires },
+               { "conflicts", pakfire_package_get_conflicts },
+               { "obsoletes", pakfire_package_get_obsoletes },
+               { "recommends", pakfire_package_get_recommends },
+               { "suggests", pakfire_package_get_suggests },
+               { NULL, NULL },
+       };
+
+       for (const struct __relation* relation = relations; relation->type; relation++) {
+               PakfireRelationList list = relation->func(pkg);
+               if (!list)
+                       continue;
+
+               for (int i = 0; i < pakfire_relationlist_count(list); i++) {
+                       PakfireRelation rel = pakfire_relationlist_get_clone(list, i);
+                       if (!rel)
+                               goto END;
+
+                       char* dependency = pakfire_relation_str(rel);
+                       if (!dependency) {
+                               pakfire_relation_unref(rel);
+                               r = 1;
+                               goto END;
+                       }
+
+                       // Bind package ID
+                       r = sqlite3_bind_int64(stmt, 1, id);
+                       if (r) {
+                               ERROR(db->pakfire, "Could not bind id: %s\n",
+                                       sqlite3_errmsg(db->handle));
+                               pakfire_relation_unref(rel);
+                               goto END;
+                       }
+
+                       // Bind type
+                       r = sqlite3_bind_text(stmt, 2, relation->type, -1, NULL);
+                       if (r) {
+                               ERROR(db->pakfire, "Could not bind type: %s\n",
+                                       sqlite3_errmsg(db->handle));
+                               pakfire_relation_unref(rel);
+                               goto END;
+                       }
+
+                       // Bind dependency
+                       r = sqlite3_bind_text(stmt, 3, dependency, -1, NULL);
+                       if (r) {
+                               ERROR(db->pakfire, "Could not bind dependency: %s\n",
+                                       sqlite3_errmsg(db->handle));
+                               pakfire_relation_unref(rel);
+                               goto END;
+                       }
+
+                       // Execute query
+                       do {
+                               r = sqlite3_step(stmt);
+                       } while (r == SQLITE_BUSY);
+
+                       pakfire_relation_unref(rel);
+                       free(dependency);
+
+                       // Reset bound values
+                       sqlite3_reset(stmt);
+               }
+
+               pakfire_relationlist_unref(list);
+       }
+
+       // All okay
+       r = 0;
+
+END:
+       if (stmt)
+               sqlite3_finalize(stmt);
+
+       return r;
+}
+
 static int pakfire_db_add_files(struct pakfire_db* db, unsigned long id, PakfireArchive archive) {
        sqlite3_stmt* stmt = NULL;
        int r = 1;
@@ -859,7 +957,6 @@ END:
        return r;
 }
 
-
 PAKFIRE_EXPORT int pakfire_db_add_package(struct pakfire_db* db,
                PakfirePackage pkg, PakfireArchive archive) {
        sqlite3_stmt* stmt = NULL;
@@ -1077,6 +1174,11 @@ PAKFIRE_EXPORT int pakfire_db_add_package(struct pakfire_db* db,
        if (r == SQLITE_OK)
                stmt = NULL;
 
+       // Add dependencies
+       r = pakfire_db_add_dependencies(db, packages_id, pkg);
+       if (r)
+               goto ROLLBACK;
+
        // Add files
        r = pakfire_db_add_files(db, packages_id, archive);
        if (r)