class DuckDB::Result
The Result
class encapsulates a execute result of DuckDB
database.
The usage is as follows:
require 'duckdb' db = DuckDB::Database.open # database in memory con = db.connect con.execute('CREATE TABLE users (id INTEGER, name VARCHAR(30))') con.execute("INSERT into users VALUES(1, 'Alice')") con.execute("INSERT into users VALUES(2, 'Bob')") con.execute("INSERT into users VALUES(3, 'Cathy')") result = con.execute('SELECT * from users') result.each do |row| p row end
Constants
- RETURN_TYPES
Public Class Methods
Source
# File lib/duckdb/result.rb, line 32 def new raise DuckDB::Error, 'DuckDB::Result cannot be instantiated directly.' end
Public Instance Methods
Source
static VALUE duckdb_result_column_count(VALUE oDuckDBResult) { rubyDuckDBResult *ctx; TypedData_Get_Struct(oDuckDBResult, rubyDuckDBResult, &result_data_type, ctx); return LL2NUM(duckdb_column_count(&(ctx->result))); }
Returns the column size of the result.
DuckDB::Database.open do |db| db.connect do |con| r = con.query('CREATE TABLE t2 (id INT, name VARCHAR(128))') r = con.query("INSERT INTO t2 VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Catherine')") r = con.query('SELECT id FROM t2') r.column_count # => 1 r = con.query('SELECT id, name FROM t2') r.column_count # => 2 end end
Also aliased as: column_size
Alias for: column_count
Source
static VALUE duckdb_result_columns(VALUE oDuckDBResult) { rubyDuckDBResult *ctx; TypedData_Get_Struct(oDuckDBResult, rubyDuckDBResult, &result_data_type, ctx); idx_t col_idx; idx_t column_count = duckdb_column_count(&(ctx->result)); VALUE ary = rb_ary_new2(column_count); for(col_idx = 0; col_idx < column_count; col_idx++) { VALUE column = rbduckdb_create_column(oDuckDBResult, col_idx); rb_ary_store(ary, col_idx, column); } return ary; }
Returns the column class Lists.
Source
# File lib/duckdb/result.rb, line 37 def each(&) if streaming? return _chunk_stream unless block_given? _chunk_stream(&) else return chunk_each unless block_given? chunk_each(&) end end
Source
# File lib/duckdb/result.rb, line 88 def enum_dictionary_values(col_index) values = [] _enum_dictionary_size(col_index).times do |i| values << _enum_dictionary_value(col_index, i) end values end
returns all available ENUM type values of the specified column index.
require 'duckdb' db = DuckDB::Database.open('duckdb_database') con = db.connect con.execute("CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy', 'πΎπΎΦ Ι π')") con.execute("CREATE TABLE enums (id INTEGER, mood mood)") result = con.query('SELECT * FROM enums') result.enum_dictionary_values(1) # => ['sad', 'ok', 'happy', 'πΎπΎΦ Ι π']
Source
# File lib/duckdb/result.rb, line 57 def return_type i = _return_type raise DuckDB::Error, "Unknown return type: #{i}" if i >= RETURN_TYPES.size RETURN_TYPES[i] end
returns return type. The return value is one of the following symbols:
:invalid, :changed_rows, :nothing, :query_result require 'duckdb' db = DuckDB::Database.open('duckdb_database') con = db.connect result = con.execute('CREATE TABLE users (id INTEGER, name VARCHAR(30))') result.return_type # => :nothing
Source
static VALUE duckdb_result_rows_changed(VALUE oDuckDBResult) { rubyDuckDBResult *ctx; TypedData_Get_Struct(oDuckDBResult, rubyDuckDBResult, &result_data_type, ctx); return LL2NUM(duckdb_rows_changed(&(ctx->result))); }
Returns the count of rows changed.
DuckDB::Database.open do |db| db.connect do |con| r = con.query('CREATE TABLE t2 (id INT)') r.rows_changed # => 0 r = con.query('INSERT INTO t2 VALUES (1), (2), (3)') r.rows_changed # => 3 r = con.query('UPDATE t2 SET id = id + 1 WHERE id > 1') r.rows_changed # => 2 r = con.query('DELETE FROM t2 WHERE id = 0') r.rows_changed # => 0 r = con.query('DELETE FROM t2 WHERE id = 4') r.rows_changed # => 1 end end
Source
# File lib/duckdb/result.rb, line 75 def statement_type i = _statement_type Converter::IntToSym.statement_type_to_sym(i) end
returns statement type. The return value is one of the following symbols:
:invalid, :select, :insert, :update, :explain, :delete, :prepare, :create, :execute, :alter, :transaction, :copy, :analyze, :variable_set, :create_func, :drop, :export, :pragma, :vacuum, :call, :set, :load, :relation, :extension, :logical_plan, :attach, :detach, :multi require 'duckdb' db = DuckDB::Database.open('duckdb_database') con = db.connect result = con.execute('CREATE TABLE users (id INTEGER, name VARCHAR(30))') result.statement_type # => :create
Source
static VALUE duckdb_result_streaming_p(VALUE oDuckDBResult) { rubyDuckDBResult *ctx; #ifdef DUCKDB_API_NO_DEPRECATED return Qtrue; #else /* FIXME streaming is allways true. so this method is not useful and deprecated. */ TypedData_Get_Struct(oDuckDBResult, rubyDuckDBResult, &result_data_type, ctx); return duckdb_result_is_streaming(ctx->result) ? Qtrue : Qfalse; #endif }
Returns true if the result is streaming, otherwise false.