Pull request: 32-clean-db-handler #34
177
BlogDB.c
177
BlogDB.c
|
|
@ -31,6 +31,7 @@ static int PostIDs(int** arr, MYSQL* con);
|
|||
static int ActivePostIDs(int** arr, MYSQL* con);
|
||||
static int AuthorIDs(int** arr, MYSQL* con);
|
||||
|
||||
|
||||
// Connects to the MariaDB database and returns the connection
|
||||
MYSQL* conn2maria()
|
||||
{
|
||||
|
|
@ -55,10 +56,7 @@ MYSQL* conn2maria()
|
|||
// Adds a new post to the database, with given values
|
||||
int AddPost(char title[], int authorId, char datePosted[], char post[], int tags[], size_t tagSize, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* query;
|
||||
if(0 > asprintf(&query,
|
||||
|
|
@ -107,10 +105,7 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags
|
|||
// Edits a post in the database, to the new given values
|
||||
int EditPost(int postID, char title[], char post[], MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* selectQuery;
|
||||
if(0 > asprintf(&selectQuery,
|
||||
|
|
@ -129,7 +124,8 @@ int EditPost(int postID, char title[], char post[], MYSQL* con)
|
|||
MYSQL_RES *result = mysql_store_result(con);
|
||||
int num_fields = mysql_num_fields(result);
|
||||
|
||||
if (result == NULL) {
|
||||
if (result == NULL)
|
||||
{
|
||||
free(selectQuery);
|
||||
exit(1);
|
||||
}
|
||||
|
|
@ -164,13 +160,8 @@ int EditPost(int postID, char title[], char post[], MYSQL* con)
|
|||
// Adds a post using file data
|
||||
int AddFilePost(int authorID, char filePath[], MYSQL* con)
|
||||
{
|
||||
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
|
||||
if (authorID <= 0) { authorID = 1; }
|
||||
if (con == NULL) con = conn2maria();
|
||||
if (authorID <= 0) authorID = 1;
|
||||
|
||||
time_t t = time(NULL);
|
||||
struct tm *tm = localtime(&t);
|
||||
|
|
@ -191,15 +182,17 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
|
|||
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEnd[1]};
|
||||
char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEnd[1]};
|
||||
|
||||
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0)) {
|
||||
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0))
|
||||
{
|
||||
printf("Start Test : '%s'\nEnd Test : '%s'\n", startTest, endTest);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Remove the <h3> tags
|
||||
for (int i = 4; i < BufferLength; ++i) {
|
||||
|
||||
if (Buffer[i + 6] == StringEnd[0]) {
|
||||
for (int i = 4; i < BufferLength; ++i)
|
||||
{
|
||||
if (Buffer[i + 6] == StringEnd[0])
|
||||
{
|
||||
Buffer[i - 4] = StringEnd[0];
|
||||
break;
|
||||
}
|
||||
|
|
@ -235,11 +228,7 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con)
|
|||
// Edits a post using file data
|
||||
int EditFilePost(char filePath[], int postID, MYSQL* con)
|
||||
{
|
||||
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
FILE* FilePointer;
|
||||
int BufferLength = 8192;
|
||||
|
|
@ -255,15 +244,17 @@ int EditFilePost(char filePath[], int postID, MYSQL* con)
|
|||
char startTest[5] = {Buffer[0], Buffer[1], Buffer[2], Buffer[3], StringEnd[1]};
|
||||
char endTest[6] = {Buffer[LineLength - 6], Buffer[LineLength - 5], Buffer[LineLength - 4], Buffer[LineLength - 3], Buffer[LineLength - 2], StringEnd[1]};
|
||||
|
||||
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0)) {
|
||||
if (!(strcmp(startTest, "<h3>") == 0 && strcmp(endTest, "</h3>") == 0))
|
||||
{
|
||||
printf("Start Test : %s\nEnd Test : %s\n", startTest, endTest);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Remove the <h3> tags
|
||||
for (int i = 4; i < BufferLength; ++i) {
|
||||
|
||||
if (Buffer[i + 6] == StringEnd[0]) {
|
||||
for (int i = 4; i < BufferLength; ++i)
|
||||
{
|
||||
if (Buffer[i + 6] == StringEnd[0])
|
||||
{
|
||||
Buffer[i - 4] = StringEnd[0];
|
||||
break;
|
||||
}
|
||||
|
|
@ -299,6 +290,8 @@ int EditFilePost(char filePath[], int postID, MYSQL* con)
|
|||
// Open a temporary file containing the standard template, and add the contents as a post
|
||||
int BlogWrite(MYSQL* con)
|
||||
{
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
FILE *fp;
|
||||
char* tmpName = tmpnam(NULL);
|
||||
|
||||
|
|
@ -320,6 +313,8 @@ int BlogWrite(MYSQL* con)
|
|||
// Open a temporary file containing the contents of the given post and update the contents as a post
|
||||
int BlogEdit(int postID, MYSQL* con)
|
||||
{
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
FILE *fp;
|
||||
char* tmpName = tmpnam(NULL);
|
||||
|
||||
|
|
@ -344,10 +339,7 @@ int BlogEdit(int postID, MYSQL* con)
|
|||
// Pulls a single post's data into the supplied array
|
||||
int PullPostData(char* strings[6], int postID, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* query;
|
||||
if(0 > asprintf(&query, "SELECT * FROM blog_posts WHERE id=(%d)", postID)) exit(1);
|
||||
|
|
@ -416,23 +408,25 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date
|
|||
*dates = (int *)malloc(sizeof(int) * (count + 1));
|
||||
*Titles = (char **)malloc((count + 1) * sizeof(char *));
|
||||
|
||||
for (int i = 0; i < count + 1; ++i) {
|
||||
for (int i = 0; i < count + 1; ++i)
|
||||
{
|
||||
Titles[i] = malloc(sizeof(char) * 255);
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
row = mysql_fetch_row(result2);
|
||||
|
||||
if (row != NULL) {
|
||||
if (row != NULL)
|
||||
{
|
||||
*(*IDs + i) = atoi(row[0]);
|
||||
*(*authors + i) = atoi(row[2]);
|
||||
*(*dates + i) = atoi(row[3]);
|
||||
|
||||
for (int y = 0; y < 255; ++y) {
|
||||
for (int y = 0; y < 255; ++y)
|
||||
{
|
||||
Titles[i][y] = row[1][y];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -451,10 +445,7 @@ int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** date
|
|||
// Adds a new tag
|
||||
int AddTag(char tagTitle[], MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* insertQuery;
|
||||
if (0 > asprintf(&insertQuery, "INSERT INTO tags (name) VALUES (\"%s\")",
|
||||
|
|
@ -490,10 +481,7 @@ int AddTag(char tagTitle[], MYSQL* con)
|
|||
// Removes a tag, and unassigns itself from any and all posts.
|
||||
int DeleteTag(int tagID, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* deleteQuery;
|
||||
if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)",
|
||||
|
|
@ -529,11 +517,7 @@ int DeleteTag(int tagID, MYSQL* con)
|
|||
// Edit the tag name
|
||||
int EditTag(int tagID, char tagTitle[], MYSQL* con)
|
||||
{
|
||||
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* selectQuery;
|
||||
if(0 > asprintf(&selectQuery,
|
||||
|
|
@ -582,10 +566,7 @@ int EditTag(int tagID, char tagTitle[], MYSQL* con)
|
|||
// Assign a post a given tag.
|
||||
int AssignTag(int tagID, int postID, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* selectQuery;
|
||||
if(0 > asprintf(&selectQuery, "SELECT * FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)",
|
||||
|
|
@ -603,7 +584,8 @@ int AssignTag(int tagID, int postID, MYSQL* con)
|
|||
MYSQL_RES *result = mysql_store_result(con);
|
||||
int num_fields = mysql_num_fields(result);
|
||||
|
||||
if (result == NULL) {
|
||||
if (result == NULL)
|
||||
{
|
||||
return postID;
|
||||
}
|
||||
|
||||
|
|
@ -638,10 +620,7 @@ int AssignTag(int tagID, int postID, MYSQL* con)
|
|||
// Unassign a tag from the given post
|
||||
int RetractTag(int tagID, int postID, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* deleteQuery;
|
||||
if (0 > asprintf(&deleteQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d) AND blog_post_id=(%d)",
|
||||
|
|
@ -664,10 +643,7 @@ int RetractTag(int tagID, int postID, MYSQL* con)
|
|||
// Add an author
|
||||
int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* insertQuery;
|
||||
|
||||
|
|
@ -706,10 +682,7 @@ int AddAuthor(char firstName[], char lastName[], char email[], MYSQL* con)
|
|||
// Pull the full name and email of the given author ID
|
||||
int PullAuthorData(char* strings[4], int authorID, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* query;
|
||||
if(0 > asprintf(&query, "SELECT * FROM people WHERE id=(%d)", authorID)) exit(1);
|
||||
|
|
@ -742,11 +715,7 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con)
|
|||
// Change the author of a given post
|
||||
int ChangeAuthor(int postID, int authorID, MYSQL* con)
|
||||
{
|
||||
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* selectQuery;
|
||||
if(0 > asprintf(&selectQuery,
|
||||
|
|
@ -796,10 +765,7 @@ int ChangeAuthor(int postID, int authorID, MYSQL* con)
|
|||
// Toggles the posts state, which determines whether the code ignores it.
|
||||
int TogglePost(int postID, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* selectQuery;
|
||||
if(0 > asprintf(&selectQuery,
|
||||
|
|
@ -820,10 +786,7 @@ int TogglePost(int postID, MYSQL* con)
|
|||
MYSQL_ROW row;
|
||||
row = mysql_fetch_row(result);
|
||||
|
||||
if (row == NULL)
|
||||
{
|
||||
return postID;
|
||||
}
|
||||
if (row == NULL) return postID;
|
||||
|
||||
int state = atoi(row[5]);
|
||||
free(selectQuery);
|
||||
|
|
@ -849,10 +812,7 @@ int TogglePost(int postID, MYSQL* con)
|
|||
// Return true if state is 0, false if it doesn't exist or 1
|
||||
bool IsEnabled(int postID, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
char* selectQuery;
|
||||
if(0 > asprintf(&selectQuery,
|
||||
|
|
@ -875,13 +835,11 @@ bool IsEnabled(int postID, MYSQL* con)
|
|||
|
||||
row = mysql_fetch_row(result);
|
||||
|
||||
if (row == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (row == NULL) return false;
|
||||
|
||||
int state = atoi(row[5]);
|
||||
free(selectQuery);
|
||||
|
||||
return !state;
|
||||
}
|
||||
|
||||
|
|
@ -889,10 +847,7 @@ bool IsEnabled(int postID, MYSQL* con)
|
|||
// the last element in the array is set to be negative as to prevent going into unallocated data.
|
||||
int PostIDs(int** arr, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts"))
|
||||
{
|
||||
|
|
@ -921,11 +876,13 @@ int PostIDs(int** arr, MYSQL* con)
|
|||
// Allocate the array that was passed by reference to be one larger than needed
|
||||
*arr = (int *)malloc(sizeof(int) * (count + 1));
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
row = mysql_fetch_row(result2);
|
||||
|
||||
if (row != NULL) {
|
||||
*(*arr + i) = atoi(row[0]);
|
||||
if (row != NULL)
|
||||
{
|
||||
*(*arr + i) = atoi(row[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -941,10 +898,7 @@ int PostIDs(int** arr, MYSQL* con)
|
|||
// the last element in the array is set to be negative as to prevent going into unallocated data.
|
||||
int ActivePostIDs(int** arr, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts WHERE state=(0)"))
|
||||
{
|
||||
|
|
@ -973,11 +927,13 @@ int ActivePostIDs(int** arr, MYSQL* con)
|
|||
// Allocate the array that was passed by reference to be one larger than needed
|
||||
*arr = (int *)malloc(sizeof(int) * (count + 1));
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
row = mysql_fetch_row(result2);
|
||||
|
||||
if (row != NULL) {
|
||||
*(*arr + i) = atoi(row[0]);
|
||||
if (row != NULL)
|
||||
{
|
||||
*(*arr + i) = atoi(row[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -993,10 +949,7 @@ int ActivePostIDs(int** arr, MYSQL* con)
|
|||
// the last element in the array is set to be negative as to prevent going into unallocated data.
|
||||
int AuthorIDs(int** arr, MYSQL* con)
|
||||
{
|
||||
if (con == NULL)
|
||||
{
|
||||
con = conn2maria();
|
||||
}
|
||||
if (con == NULL) con = conn2maria();
|
||||
|
||||
if (mysql_query(con, "SELECT COUNT(*) FROM people"))
|
||||
{
|
||||
|
|
@ -1025,11 +978,13 @@ int AuthorIDs(int** arr, MYSQL* con)
|
|||
// Allocate the array that was passed by reference to be one larger than needed
|
||||
*arr = (int *)malloc(sizeof(int) * (count + 1));
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
row = mysql_fetch_row(result2);
|
||||
|
||||
if (row != NULL) {
|
||||
*(*arr + i) = atoi(row[0]);
|
||||
if (row != NULL)
|
||||
{
|
||||
*(*arr + i) = atoi(row[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue