From 6e1be0ceb2a550fac244f91c18d70f0362c909cf Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Mon, 15 Nov 2021 22:22:13 -0500 Subject: Print out query results --- src/clog.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/clog.c b/src/clog.c index ea5c9b2..9e11790 100644 --- a/src/clog.c +++ b/src/clog.c @@ -8,15 +8,13 @@ int init(int); int page(struct http_request *); /* Called when our module is loaded (see config) */ -int -init(int state) +int init(int state) { /* Register our database. */ int err = kore_pgsql_register( "db", "host=127.0.0.1 port=5432 user=postgres password=p0stgres dbname=clog sslmode=disable" ); - // kore_pgsql_register("db", "host=/tmp dbname=clog"); return (err); } @@ -25,14 +23,17 @@ int page(struct http_request *req) { struct kore_buf *buf; - buf = kore_buf_alloc(2048); + buf = kore_buf_alloc(4096); kore_buf_append(buf, asset_index_begin_html, asset_len_index_begin_html); - /* sql stuff */ + // SQL vars struct kore_pgsql sql; - char *name; - int rows, i; + int row, rows; + + // Results + char *id; + char *title, *body; /* req->status = HTTP_STATUS_INTERNAL_ERROR; */ @@ -45,21 +46,17 @@ int page(struct http_request *req) * query (KORE_PGSQL_SYNC). */ int err = kore_pgsql_setup(&sql, "db", KORE_PGSQL_SYNC); - kore_log(1, "kore_pgsql_setup: %d", err); - kore_log(1, "sql->state: %d", sql.state); + kore_log(LOG_ERR, "kore_pgsql_setup: %d", err); if (!err) { - kore_log(1, "here1!"); kore_pgsql_logerror(&sql); goto out; } - kore_log(1, "here!"); - /* * Now we can fire off the query, once it returns we either have * a result on which we can operate or an error occurred. */ - if (!kore_pgsql_query(&sql, "SELECT * FROM entries")) { + if (!kore_pgsql_query(&sql, "SELECT id, title, body FROM entries")) { kore_pgsql_logerror(&sql); goto out; } @@ -68,9 +65,13 @@ int page(struct http_request *req) * Iterate over the result and dump it to somewhere. */ rows = kore_pgsql_ntuples(&sql); - for (i = 0; i < rows; i++) { - name = kore_pgsql_getvalue(&sql, i, 0); - kore_log(LOG_NOTICE, "name: '%s'", name); + for (row = 0; row < rows; row++) { + id = kore_pgsql_getvalue(&sql, row, 0); + title = kore_pgsql_getvalue(&sql, row, 1); + body = kore_pgsql_getvalue(&sql, row, 2); + kore_log(LOG_NOTICE, "id: '%s'", id); + kore_log(LOG_NOTICE, "title: '%s'", title); + kore_log(LOG_NOTICE, "body: '%s'", body); } out: ; -- cgit v1.2.3