Get talk title and iterate through its talkers.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 9 Oct 2009 12:07:37 +0000 (09:07 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 9 Oct 2009 12:08:10 +0000 (09:08 -0300)
helper.c
helper.h
helper_test2.c [new file with mode: 0644]

index fd3a083..6834e67 100644 (file)
--- a/helper.c
+++ b/helper.c
@@ -59,6 +59,23 @@ static char * get_oneliner_field_by_id(sqlite3 *db, char *query,
        return value;
 }
 
        return value;
 }
 
+static int get_many_ids(sqlite3 *db, char *query, size_t qlen, int *ids,
+                       size_t len)
+{
+       sqlite3_stmt *stmt;
+       int r;
+       int i = 0;
+       if (!qlen)
+               qlen = strlen(query);
+       r = sqlite3_prepare_v2(db, query, qlen, &stmt, NULL);
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               if (i < len)
+                       ids[i++] = sqlite3_column_int(stmt, 0);
+       }
+       sqlite3_finalize(stmt);
+       return i;
+}
+
 char * get_talker_email(sqlite3 *db, int id)
 {
        return get_oneliner_field_by_id(db, talker_user_query, "email", id);
 char * get_talker_email(sqlite3 *db, int id)
 {
        return get_oneliner_field_by_id(db, talker_user_query, "email", id);
@@ -70,4 +87,21 @@ char * get_talker_name(sqlite3 *db, int id)
                                        id);
 }
 
                                        id);
 }
 
+static char * talk_title_query = "select %s from events_talk where id = %d;";
+char * get_talk_title(sqlite3 *db, int id)
+{
+       return get_oneliner_field_by_id(db, talk_title_query, "title", id);
+}
 
 
+static char *talk_talkers_query = "select speaker_id from "
+       "events_talk_speakers where talk_id = %d;";
+int get_talk_talkers(sqlite3 *db, int id, int *ids, int len)
+{
+       int r;
+       char *query;
+       size_t qlen;
+       qlen = asprintf(&query, talk_talkers_query, id);
+       r = get_many_ids(db, query, qlen, ids, len);
+       free(query);
+       return r;
+}
index df43f9c..a5af310 100644 (file)
--- a/helper.h
+++ b/helper.h
@@ -24,5 +24,7 @@
 
 char * get_talker_email(sqlite3 *db, int id);
 char * get_talker_name(sqlite3 *db, int id);
 
 char * get_talker_email(sqlite3 *db, int id);
 char * get_talker_name(sqlite3 *db, int id);
+char * get_talk_title(sqlite3 *db, int id);
+int get_talk_talkers(sqlite3 *db, int id, int *ids, int len);
 
 #endif
 
 #endif
diff --git a/helper_test2.c b/helper_test2.c
new file mode 100644 (file)
index 0000000..2aa1e16
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ *  Copyright (C) 2009  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include "helper.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv)
+{
+       sqlite3 *db;
+       char *dbname;
+       char *title;
+       char *email;
+       int talk_id;
+       int ids[16];
+       int nids;
+       int i;
+       if (argc < 3) {
+               fprintf(stderr, "<program> <db> <id>\n");
+               return 1;
+       }
+       dbname = argv[1];
+       talk_id = atoi(argv[2]);
+       sqlite3_open(dbname, &db);
+       title = get_talk_title(db, talk_id);
+       if (title) {
+               printf("Talk title is %s\n", title);
+               free(title);
+       } else {
+               fprintf(stderr, "Could not find talk\n");
+       }
+       nids = get_talk_talkers(db, talk_id, ids, 16);
+       for (i = 0; i < nids; i++) {
+               email = get_talker_email(db, ids[i]);
+               if (email) {
+                       printf("Talker email is %s\n", email);
+                       free(email);
+               } else {
+                       fprintf(stderr, "Could not find talker\n");
+               }
+       }
+       sqlite3_close(db);
+       return 0;
+}