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 */
|
|
|
|
|
|
|
|
#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-11 12:48:15 -05:00
|
|
|
const char errmsg_bind[] = "Error binding parameter: %s\n";
|
|
|
|
const char errmsg_clear[] = "Error clearing bindings: %s\n";
|
|
|
|
const char errmsg_insert[] = "Error inserting data: %s\n";
|
|
|
|
const char errmsg_prepare[] = "Error preparing data: %s\n";
|
|
|
|
const char errmsg_readfile[] = "Error reading file: %s\n";
|
|
|
|
const char errmsg_reset[] = "Error resetting statement: %s\n";
|
|
|
|
const char errmsg_update[] = "Error updating data: %s\n";
|
|
|
|
|
|
|
|
int print_col(sqlite3_stmt * pTableInfo, int col) {
|
|
|
|
int n, rc;
|
|
|
|
static int ct = 0;
|
|
|
|
char outfile[50];
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
printf("%s: ", sqlite3_column_name(pTableInfo, col));
|
|
|
|
switch (sqlite3_column_type(pTableInfo, col)) {
|
|
|
|
case SQLITE_INTEGER:
|
|
|
|
printf("%d ", sqlite3_column_int(pTableInfo, col));
|
|
|
|
break;
|
|
|
|
case SQLITE_FLOAT:
|
|
|
|
printf("%f ", sqlite3_column_double(pTableInfo, col));
|
|
|
|
break;
|
|
|
|
case SQLITE_TEXT:
|
|
|
|
printf("%s ", sqlite3_column_text(pTableInfo, col));
|
|
|
|
break;
|
|
|
|
case SQLITE_BLOB: //printf("%s",sqlite3_column_blob(pTableInfo, col));
|
|
|
|
/* fprintf(stderr, "IN BLOB bytes %d\n",
|
|
|
|
sqlite3_column_bytes(pTableInfo, col)); */
|
|
|
|
snprintf(outfile, 20, "outdata.%d.png", ct++);
|
|
|
|
n = sqlite3_column_bytes(pTableInfo, col);
|
|
|
|
if (n > 0)
|
|
|
|
size = (size_t)n;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if ((rc = (int)file_write(outfile, sqlite3_column_blob(pTableInfo, col), size))) {
|
|
|
|
assert(rc == (int)size);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SQLITE_NULL:
|
|
|
|
printf("Null ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf(" *Cannot determine SQLITE TYPE* col=%d ", col);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-11 12:48:15 -05:00
|
|
|
int run_script(sqlite3 * db, const char *script_filename) {
|
2021-03-09 01:39:19 -05:00
|
|
|
int j, rc;
|
|
|
|
char *copy_statements, *orig_statements, *saveptr1, *str1, *token, *zErrMsg;
|
2021-03-08 16:59:27 -05:00
|
|
|
|
|
|
|
zErrMsg = 0;
|
2021-03-11 12:48:15 -05:00
|
|
|
orig_statements = file_read(script_filename, 0);
|
2021-03-08 16:59:27 -05:00
|
|
|
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 {
|
2021-03-11 12:48:15 -05:00
|
|
|
fprintf(stderr, errmsg_readfile, script_filename);
|
2021-03-09 01:39:19 -05:00
|
|
|
return -1;
|
2021-03-08 16:59:27 -05:00
|
|
|
}
|
|
|
|
|
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-11 12:48:15 -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-11 12:48:15 -05:00
|
|
|
if (SQLITE_OK != rc) {
|
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);
|
|
|
|
fprintf(stderr, "Bye!\n");
|
|
|
|
|
2021-03-11 12:48:15 -05:00
|
|
|
return SQLITE_OK;
|
2021-03-08 16:59:27 -05:00
|
|
|
}
|
|
|
|
|
2021-03-11 12:48:15 -05:00
|
|
|
int run_one(sqlite3 * db, const char *query) {
|
|
|
|
int i, rc;
|
|
|
|
sqlite3_stmt *stmt = NULL;
|
|
|
|
|
|
|
|
printf("> %s\n", query);
|
|
|
|
if (SQLITE_OK != (rc = sqlite3_prepare_v2(db, query, -1, &stmt, 0))) {
|
|
|
|
fprintf(stderr, errmsg_prepare, sqlite3_errmsg(db));
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
while (SQLITE_ROW == (rc = sqlite3_step(stmt))) {
|
|
|
|
for (i = 0; i < sqlite3_column_count(stmt); ++i) {
|
|
|
|
printf(" => ");
|
|
|
|
print_col(stmt, i);
|
|
|
|
puts("");
|
|
|
|
}
|
2021-03-08 16:59:27 -05:00
|
|
|
}
|
2021-03-11 12:48:15 -05:00
|
|
|
if (SQLITE_DONE != (rc = sqlite3_step(stmt))) {
|
|
|
|
fprintf(stderr, errmsg_update, sqlite3_errmsg(db));
|
|
|
|
}
|
|
|
|
if (SQLITE_OK != (rc = sqlite3_reset(stmt))) {
|
|
|
|
fprintf(stderr, errmsg_reset, sqlite3_errmsg(db));
|
|
|
|
}
|
|
|
|
if (SQLITE_OK != (rc = sqlite3_clear_bindings(stmt))) {
|
|
|
|
fprintf(stderr, errmsg_clear, sqlite3_errmsg(db));
|
|
|
|
}
|
|
|
|
return sqlite3_finalize(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int update_one(sqlite3 * db, int rowid, const char *param1, const char *param2) {
|
|
|
|
const char insert_stmt[] = "update quotedb set ? = ? where quote_id = ?";
|
|
|
|
int n, rc;
|
|
|
|
size_t maxlen;
|
|
|
|
sqlite3_stmt *stmt = NULL;
|
|
|
|
maxlen = 4096;
|
|
|
|
n = 0;
|
|
|
|
if (SQLITE_OK != (rc = sqlite3_prepare_v2(db, insert_stmt, -1, &stmt, 0))) {
|
|
|
|
fprintf(stderr, errmsg_prepare, sqlite3_errmsg(db));
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define S(x) if (SQLITE_OK != (rc = sqlite3_bind_text(stmt, ++n, x, (int)strnlen(x, maxlen), SQLITE_STATIC))) { \
|
|
|
|
fprintf(stderr, errmsg_bind, sqlite3_errmsg(db)); \
|
|
|
|
return SQLITE_ERROR; \
|
|
|
|
}
|
|
|
|
|
|
|
|
S(param1);
|
|
|
|
S(param2);
|
|
|
|
#undef S
|
|
|
|
|
|
|
|
sqlite3_bind_int(stmt, 3, rowid);
|
|
|
|
|
|
|
|
if (SQLITE_DONE != (rc = sqlite3_step(stmt))) {
|
|
|
|
fprintf(stderr, errmsg_update, sqlite3_errmsg(db));
|
|
|
|
}
|
|
|
|
if (SQLITE_OK != (rc = sqlite3_reset(stmt))) {
|
|
|
|
fprintf(stderr, errmsg_reset, sqlite3_errmsg(db));
|
|
|
|
}
|
|
|
|
return sqlite3_finalize(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int insert_row(sqlite3 * db, const char *channel, const char *added_by, const char *subject, const char *words) {
|
|
|
|
const char insert_stmt[] = "insert into quotedb (date_added, channel, added_by, subject, words) values (datetime('now'), ?, ?, ?, ?)";
|
|
|
|
int n, rc;
|
|
|
|
size_t maxlen;
|
|
|
|
sqlite3_stmt *stmt = NULL;
|
|
|
|
|
|
|
|
maxlen = 4096;
|
|
|
|
n = 0;
|
|
|
|
if (SQLITE_OK != (rc = sqlite3_prepare_v2(db, insert_stmt, -1, &stmt, 0))) {
|
|
|
|
fprintf(stderr, errmsg_prepare, sqlite3_errmsg(db));
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
#define S(x) if (SQLITE_OK != (rc = sqlite3_bind_text(stmt, ++n, x, (int)strnlen(x, maxlen), SQLITE_STATIC))) { \
|
|
|
|
fprintf(stderr, errmsg_bind, sqlite3_errmsg(db)); \
|
|
|
|
return SQLITE_ERROR; \
|
|
|
|
}
|
|
|
|
|
|
|
|
S(channel);
|
|
|
|
S(added_by);
|
|
|
|
S(subject);
|
|
|
|
S(words);
|
|
|
|
#undef S
|
|
|
|
|
|
|
|
if (SQLITE_DONE != (rc = sqlite3_step(stmt))) {
|
|
|
|
fprintf(stderr, errmsg_insert, sqlite3_errmsg(db));
|
|
|
|
}
|
|
|
|
if (SQLITE_OK != (rc = sqlite3_reset(stmt))) {
|
|
|
|
fprintf(stderr, errmsg_reset, sqlite3_errmsg(db));
|
|
|
|
}
|
|
|
|
return sqlite3_finalize(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sqlite_version(sqlite3 * db) {
|
|
|
|
int rc;
|
|
|
|
sqlite3_stmt *stmt = NULL;
|
|
|
|
|
|
|
|
if (SQLITE_OK != (rc = sqlite3_prepare_v2(db, "SELECT SQLITE_VERSION()", -1, &stmt, 0))) {
|
|
|
|
fprintf(stderr, errmsg_prepare, sqlite3_errmsg(db));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SQLITE_ROW == (rc = sqlite3_step(stmt))) {
|
|
|
|
printf("SQLite version: %s\n", sqlite3_column_text(stmt, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
return 0;
|
2021-03-08 16:59:27 -05:00
|
|
|
}
|