/** * Verifies if the tables are created whithin the database and creates * them if there is no tables at all. * @param database : the structure to manage database's connexion. */ static void verify_if_tables_exists(db_t *database) { char *error_message = NULL; int result = 0; int *i = NULL; /** int *i is used to count the number of row */ i = (int *) g_malloc0(sizeof(int)); *i = 0; /* Trying to get all the tables that are in the database */ result = sqlite3_exec(database->db, "SELECT * FROM sqlite_master WHERE type='table';", table_callback, i, &error_message); if (result == SQLITE_OK && *i == 0) /* No row (0) means that there is no table */ { print_debug(_("Creating tables into the database\n")); /* The database does not contain any tables. So we have to create them. */ /* Creation of buffers table that contains checksums and their associated data */ exec_sql_cmd(database, "CREATE TABLE buffers (buffer_id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT, data TEXT);", _("(%d) Error while creating database table 'buffers': %s\n")); /* Creation of transmited table that may contain id of transmited buffers if any + creation of its indexes */ exec_sql_cmd(database, "CREATE TABLE transmited (buffer_id INTEGER PRIMARY KEY);", _("(%d) Error while creating database table 'transmited': %s\n")); exec_sql_cmd(database, "CREATE INDEX main.transmited_buffer_id ON transmited (buffer_id ASC)", _("(%d) Error while creating index 'transmited_buffer_id': %s\n")); /* Creation of files table that contains everything about a file */ exec_sql_cmd(database, "CREATE TABLE files (file_id INTEGER PRIMARY KEY AUTOINCREMENT, cache_time INTEGER, type INTEGER, inode INTEGER, file_user TEXT, file_group TEXT, uid INTEGER, gid INTEGER, atime INTEGER, ctime INTEGER, mtime INTEGER, mode INTEGER, size INTEGER, name TEXT, transmitted BOOL, link TEXT);", _("(%d) Error while creating database table 'files': %s\n")); exec_sql_cmd(database, "CREATE INDEX main.files_inodes ON files (inode ASC)", _("(%d) Error while creating index 'files_inodes': %s\n")); } free_variable(i); }
/** * 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; }
/** * Insert file into cache. One should have verified that the file * does not already exists in the database. * @note insert_file_into_cache is fast but does not garantee that the * data is on the disk ! * @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) { gchar *sql_command = NULL; /** gchar *sql_command is the command to be executed */ guint64 cache_time = 0; if (meta != NULL && database != NULL) { cache_time = g_get_real_time(); /* beginning a transaction */ exec_sql_cmd(database, "BEGIN;", _("(%d) Error openning the transaction: %s\n")); /* Inserting the file into the files table */ sql_command = g_strdup_printf("INSERT INTO files (cache_time, type, inode, file_user, file_group, uid, gid, atime, ctime, mtime, mode, size, name, transmitted, link) VALUES (%" G_GUINT64_FORMAT ", %d, %" G_GUINT64_FORMAT ", '%s', '%s', %d, %d, %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %d, %" G_GUINT64_FORMAT ", '%s', %d, '%s');", cache_time, meta->file_type, meta->inode, meta->owner, meta->group, meta->uid, meta->gid, meta->atime, meta->ctime, meta->mtime, meta->mode, meta->size, meta->name, only_meta, meta->link); exec_sql_cmd(database, sql_command, _("(%d) Error while inserting into the table 'files': %s\n")); free_variable(sql_command); /* ending the transaction here */ exec_sql_cmd(database, "COMMIT;", _("(%d) Error commiting to the database: %s\n")); } }
/** * Verifies if the tables are created whithin the database and creates * them if there is no tables at all. * @param database : the structure to manage database's connexion. */ static void verify_if_tables_exists(db_t *database) { char *error_message = NULL; int result = 0; int *i = NULL; /** int *i is used to count the number of row */ i = (int *) g_malloc0(sizeof(int)); *i = 0; /* Trying to get all the tables that are in the database */ result = sqlite3_exec(database->db, "SELECT * FROM sqlite_master WHERE type='table';", table_callback, i, &error_message); if (result == SQLITE_OK && *i == 0) /* No row (0) means that there is no table */ { print_debug(_("Creating tables into the database\n")); /* The database does not contain any tables. So we have to create them. */ /* Creation of checksum table that contains checksums and their associated data */ exec_sql_cmd(database, "CREATE TABLE data (checksum TEXT PRIMARY KEY, size INTEGER, data TEXT);", _("(%d) Error while creating database table 'data': %s\n")); /* Creation of buffers table that contains checksums and their associated data */ exec_sql_cmd(database, "CREATE TABLE buffers (cache_time INTEGER, buf_order INTEGER, checksum TEXT);", _("(%d) Error while creating database table 'buffers': %s\n")); /* Creation of files table that contains everything about a file */ exec_sql_cmd(database, "CREATE TABLE files (file_id INTEGER PRIMARY KEY AUTOINCREMENT, cache_time INTEGER, type INTEGER, inode INTEGER, file_user TEXT, file_group TEXT, uid INTEGER, gid INTEGER, atime INTEGER, ctime INTEGER, mtime INTEGER, mode INTEGER, size INTEGER, name TEXT, transmitted BOOL, link TEXT);", _("(%d) Error while creating database table 'files': %s\n")); } free_variable(i); /** * We are setting the asynchronous mode of SQLITE here. Tradeoff is that any * powerloss is leading to a database corruption and data loss ! * @todo make this PRAGMA selection an option from the command line. */ /* exec_sql_cmd(database, "PRAGMA synchronous = OFF;", _("Error while trying to set asynchronous mode.\n")); */ }
/** * 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; }
/** * Checks if a table or an index exists and creates it if not. * @param database : the structure to manage database's connexion. * @param name is the name of the table or index to look for and * may be create. * @param is a gint that should be one of SQLITE_TYPE_INDEX or * SQLITE_TYPE_TABLE * @param sql_creation_cmd is the SQL command to create the table or * the index if needed. * @param err_msg is the error message to be displayed in case of an * error when trying to create the table or index. */ static void check_and_create_object(db_t *database, gchar *name, gint type, gchar *sql_creation_cmd, gchar *err_msg) { int result = 0; if (type == SQLITE_TYPE_TABLE) { result = does_table_exists(database, name); } else if (type == SQLITE_TYPE_INDEX) { result = does_index_exists(database, name); } else { result = -1; } if (result == 1) { /* table or index does not exists and we have to create it */ print_debug(_("\t-> Creating database object: %s\n"), name); exec_sql_cmd(database, sql_creation_cmd , err_msg); } }
/** * Does a commit on the database * @param database : the db_t * structure that contains the database connexion */ static void sql_begin(db_t *database) { exec_sql_cmd(database, "BEGIN;", _("(%d) Error openning the transaction: %s\n")); }
/** * Does a commit on the database * @param database : the db_t * structure that contains the database connexion */ static void sql_commit(db_t *database) { exec_sql_cmd(database, "COMMIT;", _("(%d) Error commiting to the database: %s\n")); }