aboutsummaryrefslogtreecommitdiff
path: root/src/queries.h
blob: 6c11317f19c71f8a3224113ef5142d89fd3f966c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const char *q_select_entry =
"SELECT id, title, created_at::DATE, updated_at::DATE, body "
"FROM entries "
"WHERE id = $1;";

const char *q_select_entries =
"WITH inner_query AS "
"( "
"   SELECT id, title, created_at, updated_at, body "
"   FROM entries "
"   ORDER BY updated_at DESC, created_at DESC "
"   LIMIT $1 "
") "
"SELECT id, title, created_at::DATE, updated_at::DATE, body FROM inner_query;";

const char *q_search_entries =
"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 search_query;";

const char *q_insert_entry =
"INSERT INTO entries (title, body, search_vector) "
"VALUES ($1, $2, TO_TSVECTOR('english', $3)) "
"RETURNING id;";

const char *q_update_entry =
"UPDATE entries "
"SET title = $1, body = $2, updated_at = NOW(), search_vector = TO_TSVECTOR('english', $3) "
"WHERE id = $4 "
"RETURNING id;";

const char *q_delete_entry =
"DELETE "
"FROM entries "
"WHERE id = $1 "
"RETURNING id;";