class Rubydex::Query::Result
The result of running a Rubydex::Query against a graph: the columns and rows produced by one execution. Enumerable over its rows.
Public Instance Methods
Source
static VALUE rdxr_query_result_columns(VALUE self) {
QueryResultData *data = query_result_data(self);
size_t count = rdx_result_set_column_count(data->result_set);
VALUE columns = rb_ary_new_capa((long)count);
for (size_t i = 0; i < count; i++) {
rb_ary_push(columns, rdxi_owned_c_string_to_ruby(rdx_result_set_column(data->result_set, i)));
}
return columns;
}
Returns the RETURN column names, in order. The names are known even when the query matched no rows.
static VALUE rdxr_query_result_each(VALUE self) {
RETURN_ENUMERATOR(self, 0, 0);
VALUE rows = query_result_data(self)->rows;
if (NIL_P(rows)) {
query_with_rows(self, query_rows_stream);
return self;
}
long length = RARRAY_LEN(rows);
for (long i = 0; i < length; i++) {
rb_yield(RARRAY_AREF(rows, i));
}
return self;
}
Yields every row as a Hash keyed by RETURN column name. Rubydex::Query::Result is Enumerable, so map, select, and the rest of Enumerable work on the rows.
Unless rows already built the whole array, each converts one row at a time and discards it after the block returns. A large result therefore needs memory for one row, not for all of them, and first or find stops converting as soon as the block breaks.
Source
static VALUE rdxr_query_result_empty_p(VALUE self) {
return rdx_result_set_row_count(query_result_data(self)->result_set) == 0 ? Qtrue : Qfalse;
}
Returns true when the query matched no rows.
static VALUE rdxr_query_result_render(int argc, VALUE *argv, VALUE self) {
VALUE format;
rb_scan_args(argc, argv, "01", &format);
QueryResultData *data = query_result_data(self);
struct CQueryResult result = rdx_result_set_format(data->result_set, rdxi_symbol_or_string_cstr(format, "table"));
if (result.error != NULL) {
VALUE message = rb_utf8_str_new_cstr(result.error);
free_c_string(result.error);
rb_raise(rb_eArgError, "%s", StringValueCStr(message));
}
VALUE output = result.output == NULL ? rb_utf8_str_new_cstr("") : rb_utf8_str_new_cstr(result.output);
if (result.output != NULL) {
free_c_string(result.output);
}
return output;
}
Returns the result set as formatted output. format may be :table (default) or :json. The query is not run again. Raises ArgumentError on an unknown format.
static VALUE rdxr_query_result_rows(VALUE self) {
if (!NIL_P(query_result_data(self)->rows)) {
return query_result_data(self)->rows;
}
VALUE rows = rb_ary_freeze(query_with_rows(self, query_rows_collect));
query_result_data(self)->rows = rows;
return rows;
}
Returns the rows as Ruby objects: a frozen Array in which each row is a Hash keyed by RETURN column name. Scalar cells become String/Integer/true/false/nil, lists become Arrays, maps become Hashes, and node cells become Declaration / Definition / Document handles. The array is built on the first call and reused afterwards.
Source
static VALUE rdxr_query_result_size(VALUE self) {
return SIZET2NUM(rdx_result_set_row_count(query_result_data(self)->result_set));
}
Returns the number of rows, without building the row objects.