Added object files that should be ignored.
[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_user_query = "select %s from auth_user,"
30         "events_speaker where auth_user.id = events_speaker.user_id "
31         "and events_speaker.id = %d;";
32
33 static char * get_oneliner(sqlite3 *db, char *query, size_t qlen)
34 {
35         sqlite3_stmt *stmt;
36         int r;
37         char *value = NULL;
38         if (!qlen)
39                 qlen = strlen(query);
40         r = sqlite3_prepare_v2(db, query, qlen, &stmt, NULL);
41         while (sqlite3_step(stmt) == SQLITE_ROW) {
42                 if (value)
43                         free(value);
44                 value = strdup(sqlite3_column_text(stmt, 0));
45         }
46         sqlite3_finalize(stmt);
47         return value;
48 }
49
50 static char * get_oneliner_field_by_id(sqlite3 *db, char *query,
51                                        char *field, int id)
52 {
53         char *s;
54         int r;
55         char *value;
56         r = asprintf(&s, query, field, id);
57         value = get_oneliner(db, s, r);
58         free(s);
59         return value;
60 }
61
62 static int get_many_ids(sqlite3 *db, char *query, size_t qlen, int *ids,
63                         size_t len)
64 {
65         sqlite3_stmt *stmt;
66         int r;
67         int i = 0;
68         if (!qlen)
69                 qlen = strlen(query);
70         r = sqlite3_prepare_v2(db, query, qlen, &stmt, NULL);
71         while (sqlite3_step(stmt) == SQLITE_ROW) {
72                 if (i < len)
73                         ids[i++] = sqlite3_column_int(stmt, 0);
74         }
75         sqlite3_finalize(stmt);
76         return i;
77 }
78
79 char * get_talker_email(sqlite3 *db, int id)
80 {
81         return get_oneliner_field_by_id(db, talker_user_query, "email", id);
82 }
83
84 char * get_talker_name(sqlite3 *db, int id)
85 {
86         return get_oneliner_field_by_id(db, talker_user_query, "first_name",
87                                         id);
88 }
89
90 static char * talk_title_query = "select %s from events_talk where id = %d;";
91 char * get_talk_title(sqlite3 *db, int id)
92 {
93         return get_oneliner_field_by_id(db, talk_title_query, "title", id);
94 }
95
96 static char *talk_talkers_query = "select speaker_id from "
97         "events_talk_speakers where talk_id = %d;";
98 int get_talk_talkers(sqlite3 *db, int id, int *ids, int len)
99 {
100         int r;
101         char *query;
102         size_t qlen;
103         qlen = asprintf(&query, talk_talkers_query, id);
104         r = get_many_ids(db, query, qlen, ids, len);
105         free(query);
106         return r;
107 }