Merge ActivePostIDS#1 into Master #5

Merged
asdam9 merged 4 commits from ActivePostIDS#1 into master 2021-11-01 19:19:29 +00:00
3 changed files with 52 additions and 29 deletions
Showing only changes of commit a835b484cd - Show all commits

2
.gitignore vendored
View File

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

BIN
BlogDB

Binary file not shown.

View File

@ -19,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()
@ -636,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)
@ -710,7 +709,10 @@ bool IsEnabled(int postID, MYSQL* con)
return !state; return !state;
} }
void *ActivePostIDs(MYSQL* con) // 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)
{ {
@ -729,17 +731,13 @@ void *ActivePostIDs(MYSQL* con)
// Get results // Get results
MYSQL_RES *result = mysql_store_result(con); MYSQL_RES *result = mysql_store_result(con);
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);
int count = atoi(row[0]); int count = atoi(row[0]);
//printf("%d\n", count);
printf("%d\n", count); free(result);
// Query the server, if successful continue, else throw error // Query the server, if successful continue, else throw error
if (mysql_query(con, "SELECT id FROM blog_posts WHERE state=(0)")) if (mysql_query(con, "SELECT id FROM blog_posts WHERE state=(0)"))
@ -749,17 +747,20 @@ void *ActivePostIDs(MYSQL* con)
exit(1); exit(1);
} }
free(result);
MYSQL_RES *result2 = mysql_store_result(con); MYSQL_RES *result2 = mysql_store_result(con);
int *arr = (int *)realloc(arr, sizeof(int) * count); 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) { for (int i = 0; i < count; ++i) {
row = mysql_fetch_row(result2); row = mysql_fetch_row(result2);
arr[i] = atoi(row[0]); if (row != NULL) {
arr[i] = atoi(row[0]);
}
} }
arr[count] = -1;
//return sizeof(int) * count; //return sizeof(int) * count;
//return count; //return count;
@ -773,9 +774,9 @@ void *ActivePostIDs(MYSQL* con)
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'
@ -789,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);
@ -802,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));
@ -823,14 +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");
int *num;
ActivePostIDs(con);
// ================================= // =================================