Method __builtin.Sql.Connection()->promise_query()


Method promise_query

variant __experimental__ Concurrent.Future promise_query(string q, void|mapping(string|int:mixed) bindings, void|function(array, .Result, array:array) map_cb)
variant __experimental__ Concurrent.Future promise_query(string q, function(array, .Result, array:array) map_cb)

Description

Sends a typed query to the database asynchronously.

Returns

An Sql.Promise object which can be used to obtain an Sql.FutureResult object to evaluate the query.

See also

streaming_typed_query(), Sql.Promise, Sql.FutureResult

Parameter map_cb

Callback function which is called for every row returned. First parameter is the row, second parameter is the result object being processed, and the third parameter is the array of result rows already collected so far. The function should return the modified version of the row that needs to be stored, or it should return 0 to discard the row.

Example
Sql.Connection db = Sql.Connection("...");
Sql.Promise q1 = db->promise_query("SELECT 42")->max_records(10);
Sql.Promise q2 = db->promise_query("SELECT :foo::INT", (["foo":2]));

array(Concurrent.Future) all = ({ q1, q2 })->future();

// To get a callback for each of the requests

all->on_success(lambda (Sql.FutureResult resp) {
  werror("Got result %O from %O\n", resp->get(), resp->query);
});
all->on_failure(lambda (Sql.FutureResult resp) {
  werror("Request %O failed: %O\n", resp->query,
   resp->status_command_complete);
});

// To get a callback when all of the requests are done. In this case
// on_failure will be called if any of the requests fails.

Concurrent.Future all2 = Concurrent.results(all);

all2->on_success(lambda (array(Sql.FutureResult) resp) {
  werror("All requests were successful: %O\n", resp);
});
all->on_failure(lambda (Sql.FutureResult resp) {
  werror("Requests %O failed with %O.\n", resp->query,
   resp->status_command_complete);
});
Note

This is an experimental API, and is likely to be changed to return other objects in future releases of Pike.