16. Dump
Dumps all of the contents of a weewiki database into a
janet script. When the script is run with weewiki janet
,
it will regenerate the database.
This is particularly useful for bootstrapping or updating a database across multiple computers.
<<argparse_entries>>=
{"dump", 4, p_dump, NULL},
<<static_funcdefs>>=
static int p_dump(int argc, char *argv[]);
<<functions>>=
static int p_dump(int argc, char *argv[])
{
weewiki_d ww;
sqlite3 *db;
int rc;
const char *key, *value, *fname;
sqlite3_stmt *stmt;
FILE *fp;
if (argc < 2) {
fp = stdout;
} else {
fp = fopen(argv[1], "w");
if (fp == NULL) {
fprintf(stderr,
"Could not open '%s' for writing.\n",
argv[1]);
return 1;
}
}
weewiki_init(&ww);
if (argc >= 3) {
weewiki_open(&ww, argv[2]);
} else {
weewiki_open(&ww, weewiki_dbname_get());
}
db = ww.db;
sqlite3_prepare_v2(db,
"SELECT wiki.key, wiki.value "
"FROM wiki LEFT JOIN wikilinks "
"ON wiki.key = wikilinks.key "
"WHERE wikilinks.key IS NULL;"
"FROM wikilinks;",
-1,
&stmt,
NULL);
fprintf(fp, "# open and clear wiki db\n\n");
/* TODO: add ww-dbname-get janet word */
fprintf(fp, "(ww-open \"a.db\")\n");
fprintf(fp, "(ww-clear)\n");
fprintf(fp, "\n# unlinked pages\n\n");
while (1) {
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW) break;
key = (const char *)sqlite3_column_text(stmt, 0);
value = (const char *)sqlite3_column_text(stmt, 1);
if (value == NULL) {
fprintf(fp, "(ww-add-page \"%s\" \"\")\n", key);
} else {
fprintf(fp, "(ww-add-page \"%s\" `%s`)\n",
key, value);
}
}
sqlite3_finalize(stmt);
sqlite3_prepare_v2(db,
"SELECT key, filename "
"FROM wikilinks;",
-1,
&stmt,
NULL);
fprintf(fp, "\n# linked pages\n\n");
while (1) {
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW) break;
key = (const char *)sqlite3_column_text(stmt, 0);
fname = (const char *)sqlite3_column_text(stmt, 1);
fprintf(fp, "(ww-add-link \"%s\" \"%s\")\n",
key,
fname);
}
sqlite3_finalize(stmt);
fprintf(fp, "\n# sync and close\n\n");
fprintf(fp, "(ww-sync)\n");
fprintf(fp, "(ww-close)\n");
weewiki_close(&ww);
weewiki_clean(&ww);
return 0;
}
prev | home | next