You are not logged in.

#1 2008-11-12 20:02:21

u_no_hu
Member
Registered: 2008-06-15
Posts: 453

Dumping the pacman DB to sqlite.....

Hi ,

Just for fun and as a learining/programming exercise i am writing a simple program to dump the local and sync dbs  to Sqlite3.... Here is the pre-pre-pre-alpa version :-) ....... any comments are most welcome.....

#include <alpm.h>
#include <alpm_list.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h> 
#include <sqlite3.h>

static int on_progress = 0;
static alpm_list_t *output = NULL;
pmdb_t *db_local;
alpm_list_t *i , *j;
int ret = 0 ;
sqlite3 *db_sqlite; 
sqlite3_stmt  *ppStmtfiles, *ppStmtdeps;



static void insert_files(pmpkg_t *pkg)
{

    if ((ret = sqlite3_bind_text(ppStmtfiles,1,alpm_pkg_get_name(pkg),-1,SQLITE_STATIC)) != SQLITE_OK) {
        printf("bind1 %d\n",ret) ;
    }

    if ((ret = sqlite3_bind_text(ppStmtdeps,1,alpm_pkg_get_name(pkg),-1,SQLITE_STATIC)) != SQLITE_OK) {
        printf("bind1 %d\n",ret) ;
    }

    for (j = alpm_pkg_get_files(pkg); j ; j = alpm_list_next(j) )
    {
        if ((ret = sqlite3_bind_text(ppStmtfiles,2,j->data,-1,SQLITE_STATIC)) != SQLITE_OK) {
            printf("bind2 %d\n",ret) ;
        }
        if ((ret = sqlite3_step(ppStmtfiles)) != SQLITE_DONE){
            printf("step %d\n",ret) ;
        }
      sqlite3_reset(ppStmtfiles);
    }

    for (j = alpm_pkg_get_depends(pkg); j ; j = alpm_list_next(j) )
    {
        if ((ret = sqlite3_bind_text(ppStmtdeps,2,alpm_pkg_get_name(j->data),-1,SQLITE_STATIC)) != SQLITE_OK) {
            printf("bind2 %d\n",ret) ;
        }
        if ((ret = sqlite3_step(ppStmtdeps)) != SQLITE_DONE){
            printf("step %d\n",ret) ;
        }
      sqlite3_reset(ppStmtdeps);
    }

}


static void cleanup() {
    /* free alpm library resources */
    if(alpm_release() == -1) {
        printf("%s", alpm_strerrorlast());
    }

    if((ret=sqlite3_close(db_sqlite)) != SQLITE_OK){
        printf("sqlite close faildi: %d",ret);
    }
}




/* Callback to handle notifications from the library */
void cb_log(pmloglevel_t level, char *fmt, va_list args)
{
    if(!fmt || strlen(fmt) == 0) {
        return;
    }

}


int populate_files_db ()
{

    char *zSqlfiles = sqlite3_mprintf("INSERT INTO FILELIST (PACKAGE,FILE) VALUES (:name,:file)");
    char *zSqldeps = sqlite3_mprintf("INSERT INTO DEPLIST  (PACKAGE,DEPENDENCY) VALUES (:name,:dep)");

    if ((ret=sqlite3_prepare_v2(db_sqlite,zSqlfiles,strlen(zSqlfiles),&ppStmtfiles,NULL)) != SQLITE_OK) {
        printf("sqlite3_prepare failure: %d",ret);
        return(-1);
    }

    if ((ret=sqlite3_prepare_v2(db_sqlite,zSqldeps,strlen(zSqldeps),&ppStmtdeps,NULL)) != SQLITE_OK) {
        printf("sqlite3_prepare failure: %d",ret);
        return(-1);
    }

    if (ppStmtfiles == NULL) {
        printf("sqlite3_prepare returned NULL");
        return(-1);
        }

    if (ppStmtdeps == NULL) {
        printf("sqlite3_prepare returned NULL");
        return(-1);
        }

  if ((ret=sqlite3_exec(db_sqlite,"BEGIN;",NULL,NULL,NULL))!=SQLITE_OK){
        printf("sqlite3_exec begin failure: %d",ret);
        return(-1);
    }

    for(i = alpm_db_getpkgcache(db_local); i; i = alpm_list_next(i)) {
            pmpkg_t *pkg = alpm_list_getdata(i);
                insert_files(pkg);
    }    
    

  if((ret=sqlite3_exec(db_sqlite,"END;",NULL,NULL,NULL))!=SQLITE_OK){
        printf("sqlite3_exec end failure: %d",ret);
        return(-1);
    }


  if((ret=sqlite3_finalize(ppStmtfiles))!=SQLITE_OK){
        printf("sqlite3_exec end failure: %d",ret);
        return(-1);
    }
  if((ret=sqlite3_finalize(ppStmtdeps))!=SQLITE_OK){
        printf("sqlite3_exec end failure: %d",ret);
        return(-1);
    }
}


int initialize_db()
{
    if (sqlite3_open("local",&db_sqlite) != SQLITE_OK) {
            printf("Error opening sqlite db\n");
            return (-1);
    }
}




int main(int argc, char *argv[])
{


    /* initialize library */
    alpm_initialize(); 
    alpm_option_set_logcb(cb_log);
    alpm_option_set_root("/");
    alpm_option_set_dbpath("/var/lib/pacman");
    alpm_option_set_logfile("/home/shankar/new.log");


    /* Opening local database */
    db_local = alpm_db_register_local();

    if ((ret=initialize_db())==-1){
        printf("Error initializing the SQL database: %d\n",ret);
        return (EXIT_FAILURE);
    }

    if (populate_files_db()==-1){
        cleanup();
        return(EXIT_FAILURE);
    }

    cleanup();
    /* not reached */
    return(EXIT_SUCCESS);
}


/* vim: set ts=2 sw=2 noet: */

Don't be a HELP VAMPIRE. Please search before you ask.

Subscribe to The Arch Daily News.

Offline

#2 2008-11-12 22:52:01

Cene
Member
From: Laukaa, Finland
Registered: 2008-11-06
Posts: 16
Website

Re: Dumping the pacman DB to sqlite.....

I can't really comment on the code since, albeit being pretty familiar with C, I've never done any serious database stuff with anything but Perl (just seeing stuff like that being done with C gives me shivers... wink), but I've got one general tip.

Never break the 80-column limit. That is, none of your code lines should be wider than 80 columns. If not by any other means, then by splitting expressions to multiple lines. It makes the code a lot more pleasant to read on small screens or terminals.

Offline

#3 2008-11-13 09:58:23

vacant
Member
From: downstairs
Registered: 2004-11-05
Posts: 816

Re: Dumping the pacman DB to sqlite.....

Put some more comments in your code perhaps, e.g. when a variable is declared, what is it for?

Last edited by vacant (2008-11-13 10:00:09)

Offline

Board footer

Powered by FluxBB