From f81f88ae569c6724da18868d1abe92036b895fc6 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Fri, 26 Jan 2024 23:18:51 -0600 Subject: Limit results --- README.md | 15 +++++++-------- src/clog.c | 26 ++++++++++++++++++-------- src/queries.h | 18 ++++++++++-------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 36db08c..5ff9959 100644 --- a/README.md +++ b/README.md @@ -38,14 +38,13 @@ Use `psql` and load `db/db.sql` for models, ## TODO -1. Fix `tests.py` -2. pagination of index -3. Have `tests.py` load and drop database. -4. Get rid of `requests` dependency in `tests.py` -5. Add preview functionality to edit form -6. Fix query parsing for search -7. Create entries. -8. Delete entries. +1. Pagination of index. +2. Delete entries. +3. Fix `tests.py` to work after refactor. +4. Have `tests.py` load and drop database. +5. Get rid of `requests` dependency in `tests.py`. +6. Fix query parsing for search. +7. Add preview functionality to edit form. ## But using C in 2024 is dangerous? diff --git a/src/clog.c b/src/clog.c index 04f5bb2..c9aab60 100644 --- a/src/clog.c +++ b/src/clog.c @@ -23,6 +23,10 @@ KORE_SECCOMP_FILTER("clog", KORE_SYSCALL_ALLOW(uname) ) +#define STR_HELPER(s) #s +#define STR(s) STR_HELPER(s) +#define MAX_ENTRIES 15 + enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND }; struct entry { @@ -102,8 +106,7 @@ void entry_query_init(struct entry_query *eq, const char *id, const char *query) eq->query = NULL; } - // FIXME: define MAX_ENTRIES and LIMIT queries with it. - eq->entries = kore_malloc(sizeof(struct entry *) * 100); + eq->entries = kore_malloc(sizeof(struct entry *) * MAX_ENTRIES); eq->num_entries = 0; eq->status = QUERY_STATUS_OK; @@ -142,8 +145,9 @@ void entry_query_cleanup(struct entry_query *eq) { int validate_uuid(const char *uuid) { size_t i = 0; - if (strlen(uuid) != 36) + if (strlen(uuid) != 36) { return KORE_RESULT_ERROR; + } for (i = 0; i <= 36; i++) { if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) { @@ -535,15 +539,21 @@ int sql_select(struct entry_query *eq) { ); } else if (eq->query) { + // Search for entries. err = kore_pgsql_query_params(&sql, q_search_entries, 0, // return string data - 2, // param count - KORE_PGSQL_PARAM_TEXT(eq->query), - KORE_PGSQL_PARAM_TEXT(eq->query) + 3, // param count + KORE_PGSQL_PARAM_TEXT(eq->query), KORE_PGSQL_PARAM_TEXT(eq->query), + KORE_PGSQL_PARAM_TEXT(STR(MAX_ENTRIES)) ); } - else { // Query for index. - err = kore_pgsql_query(&sql, q_select_entries); + else { + // Query for index. + err = kore_pgsql_query_params(&sql, q_select_entries, + 0, // return string data + 1, // param count + KORE_PGSQL_PARAM_TEXT(STR(MAX_ENTRIES)) + ); } if (err == KORE_RESULT_ERROR) { diff --git a/src/queries.h b/src/queries.h index 696d42a..7b5b55d 100644 --- a/src/queries.h +++ b/src/queries.h @@ -6,18 +6,20 @@ const char *q_select_entry = const char *q_select_entries = "SELECT id, title, created_at::DATE, updated_at::DATE, body " "FROM entries " -"ORDER BY updated_at DESC, created_at DESC;"; +"ORDER BY updated_at DESC, created_at DESC " +"LIMIT $1;"; const char *q_search_entries = -"WITH query AS " +"WITH search_query AS " "( " " SELECT id, title, created_at::DATE, updated_at::DATE, body, " " TS_RANK(search_vector, TO_TSQUERY('english', $1)) AS rank " " FROM entries " " WHERE search_vector @@ TO_TSQUERY('english', $2) " " ORDER BY rank DESC " +" LIMIT $3 " ") " -"SELECT id, title, created_at::DATE, updated_at::DATE, body FROM query;"; +"SELECT id, title, created_at::DATE, updated_at::DATE, body FROM search_query;"; const char *q_insert_entry = "INSERT INTO entries (title, body, search_vector) " @@ -30,8 +32,8 @@ const char *q_update_entry = "WHERE id = $4 " "RETURNING id;"; -// const char *q_delete_entry = -// "DELETE " -// "FROM entries " -// "WHERE id = $1 " -// "RETURNING id;"; +const char *q_delete_entry = +"DELETE " +"FROM entries " +"WHERE id = $1 " +"RETURNING id;"; -- cgit v1.2.3