| 1 | /* |
| 2 | * Copyright (c) 2015 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * This example demonstrates how to use synchronous PGSQL queries |
| 19 | * with Kore. For an asynchronous example see pgsql/ under examples/. |
| 20 | * |
| 21 | * This example does the same as the asynchronous one, select all entries |
| 22 | * from a table called "coders". |
| 23 | */ |
| 24 | |
| 25 | #include <kore/kore.h> |
| 26 | #include <kore/http.h> |
| 27 | #include <kore/pgsql.h> |
| 28 | |
| 29 | int init(int); |
| 30 | int page(struct http_request *); |
| 31 | |
| 32 | /* Called when our module is loaded (see config) */ |
| 33 | int |
| 34 | init(int state) |
| 35 | { |
| 36 | /* Register our database. */ |
| 37 | kore_pgsql_register("db", "host=/tmp dbname=test"); |
| 38 | |
| 39 | return (KORE_RESULT_OK); |
| 40 | } |
| 41 | |
| 42 | /* Page handler entry point (see config) */ |
| 43 | int |
| 44 | page(struct http_request *req) |
| 45 | { |
| 46 | struct kore_pgsql sql; |
| 47 | char *name; |
| 48 | int rows, i; |
| 49 | |
| 50 | req->status = HTTP_STATUS_INTERNAL_ERROR; |
| 51 | |
| 52 | kore_pgsql_init(&sql); |
| 53 | |
| 54 | /* |
| 55 | * Initialise our kore_pgsql data structure with the database name |
| 56 | * we want to connect to (note that we registered this earlier with |
| 57 | * kore_pgsql_register()). We also say we will perform a synchronous |
| 58 | * query (KORE_PGSQL_SYNC). |
| 59 | */ |
| 60 | if (!kore_pgsql_setup(&sql, "db", KORE_PGSQL_SYNC)) { |
| 61 | kore_pgsql_logerror(&sql); |
| 62 | goto out; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Now we can fire off the query, once it returns we either have |
| 67 | * a result on which we can operate or an error occurred. |
| 68 | */ |
| 69 | if (!kore_pgsql_query(&sql, "SELECT * FROM coders")) { |
| 70 | kore_pgsql_logerror(&sql); |
| 71 | goto out; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Iterate over the result and dump it to somewhere. |
| 76 | */ |
| 77 | rows = kore_pgsql_ntuples(&sql); |
| 78 | for (i = 0; i < rows; i++) { |
| 79 | name = kore_pgsql_getvalue(&sql, i, 0); |
| 80 | kore_log(LOG_NOTICE, "name: '%s'", name); |
| 81 | } |
| 82 | |
| 83 | /* All good. */ |
| 84 | req->status = HTTP_STATUS_OK; |
| 85 | |
| 86 | out: |
| 87 | http_response(req, req->status, NULL, 0); |
| 88 | |
| 89 | /* Don't forget to cleanup the kore_pgsql data structure. */ |
| 90 | kore_pgsql_cleanup(&sql); |
| 91 | |
| 92 | return (KORE_RESULT_OK); |
| 93 | } |