Implemented helpers to get talker email and name.
[cascardo/avaliacao2008.git] / helper.c
1 /*
2  *  Copyright (C) 2009  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 /*
20  * Somer helper functions to get some fields from the sqlite database.
21  */
22
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sqlite3.h>
28
29 static char * talker_email_query = "select email from auth_user,"
30         "events_speaker where auth_user.id = events_speaker.user_id "
31         "and events_speaker.id = %d;";
32 static char * talker_name_query = "select first_name from auth_user,"
33         "events_speaker where auth_user.id = events_speaker.user_id "
34         "and events_speaker.id = %d;";
35
36 static char * get_talker_userfield(sqlite3 *db, char *query, int id)
37 {
38         sqlite3_stmt *stmt;
39         char * s;
40         int r;
41         char *email = NULL;
42         r = asprintf(&s, query, id);
43         r = sqlite3_prepare_v2(db, s, r, &stmt, NULL);
44         free(s);
45         while (sqlite3_step(stmt) == SQLITE_ROW) {
46                 if (email)
47                         free(email);
48                 email = strdup(sqlite3_column_text(stmt, 0));
49         }
50         sqlite3_finalize(stmt);
51         return email;
52 }
53
54 char * get_talker_email(sqlite3 *db, int id)
55 {
56         return get_talker_userfield(db, talker_email_query, id);
57 }
58
59 char * get_talker_name(sqlite3 *db, int id)
60 {
61         return get_talker_userfield(db, talker_name_query, id);
62 }
63
64