/* DDS3.6: Get Electorate Code from Predefined Batch Details from v2C*/ struct predefined_batch *resolve_batch_source(PGconn *conn, unsigned int batch_number) { /* return the electorate code of the batch number */ struct predefined_batch *batch; PGresult *result; result = SQL_query(conn, "SELECT electorate_code,polling_place_code " "FROM batch WHERE number = %u;", batch_number); if (PQresultStatus(result) != PGRES_TUPLES_OK) { PQclear(result); return NULL; } if (PQntuples(result) == 0) { PQclear(result); return NULL; } batch = malloc(sizeof(struct predefined_batch)); batch->electorate_code = atoi(PQgetvalue(result,0,0)); batch->polling_place_code = atoi(PQgetvalue(result,0,1)); batch->batch_number = batch_number; /* make sure this struct knows its a singleton */ batch->next = NULL; PQclear(result); return batch; }
extern void get_active_entries(PGconn *conn, const char *electorate_name, unsigned int batch_number, unsigned int paper_index, int *active_entry1, int *active_entry2) { PGresult *result; /* Check paper exists */ result = SQL_query(conn, "SELECT entry_id1,entry_id2 FROM %s_paper " "WHERE index = %d " "AND batch_number = %u;", electorate_name, paper_index,batch_number ); if (PQntuples(result) > 0 ) { *active_entry1 = atoi(PQgetvalue(result,0,0)); *active_entry2 = atoi(PQgetvalue(result,0,1)); } else { /* paper not found */ active_entry1 = active_entry2 = 0; } PQclear(result); }
void get_prefs_for_entry(PGconn *conn, unsigned int entry_id, struct preference preferences[], char *electorate_table) /* load the entry structure pointed to by entry with preference data*/ { unsigned int i; PGresult *result; unsigned int num_rows; result = SQL_query(conn, "SELECT preference_number, " "party_index," "candidate_index " "FROM %s " "WHERE entry_id= %u " "ORDER BY preference_number;", electorate_table,entry_id); num_rows = PQntuples(result); /* build a return structure */ for (i=0; i<num_rows; i++) { preferences[i].prefnum = atoi(PQgetvalue(result,i,0)); preferences[i].group_index = atoi(PQgetvalue(result,i,1)); preferences[i].db_candidate_index = atoi(PQgetvalue(result,i,2)); } PQclear(result); }
void get_papers_for_batch(PGconn *conn, struct batch *batch, char *electorate_table_name) /* load the batch structure pointed to by batch with paper data*/ { unsigned int i; PGresult *result; unsigned int num_rows,paper_id; result = SQL_query(conn, "SELECT id, " "index, " "supervisor_tick " "FROM paper " "WHERE batch_number= %u " "ORDER BY index;", batch->b.batch_number); num_rows = PQntuples(result); /* build a return structure */ for (i=0; i<num_rows; i++) { paper_id = atoi(PQgetvalue(result, i, 0)); batch->papers[i].p.index = atoi(PQgetvalue(result,i,1)); batch->papers[i].p.supervisor_tick = (*PQgetvalue(result, i, 2) == 't')?true:false; /* entries and sub-structures inserted into batch->papers */ batch->papers[i].entries = get_entries_for_paper(conn, paper_id,electorate_table_name); } PQclear(result); }
/* DDS????: Get Ballot Contents */ static struct http_vars *get_ballot_contents(PGconn *conn,unsigned int ecode, struct http_vars *vars) { PGresult *result; unsigned int num_groups,group; result = SQL_query(conn, "SELECT party_index,count(index) FROM candidate " "WHERE electorate_code = %u " "GROUP BY party_index;",ecode); if ( (num_groups = PQntuples(result)) == 0 ) bailout("get_ballot_contents failed. " "No groups found for this electorate.\n"); vars = realloc(vars, sizeof(*vars) * (3 + 1 + num_groups + 1)); vars[3].name = strdup("num_groups"); vars[3].value = sprintf_malloc("%u", num_groups); for (group=0;group<num_groups;group++) { vars[group+4].name = sprintf_malloc("group%s",PQgetvalue( result,group,0)); vars[group+4].value = strdup(PQgetvalue(result,group,1)); } vars[group+4].name = vars[group+4].value = NULL; PQclear(result); return vars; }
/* DDS3.6: Get Entered Papers from v2B */ struct batch *get_entered_batch(PGconn *conn, unsigned int batch_number) /* Return all preferences for the given batch number */ { PGresult *result; struct batch *head=NULL, *tmp=NULL; struct predefined_batch *batch; char *electorate_name; unsigned int num_papers,electorate_code; /* get electorate code */ batch = resolve_batch_source(conn, batch_number); electorate_code = batch->electorate_code; /* get electorate name in order to access that Electorates preference table */ electorate_name = resolve_electorate_name(conn,electorate_code); num_papers = (unsigned int )SQL_singleton_int(conn, "SELECT COUNT(*) " "FROM %s_paper " "WHERE batch_number = %u;", electorate_name, batch_number); /* build the batch structure */ tmp = malloc(sizeof(*tmp) + (sizeof(tmp->papers[0]) * num_papers)); tmp->b.batch_number = batch_number; result = SQL_query(conn, "SELECT size, committed " "FROM batch " "WHERE number = %u;", batch_number); tmp->b.batch_size = atoi(PQgetvalue(result, 0, 0)); tmp->b.num_papers = num_papers; if (*PQgetvalue(result,0,1) == 't') tmp->b.committed = true; else tmp->b.committed = false; /* papers and sub-structures inserted into tmp */ if (num_papers > 0) get_papers_for_batch(conn, tmp, electorate_name); tmp->next = NULL; head = tmp; PQclear(result); free(electorate_name); return head; }
struct entry *get_entries_for_paper(PGconn *conn, unsigned int paper_id, char *electorate_name) /* load the paper structure pointed to by paper with entry data*/ { PGresult *result; struct entry *head = NULL, *tmp = NULL; unsigned int i, num_rows, entry_id; result = SQL_query(conn, "SELECT id,index," "operator_id," "num_preferences," "paper_version, preference_list " "FROM %s_entry " "WHERE paper_id= %u " "ORDER BY index DESC;", electorate_name,paper_id); num_rows = PQntuples(result); /* build a return structure */ for (i=0;i<num_rows;i++) { entry_id = atoi(PQgetvalue(result,i,0)); /* entry id */ tmp = new_entry(atoi(PQgetvalue(result,i,3)), /* num prefs */ PQgetvalue(result,i,2), /* operator */ atoi(PQgetvalue(result,i,1)), /* entry ix */ atoi(PQgetvalue(result,i,4)));/* pvn */ /* link the entries such that the last entry is at the head of the list */ tmp->next = head; head = tmp; get_prefs_for_entry(entry_id, atoi(PQgetvalue(result,i,3)),/* num_prefs */ &head->preferences[0], /* 'out' var */ PQgetvalue(result,i,5)); /* pref list */ } PQclear(result); return(head); }
/* Returns electorate code, or NULL if not found */ static struct barcode_hash_entry *get_bhash_table(PGconn *conn, const char *barcodehash) { struct barcode_hash_entry *ret=NULL; PGresult *result=NULL; result = SQL_query(conn, "SELECT electorate_code,polling_place_code, used " "FROM barcode " "WHERE hash = '%s';",barcodehash); if (PQntuples(result) >= 1) { ret = malloc(sizeof(*ret)); ret->ecode = atoi(PQgetvalue(result,0,0)); ret->ppcode = atoi(PQgetvalue(result,0,1)); /* Booleans are either "t" or "f" */ ret->used = (PQgetvalue(result,0,2)[0] == 't'); strcpy(ret->barcodehash,barcodehash); } PQclear(result); return ret; }