Implemented helpers to get talker email and name.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 9 Oct 2009 11:31:13 +0000 (08:31 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 9 Oct 2009 11:31:13 +0000 (08:31 -0300)
helper.c [new file with mode: 0644]
helper.h [new file with mode: 0644]
helper_test.c [new file with mode: 0644]

diff --git a/helper.c b/helper.c
new file mode 100644 (file)
index 0000000..847b356
--- /dev/null
+++ b/helper.c
@@ -0,0 +1,64 @@
+/*
+ *  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.
+ */
+
+/*
+ * Somer helper functions to get some fields from the sqlite database.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sqlite3.h>
+
+static char * talker_email_query = "select email from auth_user,"
+       "events_speaker where auth_user.id = events_speaker.user_id "
+       "and events_speaker.id = %d;";
+static char * talker_name_query = "select first_name from auth_user,"
+       "events_speaker where auth_user.id = events_speaker.user_id "
+       "and events_speaker.id = %d;";
+
+static char * get_talker_userfield(sqlite3 *db, char *query, int id)
+{
+       sqlite3_stmt *stmt;
+       char * s;
+       int r;
+       char *email = NULL;
+       r = asprintf(&s, query, id);
+       r = sqlite3_prepare_v2(db, s, r, &stmt, NULL);
+       free(s);
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               if (email)
+                       free(email);
+               email = strdup(sqlite3_column_text(stmt, 0));
+       }
+       sqlite3_finalize(stmt);
+       return email;
+}
+
+char * get_talker_email(sqlite3 *db, int id)
+{
+       return get_talker_userfield(db, talker_email_query, id);
+}
+
+char * get_talker_name(sqlite3 *db, int id)
+{
+       return get_talker_userfield(db, talker_name_query, id);
+}
+
+
diff --git a/helper.h b/helper.h
new file mode 100644 (file)
index 0000000..df43f9c
--- /dev/null
+++ b/helper.h
@@ -0,0 +1,28 @@
+/*
+ *  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.
+ */
+
+
+#ifndef _HELPER_H
+#define _HELPER_H
+
+#include <sqlite3.h>
+
+char * get_talker_email(sqlite3 *db, int id);
+char * get_talker_name(sqlite3 *db, int id);
+
+#endif
diff --git a/helper_test.c b/helper_test.c
new file mode 100644 (file)
index 0000000..d29b0da
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ *  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 *email;
+       int talker_id;
+       if (argc < 3) {
+               fprintf(stderr, "<program> <db> <id>\n");
+               return 1;
+       }
+       dbname = argv[1];
+       talker_id = atoi(argv[2]);
+       sqlite3_open(dbname, &db);
+       email = get_talker_email(db, talker_id);
+       sqlite3_close(db);
+       if (email) {
+               printf("Talker email is %s\n", email);
+               free(email);
+       } else {
+               fprintf(stderr, "Could not find talker\n");
+       }
+       return 0;
+}