1. Worgle Database Top

1.1. Top-level files

The worgle database interface generates a single C and header file.

<<db-top>>=
#include <stdio.h>
#include <sqlite3.h>
#include "worgle.h"
#include "worgle_private.h"
#include "db.h"
<<static_db_functions>>
<<functions>>
<<db-header>>=
#ifndef WORGLE_DB_H
#define WORGLE_DB_H
<<macros>>
<<function_declarations>>
#endif

1.2. Top-level functions

1.2.1. Insert Schemas

The initial worgle schemas are created inside the database using the function worgle_db_schemas. This must be called before attempting to do anything else, and it should only be called once.

<<function_declarations>>=
void worgle_db_schemas(worgle_d *worg, sqlite3 *db);
<<functions>>=
void worgle_db_schemas(worgle_d *worg, sqlite3 *db)
{
<<schemas>>
}

1.2.2. Insert Data

When worgle is done parsing data, the data is written to the database using the funtion worgle_db_generate.

<<function_declarations>>=
void worgle_db_generate(worgle_d *worg, sqlite3 *db);
<<functions>>=
void worgle_db_generate(worgle_d *worg, sqlite3 *db)
{
    sqlite3_exec(db, "BEGIN;\n", NULL, NULL, NULL);
<<inserts>>
    sqlite3_exec(db, "COMMIT;\n", NULL, NULL, NULL);
}

1.2.3. Clear Tables

Will clear any existing tables where information is stored. This will only eliminate tables that Worgle writes to, leaving other tables untouched. This is particularly useful when utilizing the SQLite database for other things.

<<function_declarations>>=
void worgle_db_clear(sqlite3 *db, int program);
<<functions>>=
static void delete_table(sqlite3 *db,
                         char *sbuf,
                         const char *tbl,
                         int program)
{
    if(program < 0) {
        sprintf(sbuf,
                "DELETE FROM %s WHERE 1;\n",
                tbl);
    } else {
        sprintf(sbuf,
                "DELETE FROM %s WHERE program == %d;\n",
                tbl,
                program);
    }
    sqlite3_exec(db, sbuf, NULL, NULL, NULL);
}

void worgle_db_clear(sqlite3 *db, int program)
{
    char sbuf[256];
    delete_table(db, sbuf, "resources", program);
    delete_table(db, sbuf, "blocks", program);
    delete_table(db, sbuf, "headers", program);
    delete_table(db, sbuf, "segments", program);
    delete_table(db, sbuf, "files", program);
    delete_table(db, sbuf, "content", program);
    delete_table(db, sbuf, "blkref", program);
}



prev | home | next