diff options
author | Michael McVady <femtonaut@gmail.com> | 2021-11-15 22:22:13 -0500 |
---|---|---|
committer | Michael McVady <femtonaut@gmail.com> | 2021-11-15 22:22:13 -0500 |
commit | 6e1be0ceb2a550fac244f91c18d70f0362c909cf (patch) | |
tree | 2ff90b8c97d8a1111deccf1ac62863770e448d7f /src/clog.c | |
parent | ba99adb890ac823ec5505861071f03ebcb889ac1 (diff) |
Print out query results
Diffstat (limited to 'src/clog.c')
-rw-r--r-- | src/clog.c | 33 |
1 files changed, 17 insertions, 16 deletions
@@ -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: ; |