-
Notifications
You must be signed in to change notification settings - Fork 10
/
sbk-thread.c
134 lines (114 loc) · 3.26 KB
/
sbk-thread.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (c) 2018 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <stdlib.h>
#include "sbk-internal.h"
/* For database versions < THREAD_AUTOINCREMENT */
#define SBK_QUERY_1 \
"SELECT " \
"_id, " \
"date, " \
"message_count, " /* meaningful_messages */ \
"recipient_ids " /* recipient_id */ \
"FROM thread " \
"ORDER BY _id"
/*
* For database versions
* [THREAD_AUTOINCREMENT, THREAD_AND_MESSAGE_FOREIGN_KEYS)
*/
#define SBK_QUERY_2 \
"SELECT " \
"_id, " \
"date, " \
"message_count, " /* meaningful_messages */ \
"thread_recipient_id " /* recipient_id */ \
"FROM thread " \
"ORDER BY _id"
/* For database versions >= THREAD_AND_MESSAGE_FOREIGN_KEYS */
#define SBK_QUERY_3 \
"SELECT " \
"_id, " \
"date, " \
"meaningful_messages, " \
"recipient_id " \
"FROM thread " \
"ORDER BY _id"
#define SBK_COLUMN__ID 0
#define SBK_COLUMN_DATE 1
#define SBK_COLUMN_MEANINGFUL_MESSAGES 2
#define SBK_COLUMN_RECIPIENT_ID 3
void
sbk_free_thread_list(struct sbk_thread_list *lst)
{
struct sbk_thread *thd;
if (lst != NULL) {
while ((thd = SIMPLEQ_FIRST(lst)) != NULL) {
SIMPLEQ_REMOVE_HEAD(lst, entries);
free(thd);
}
free(lst);
}
}
struct sbk_thread_list *
sbk_get_threads(struct sbk_ctx *ctx)
{
struct sbk_thread_list *lst;
struct sbk_thread *thd;
sqlite3_stmt *stm;
const char *query;
int ret;
if (sbk_create_database(ctx) == -1)
return NULL;
if ((lst = malloc(sizeof *lst)) == NULL) {
warn(NULL);
return NULL;
}
SIMPLEQ_INIT(lst);
if (ctx->db_version >=
SBK_DB_VERSION_THREAD_AND_MESSAGE_FOREIGN_KEYS)
query = SBK_QUERY_3;
else if (ctx->db_version >= SBK_DB_VERSION_THREAD_AUTOINCREMENT)
query = SBK_QUERY_2;
else
query = SBK_QUERY_1;
if (sbk_sqlite_prepare(ctx, &stm, query) == -1)
goto error;
while ((ret = sbk_sqlite_step(ctx, stm)) == SQLITE_ROW) {
if ((thd = malloc(sizeof *thd)) == NULL) {
warn(NULL);
goto error;
}
thd->recipient = sbk_get_recipient_from_id_from_column(ctx,
stm, SBK_COLUMN_RECIPIENT_ID);
if (thd->recipient == NULL) {
free(thd);
goto error;
}
thd->id = sqlite3_column_int64(stm, SBK_COLUMN__ID);
thd->date = sqlite3_column_int64(stm, SBK_COLUMN_DATE);
thd->nmessages = sqlite3_column_int64(stm,
SBK_COLUMN_MEANINGFUL_MESSAGES);
SIMPLEQ_INSERT_TAIL(lst, thd, entries);
}
if (ret != SQLITE_DONE)
goto error;
sqlite3_finalize(stm);
return lst;
error:
sbk_free_thread_list(lst);
sqlite3_finalize(stm);
return NULL;
}