add: database functions and generalisation
This commit is contained in:
parent
7c6eed8dc2
commit
14c35a4227
8 changed files with 193 additions and 149 deletions
src
155
src/database.c
155
src/database.c
|
@ -121,3 +121,158 @@ database_transaction_rollback()
|
|||
sqlite3_exec(global_database, "ROLLBACK TRANSACTION;", NULL, NULL, NULL);
|
||||
in_transaction = 0;
|
||||
}
|
||||
|
||||
int
|
||||
database_helper_get_id(sqlite3_stmt *stmt)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
result = sqlite3_column_int(stmt, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERR("error selecting id from database: %s\n", sqlite3_errstr(s));
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int*
|
||||
database_helper_get_ids(sqlite3_stmt *stmt)
|
||||
{
|
||||
int *result = malloc(sizeof(int));
|
||||
int new_id;
|
||||
|
||||
int row = 0;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
new_id = sqlite3_column_int(stmt, 0);
|
||||
if(new_id != 0) // found row for other target (relay <> schedule)
|
||||
{
|
||||
row++;
|
||||
|
||||
result = (int*)realloc(result, sizeof(int) * (row + 1));
|
||||
result[row - 1] = new_id;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERR("error selecting ids from database: %s\n", sqlite3_errstr(s));
|
||||
sqlite3_finalize(stmt);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
result[row] = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char*
|
||||
database_helper_get_string(sqlite3_stmt *stmt)
|
||||
{
|
||||
char *result = NULL;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
const char *found_string = (const char *)sqlite3_column_text(stmt, 0);
|
||||
result = (char*)malloc(sizeof(char) * (strlen(found_string) + 1));
|
||||
strcpy(result, found_string);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERR("error selecting string from database: %s\n", sqlite3_errstr(s));
|
||||
sqlite3_finalize(stmt);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char**
|
||||
database_helper_get_strings(sqlite3_stmt *stmt)
|
||||
{
|
||||
char **result = malloc(sizeof(char*));
|
||||
|
||||
int row = 0;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
const char *new_string = (const char *)sqlite3_column_text(stmt, 0);
|
||||
int new_string_len = strlen(new_string);
|
||||
row++;
|
||||
|
||||
result = (char**)realloc(result, sizeof(char*) * (row + 1));
|
||||
result[row - 1] = malloc(sizeof(char) * (new_string_len + 1));
|
||||
strcpy(result[row - 1], new_string);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERR("error selecting strings from database: %s\n", sqlite3_errstr(s));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
result[row] = NULL;
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue