Example #1
0
File: pgsql.c Project: 2ion/kore
/* The initial state, we setup our context and fire off the pgsql query. */
int
request_perform_query(struct http_request *req)
{
	struct rstate	*state;

	/* Setup our state context. */
	state = kore_malloc(sizeof(*state));

	/* Attach the state to our request. */
	req->hdlr_extra = state;

	/* We want to move to read result after this. */
	req->fsm_state = REQ_STATE_DB_WAIT;

	/* Fire off the query. */
	if (!kore_pgsql_query(&state->sql, req, "SELECT * FROM coders")) {
		/* If the state was still INIT, we'll try again later. */
		if (state->sql.state == KORE_PGSQL_STATE_INIT) {
			req->fsm_state = REQ_STATE_QUERY;
			return (HTTP_STATE_RETRY);
		}

		/*
		 * Let the state machine continue immediately since we
		 * have an error anyway.
		 */
		return (HTTP_STATE_CONTINUE);
	}

	/* Resume state machine later when the query results start coming in. */
	return (HTTP_STATE_RETRY);
}
Example #2
0
void
db_init(struct connection *c, struct kore_pgsql *pgsql)
{
	if (!kore_pgsql_setup(pgsql, "db", KORE_PGSQL_ASYNC)) {
		if (pgsql->state == KORE_PGSQL_STATE_INIT) {
			printf("\twaiting for available pgsql connection\n");
			return;
		}

		kore_pgsql_logerror(pgsql);
		kore_connection_disconnect(c);
		return;
	}

	printf("\tgot pgsql connection\n");
	if (!kore_pgsql_query(pgsql, "SELECT * FROM coders, pg_sleep(5)")) {
		kore_pgsql_logerror(pgsql);
		kore_connection_disconnect(c);
		return;
	}
	printf("\tquery fired off!\n");
}