From: Thadeu Lima de Souza Cascardo Date: Fri, 9 Oct 2009 11:31:13 +0000 (-0300) Subject: Implemented helpers to get talker email and name. X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Favaliacao2008.git;a=commitdiff_plain;h=a7aef8cda99d1c4ed45d25f45f4d81dc23e4bf77 Implemented helpers to get talker email and name. --- diff --git a/helper.c b/helper.c new file mode 100644 index 0000000..847b356 --- /dev/null +++ b/helper.c @@ -0,0 +1,64 @@ +/* + * 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. + */ + +/* + * Somer helper functions to get some fields from the sqlite database. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include + +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 index 0000000..df43f9c --- /dev/null +++ b/helper.h @@ -0,0 +1,28 @@ +/* + * 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. + */ + + +#ifndef _HELPER_H +#define _HELPER_H + +#include + +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 index 0000000..d29b0da --- /dev/null +++ b/helper_test.c @@ -0,0 +1,46 @@ +/* + * 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 *email; + int talker_id; + if (argc < 3) { + fprintf(stderr, " \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; +}