static int open_db (CcnetUserManager *manager) { CcnetDB *db = NULL; switch (ccnet_db_type(manager->session->db)) { /* To be compatible with the db file layout of 0.9.1 version, * we don't use conf-dir/ccnet.db for user and peer info, but * user conf-dir/PeerMgr/peermgr.db and conf-dir/PeerMgr/usermgr.db instead. */ case CCNET_DB_TYPE_SQLITE: db = open_sqlite_db (manager); break; case CCNET_DB_TYPE_PGSQL: case CCNET_DB_TYPE_MYSQL: db = manager->session->db; break; } if (!db) return -1; manager->priv->db = db; return check_db_table (db); }
static int open_db (CcnetGroupManager *manager) { CcnetDB *db = NULL; switch (ccnet_db_type(manager->session->db)) { case CCNET_DB_TYPE_SQLITE: db = open_sqlite_db (manager); break; case CCNET_DB_TYPE_PGSQL: case CCNET_DB_TYPE_MYSQL: db = manager->session->db; break; } if (!db) return -1; manager->priv->db = db; return check_db_table (db); }
cr_SqliteDb * cr_db_open(const char *path, cr_DatabaseType db_type, GError **err) { cr_SqliteDb *sqlitedb = NULL; int exists; sqlite3 *db = NULL; GError *tmp_err = NULL; void *statements; assert(path); assert(db_type < CR_DB_SENTINEL); assert(!err || *err == NULL); if (path[0] == '\0') { g_set_error(err, ERR_DOMAIN, CRE_BADARG, "Bad path: \"%s\"", path); return NULL; } exists = g_file_test(path, G_FILE_TEST_IS_REGULAR); if (exists) { struct stat stat_buf; if (stat(path, &stat_buf) == -1) { g_set_error(err, ERR_DOMAIN, CRE_IO, "Cannot stat %s: %s", path, g_strerror(errno)); return NULL; } if (stat_buf.st_size == 0) // File exists, but is just a placeholder created by g_mkstemp() // because --local-sqlite option was used exists = FALSE; } sqlite3_enable_shared_cache(1); db = open_sqlite_db(path, &tmp_err); if (tmp_err) { g_propagate_error(err, tmp_err); return NULL; } sqlite3_exec(db, "BEGIN", NULL, NULL, NULL); db_tweak(db, &tmp_err); if (tmp_err) { g_propagate_error(err, tmp_err); sqlite3_close(db); return NULL; } db_create_dbinfo_table(db, &tmp_err); if (tmp_err) { g_propagate_error(err, tmp_err); sqlite3_close(db); return NULL; } if (!exists) { // Do not recreate tables, indexes and triggers if db has existed. switch (db_type) { case CR_DB_PRIMARY: db_create_primary_tables(db, &tmp_err); break; case CR_DB_FILELISTS: db_create_filelists_tables(db, &tmp_err); break; case CR_DB_OTHER: db_create_other_tables(db, &tmp_err); break; default: g_critical("%s: Bad db_type", __func__); assert(0); g_set_error(err, ERR_DOMAIN, CRE_ASSERT, "Bad db type"); return NULL; } if (tmp_err) { g_propagate_error(err, tmp_err); sqlite3_close(db); return NULL; } } // Compile SQL statements switch (db_type) { case CR_DB_PRIMARY: statements = cr_db_prepare_primary_statements(db, &tmp_err); break; case CR_DB_FILELISTS: statements = cr_db_prepare_filelists_statements(db, &tmp_err); break; case CR_DB_OTHER: statements = cr_db_prepare_other_statements(db, &tmp_err); break; default: g_critical("%s: Bad db_type", __func__); assert(0); g_set_error(err, ERR_DOMAIN, CRE_ASSERT, "Bad db type"); return NULL; } if (!statements) { g_propagate_error(err, tmp_err); sqlite3_close(db); return NULL; } sqlitedb = g_new0(cr_SqliteDb, 1); sqlitedb->db = db; sqlitedb->type = db_type; switch (db_type) { case CR_DB_PRIMARY: sqlitedb->statements.pri = statements; break; case CR_DB_FILELISTS: sqlitedb->statements.fil = statements; break; case CR_DB_OTHER: sqlitedb->statements.oth = statements; break; default: g_critical("%s: Bad db_type", __func__); assert(0); g_set_error(err, ERR_DOMAIN, CRE_ASSERT, "Bad db type"); return NULL; } return sqlitedb; }