From 720bf822cbec71fa202c162f1fffd8d9589f5889 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Fri, 9 Oct 2009 09:07:37 -0300 Subject: [PATCH] Get talk title and iterate through its talkers. --- helper.c | 34 ++++++++++++++++++++++++++++ helper.h | 2 ++ helper_test2.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 helper_test2.c diff --git a/helper.c b/helper.c index fd3a083..6834e67 100644 --- a/helper.c +++ b/helper.c @@ -59,6 +59,23 @@ static char * get_oneliner_field_by_id(sqlite3 *db, char *query, 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); @@ -70,4 +87,21 @@ char * get_talker_name(sqlite3 *db, int 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; +} diff --git a/helper.h b/helper.h index df43f9c..a5af310 100644 --- 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_talk_title(sqlite3 *db, int id); +int get_talk_talkers(sqlite3 *db, int id, int *ids, int len); #endif diff --git a/helper_test2.c b/helper_test2.c new file mode 100644 index 0000000..2aa1e16 --- /dev/null +++ b/helper_test2.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo + * + * 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 +#include + +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, " \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; +} -- 2.20.1