Commit 661ab130 authored by Shawn Webb's avatar Shawn Webb
Browse files

Update sample program


Signed-off-by: Shawn Webb's avatarShawn Webb <shawn.webb@hardenedbsd.org>
parent 189015d3
......@@ -10,6 +10,6 @@ CFLAGS+= -I/usr/local/include
LDFLAGS+= -L${.CURDIR}/../obj
LDFLAGS+= -L/usr/local/lib
LDADD+= -llattutil
LDADD+= -llattutil -lucl
.include <bsd.prog.mk>
......@@ -34,13 +34,102 @@
int
main(int argc, char *argv[])
{
lattutil_sqlite_query_t *query;
const ucl_object_t *cur, *tmp;
lattutil_sqlite_ctx_t *sqlctx;
ucl_object_iter_t it, it_obj;
lattutil_log_t *logp;
const char *val;
size_t i;
logp = lattutil_log_init(NULL, -1);
lattutil_log_syslog_init(logp, LOG_INFO | LOG_PID | LOG_NDELAY,
lattutil_log_syslog_init(logp, LOG_PID | LOG_NDELAY,
LOG_USER);
logp->ll_log_info(logp, -1, "%s: testing\n", __func__);
sqlctx = lattutil_sqlite_ctx_new("/tmp/db.sqlite3", logp, 0);
if (sqlctx == NULL) {
logp->ll_log_err(logp, -1, "Unable to create sqlite3 object");
return (1);
}
query = lattutil_sqlite_prepare(sqlctx,
"CREATE TABLE IF NOT EXISTS test_table ("
" colname TEXT NOT NULL, "
" colval TEXT NOT NULL"
")");
if (query == NULL) {
logp->ll_log_err(logp, -1, "Unable to create query");
return (1);
}
if (!lattutil_sqlite_exec(query)) {
logp->ll_log_err(logp, -1, "Unable to exec query");
return (1);
}
lattutil_sqlite_query_free(&query);
query = lattutil_sqlite_prepare(sqlctx,
"INSERT INTO test_table (colname, colval) VALUES (?, ?)");
if (query == NULL) {
logp->ll_log_err(logp, -1, "Unable to create second query");
return (1);
}
if (!lattutil_sqlite_bind_string(query, 1, "testname")) {
logp->ll_log_err(logp, -1, "Unable to bind colname");
return (1);
}
if (!lattutil_sqlite_bind_string(query, 2, "testval")) {
logp->ll_log_err(logp, -1, "Unable to bind colval");
return (1);
}
if (!lattutil_sqlite_exec(query)) {
logp->ll_log_err(logp, -1, "Unable to exec second query");
return (1);
}
lattutil_sqlite_query_free(&query);
query = lattutil_sqlite_prepare(sqlctx,
"SELECT * FROM test_table");
if (query == NULL) {
logp->ll_log_err(logp, -1, "Unable to create third query");
return (1);
}
if (!lattutil_sqlite_exec(query)) {
logp->ll_log_err(logp, -1, "Unable to exec third query");
return (1);
}
for (i = 0; i < query->lsq_result.lsr_ncolumns; i++) {
printf("Column name: %s\n", query->lsq_result.lsr_column_names[i]);
}
it = NULL;
while ((cur = ucl_iterate_object(query->lsq_result.lsr_rows, &it, true))) {
it_obj = NULL;
while ((tmp = ucl_iterate_object(cur, &it_obj, true))) {
switch (ucl_object_type(tmp)) {
case UCL_STRING:
printf("%s\n", ucl_object_tostring(tmp));
break;
default:
printf("Unkown type: %s\n",
ucl_object_type_to_string(ucl_object_type(tmp)));
}
}
}
printf("JSON blob:\n%s\n",
ucl_object_emit(query->lsq_result.lsr_rows, UCL_EMIT_JSON));
lattutil_sqlite_query_free(&query);
lattutil_log_free(&logp);
return (0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment