aboutsummaryrefslogtreecommitdiff
path: root/db.c
blob: fcb742d342711c46aa980b2ee8d282d2585f775f (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
#include <err.h>
#include <stdlib.h>
#include <sqlite3.h>
#include "db.h"

struct user *
getuser(int id)
{
	sqlite3_stmt *stmt;
	struct user *user;

	if(sqlite3_prepare(db,
	    "SELECT name, hash FROM users WHERE oid = ?",
	    -1, &stmt, 0) != SQLITE_OK)
		goto null;
	
	if(sqlite3_bind_int(stmt, 1, id) != SQLITE_OK)
		goto null;

	if(sqlite3_step(stmt) != SQLITE_ROW)
		goto null;
	
	if(!(user = malloc(sizeof(struct user))))
		err(1, "malloc");
	
	user->id = id;
	user->name = sqlite3_column_text(stmt, 0);
	user->hash = sqlite3_column_text(stmt, 1);
	
	sqlite3_finalize(stmt);
	return user;

null:
	sqlite3_finalize(stmt);
	return NULL;
}