aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/clog.c26
-rw-r--r--src/queries.h18
2 files changed, 28 insertions, 16 deletions
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;";