summaryrefslogtreecommitdiff
path: root/src/db/sqlite3/db.c
blob: 09a0bf409a3ddfd5d14e8d699f6854697acf74c8 (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
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
#include <asm-generic/errno-base.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <sys/stat.h>
#include <unistd.h>

#include "config.h"
#include "db.h"

/* return values:
     0 -> success
	 1 -> failure
*/
int create_tables(const char *path);

// callback function prototypes
int group_count_callback(void *first_arg, int argc, char **argv, char **azColName);
int entry_count_callback(void *first_arg, int argc, char **argv, char **azColName);
int load_groups_callback(void *first_arg, int argc, char **argv, char **azColName);
int load_entries_callback(void *first_arg, int argc, char **argv, char **azColName);

// globals
static int callback_index; // keep track of index when filling arrays using callback functions

int db_init() {
	int res;
	char db_path[BUF_LEN];

	// create USER_DATA_DIR if not exists
	res = mkdir(USER_DATA_DIR, S_IRWXU);
	//if(res != 0 && res != EEXIST) return 1; // TODO write a better check incase mkdir fails

	// return if db already exists
	res = access(USER_DATA_PATH, F_OK);
	if(res == 0) return 0;

	// create database with tables
	res = create_tables(USER_DATA_PATH);
	if(res != 0) return 2;

	return 0;
}

Group *db_load_groups(int *group_count) {
	Group *output;
	sqlite3 *db;
	const char *count_query = "SELECT COUNT(*) FROM 'Group'";
	const char *query = "SELECT ROWID, * FROM 'Group'";
	char *zErrMsg = NULL; // hold error from sqlite3_exec
	int res;

	res = sqlite3_open(USER_DATA_PATH, &db);
	if(res) {
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return NULL;
	}
	
	// count rows in table
	res = sqlite3_exec(db, count_query, group_count_callback, &output, &zErrMsg);
	if(res != SQLITE_OK) {
		fprintf(stderr, "SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
		return NULL;
	}

	// do select query
	callback_index = 0;
	res = sqlite3_exec(db, query, load_groups_callback, &output, &zErrMsg);
	if(res != SQLITE_OK) {
		fprintf(stderr, "SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
		return NULL;
	}
	*group_count = callback_index;

	sqlite3_close(db);
	return output;
}

Entry *db_load_entries(int *entry_count) {
	Entry *output;
	sqlite3 *db;
	const char *count_query = "SELECT COUNT(*) FROM 'Entry'";
	const char *query = "SELECT ROWID, * FROM 'Entry'";
	char *zErrMsg = NULL; // hold error from sqlite3_exec
	int res;

	res = sqlite3_open(USER_DATA_PATH, &db);
	if(res) {
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return NULL;
	}
	
	// count rows in table
	res = sqlite3_exec(db, count_query, entry_count_callback, &output, &zErrMsg);
	if(res != SQLITE_OK) {
		fprintf(stderr, "SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
		return NULL;
	}

	// do select query
	callback_index = 0;
	res = sqlite3_exec(db, query, load_entries_callback, &output, &zErrMsg);
	if(res != SQLITE_OK) {
		fprintf(stderr, "SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
		return NULL;
	}
	*entry_count = callback_index;

	sqlite3_close(db);
	return output;
}



int create_tables(const char *path) {
	sqlite3 *db;
	char queries[2][BUF_LEN]; 
	int num_queries = 2;
	char *zErrMsg = NULL; // hold error from sqlite3_exec
	int res;
	int i;

	// Group table - stores groups of entries
	// note: in sqlite, use rowid, don't create id column
	sprintf(queries[0], 
			"CREATE TABLE IF NOT EXISTS 'Group' ("
			"'Name' varchar(%d) NOT NULL, "
			"'Desc' varchar(%d) DEFAULT NULL, "
			"'Url' varchar(%d) DEFAULT NULL);",
			BUF_LEN, BUF_LEN, BUF_LEN);

	// Entry table - stores individual entries
	sprintf(queries[1],
			"CREATE TABLE IF NOT EXISTS 'Entry' ("
			"'Group_ID' int NOT NULL, " // FIXME might want foreign key constraint
			"'Due_date' date DEFAULT NULL, "
			"'Alt_due_date' varchar(%d) DEFAULT NULL, "
			"'Title' varchar(%d) NOT NULL, "
			"'Color' varchar(%d) DEFAULT NULL, "
			"'Highlight' varchar(%d) DEFAULT NULL, "
			"'Done' boolean DEFAULT NULL, "
			"'Url' varchar(%d) DEFAULT NULL);",
			BUF_LEN, BUF_LEN, BUF_LEN, BUF_LEN, BUF_LEN);

	// open the database and run each query
	res = sqlite3_open(path, &db);
	if(res) {
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return 1;
	}
	for(i = 0; i < num_queries; ++i) {
		res = sqlite3_exec(db, queries[i], NULL, NULL, &zErrMsg);
		if(res != SQLITE_OK) {
			fprintf(stderr, "SQL error: %s\n", zErrMsg);
			sqlite3_free(zErrMsg);
		}
	}

	// wrap-up
	sqlite3_close(db);
	return 0;
}

// callback functions
int group_count_callback(void *first_arg, int argc, char **argv, char **azColName) {
	Group **groups = (Group **) first_arg;
	*groups = calloc(atoi(argv[0]), sizeof(Group));

	return 0;
}

int entry_count_callback(void *first_arg, int argc, char **argv, char **azColName) {
	Entry **entries = (Entry **) first_arg;
	*entries = calloc(atoi(argv[0]), sizeof(Entry));

	return 0;
}

int load_groups_callback(void *first_arg, int argc, char **argv, char **azColName) {
	Group **groups = (Group **) first_arg;

	// check that enough arguments were passed
	if(argc < 4) {
		fprintf(stderr, "Error: not enough rows returned in 'Group' table\n");
		return 1;
	}

	group_set_id(&((*groups)[callback_index]), (argv[0] ? atoi(argv[0]) : 0));
	group_set_name(&((*groups)[callback_index]), (argv[1] ? argv[1] : ""));
	group_set_desc(&((*groups)[callback_index]), (argv[2] ? argv[2] : ""));
	group_set_url(&((*groups)[callback_index]), (argv[3] ? argv[3] : ""));

	++callback_index;
	return 0;
}

int load_entries_callback(void *first_arg, int argc, char **argv, char **azColName) {
	Entry **entries = (Entry **) first_arg;
	struct tm due_date = {0};

	// check that enough arguments were passed
	if(argc < 8) {
		fprintf(stderr, "Error: not enough rows returned in 'Entry' table\n");
		return 1;
	}

	entry_set_id(&((*entries)[callback_index]), (argv[0] ? atoi(argv[0]) : 0));
	entry_set_group_id(&((*entries)[callback_index]), (argv[1] ? atoi(argv[1]) : 0));
	if(argv[2] && strlen(argv[2])) {
		strptime(argv[2], "%F", &due_date);
		printf("DEBUG: %s\n", argv[2]);
		printf("DEBUG: %d\n", due_date.tm_year);
		entry_set_due_date(&((*entries)[callback_index]), &due_date);
	}
	else entry_set_due_date(&((*entries)[callback_index]), NULL);
	entry_set_alt_due_date(&((*entries)[callback_index]), (argv[3] ? argv[3] : ""));
	entry_set_title(&((*entries)[callback_index]), (argv[4] ? argv[4] : ""));
	entry_set_color(&((*entries)[callback_index]), (argv[5] ? argv[5] : ""));
	entry_set_highlight(&((*entries)[callback_index]), (argv[6] ? argv[6] : ""));
	entry_set_done(&((*entries)[callback_index]), (argv[7] ? argv[7] : false));
	entry_set_url(&((*entries)[callback_index]), (argv[8] ? argv[8] : ""));

	++callback_index;
	return 0;
}