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

class __builtin.Sql.Connection

Method promise_query

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

Description

Sends a typed query to the database asynchronously.

Returns

A Concurrent.Future carrying a buffered Sql.Result. A failed query rejects the future with an error. The result cursor starts at the first row. Use res_obj_to_array() to convert the result to labeled rows.

Parameter bindings

Bindings for the query.

Parameter map_cb

Called for every row returned. The arguments are the row, the result object, and the rows already collected. Return the modified row, or 0 to discard it.

Parameter options

Optional mapping with the following keys:

"min_records" : int

Fail if fewer rows are returned. Defaults to 0.

"max_records" : int

Fail if more rows are returned. -1 disables the limit.

"discard_records" : int

Keep at most this many rows. -1 keeps all rows.

Example
Sql.Connection db = Sql.Sql("mysql://localhost/testdb");
Concurrent.Future(<Sql.Result>) query =
  db->promise_query("SELECT 42", 0, 0, (["max_records":10]));
query->on_success(lambda (Sql.Result result) {
  werror("Got %O\n", result->fetch_row());
});
query->on_failure(lambda (mixed error) {
  werror("Query failed: %O\n", error);
});
See also

streaming_typed_query(), Sql.Result