add: database functions and generalisation
This commit is contained in:
parent
7c6eed8dc2
commit
14c35a4227
8 changed files with 193 additions and 149 deletions
src/models
|
@ -56,51 +56,6 @@ junction_tag_insert(int tag_id, int relay_id, int schedule_id)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int*
|
||||
get_ids(sqlite3_stmt *stmt)
|
||||
{
|
||||
int *ids = malloc(sizeof(int));
|
||||
int new_id;
|
||||
|
||||
int row = 0;
|
||||
|
||||
while(true)
|
||||
{
|
||||
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++;
|
||||
|
||||
ids = (int*)realloc(ids, sizeof(int) * (row + 1));
|
||||
ids[row - 1] = new_id;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERR("error selecting relays from database: %s\n", sqlite3_errstr(s));
|
||||
sqlite3_finalize(stmt);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
ids[row] = 0;
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
int*
|
||||
junction_tag_get_relays_for_tag_id(int tag_id)
|
||||
{
|
||||
|
@ -109,7 +64,7 @@ junction_tag_get_relays_for_tag_id(int tag_id)
|
|||
sqlite3_prepare_v2(global_database, "SELECT relay_id FROM junction_tag WHERE tag_id=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_int(stmt, 1, tag_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
return database_helper_get_ids(stmt);
|
||||
}
|
||||
|
||||
int*
|
||||
|
@ -120,7 +75,7 @@ junction_tag_get_schedules_for_tag_id(int tag_id)
|
|||
sqlite3_prepare_v2(global_database, "SELECT schedule_id FROM junction_tag WHERE tag_id=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_int(stmt, 1, tag_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
return database_helper_get_ids(stmt);
|
||||
}
|
||||
|
||||
int*
|
||||
|
@ -131,7 +86,7 @@ junction_tag_get_tags_for_relay_id(int relay_id)
|
|||
sqlite3_prepare_v2(global_database, "SELECT tag_id FROM junction_tag WHERE relay_id=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_int(stmt, 1, relay_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
return database_helper_get_ids(stmt);
|
||||
}
|
||||
|
||||
int*
|
||||
|
@ -142,7 +97,7 @@ junction_tag_get_tags_for_schedule_id(int schedule_id)
|
|||
sqlite3_prepare_v2(global_database, "SELECT tag_id FROM junction_tag WHERE schedule_id=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_int(stmt, 1, schedule_id);
|
||||
|
||||
return get_ids(stmt);
|
||||
return database_helper_get_ids(stmt);
|
||||
}
|
||||
|
||||
int
|
||||
|
|
|
@ -430,3 +430,14 @@ relay_get_by_controller_id(int controller_id)
|
|||
return relay_db_select(stmt);
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
relay_get_controller_id_for_relay(int relay_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sqlite3_prepare_v2(global_database, "SELECT controller_id FROM relays WHERE relay_id=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_int(stmt, 1, relay_id);
|
||||
|
||||
return database_helper_get_id(stmt);
|
||||
}
|
||||
|
|
|
@ -47,37 +47,7 @@ tag_get_tag(int id)
|
|||
sqlite3_prepare_v2(global_database, "SELECT tag FROM tags WHERE id=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
|
||||
char *result = NULL;
|
||||
|
||||
while(1)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
const char *found_tag = (const char *)sqlite3_column_text(stmt, 0);
|
||||
result = (char*)malloc(sizeof(char) * (strlen(found_tag) + 1));
|
||||
strcpy(result, found_tag);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERR("error selecting tags from database: %s\n", sqlite3_errstr(s));
|
||||
sqlite3_finalize(stmt);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return result;
|
||||
return database_helper_get_string(stmt);
|
||||
}
|
||||
|
||||
char**
|
||||
|
@ -87,41 +57,7 @@ tag_get_all()
|
|||
|
||||
sqlite3_prepare_v2(global_database, "SELECT tag FROM tags;", -1, &stmt, NULL);
|
||||
|
||||
char **all_tags = malloc(sizeof(char*));
|
||||
|
||||
int row = 0;
|
||||
|
||||
while(true)
|
||||
{
|
||||
int s;
|
||||
|
||||
s = sqlite3_step(stmt);
|
||||
if (s == SQLITE_ROW)
|
||||
{
|
||||
const char *new_tag = (const char *)sqlite3_column_text(stmt, 0);
|
||||
int new_tag_len = strlen(new_tag);
|
||||
row++;
|
||||
|
||||
all_tags = (char**)realloc(all_tags, sizeof(char*) * (row + 1));
|
||||
all_tags[row - 1] = malloc(sizeof(char) * (new_tag_len + 1));
|
||||
strcpy(all_tags[row - 1], new_tag);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(s == SQLITE_DONE)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERR("error selecting tags from database: %s\n", sqlite3_errstr(s));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
all_tags[row] = NULL;
|
||||
return all_tags;
|
||||
return database_helper_get_strings(stmt);
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -132,35 +68,7 @@ tag_get_id(const char *tag)
|
|||
sqlite3_prepare_v2(global_database, "SELECT id FROM tags WHERE tag=?1;", -1, &stmt, NULL);
|
||||
sqlite3_bind_text(stmt, 1, tag, -1, SQLITE_STATIC);
|
||||
|
||||
int result = 0;
|
||||
|
||||
while(1)
|
||||
{
|
||||
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 tags from database: %s\n", sqlite3_errstr(s));
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return result;
|
||||
return database_helper_get_id(stmt);
|
||||
}
|
||||
|
||||
int
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue