52 lines
1010 B
C
52 lines
1010 B
C
#include "db.h"
|
|
|
|
#include <ropkg/status.h>
|
|
|
|
fx_result open_database(
|
|
fx_directory *root,
|
|
const char *path,
|
|
int flags,
|
|
sqlite3 **out)
|
|
{
|
|
fx_path *abs_path = fx_path_join_list(
|
|
2,
|
|
&FX_VALUE_OBJECT(fx_directory_get_path(root)),
|
|
&FX_CSTR(path));
|
|
if (!abs_path) {
|
|
return FX_RESULT_ERR(NO_MEMORY);
|
|
}
|
|
|
|
if (fx_path_exists(abs_path) && (flags & SQLITE_OPEN_CREATE)) {
|
|
if (fx_path_is_directory(abs_path)) {
|
|
fx_path_unref(abs_path);
|
|
return FX_RESULT_ERR(IS_DIRECTORY);
|
|
}
|
|
|
|
fx_path_unlink(abs_path);
|
|
}
|
|
|
|
sqlite3 *db = NULL;
|
|
int err = sqlite3_open_v2(fx_path_get_cstr(abs_path), &db, flags, NULL);
|
|
fx_path_unref(abs_path);
|
|
|
|
if (err != SQLITE_OK) {
|
|
fx_result result = FX_RESULT_SUCCESS;
|
|
if (db) {
|
|
result = fx_error_with_string(
|
|
ROPKG_ERROR_VENDOR,
|
|
ROPKG_ERR_DB_OP_FAILED,
|
|
sqlite3_errmsg(db));
|
|
sqlite3_close(db);
|
|
} else {
|
|
result = fx_error_with_code(
|
|
ROPKG_ERROR_VENDOR,
|
|
ROPKG_ERR_NO_MEMORY);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
*out = db;
|
|
return FX_RESULT_SUCCESS;
|
|
}
|