Store DB name in driver

This commit is contained in:
Alexey Romanov
2014-10-13 10:18:49 +04:00
parent 93b50dc058
commit 35554f7cb1
2 changed files with 9 additions and 6 deletions

View File

@@ -129,6 +129,8 @@ static ErlDrvData start(ErlDrvPort port, char* cmd) {
struct sqlite3 *db = NULL; struct sqlite3 *db = NULL;
int status = 0; int status = 0;
char *db_name = strstr(cmd, " "); char *db_name = strstr(cmd, " ");
int db_name_len;
char *db_name_copy;
#ifdef DEBUG #ifdef DEBUG
errno_t file_open_errno; errno_t file_open_errno;
@@ -176,10 +178,14 @@ static ErlDrvData start(ErlDrvPort port, char* cmd) {
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma warning(default: 4306) #pragma warning(default: 4306)
#endif #endif
db_name_len = strlen(db_name) + 1; // include terminator
db_name_copy = driver_alloc(sizeof(char) * db_name_len);
strcpy(db_name_copy, db_name);
// Set the state for the driver // Set the state for the driver
drv->port = port; drv->port = port;
drv->db = db; drv->db = db;
drv->db_name = db_name_copy;
drv->key = 42; drv->key = 42;
// FIXME Any way to get canonical path to the DB? // FIXME Any way to get canonical path to the DB?
// We need to ensure equal keys for different paths to the same file // We need to ensure equal keys for different paths to the same file
@@ -216,18 +222,14 @@ static void stop(ErlDrvData handle) {
close_result = sqlite3_close(drv->db); close_result = sqlite3_close(drv->db);
if (close_result != SQLITE_OK) { if (close_result != SQLITE_OK) {
#if SQLITE_VERSION_NUMBER >= 3007010 LOG_ERROR("Failed to close DB %s, some resources aren't finalized!", drv->db_name);
LOG_ERROR("Failed to close DB %s, some resources aren't finalized!", sqlite3_db_filename(drv->db, "main"));
#else
// TODO store DB name in driver?
LOG_ERROR("Failed to close DB, some resources aren't finalized!", "");
#endif
} }
if (drv->log && (drv->log != stderr)) { if (drv->log && (drv->log != stderr)) {
fclose(drv->log); fclose(drv->log);
} }
driver_free(drv->db_name);
driver_free(drv); driver_free(drv);
} }

View File

@@ -64,6 +64,7 @@ typedef struct sqlite3_drv_t {
ErlDrvPort port; ErlDrvPort port;
unsigned int key; unsigned int key;
struct sqlite3 *db; struct sqlite3 *db;
char* db_name;
long async_handle; long async_handle;
FILE *log; FILE *log;
sqlite3_stmt **prepared_stmts; sqlite3_stmt **prepared_stmts;