aboutsummaryrefslogtreecommitdiff
path: root/db.c
blob: f4517f142f2195817d6fc72d1536a6380e3eec5f (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
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
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sqlite3.h>
#include "db.h"
#include "schema.sql.h"


#define SQLITE(func,...) do{if(sqlite3_##func(__VA_ARGS__)!=SQLITE_OK){die_sqlite("sqlite3_" #func);}}while(0)


sqlite3 *database=NULL;

__attribute__((noreturn))
static void die_sqlite(const char *func){
	die("%s: %s",func,sqlite3_errmsg(database));
}


void db_init(void){
	SQLITE(config,SQLITE_CONFIG_SERIALIZED);
	SQLITE(initialize);
	SQLITE(open_v2,"db.db",&database,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,NULL);
	char *str=malloc(schema_sql_len+1,char);
	memcpy(str,schema_sql,schema_sql_len);
	str[schema_sql_len]='\0';
	sqlite3_exec(database,str,NULL,NULL,NULL);
	free(str);
}

void db_close(void){
	sqlite3_close(database);
	SQLITE(shutdown);
	database=NULL;
}


static char* gen_room_name(void){
	const int name_len=8;
	const char *alphabet="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	const int alpha_len=strlen(alphabet);

	char *name=malloc(name_len+1,char);
	for(int i=0;i<name_len;i++){
		name[i]=alphabet[random()%alpha_len];
	}
	name[name_len]='\0';
	debug("Generated name: %s",name);
	return name;
}

struct db_name_id db_create_room(void){
	char *name=gen_room_name();

	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"insert into Rooms (name) values (?)",-1,&stmt,NULL);
	SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC);
	bool success=sqlite3_step(stmt)==SQLITE_DONE;
	SQLITE(finalize,stmt);

	i64 userid=sqlite3_last_insert_rowid(database);

	if(!success){
		free(name);
		return (struct db_name_id){NULL,-1};
	}

	return (struct db_name_id){name,userid};
}

bool db_add_member(i64 roomid,i64 userid){
	assert(roomid!=-1&&userid!=-1);
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"insert into Members (room, user) values (?, ?)",-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,roomid);
	SQLITE(bind_int64,stmt,2,userid);
	bool success=sqlite3_step(stmt)==SQLITE_DONE;
	SQLITE(finalize,stmt);
	return success;
}

bool db_is_member(i64 roomid,i64 userid){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"select 1 from Members where room = ? and user = ?",-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,roomid);
	SQLITE(bind_int64,stmt,2,userid);
	bool success=sqlite3_step(stmt)==SQLITE_ROW;
	SQLITE(finalize,stmt);
	return success;
}

struct db_user_list db_list_members(i64 roomid){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,
			"select U.id, U.name "
			"from Users as U, Members as M "
			"where M.room = ? and M.user = U.id"
		,-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,roomid);

	struct db_user_list ul;
	i64 cap=4;
	ul.count=0;
	ul.list=malloc(cap,struct db_name_id);

	int ret;
	while((ret=sqlite3_step(stmt))==SQLITE_ROW){
		if(ul.count==cap){
			cap*=2;
			ul.list=realloc(ul.list,cap,struct db_name_id);
		}
		ul.list[ul.count].id=sqlite3_column_int64(stmt,0);
		ul.list[ul.count].name=strdup((const char*)sqlite3_column_text(stmt,1));
		ul.count++;
	}

	if(ret!=SQLITE_DONE)die_sqlite("sqlite3_step");
	SQLITE(finalize,stmt);

	return ul;
}

i64 db_find_room(const char *name){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"select id from Rooms where name = ?",-1,&stmt,NULL);
	SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC);
	i64 roomid=-1;
	if(sqlite3_step(stmt)==SQLITE_ROW){
		roomid=sqlite3_column_int64(stmt,0);
	}
	SQLITE(finalize,stmt);
	return roomid;
}

char* db_get_roomname(i64 roomid){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"select name from Rooms where id = ?",-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,roomid);
	const unsigned char *name_sq=NULL;
	if(sqlite3_step(stmt)==SQLITE_ROW){
		name_sq=sqlite3_column_text(stmt,0);
	}
	char *name=NULL;
	if(name_sq)name=strdup((const char*)name_sq);
	SQLITE(finalize,stmt);
	return name;
}

struct db_room_list db_list_rooms(i64 userid){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,
			"select M.room, R.name "
			"from Members as M, Rooms as R "
			"where M.user = ? and M.room = R.id"
		,-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,userid);

	struct db_room_list rl;
	i64 cap=4;
	rl.count=0;
	rl.list=malloc(cap,struct db_name_id);

	int ret;
	while((ret=sqlite3_step(stmt))==SQLITE_ROW){
		if(rl.count==cap){
			cap*=2;
			rl.list=realloc(rl.list,cap,struct db_name_id);
		}
		rl.list[rl.count].id=sqlite3_column_int64(stmt,0);
		rl.list[rl.count].name=strdup((const char*)sqlite3_column_text(stmt,1));
		rl.count++;
	}

	if(ret!=SQLITE_DONE)die_sqlite("sqlite3_step");
	SQLITE(finalize,stmt);

	return rl;
}


i64 db_create_user(const char *name,const char *pass){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"insert into Users (name, pass) values (?, ?)",-1,&stmt,NULL);
	SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC);
	SQLITE(bind_text,stmt,2,pass,-1,SQLITE_STATIC);
	bool success=sqlite3_step(stmt)==SQLITE_DONE;
	SQLITE(finalize,stmt);
	if(success){
		return sqlite3_last_insert_rowid(database);
	} else {
		return -1;
	}
}

char* db_get_username(i64 userid){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"select name from Users where id = ?",-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,userid);
	const unsigned char *name_sq=NULL;
	if(sqlite3_step(stmt)==SQLITE_ROW){
		name_sq=sqlite3_column_text(stmt,0);
	}
	char *name=NULL;
	if(name_sq)name=strdup((const char*)name_sq);
	SQLITE(finalize,stmt);
	return name;
}

char* db_get_pass(i64 userid){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"select pass from Users where id = ?",-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,userid);
	const unsigned char *pass_sq=NULL;
	if(sqlite3_step(stmt)==SQLITE_ROW){
		pass_sq=sqlite3_column_text(stmt,0);
	}
	char *pass=NULL;
	if(pass_sq)pass=strdup((const char*)pass_sq);
	SQLITE(finalize,stmt);
	return pass;
}

i64 db_find_user(const char *name){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"select id from Users where name = ?",-1,&stmt,NULL);
	SQLITE(bind_text,stmt,1,name,-1,SQLITE_STATIC);
	i64 userid=-1;
	if(sqlite3_step(stmt)==SQLITE_ROW){
		userid=sqlite3_column_int64(stmt,0);
	}
	SQLITE(finalize,stmt);
	return userid;
}

struct db_strings_list db_user_tokens(i64 userid){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"select token from Firebase where user = ?",-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,userid);

	struct db_strings_list sl;
	i64 cap=4;
	sl.count=0;
	sl.list=malloc(cap,char*);

	int ret;
	while((ret=sqlite3_step(stmt))==SQLITE_ROW){
		if(sl.count==cap){
			cap*=2;
			sl.list=realloc(sl.list,cap,char*);
		}
		sl.list[sl.count]=strdup((const char*)sqlite3_column_text(stmt,0));
		sl.count++;
	}

	if(ret!=SQLITE_DONE)die_sqlite("sqlite3_step");
	SQLITE(finalize,stmt);

	return sl;
}

bool db_add_token(i64 userid,const char *token){
	assert(userid!=-1);

	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"delete from Firebase where token = ?",-1,&stmt,NULL);
	SQLITE(bind_text,stmt,1,token,-1,SQLITE_STATIC);
	bool success=sqlite3_step(stmt)==SQLITE_DONE;
	SQLITE(finalize,stmt);
	if(!success)return false;

	SQLITE(prepare_v2,database,"insert into Firebase (user, token) values (?, ?)",-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,userid);
	SQLITE(bind_text,stmt,2,token,-1,SQLITE_STATIC);
	success=sqlite3_step(stmt)==SQLITE_DONE;
	SQLITE(finalize,stmt);
	return success;
}

bool db_delete_token(i64 userid,const char *token){
	assert(userid!=-1);
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,"delete from Firebase where user = ? and token = ?",-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,userid);
	SQLITE(bind_text,stmt,2,token,-1,SQLITE_STATIC);
	bool success=sqlite3_step(stmt)==SQLITE_DONE;
	SQLITE(finalize,stmt);
	return success;
}


void db_create_message(i64 roomid,i64 userid,i64 timestamp,const char *message){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,
			"insert into Messages (room, user, time, message) "
			"values (?, ?, ?, ?)"
		,-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,roomid);
	SQLITE(bind_int64,stmt,2,userid);
	SQLITE(bind_int64,stmt,3,timestamp);
	SQLITE(bind_blob,stmt,4,message,strlen(message),SQLITE_STATIC);
	if(sqlite3_step(stmt)!=SQLITE_DONE)die_sqlite("sqlite3_step");
	SQLITE(finalize,stmt);
}

struct db_message_list db_get_messages(i64 roomid,i64 count){
	sqlite3_stmt *stmt;
	SQLITE(prepare_v2,database,
			"select user, time, message "
			"from Messages "
			"where room = ? "
			"order by time desc "
			"limit ?"
		,-1,&stmt,NULL);
	SQLITE(bind_int64,stmt,1,roomid);
	SQLITE(bind_int64,stmt,2,count);

	struct db_message_list ml;
	i64 cap=count;
	ml.count=0;
	ml.list=malloc(cap,struct db_message);

	int ret;
	while((ret=sqlite3_step(stmt))==SQLITE_ROW){
		if(ml.count==cap){
			die("sqlite gave too many rows while 'limit %" PRIi64 "' was present",count);
		}
		ml.list[ml.count].roomid=roomid;
		ml.list[ml.count].userid=sqlite3_column_int64(stmt,0);
		ml.list[ml.count].timestamp=sqlite3_column_int64(stmt,1);
		ml.list[ml.count].message=strdup((const char*)sqlite3_column_text(stmt,2));
		ml.count++;
	}

	if(ret!=SQLITE_DONE)die_sqlite("sqlite3_step");
	SQLITE(finalize,stmt);

	return ml;
}


void db_nullify_name_id(struct db_name_id ni){
	if(ni.name)free(ni.name);
}

void db_nullify_room_list(struct db_room_list rl){
	for(i64 i=0;i<rl.count;i++){
		free(rl.list[i].name);
	}
	if(rl.list)free(rl.list);
	rl.list=NULL;
}

void db_nullify_user_list(struct db_user_list ul){
	for(i64 i=0;i<ul.count;i++){
		free(ul.list[i].name);
	}
	if(ul.list)free(ul.list);
	ul.list=NULL;
}

void db_nullify_message_list(struct db_message_list ml){
	for(i64 i=0;i<ml.count;i++){
		free(ml.list[i].message);
	}
	if(ml.list)free(ml.list);
	ml.list=NULL;
}

void db_nullify_strings_list(struct db_strings_list sl){
	for(i64 i=0;i<sl.count;i++){
		free(sl.list[i]);
	}
	if(sl.list)free(sl.list);
	sl.list=NULL;
}