From dfe3466ea58a073b79de7864fdf8a1fc9aa56405 Mon Sep 17 00:00:00 2001 From: Adam Fordsmand Date: Sat, 16 Apr 2022 15:26:54 +0200 Subject: [PATCH] Fixed Consistency --- BlogDB.c | 89 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/BlogDB.c b/BlogDB.c index d1d94c6..a371103 100644 --- a/BlogDB.c +++ b/BlogDB.c @@ -58,8 +58,8 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags { if (con == NULL) con = conn2maria(); - char* query; - if(0 > asprintf(&query, + char* insertQuery; + if(0 > asprintf(&insertQuery, "INSERT INTO blog_posts (title,author_id,date_posted,post,state) VALUES (\"%s\", \"%d\", \"%s\", \"%s\", \"0\")", title, authorId, @@ -67,14 +67,14 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags post )) exit(1); - if (mysql_query(con, query)) + if (mysql_query(con, insertQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(query); + free(insertQuery); if (mysql_query(con, "SELECT LAST_INSERT_ID()")) { @@ -83,10 +83,8 @@ int AddPost(char title[], int authorId, char datePosted[], char post[], int tags exit(1); } - MYSQL_RES *result = mysql_store_result(con); - int num_fields = mysql_num_fields(result); - MYSQL_ROW row; + MYSQL_RES *result = mysql_store_result(con); row = mysql_fetch_row(result); int postID = atoi(row[0]); @@ -138,7 +136,8 @@ int EditPost(int postID, char title[], char post[], MYSQL* con) free(selectQuery); char* updateQuery; - if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET title=(\"%s\"), post=(\"%s\") WHERE id=(%d)", + if (0 > asprintf(&updateQuery, + "UPDATE blog_posts SET title=(\"%s\"), post=(\"%s\") WHERE id=(%d)", title, post, postID @@ -173,7 +172,6 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) char StringEndIndicator[] = "\0"; FilePointer = fopen(filePath, "r"); - fgets(Buffer, BufferLength, FilePointer); // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-5:-1] @@ -188,29 +186,29 @@ int AddFilePost(int authorID, char filePath[], MYSQL* con) } Buffer[LineLength - 6] = StringEndIndicator[0]; - char* trimBuffer; - trimBuffer = &Buffer[4]; + char* trimmedBufferTitle; + trimmedBufferTitle = &Buffer[4]; // Add a new post with correct title, author and date but with an empty post. - int postID = AddPost(trimBuffer, authorID, date, "", NULL, 0, con); + int postID = AddPost(trimmedBufferTitle, authorID, date, "", NULL, 0, con); while (fgets(Buffer, BufferLength, FilePointer)) { - char* query; - if(0 > asprintf(&query, + char* concatQuery; + if(0 > asprintf(&concatQuery, "UPDATE blog_posts SET post = CONCAT(post, \"%s\n\") WHERE id=(%d)", Buffer, postID )) exit(1); - if (mysql_query(con, query)) + if (mysql_query(con, concatQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(query); + free(concatQuery); } return postID; @@ -227,7 +225,6 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) char StringEndIndicator[] = "\0"; FilePointer = fopen(filePath, "r"); - fgets(Buffer, BufferLength, FilePointer); // Get the substrings startTest = Buffer[0:3] and endTest = Buffer[-6:-1] @@ -243,29 +240,29 @@ int EditFilePost(char filePath[], int postID, MYSQL* con) Buffer[LineLength - 6] = StringEndIndicator[0]; - char* Title; - Title = &Buffer[4]; + char* trimmedBufferTitle; + trimmedBufferTitle = &Buffer[4]; // Edits post with correct title and empties post. - EditPost(postID, Title, "", con); + EditPost(postID, trimmedBufferTitle, "", con); while (fgets(Buffer, BufferLength, FilePointer)) { - char* query; - if(0 > asprintf(&query, + char* concatQuery; + if(0 > asprintf(&concatQuery, "UPDATE blog_posts SET post = CONCAT(post, \"%s\") WHERE id=(%d)", Buffer, postID )) exit(1); - if (mysql_query(con, query)) + if (mysql_query(con, concatQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(query); + free(concatQuery); } return postID; @@ -284,7 +281,10 @@ int BlogWrite(MYSQL* con) fclose(fp); char* command; - if(0 > asprintf(&command, "${EDITOR:-'vi'} %s", tmpName)) exit(1); + if(0 > asprintf(&command, + "${EDITOR:-'vi'} %s", + tmpName + )) exit(1); system(command); int postID = AddFilePost(1, tmpName, con); @@ -354,10 +354,7 @@ int PullPostData(char* strings[6], int postID, MYSQL* con) // Pull all coloums of all posts except for the post coloumn int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con) { - if (con == NULL) - { - con = conn2maria(); - } + if (con == NULL) con = conn2maria(); if (mysql_query(con, "SELECT COUNT(*) FROM blog_posts")) { @@ -463,33 +460,33 @@ int DeleteTag(int tagID, MYSQL* con) { if (con == NULL) con = conn2maria(); - char* deleteQuery; - if (0 > asprintf(&deleteQuery, "DELETE FROM tags WHERE id=(%d)", + char* deleteTagQuery; + if (0 > asprintf(&deleteTagQuery, "DELETE FROM tags WHERE id=(%d)", tagID )) exit(1); - if (mysql_query(con, deleteQuery)) + if (mysql_query(con, deleteTagQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(deleteQuery); + free(deleteTagQuery); - char* deleteTagsQuery; - if (0 > asprintf(&deleteTagsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)", + char* deleteTagAssignmentsQuery; + if (0 > asprintf(&deleteTagAssignmentsQuery, "DELETE FROM blog_post_tags WHERE tag_id=(%d)", tagID )) exit(1); - if (mysql_query(con, deleteTagsQuery)) + if (mysql_query(con, deleteTagAssignmentsQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(deleteTagsQuery); + free(deleteTagAssignmentsQuery); return tagID; } @@ -663,17 +660,20 @@ int PullAuthorData(char* strings[4], int authorID, MYSQL* con) { if (con == NULL) con = conn2maria(); - char* query; - if(0 > asprintf(&query, "SELECT * FROM people WHERE id=(%d)", authorID)) exit(1); + char* selectQuery; + if(0 > asprintf(&selectQuery, + "SELECT * FROM people WHERE id=(%d)", + authorID + )) exit(1); - if (mysql_query(con, query)) + if (mysql_query(con, selectQuery)) { fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); exit(1); } - free(query); + free(selectQuery); MYSQL_RES *result = mysql_store_result(con); int num_fields = mysql_num_fields(result); @@ -722,8 +722,8 @@ int ChangeAuthor(int postID, int authorID, MYSQL* con) free(selectQuery); char* updateQuery; - - if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET author_id=(%d) WHERE id=(%d)", + if (0 > asprintf(&updateQuery, + "UPDATE blog_posts SET author_id=(%d) WHERE id=(%d)", authorID, postID )) exit(1); @@ -770,7 +770,8 @@ int TogglePost(int postID, MYSQL* con) free(selectQuery); char* updateQuery; - if (0 > asprintf(&updateQuery, "UPDATE blog_posts SET state=(%d) WHERE id=(%d)", + if (0 > asprintf(&updateQuery, + "UPDATE blog_posts SET state=(%d) WHERE id=(%d)", !state, postID )) exit(1);