Merge ActivePostIDS#1 into Master #5

Merged
asdam9 merged 4 commits from ActivePostIDS#1 into master 2021-11-01 19:19:29 +00:00
2 changed files with 101 additions and 12 deletions

3
.gitignore vendored
View File

@ -1,5 +1,8 @@
# ---> C # ---> C
# Prerequisites # Prerequisites
*
!*.*
!LICENSE
*.d *.d
# Object files # Object files

110
BlogDB.c
View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <malloc.h>
static MYSQL* conn2maria(); static MYSQL* conn2maria();
static int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con); static int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con);
@ -18,6 +19,7 @@ static int PullAuthorData(char* strings[4], int authorID, MYSQL* con);
static int ChangeAuthor(int postID, int authorID, MYSQL* con); static int ChangeAuthor(int postID, int authorID, MYSQL* con);
static int TogglePost(int postID, MYSQL* con); static int TogglePost(int postID, MYSQL* con);
static bool IsEnabled(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 // Connects to the MariaDB database and returns the connection
MYSQL* conn2maria() MYSQL* conn2maria()
@ -635,8 +637,6 @@ int TogglePost(int postID, MYSQL* con)
int num_fields = mysql_num_fields(result); int num_fields = mysql_num_fields(result);
MYSQL_ROW row; MYSQL_ROW row;
int postFound = 1;
row = mysql_fetch_row(result); row = mysql_fetch_row(result);
if (row == NULL) if (row == NULL)
@ -709,14 +709,74 @@ bool IsEnabled(int postID, MYSQL* con)
return !state; return !state;
} }
// 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();
}
// Query the server, if successful continue, else throw error
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts WHERE state=(0)"))
{
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 WHERE state=(0)"))
{
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 sizeof(int) * count;
//return count;
return arr;
}
// Main // Main
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
/* /*
// ================================= // ===================================
// Example of using 'PullPostData()' // Example of using 'PullPostData()'
// ================================= // ===================================
// Init array of six strings // Init array of six strings
// Pull data of postID '1' into 'result' // Pull data of postID '1' into 'result'
@ -730,10 +790,16 @@ int main(int argc, char *argv[])
printf("%s\n", result[i]); printf("%s\n", result[i]);
} }
// ================================= // ===================================
*/ */
/* /*
// ===================================
// Example of using 'PullAuthorData()'
// ===================================
// Init array of four strings
// Pull data of authorID '1' into 'result'
char *result[4]; char *result[4];
PullAuthorData(result, 1, NULL); PullAuthorData(result, 1, NULL);
@ -743,19 +809,39 @@ int main(int argc, char *argv[])
// Print contents on a new line // Print contents on a new line
printf("%s\n", result[i]); printf("%s\n", result[i]);
} }
// ===================================
*/ */
/*
// ===================================
// Example of using 'ActivePostIDs()'
// ===================================
// Generate array of IDs
int *num = ActivePostIDs(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 // Open the connection
MYSQL *con = conn2maria(); MYSQL *con = conn2maria();
// =================================
// Example of using 'PullPostData()'
// =================================
// AddPost return the inserted rows, NULL pointer for no tags, 0 for size // AddPost return the inserted rows, NULL pointer for no tags, 0 for size
//
//int tags[] = {1, 2, 3}; //int tags[] = {1, 2, 3};
//printf("%d", AddPost("Something", 1, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags))); //printf("%d", AddPost("Something", 1, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags)));
//AssignTag(6, 42, con); //AssignTag(6, 42, con);
//EditPost(69, "HEY", "THERE BUDDY", con); //EditPost(69, "HEY", "THERE BUDDY", con);
//printf("%d\n", AddTag("Test", con)); //printf("%d\n", AddTag("Test", con));
@ -764,8 +850,8 @@ int main(int argc, char *argv[])
//EditTag(5, "Projects", con); //EditTag(5, "Projects", con);
//AddAuthor("Me", "Meson", "email@me.com", con); //AddAuthor("Me", "Meson", "email@me.com", con);
//ChangeAuthor(62, 2, con); //ChangeAuthor(62, 2, con);
//TogglePost(51, con); //TogglePost(81, con);
//printf("%s\n", IsEnabled(51, con) ? "True" : "False"); //printf("%s\n", IsEnabled(81, con) ? "True" : "False");
// ================================= // =================================