void db_state_change(struct kore_pgsql *pgsql, void *arg) { struct connection *c = arg; printf("%p: state change on pgsql %d\n", arg, pgsql->state); switch (pgsql->state) { case KORE_PGSQL_STATE_INIT: db_init(c, pgsql); break; case KORE_PGSQL_STATE_WAIT: break; case KORE_PGSQL_STATE_COMPLETE: kore_connection_disconnect(c); break; case KORE_PGSQL_STATE_ERROR: kore_pgsql_logerror(pgsql); kore_connection_disconnect(c); break; case KORE_PGSQL_STATE_RESULT: db_results(pgsql, c); break; default: kore_pgsql_continue(pgsql); break; } }
/* * After firing off the query, we returned HTTP_STATE_RETRY (see above). * When request_db_wait() finally is called by Kore we will have results * from pgsql so we'll process them. */ int request_db_wait(struct http_request *req) { struct rstate *state = req->hdlr_extra; kore_log(LOG_NOTICE, "request_db_wait: %d", state->sql.state); /* * When we get here, our asynchronous pgsql query has * given us something, check the state to figure out what. */ switch (state->sql.state) { case KORE_PGSQL_STATE_WAIT: return (HTTP_STATUS_RETRY); case KORE_PGSQL_STATE_COMPLETE: req->fsm_state = REQ_STATE_DONE; break; case KORE_PGSQL_STATE_ERROR: req->fsm_state = REQ_STATE_ERROR; kore_pgsql_logerror(&state->sql); break; case KORE_PGSQL_STATE_RESULT: req->fsm_state = REQ_STATE_DB_READ; break; default: /* This MUST be present in order to advance the pgsql state */ kore_pgsql_continue(req, &state->sql); break; } return (HTTP_STATE_CONTINUE); }
void db_results(struct kore_pgsql *pgsql, struct connection *c) { char *name; int i, rows; rows = kore_pgsql_ntuples(pgsql); for (i = 0; i < rows; i++) { name = kore_pgsql_getvalue(pgsql, i, 0); net_send_queue(c, name, strlen(name)); } net_send_flush(c); kore_pgsql_continue(pgsql); }
/* * Called when there's an actual result to be gotten. After we handle the * entire result, we'll drop back into REQ_STATE_DB_WAIT (above) in order * to continue until the pgsql API returns KORE_PGSQL_STATE_COMPLETE. */ int request_db_read(struct http_request *req) { char *name; int i, rows; struct rstate *state = req->hdlr_extra; /* We have sql data to read! */ rows = kore_pgsql_ntuples(&state->sql); for (i = 0; i < rows; i++) { name = kore_pgsql_getvalue(&state->sql, i, 0); kore_log(LOG_NOTICE, "name: '%s'", name); } /* Continue processing our query results. */ kore_pgsql_continue(req, &state->sql); /* Back to our DB waiting state. */ req->fsm_state = REQ_STATE_DB_WAIT; return (HTTP_STATE_CONTINUE); }