5. Iterating through pages

This is a process that gets applied to each page, and works very similarly to the export command.

<<funcdefs>>=
static void parse_all_pages(void);
<<funcs>>=
static void parse_all_pages(void)
{
    weewiki_d *ww;
    sqlite3_stmt *stmt;
    sqlite3 *db;
    int rc;
<<init_and_open>>
<<create_sqlite_statement>>
<<iterate_through_pages>>
<<cleanup>>
}

Before any iteration can begin, a few bits of initialization. The database is opened.

<<init_and_open>>=
ww = malloc(weewiki_sizeof());
weewiki_init(ww);
weewiki_open(ww, "a.db");

db = weewiki_db(ww);

All the pages in the wiki table are queried, their keys and values extracted. The keys and values are fed into the analyzer. First, the SQLite statment is created, then it is stepped through.

<<create_sqlite_statement>>=
sqlite3_prepare_v2(db,
                    "SELECT key, value "
                    "FROM wiki "
                    "WHERE key NOT LIKE \"@%\";",
                    -1,
                    &stmt,
                    NULL);
<<iterate_through_pages>>=
rc = sqlite3_step(stmt);

while (rc == SQLITE_ROW) {
    const char *key;
    size_t keysz;
    const char *val;
    size_t valsz;

    key = (const char *)sqlite3_column_text(stmt, 0);
    keysz = sqlite3_column_bytes(stmt, 0);
    val = (const char *)sqlite3_column_text(stmt, 1);
    valsz = sqlite3_column_bytes(stmt, 1);

    parse_file(key, keysz, val, valsz, stdout);
    rc = sqlite3_step(stmt);
}

At cleanup, the database is closed, and the statment cleaned up.

<<cleanup>>=
sqlite3_finalize(stmt);
weewiki_close(ww);
weewiki_clean(ww);
free(ww);



prev | home | next