示例#1
0
/**
 * Transmits each row found in the database
 * @param userp is a pointer to a transmited_t * structure that must contain
 *        a comm_t * pointer and a db_t * pointer.
 * @param nb_col gives the number of columns in this row.
 * @param data contains the data of each column.
 * @param name_col contains the name of each column.
 * @returns always 0.
 */
static int transmit_callback(void *userp, int nb_col, char **data, char **name_col)
{
    transmited_t *trans = (transmited_t *) userp;
    gchar *sql_command = NULL;
    gint success = 0;

    if (trans != NULL && data != NULL && trans->comm != NULL && trans->database != NULL)
        {

            trans->comm->readbuffer = data[2];          /** data[2] is the data column in buffers table of the database */
            success = post_url(trans->comm, data[1]);   /** data[1] is the url column in buffers table of the database  */

            if (success == CURLE_OK)
                {
                    sql_begin(trans->database);

                    sql_command = g_strdup_printf("INSERT INTO transmited (buffer_id) VALUES ('%s');", data[0]);
                    exec_sql_cmd(trans->database, sql_command,  _("(%d) Error while inserting into the table 'transmited': %s\n"));
                    free_variable(sql_command);

                    sql_commit(trans->database);
                }
            /** @todo use the result of post to be able to manage errors */
        }

    return 0;
}
示例#2
0
/**
 * Insert file into cache. One should have verified that the file
 * does not already exists in the database.
 * @todo Use statements to avoid bugs when dealing with strings from user
 *       space and thus avoid sql injection a bit. See
 *       https://sqlite.org/c3ref/prepare.html
 * @param database is the structure that contains everything that is
 *        related to the database (it's connexion for instance).
 * @param meta is the file's metadata that we want to insert into the
 *        cache.
 * @param only_meta : a gboolean that when set to TRUE only meta_data will
 *        be saved and hashs data will not ! FALSE means that something
 *        went wrong with server and that all data will be cached localy.
 */
void db_save_meta_data(db_t *database, meta_data_t *meta, gboolean only_meta)
{
    guint64 cache_time = 0;
    gint result = 0;
    sqlite3_stmt *stmt = NULL;

    if (meta != NULL && database != NULL && database->stmts != NULL)
        {
            cache_time = g_get_real_time();

            /* beginning a transaction */
            sql_begin(database);

            /* Inserting the file into the files table */
            stmt = database->stmts->save_meta_stmt;
            if (stmt != NULL)
                {
                    bind_values_to_save_meta_data(database->db, stmt, meta, only_meta, cache_time);
                    result = sqlite3_step(stmt);
                    print_on_db_error(database->db, result, "sqlite3_step");
                }

            /* ending the transaction here */
            sql_commit(database);
            sqlite3_reset(stmt);
        }
}
示例#3
0
/*
** Commit the current transaction.
*/
static int conn_commit (lua_State *L) {
	conn_data *conn = getconnection (L);
	sql_commit(conn);
	if (conn->auto_commit == 0) {
		sql_begin(conn);
		lua_pushboolean (L, 1);
	} else
		lua_pushboolean (L, 0);
	return 1;
}
示例#4
0
/**
 * Deletes all transmited buffers from the buffers table in database
 * based on transmited table.
 * @param userp is a pointer to a db_t * structure
 * @param nb_col gives the number of columns in this row.
 * @param data contains the data of each column.
 * @param name_col contains the name of each column.
 * @returns always 0.
 */
static int delete_transmited_callback(void *userp, int nb_col, char **data, char **name_col)
{
    db_t *database = (db_t *) userp;
    gchar *sql_command = NULL;

    if (database != NULL && data != NULL)
        {
            sql_begin(database);
            sql_command = g_strdup_printf("DELETE FROM buffers WHERE buffer_id='%s';", data[0]);
            exec_sql_cmd(database, sql_command,  _("(%d - %d) Error while deleting from table 'buffers': %s\n"));
            sql_commit(database);
            free_variable(sql_command);
        }

    return 0;
}
示例#5
0
/**
 * Saves buffers that could not be sent to server
 * @param database is the structure that contains everything that is
 *        related to the database (it's connexion for instance).
 * @param url is the url where buffer should have been POSTed
 * @param buffer is the buffer containing data that should have been
 *        POSTed to server but couldn't.
 */
void db_save_buffer(db_t *database, gchar *url, gchar *buffer)
{
    sqlite3_stmt *stmt = NULL;
    gint result = 0;

    if (database != NULL && url != NULL && buffer != NULL)
        {
            sql_begin(database);

            stmt = database->stmts->save_buffer_stmt;
            bind_values_to_save_buffer(database->db, stmt, url, buffer);
            result = sqlite3_step(stmt);
            print_on_db_error(database->db, result, "db_save_buffer");

            sql_commit(database);
            sqlite3_reset(stmt);
        }
}