SIGN IN SIGN UP
2022-03-16 15:38:08 -07:00
#include <iostream>
2022-03-16 15:38:08 -07:00
#include <assert.h>
#include <sqlite3.h>
2022-03-16 15:38:08 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2022-04-12 16:07:13 -07:00
#include "base/auto_mem.hh"
2022-03-16 15:38:08 -07:00
#include "base/injector.hh"
#include "regexp_vtab.hh"
2022-03-16 15:38:08 -07:00
#include "sqlite-extension-func.hh"
#include "sqlitepp.client.hh"
#include "sql_util.hh"
2020-12-23 15:01:21 -08:00
#include "xpath_vtab.hh"
struct callback_state {
int cs_row;
};
2022-03-16 15:38:08 -07:00
static int
sql_callback(void* ptr, int ncols, char** colvalues, char** colnames)
{
2022-03-16 15:38:08 -07:00
struct callback_state* cs = (struct callback_state*) ptr;
printf("Row %d:\n", cs->cs_row);
for (int lpc = 0; lpc < ncols; lpc++) {
printf(" Column %10s: %s\n", colnames[lpc], colvalues[lpc]);
}
cs->cs_row += 1;
2022-03-16 15:38:08 -07:00
return 0;
}
2022-03-16 15:38:08 -07:00
int
main(int argc, char* argv[])
{
int retval = EXIT_SUCCESS;
auto_mem<sqlite3> db(sqlite3_close);
std::string stmt;
2014-05-05 06:44:58 -07:00
log_argv(argc, argv);
if (argc == 2) {
stmt = argv[1];
} else {
std::getline(std::cin, stmt, '\0');
}
if (sqlite3_open(":memory:", db.out()) != SQLITE_OK) {
fprintf(stderr, "error: unable to make sqlite memory database\n");
retval = EXIT_FAILURE;
2022-03-16 15:38:08 -07:00
} else {
{
auto stmt = prepare_stmt(db, LNAV_ATTACH_DB).unwrap();
auto exec_res = stmt.execute();
if (exec_res.isErr()) {
fprintf(stderr,
"failed to attach memory database: %s\n",
exec_res.unwrapErr().c_str());
exit(EXIT_FAILURE);
}
}
auto_mem<char> errmsg(sqlite3_free);
struct callback_state state;
memset(&state, 0, sizeof(state));
{
int register_collation_functions(sqlite3 * db);
register_sqlite_funcs(db.in(), sqlite_registration_funcs);
register_collation_functions(db.in());
}
register_regexp_vtab(db.in());
2020-12-23 15:01:21 -08:00
register_xpath_vtab(db.in());
2022-03-16 15:38:08 -07:00
if (sqlite3_exec(
db.in(), stmt.c_str(), sql_callback, &state, errmsg.out())
!= SQLITE_OK)
{
fprintf(stderr, "error: sqlite3_exec failed -- %s\n", errmsg.in());
retval = EXIT_FAILURE;
}
}
return retval;
}