diff options
-rw-r--r-- | src/clog.c | 46 | ||||
-rw-r--r-- | src/queries.h | 23 |
2 files changed, 32 insertions, 37 deletions
@@ -5,6 +5,9 @@ #include "assets.h" +// FIXME: Why does compilation fail if this is a .c file? +#include "queries.h" + #include "../lib/md4c/src/entity.h" #include "../lib/md4c/src/entity.c" #include "../lib/md4c/src/md4c.h" @@ -199,56 +202,25 @@ render_posts_query(struct post_request *post_req) } // Query for posts, check for error. - if (post_req->type == HTML && post_req->resource) + if (post_req->resource) { - // Query an HTML post. + // Query a post. err = kore_pgsql_query_params( &sql, - "SELECT id, title, created_at::DATE, body " - "FROM posts " - "WHERE id = $1;", + post_req->type == HTML ? query_html_post : query_json_post, 0, // return string data 1, // param count KORE_PGSQL_PARAM_TEXT(post_req->resource) ); } - else if (post_req->type == HTML) + else { - // Query all HTML posts. + // Query all posts. err = kore_pgsql_query( &sql, - "SELECT id, title, created_at::DATE, body " - "FROM posts " - "ORDER BY updated_at DESC;" + post_req->type == HTML ? query_html_posts : query_json_posts ); } - else if (post_req->type == JSON && post_req->resource) - { - // Query a JSON post. - err = kore_pgsql_query_params( - &sql, - "SELECT JSON_AGG(ROW_TO_JSON(row)) FROM (" - "SELECT id, title, body, created_at, updated_at " - "FROM posts " - "WHERE id = $1" - ") row;", - 0, // string data - 1, // param count - KORE_PGSQL_PARAM_TEXT(post_req->resource) - ); - } - else if (post_req->type == JSON) - { - // Query all JSON posts. - err = kore_pgsql_query( - &sql, - "SELECT JSON_AGG(ROW_TO_JSON(row)) FROM (" - "SELECT id, title, body, created_at, updated_at " - "FROM posts " - "ORDER BY updated_at DESC" - ") row;" - ); - } // else { ... if (err == KORE_RESULT_ERROR) { diff --git a/src/queries.h b/src/queries.h new file mode 100644 index 0000000..cae7d59 --- /dev/null +++ b/src/queries.h @@ -0,0 +1,23 @@ +const char *query_html_post = \ +"SELECT id, title, created_at::DATE, body " +"FROM posts " +"WHERE id = $1;"; + +const char *query_html_posts = \ +"SELECT id, title, created_at::DATE, body " +"FROM posts " +"ORDER BY updated_at DESC;"; + +const char *query_json_post = \ +"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM (" + "SELECT id, title, body, created_at, updated_at " + "FROM posts " + "WHERE id = $1" +") row;"; + +const char *query_json_posts = \ +"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM (" + "SELECT id, title, body, created_at, updated_at " + "FROM posts " + "ORDER BY updated_at DESC" +") row;"; |