Class: Rubydex::Query::Result
- Inherits:
-
Object
- Object
- Rubydex::Query::Result
- Includes:
- Enumerable
- Defined in:
- ext/rubydex/query.c
Instance Method Summary collapse
-
#columns ⇒ Array[String]
Returns the RETURN column names, in order.
-
#each ⇒ Object
Yields every row as a Hash keyed by RETURN column name.
-
#empty? ⇒ Boolean
Returns
truewhen the query matched no rows. -
#render(format = :table) ⇒ String
Returns the result set as formatted output.
-
#rows ⇒ Array[Hash[String, Object]]
Returns the rows as Ruby objects: a frozen Array in which each row is a Hash keyed by RETURN column name.
-
#size ⇒ Object
(also: #length)
Returns the number of rows, without building the row objects.
Instance Method Details
#columns ⇒ Array[String]
Returns the RETURN column names, in order. The names are known even when the query matched no rows.
366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'ext/rubydex/query.c', line 366
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;
}
|
#each {|row| ... } ⇒ self #each ⇒ Enumerator
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.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'ext/rubydex/query.c', line 391
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;
}
|
#empty? ⇒ Boolean
Returns true when the query matched no rows.
427 428 429 |
# File 'ext/rubydex/query.c', line 427
static VALUE rdxr_query_result_empty_p(VALUE self) {
return rdx_result_set_row_count(query_result_data(self)->result_set) == 0 ? Qtrue : Qfalse;
}
|
#render(format = :table) ⇒ String
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.
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'ext/rubydex/query.c', line 438
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;
}
|
#rows ⇒ Array[Hash[String, Object]]
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.
348 349 350 351 352 353 354 355 356 357 |
# File 'ext/rubydex/query.c', line 348
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;
}
|
#size ⇒ Integer #length ⇒ Integer Also known as: length
Returns the number of rows, without building the row objects.
417 418 419 |
# File 'ext/rubydex/query.c', line 417
static VALUE rdxr_query_result_size(VALUE self) {
return SIZET2NUM(rdx_result_set_row_count(query_result_data(self)->result_set));
}
|