Skip to content

Commit

Permalink
Get reports page function added
Browse files Browse the repository at this point in the history
md5sum a.out
d85aca61384cf8cfa02722881b7eac64  a.out

Added function to get a full page of reports from the database. Works
well, no memory leaks or anything. With this, the model interactions are
pretty much done. There are more complicated queries that could be made
and created, such as getting comment messages associated with markers
within the same thing and returning json or something. But really, those
are a create as I need them function.

There are some json functions that need to be made, such as correctly
formatting the paginated results, or having a status or error message
response in json. The status/error functions can be implemented when I
begin work on the http layers. The Paginated bits I could do now or
later when actually dealing with them.

There's also the fact that we've added an 'addressed' field to the
markers in the main greenup application API (which is python and GAE) in
order to see if markers have been addressed or not. This is a simple
boolean that we can add in to the model. Should be interesting to see
how much actually has to change in order to support it. This will give
me an idea of how maintainable or not the current C architecture is.
  • Loading branch information
EdgeCaseBerg committed Sep 28, 2013
1 parent 5b9c564 commit ebae11c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
20 changes: 19 additions & 1 deletion green-serv.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int main(int argc, const char* argv[]) {
struct gs_heatmap testHeatmap;
struct gs_heatmap * heatmapPage;
struct gs_report testReport;
struct gs_report * reportPage;
Decimal latitude;
Decimal longitude;
Decimal lowerBoundLat;
Expand All @@ -42,6 +43,7 @@ int main(int argc, const char* argv[]) {
int numComments;
int numMarkers;
int numHeatmap;
int numReports;
int i;
bzero(auth,65);
bzero(json,512);
Expand Down Expand Up @@ -180,8 +182,24 @@ int main(int argc, const char* argv[]) {
gs_reportToJSON(testReport,json);
printf("%s\n", json);

strncpy(auth, testReport.authorize,64);


reportPage = malloc(RESULTS_PER_PAGE * sizeof(struct gs_report));
if(reportPage != NULL){

numReports = db_getReports(0, campaign.id, reportPage, conn);;
for(i=0; i < numReports; ++i){
bzero(json,512);
gs_reportToJSON(reportPage[i], json);
printf("%s\n", json);
}

free(reportPage);
}else{
fprintf(stderr, "%s\n", "Could not allocate enough memory for marker page");
}

strncpy(auth, testReport.authorize,64);
db_getReportByAuth(auth, &testReport, conn);
bzero(json,512);
gs_reportToJSON(testReport,json);
Expand Down
2 changes: 2 additions & 0 deletions headers/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,6 @@

int db_deleteReport(struct gs_report * gsr, MYSQL * conn);

int db_getReports(int page, long scopeId, struct gs_report * gsr, MYSQL * conn);

#endif
34 changes: 34 additions & 0 deletions src/database/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,38 @@ int db_deleteReport(struct gs_report * gsr, MYSQL * conn){
}

return mysql_affected_rows(conn);
}

#ifndef REPORT_PAGE_QUERY_SIZE
#define REPORT_PAGE_QUERY_SIZE 256
#endif
int db_getReports(int page, long scopeId, struct gs_report * gsr, MYSQL * conn){
MYSQL_RES * result;
MYSQL_ROW row;
int i;
char query[REPORT_PAGE_QUERY_SIZE];
bzero(query,REPORT_PAGE_QUERY_SIZE);
sprintf(query,GS_REPORT_GET_ALL,scopeId, page);

if(0 != mysql_query(conn, query) ){
fprintf(stderr, "%s\n", mysql_error(conn));
return 0;
}

i=0;
result = mysql_use_result(conn);
while( (row=mysql_fetch_row(result)) != NULL ){
/* Initialize */
gs_report_ZeroStruct(&gsr[i]);

gs_report_setId( atol(row[0]), &gsr[i]);
gs_report_setContent( row[1], &gsr[i]);
gs_report_setScopeId( row[2] == NULL ? GS_SCOPE_INVALID_ID : atol(row[2]), &gsr[i]);
strncpy(gsr[i].origin,row[3], SHA_LENGTH);
strncpy(gsr[i].authorize, row[4], SHA_LENGTH);
gs_report_setCreatedTime( row[5], &gsr[i]);
i++;
}
mysql_free_result(result);
return i;
}

0 comments on commit ebae11c

Please sign in to comment.