~kris/hacks

stask

stask/data.c -rw-r--r-- 9.0 KiB
648b969d — Kris Yotam convert README.md to README.txt 4 months ago
                                                                                
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* stask - simple task manager
 * See LICENSE file for copyright and license details. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>

#include "stask.h"

static void
now_str(char *buf, int len)
{
	time_t t;
	struct tm *tm;

	t = time(NULL);
	tm = localtime(&t);
	strftime(buf, len, "%Y-%m-%d", tm);
}

static int
ensure_dir(const char *path)
{
	char tmp[1024];
	char *p;

	snprintf(tmp, sizeof(tmp), "%s", path);
	for (p = tmp + 1; *p; p++) {
		if (*p == '/') {
			*p = '\0';
			mkdir(tmp, 0755);
			*p = '/';
		}
	}
	return mkdir(tmp, 0755);
}

int
db_open(TaskDB *tdb)
{
	const char *home;
	char dir[1024];
	int rc;
	const char *sql;

	home = getenv("HOME");
	if (!home)
		home = "/tmp";

	snprintf(dir, sizeof(dir),
		"%s/.local/share/stask", home);
	ensure_dir(dir);

	snprintf(tdb->dbpath, sizeof(tdb->dbpath),
		"%s/stask.db", dir);
	snprintf(tdb->docdir, sizeof(tdb->docdir),
		"%s", dir);

	rc = sqlite3_open(tdb->dbpath, &tdb->db);
	if (rc != SQLITE_OK) {
		fprintf(stderr, "stask: cannot open db: %s\n",
			sqlite3_errmsg(tdb->db));
		return -1;
	}

	sql =
		"CREATE TABLE IF NOT EXISTS lists ("
		"  id INTEGER PRIMARY KEY AUTOINCREMENT,"
		"  name TEXT UNIQUE NOT NULL,"
		"  created TEXT NOT NULL"
		");"
		"CREATE TABLE IF NOT EXISTS tasks ("
		"  id INTEGER PRIMARY KEY AUTOINCREMENT,"
		"  list_id INTEGER NOT NULL,"
		"  text TEXT NOT NULL,"
		"  done INTEGER NOT NULL DEFAULT 0,"
		"  added TEXT NOT NULL,"
		"  FOREIGN KEY(list_id) REFERENCES lists(id)"
		"    ON DELETE CASCADE"
		");";

	rc = sqlite3_exec(tdb->db, sql, NULL, NULL, NULL);
	if (rc != SQLITE_OK) {
		fprintf(stderr, "stask: schema error: %s\n",
			sqlite3_errmsg(tdb->db));
		return -1;
	}

	/* enable foreign keys */
	sqlite3_exec(tdb->db,
		"PRAGMA foreign_keys = ON;",
		NULL, NULL, NULL);

	return 0;
}

void
db_close(TaskDB *tdb)
{
	if (tdb->db)
		sqlite3_close(tdb->db);
	tdb->db = NULL;
}

int
list_create(TaskDB *tdb, const char *name)
{
	sqlite3_stmt *stmt;
	char date[32];
	int rc;

	now_str(date, sizeof(date));
	rc = sqlite3_prepare_v2(tdb->db,
		"INSERT INTO lists (name, created) VALUES (?, ?);",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC);
	sqlite3_bind_text(stmt, 2, date, -1, SQLITE_STATIC);
	rc = sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	return rc == SQLITE_DONE ? 0 : -1;
}

int
list_delete(TaskDB *tdb, const char *name)
{
	sqlite3_stmt *stmt;
	int rc;

	/* delete tasks first (in case FK not enforced) */
	rc = sqlite3_prepare_v2(tdb->db,
		"DELETE FROM tasks WHERE list_id = "
		"(SELECT id FROM lists WHERE name = ?);",
		-1, &stmt, NULL);
	if (rc == SQLITE_OK) {
		sqlite3_bind_text(stmt, 1, name, -1,
			SQLITE_STATIC);
		sqlite3_step(stmt);
		sqlite3_finalize(stmt);
	}

	rc = sqlite3_prepare_v2(tdb->db,
		"DELETE FROM lists WHERE name = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC);
	rc = sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	return rc == SQLITE_DONE ? 0 : -1;
}

int
list_all(TaskDB *tdb, List **out, int *count)
{
	sqlite3_stmt *stmt;
	int rc, n, cap;
	List *arr;

	*out = NULL;
	*count = 0;

	rc = sqlite3_prepare_v2(tdb->db,
		"SELECT id, name, created FROM lists "
		"ORDER BY name;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	n = 0;
	cap = 16;
	arr = malloc(cap * sizeof(List));
	if (!arr) {
		sqlite3_finalize(stmt);
		return -1;
	}

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		if (n >= cap) {
			cap *= 2;
			arr = realloc(arr, cap * sizeof(List));
			if (!arr) {
				sqlite3_finalize(stmt);
				return -1;
			}
		}
		arr[n].id = sqlite3_column_int(stmt, 0);
		snprintf(arr[n].name, sizeof(arr[n].name),
			"%s", sqlite3_column_text(stmt, 1));
		snprintf(arr[n].created,
			sizeof(arr[n].created),
			"%s", sqlite3_column_text(stmt, 2));
		n++;
	}

	sqlite3_finalize(stmt);
	*out = arr;
	*count = n;
	return 0;
}

int
list_by_name(TaskDB *tdb, const char *name, List *out)
{
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(tdb->db,
		"SELECT id, name, created FROM lists "
		"WHERE name = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC);
	rc = sqlite3_step(stmt);
	if (rc != SQLITE_ROW) {
		sqlite3_finalize(stmt);
		return -1;
	}

	out->id = sqlite3_column_int(stmt, 0);
	snprintf(out->name, sizeof(out->name),
		"%s", sqlite3_column_text(stmt, 1));
	snprintf(out->created, sizeof(out->created),
		"%s", sqlite3_column_text(stmt, 2));
	sqlite3_finalize(stmt);
	return 0;
}

int
task_add(TaskDB *tdb, int list_id, const char *text)
{
	sqlite3_stmt *stmt;
	char date[32];
	int rc;

	now_str(date, sizeof(date));
	rc = sqlite3_prepare_v2(tdb->db,
		"INSERT INTO tasks (list_id, text, done, added)"
		" VALUES (?, ?, 0, ?);",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_int(stmt, 1, list_id);
	sqlite3_bind_text(stmt, 2, text, -1, SQLITE_STATIC);
	sqlite3_bind_text(stmt, 3, date, -1, SQLITE_STATIC);
	rc = sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	return rc == SQLITE_DONE ? 0 : -1;
}

int
task_del(TaskDB *tdb, int task_id)
{
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(tdb->db,
		"DELETE FROM tasks WHERE id = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_int(stmt, 1, task_id);
	rc = sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	return rc == SQLITE_DONE ? 0 : -1;
}

int
task_done(TaskDB *tdb, int task_id)
{
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(tdb->db,
		"UPDATE tasks SET done = 1 WHERE id = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_int(stmt, 1, task_id);
	rc = sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	return rc == SQLITE_DONE ? 0 : -1;
}

int
task_undo(TaskDB *tdb, int task_id)
{
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(tdb->db,
		"UPDATE tasks SET done = 0 WHERE id = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_int(stmt, 1, task_id);
	rc = sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	return rc == SQLITE_DONE ? 0 : -1;
}

int
task_all(TaskDB *tdb, int list_id, Task **out, int *count)
{
	sqlite3_stmt *stmt;
	int rc, n, cap;
	Task *arr;

	*out = NULL;
	*count = 0;

	rc = sqlite3_prepare_v2(tdb->db,
		"SELECT id, list_id, text, done, added "
		"FROM tasks WHERE list_id = ? "
		"ORDER BY done ASC, id ASC;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_int(stmt, 1, list_id);

	n = 0;
	cap = 32;
	arr = malloc(cap * sizeof(Task));
	if (!arr) {
		sqlite3_finalize(stmt);
		return -1;
	}

	while (sqlite3_step(stmt) == SQLITE_ROW) {
		if (n >= cap) {
			cap *= 2;
			arr = realloc(arr, cap * sizeof(Task));
			if (!arr) {
				sqlite3_finalize(stmt);
				return -1;
			}
		}
		arr[n].id = sqlite3_column_int(stmt, 0);
		arr[n].list_id = sqlite3_column_int(stmt, 1);
		snprintf(arr[n].text, sizeof(arr[n].text),
			"%s", sqlite3_column_text(stmt, 2));
		arr[n].done = sqlite3_column_int(stmt, 3);
		snprintf(arr[n].added, sizeof(arr[n].added),
			"%s", sqlite3_column_text(stmt, 4));
		n++;
	}

	sqlite3_finalize(stmt);
	*out = arr;
	*count = n;
	return 0;
}

int
task_count(TaskDB *tdb, int list_id, int *total, int *done)
{
	sqlite3_stmt *stmt;
	int rc;

	*total = 0;
	*done = 0;

	rc = sqlite3_prepare_v2(tdb->db,
		"SELECT COUNT(*), SUM(done) FROM tasks "
		"WHERE list_id = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_int(stmt, 1, list_id);
	if (sqlite3_step(stmt) == SQLITE_ROW) {
		*total = sqlite3_column_int(stmt, 0);
		*done = sqlite3_column_int(stmt, 1);
	}
	sqlite3_finalize(stmt);
	return 0;
}

int
task_get(TaskDB *tdb, int task_id, Task *out)
{
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(tdb->db,
		"SELECT id, list_id, text, done, added "
		"FROM tasks WHERE id = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_int(stmt, 1, task_id);
	rc = sqlite3_step(stmt);
	if (rc != SQLITE_ROW) {
		sqlite3_finalize(stmt);
		return -1;
	}

	out->id = sqlite3_column_int(stmt, 0);
	out->list_id = sqlite3_column_int(stmt, 1);
	snprintf(out->text, sizeof(out->text),
		"%s", sqlite3_column_text(stmt, 2));
	out->done = sqlite3_column_int(stmt, 3);
	snprintf(out->added, sizeof(out->added),
		"%s", sqlite3_column_text(stmt, 4));
	sqlite3_finalize(stmt);
	return 0;
}

int
task_update_text(TaskDB *tdb, int task_id, const char *text)
{
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(tdb->db,
		"UPDATE tasks SET text = ? WHERE id = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return -1;

	sqlite3_bind_text(stmt, 1, text, -1, SQLITE_STATIC);
	sqlite3_bind_int(stmt, 2, task_id);
	rc = sqlite3_step(stmt);
	sqlite3_finalize(stmt);
	return rc == SQLITE_DONE ? 0 : -1;
}

char *
task_list_name(TaskDB *tdb, int list_id)
{
	sqlite3_stmt *stmt;
	int rc;
	static char name[128];

	rc = sqlite3_prepare_v2(tdb->db,
		"SELECT name FROM lists WHERE id = ?;",
		-1, &stmt, NULL);
	if (rc != SQLITE_OK)
		return NULL;

	sqlite3_bind_int(stmt, 1, list_id);
	rc = sqlite3_step(stmt);
	if (rc != SQLITE_ROW) {
		sqlite3_finalize(stmt);
		return NULL;
	}

	snprintf(name, sizeof(name),
		"%s", sqlite3_column_text(stmt, 0));
	sqlite3_finalize(stmt);
	return name;
}