Merge pull request 'Added PullPostDescriptions' (#28) from 12-pullpostdescription into master
Reviewed-on: #28
This commit is contained in:
commit
e6cef5384b
117
BlogDB.c
117
BlogDB.c
|
|
@ -14,6 +14,7 @@ static int EditPost(int postID, char title[], char post[], MYSQL* con);
|
|||
static int AddFilePost(int authorID, char filePath[], MYSQL* con);
|
||||
static int BlogWrite(MYSQL* con);
|
||||
static int PullPostData(char* strings[6], int postID, MYSQL* con);
|
||||
static int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, MYSQL* con);
|
||||
static int AddTag(char tagTitle[], MYSQL* con);
|
||||
static int DeleteTag(int tagID, MYSQL* con);
|
||||
static int EditTag(int tagID, char tagTitle[], MYSQL* con);
|
||||
|
|
@ -337,6 +338,91 @@ int PullPostData(char* strings[6], int postID, MYSQL* con)
|
|||
return postID;
|
||||
}
|
||||
|
||||
int PullPostDescriptions(int** IDs, char* Titles[255], int** authors, int** dates, 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);
|
||||
|
||||
// Get the Count
|
||||
int count = atoi(row[0]);
|
||||
|
||||
free(result);
|
||||
|
||||
// Query the server, if successful continue, else throw error
|
||||
if (mysql_query(con, "SELECT id,title,author_id,date_posted 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);
|
||||
|
||||
// Allocate the array that was passed by reference
|
||||
// To be one larger than I need.
|
||||
*IDs = (int *)malloc(sizeof(int) * (count + 1));
|
||||
*authors = (int *)malloc(sizeof(int) * (count + 1));
|
||||
*dates = (int *)malloc(sizeof(int) * (count + 1));
|
||||
*Titles = (char **)malloc((count + 1) * sizeof(char *));
|
||||
|
||||
for (int i = 0; i < count + 1; ++i) {
|
||||
Titles[i] = malloc(sizeof(char) * 255);
|
||||
}
|
||||
|
||||
|
||||
//*Titles = (char *)malloc(((count + 1) * 255) * sizeof(char));
|
||||
|
||||
|
||||
// loop count times.
|
||||
for (int i = 0; i < count; ++i) {
|
||||
//Get row
|
||||
row = mysql_fetch_row(result2);
|
||||
|
||||
// If the row exists (I counted them, so it must exist.)
|
||||
if (row != NULL) {
|
||||
// Set the i-th element of the array, that was passed by reference, to be the current row's ID
|
||||
//*(*arr + i) = atoi(row[0]);
|
||||
|
||||
*(*IDs + i) = atoi(row[0]);
|
||||
*(*authors + i) = atoi(row[2]);
|
||||
*(*dates + i) = atoi(row[3]);
|
||||
|
||||
for (int y = 0; y < 255; ++y) {
|
||||
Titles[i][y] = row[1][y];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Set the last element in the array passed by reference to -1,
|
||||
// As an ID can never be negative, it can ensure that it never
|
||||
// Enters unknown memory.
|
||||
//*(*arr + count) = -1;
|
||||
*(*IDs + count) = -1;
|
||||
|
||||
*(*authors + count) = -1;
|
||||
*(*dates + count) = -1;
|
||||
*(*Titles + count) = "";
|
||||
|
||||
// Return the count, for easy looping.
|
||||
return count;
|
||||
}
|
||||
|
||||
// Adds a tag
|
||||
int AddTag(char tagTitle[], MYSQL* con)
|
||||
{
|
||||
|
|
@ -1122,6 +1208,33 @@ int main(int argc, char *argv[])
|
|||
// Free the memory
|
||||
free(authorIDs);
|
||||
|
||||
// ===================================
|
||||
*/
|
||||
|
||||
/*
|
||||
// ===================================
|
||||
// Example of using 'PullPostDescriptions()'
|
||||
// ===================================
|
||||
|
||||
// Generate array of IDs
|
||||
int *IDs;
|
||||
int *authors;
|
||||
int *dates;
|
||||
char *Titles[255];
|
||||
int count4 = PullPostDescriptions(&IDs, &Titles, &authors, &dates, NULL);
|
||||
|
||||
// Loop until element is negative i.e. the last
|
||||
for (int i = 0; i < count4; ++i)
|
||||
{
|
||||
// Print contents on a new line
|
||||
printf("%d %s %d %d\n", IDs[i], Titles[i], authors[i], dates[i]);
|
||||
}
|
||||
|
||||
// Free the memory
|
||||
free(IDs);
|
||||
free(authors);
|
||||
free(dates);
|
||||
|
||||
// ===================================
|
||||
*/
|
||||
|
||||
|
|
@ -1131,7 +1244,7 @@ int main(int argc, char *argv[])
|
|||
// AddPost return the inserted rows, NULL pointer for no tags, 0 for size
|
||||
//
|
||||
//int tags[] = {1, 2, 3};
|
||||
//printf("%d", AddPost("Something", 1, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags)));
|
||||
//printf("%d", AddPost("Something", 2, "2020-01-01", "Ssdjshdjahdkjomething", tags, sizeof(tags), con));
|
||||
|
||||
//AssignTag(6, 42, con);
|
||||
//EditPost(69, "HEY", "THERE BUDDY", con);
|
||||
|
|
@ -1140,7 +1253,7 @@ int main(int argc, char *argv[])
|
|||
//RetractTag(1, 1, con);
|
||||
//EditTag(5, "Projects", con);
|
||||
//AddAuthor("Me", "Meson", "email@me.com", con);
|
||||
//ChangeAuthor(62, 2, con);
|
||||
//ChangeAuthor(93, 1, con);
|
||||
//TogglePost(81, con);
|
||||
//printf("%s\n", IsEnabled(81, con) ? "True" : "False");
|
||||
//AddFilePost(1, "/home/adam/Documents/html/linuxenv.html", con);
|
||||
|
|
|
|||
Loading…
Reference in New Issue