From 6d969d975c4482c1d5bacf6f617ae42e42642e28 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Sat, 13 Nov 2021 12:45:36 +0100 Subject: [PATCH] Added PostIDS --- BlogDB.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 8 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index 36bbf86..9c05b8b 100755 --- a/BlogDB.c +++ b/BlogDB.c @@ -24,6 +24,7 @@ static int PullAuthorData(char* strings[4], int authorID, MYSQL* con); static int ChangeAuthor(int postID, int authorID, MYSQL* con); static int TogglePost(int postID, MYSQL* con); static bool IsEnabled(int postID, MYSQL* con); + static int* ActivePostIDs(MYSQL* con); // Connects to the MariaDB database and returns the connection @@ -827,13 +828,66 @@ bool IsEnabled(int postID, MYSQL* con) return !state; } +// Returns an array of IDs for all posts, +// the last element in the array is set to be negative as to prevent going into unallocated data. +// Remember to free() the returned array +int* PostIDs(MYSQL* con) +{ + + if (con == NULL) + { + con = conn2maria(); + } + + // Query the server, if successful continue, else throw error + if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts")) + { + fprintf(stderr, "%s\n", mysql_error(con)); + mysql_close(con); + exit(1); + } + + // Get results + MYSQL_RES *result = mysql_store_result(con); + MYSQL_ROW row; + row = mysql_fetch_row(result); + + int count = atoi(row[0]); + //printf("%d\n", count); + + free(result); + + // Query the server, if successful continue, else throw error + if (mysql_query(con, "SELECT id FROM blog_posts")) + { + fprintf(stderr, "%s\n", mysql_error(con)); + mysql_close(con); + exit(1); + } + + MYSQL_RES *result2 = mysql_store_result(con); + + int *arr = (int *)malloc(sizeof(int) * (count + 1)); + //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + + for (int i = 0; i < count; ++i) { + row = mysql_fetch_row(result2); + if (row != NULL) { + arr[i] = atoi(row[0]); + } + } + + arr[count] = -1; + + return arr; + +} + // Returns an array of IDs for all the posts that have state=0, // the last element in the array is set to be negative as to prevent going into unallocated data. // Remember to free() the returned array int* ActivePostIDs(MYSQL* con) { - - if (con == NULL) { con = conn2maria(); @@ -868,7 +922,7 @@ int* ActivePostIDs(MYSQL* con) MYSQL_RES *result2 = mysql_store_result(con); int *arr = (int *)malloc(sizeof(int) * (count + 1)); - printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); + //printf("arr was allocated: %db of memory\n", sizeof(int) * (count + 1)); for (int i = 0; i < count; ++i) { row = mysql_fetch_row(result2); @@ -879,12 +933,7 @@ int* ActivePostIDs(MYSQL* con) arr[count] = -1; - //return sizeof(int) * count; - //return count; - return arr; - - } @@ -951,6 +1000,27 @@ int main(int argc, char *argv[]) // =================================== */ + // =================================== + // Example of using 'PostIDs()' + // =================================== + + /* + // Generate array of IDs + int *num = PostIDs(NULL); + + // Loop until element is negative i.e. the last + for (int i = 0; num[i] > 0; ++i) + { + // Print contents on a new line + printf("%d\n", num[i]); + } + + // Free the memory + free(num); + + // =================================== + */ + // Open the connection MYSQL *con = conn2maria(); -- 2.30.2