void handle_grep(void) { char *zErrMsg = 0; int exists = 0; char sql[512]; struct sqlite3 * seendb = get_db_connection(); sprintf( sql, "SELECT * FROM SEEN WHERE NAME = '%s';", plugin->irc->message.prefix.nickname.nickname ); sqlite3_exec(seendb,sql,db_does_name_exist, &exists , &zErrMsg); if (exists) { sprintf( sql, "UPDATE SEEN SET LASTMSG = '%s', LASTTIME = CURRENT_TIMESTAMP WHERE NAME = '%s';", plugin->irc->message.trailing, plugin->irc->message.prefix.nickname.nickname ); sqlite3_exec(seendb,sql,0,0,NULL); } else { sprintf( sql, "INSERT INTO SEEN(NAME,LASTMSG,LASTTIME) VALUES('%s','%s', CURRENT_TIMESTAMP);", plugin->irc->message.prefix.nickname.nickname, plugin->irc->message.trailing ); sqlite3_exec(seendb,sql,0,0,NULL); } close_db_connection(seendb); }
gint64 seaf_db_get_int64 (SeafDB *db, const char *sql) { gint64 ret = -1; Connection_T conn; ResultSet_T result; SeafDBRow seaf_row; conn = get_db_connection (db); if (!conn) return -1; TRY result = Connection_executeQuery (conn, "%s", sql); CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return -1; END_TRY; seaf_row.res = result; TRY if (ResultSet_next (result)) ret = seaf_db_row_get_column_int64 (&seaf_row, 0); CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return -1; END_TRY; Connection_close (conn); return ret; }
SeafDBTrans * seaf_db_begin_transaction (SeafDB *db) { Connection_T conn; SeafDBTrans *trans; trans = g_new0 (SeafDBTrans, 1); if (!trans) return NULL; conn = get_db_connection (db); if (!conn) { g_free (trans); return NULL; } trans->conn = conn; TRY Connection_beginTransaction (trans->conn); CATCH (SQLException) g_warning ("Start transaction failed: %s.\n", Exception_frame.message); Connection_close (trans->conn); g_free (trans); return NULL; END_TRY; return trans; }
gboolean ccnet_db_check_for_existence (CcnetDB *db, const char *sql) { Connection_T conn; ResultSet_T result; gboolean ret = TRUE; conn = get_db_connection (db); if (!conn) { return FALSE; } TRY result = Connection_executeQuery (conn, "%s", sql); CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return FALSE; END_TRY; TRY if (!ResultSet_next (result)) ret = FALSE; CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return FALSE; END_TRY; Connection_close (conn); return ret; }
CcnetDBStatement * ccnet_db_prepare_statement (CcnetDB *db, const char *sql) { PreparedStatement_T p; CcnetDBStatement *ret = g_new0 (CcnetDBStatement, 1); Connection_T conn = get_db_connection (db); if (!conn) { g_free (ret); return NULL; } TRY p = Connection_prepareStatement (conn, "%s", sql); ret->p = p; ret->conn = conn; RETURN (ret); CATCH (SQLException) g_warning ("Error prepare statement %s: %s.\n", sql, Exception_frame.message); g_free (ret); Connection_close (conn); return NULL; END_TRY; /* Should not be reached. */ return NULL; }
void handle_command(void) { char *zErrMsg = 0; struct seen_t seen; char sql[512]; struct sqlite3 * seendb = get_db_connection(); char response[512]; const char * who; memset(&seen, 0, sizeof(seen)); who = get_command_parameter(plugin->irc->message.trailing); if (who == NULL || strspn(who, " \t\r\n") > 0) { snprintf(response, 512, "PRIVMSG %s :Seen who?", plugin->irc->from ); plugin->send_message(plugin->irc, response); return; } if (strcmp(plugin->irc->nickname, who) == 0) { snprintf(response, 512, "PRIVMSG %s :I'm here.", plugin->irc->from ); plugin->send_message(plugin->irc, response); return; } sprintf( sql, "select a.name, a.lastmsg, datetime(a.lasttime, '+2 hours') as lasttime from seen as a where name = '%s';", who ); sqlite3_exec(seendb, sql, db_read_seen, &seen, &zErrMsg); if (strcmp(seen.lasttime, "") == 0) { snprintf(response, 512, "PRIVMSG %s :I haven't seen %s around.", plugin->irc->from, who ); plugin->send_message(plugin->irc, response); return; } snprintf(response, 512, "PRIVMSG %s :%s is last seen saying \"%s\" at %s.", plugin->irc->from, who, seen.lastmsg, seen.lasttime ); plugin->send_message(plugin->irc, response); }
static void create_tables(void) { char *zErrMsg = 0; int sqliteStatus; struct sqlite3 * seendb = get_db_connection(); char * create_script = "CREATE TABLE IF NOT EXISTS SEEN (NAME, LASTMSG, LASTTIME);"; sqliteStatus = sqlite3_exec(seendb,create_script,0,0,&zErrMsg); if (sqliteStatus != SQLITE_OK) { abort(); } close_db_connection(seendb); }
int seaf_db_query (SeafDB *db, const char *sql) { Connection_T conn = get_db_connection (db); if (!conn) return -1; /* Handle zdb "exception"s. */ TRY Connection_execute (conn, "%s", sql); Connection_close (conn); RETURN (0); CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return -1; END_TRY; /* Should not be reached. */ return 0; }
char * seaf_db_get_string (SeafDB *db, const char *sql) { char *ret = NULL; const char *s; Connection_T conn; ResultSet_T result; SeafDBRow seaf_row; conn = get_db_connection (db); if (!conn) return NULL; TRY result = Connection_executeQuery (conn, "%s", sql); CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return NULL; END_TRY; seaf_row.res = result; TRY if (ResultSet_next (result)) { s = seaf_db_row_get_column_text (&seaf_row, 0); ret = g_strdup(s); } CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return NULL; END_TRY; Connection_close (conn); return ret; }
int seaf_db_foreach_selected_row (SeafDB *db, const char *sql, SeafDBRowFunc callback, void *data) { Connection_T conn; ResultSet_T result; SeafDBRow seaf_row; int n_rows = 0; conn = get_db_connection (db); if (!conn) return -1; TRY result = Connection_executeQuery (conn, "%s", sql); CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return -1; END_TRY; seaf_row.res = result; TRY while (ResultSet_next (result)) { n_rows++; if (!callback (&seaf_row, data)) break; } CATCH (SQLException) g_warning ("Error exec query %s: %s.\n", sql, Exception_frame.message); Connection_close (conn); return -1; END_TRY; Connection_close (conn); return n_rows; }