2021-03-08 16:59:27 -05:00
|
|
|
/* <https://gist.github.com/enile8/2424514> */
|
|
|
|
|
2021-03-08 23:16:14 -05:00
|
|
|
#define _POSIX_C_SOURCE 200809L /* strtok_r, strndup */
|
|
|
|
#include <string.h>
|
2021-03-08 16:59:27 -05:00
|
|
|
#include <assert.h> /* assert */
|
|
|
|
#include <sqlite3.h>
|
|
|
|
#include <stdio.h> /* size_t */
|
|
|
|
#include <stdlib.h> /* malloc */
|
|
|
|
/* TODO: libircclient for basic IRC functionality.
|
|
|
|
* Perhaps a database backend and a client frontend that's separate. */
|
|
|
|
|
|
|
|
#ifndef UNUSED
|
|
|
|
#define UNUSED(x) (void)(x)
|
|
|
|
#endif
|
|
|
|
|
2021-03-08 23:16:14 -05:00
|
|
|
#include "io.h"
|
2021-03-08 16:59:27 -05:00
|
|
|
|
2021-03-09 01:39:19 -05:00
|
|
|
static int callback_run_script(void *NotUsed, int argc, char **argv, char **azColName) {
|
2021-03-08 16:59:27 -05:00
|
|
|
int i;
|
|
|
|
UNUSED(NotUsed);
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
printf(" => %s = %s\n", azColName[i], (argv[i] ? argv[i] : "NULL"));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-09 01:39:19 -05:00
|
|
|
int run_script(const char *db_name, const char *script_filename) {
|
|
|
|
int j, rc;
|
|
|
|
char *copy_statements, *orig_statements, *saveptr1, *str1, *token, *zErrMsg;
|
2021-03-08 16:59:27 -05:00
|
|
|
sqlite3 *db;
|
|
|
|
|
|
|
|
zErrMsg = 0;
|
|
|
|
orig_statements = file_read(script_filename);
|
|
|
|
if (orig_statements) {
|
2021-03-09 01:39:19 -05:00
|
|
|
copy_statements = strndup(orig_statements, 4096);
|
2021-03-08 16:59:27 -05:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Error reading file: %s\n", script_filename);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = sqlite3_open(db_name, &db);
|
|
|
|
if (rc) {
|
|
|
|
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
2021-03-09 01:39:19 -05:00
|
|
|
return -1;
|
2021-03-08 16:59:27 -05:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Open database successfully\n");
|
|
|
|
}
|
|
|
|
|
2021-03-09 01:39:19 -05:00
|
|
|
for (j = 1, str1 = copy_statements;; j++, str1 = NULL) {
|
2021-03-08 16:59:27 -05:00
|
|
|
token = strtok_r(str1, "\n", &saveptr1);
|
|
|
|
if (token == NULL)
|
|
|
|
break;
|
2021-03-08 23:16:14 -05:00
|
|
|
if (strlen(token) > 2 && token[0] == '-' && token[1] == '-')
|
2021-03-09 01:39:19 -05:00
|
|
|
continue; /* Comment line; TODO multiline comment */
|
2021-03-08 16:59:27 -05:00
|
|
|
printf("%d: %s\n", j, token);
|
2021-03-09 01:39:19 -05:00
|
|
|
rc = sqlite3_exec(db, token, callback_run_script, 0, &zErrMsg);
|
2021-03-08 16:59:27 -05:00
|
|
|
if (rc != SQLITE_OK) {
|
2021-03-09 01:39:19 -05:00
|
|
|
fprintf(stderr, " !! SQL error: %s\n", sqlite3_errmsg(db));
|
2021-03-08 16:59:27 -05:00
|
|
|
sqlite3_free(zErrMsg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 01:39:19 -05:00
|
|
|
free(copy_statements);
|
2021-03-08 16:59:27 -05:00
|
|
|
free(orig_statements);
|
|
|
|
sqlite3_close(db);
|
|
|
|
fprintf(stderr, "Bye!\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST_DB
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
char *script_filename;
|
|
|
|
if (argc > 1) {
|
|
|
|
script_filename = argv[1];
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Usage: %s <script>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
2021-03-09 01:39:19 -05:00
|
|
|
return run_script("familyGuy.sqlite3", script_filename);
|
2021-03-08 16:59:27 -05:00
|
|
|
}
|
|
|
|
#endif
|