Class: DuckDB::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/duckdb/result.rb

Overview

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

Constant Summary collapse

TO_METHODS =
if Gem::Version.new(DuckDB::LIBRARY_VERSION) == Gem::Version.new('0.10.0')
  Hash.new(:_to_string).merge(
    1 => :_to_boolean,
    3 => :_to_smallint,
    4 => :_to_integer,
    5 => :_to_bigint,
    10 => :_to_float,
    11 => :_to_double,
    16 => :_to_hugeint_internal,
    19 => :_to_blob,
    20 => :_to_decimal_internal
  ).freeze
else
  Hash.new(:_to_string).merge(
    1 => :_to_boolean,
    3 => :_to_smallint,
    4 => :_to_integer,
    5 => :_to_bigint,
    10 => :_to_float,
    11 => :_to_double,
    16 => :_to_hugeint_internal,
    18 => :_to_blob,
    19 => :_to_decimal_internal
  ).freeze
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject

Raises:



59
60
61
# File 'lib/duckdb/result.rb', line 59

def new
  raise DuckDB::Error, 'DuckDB::Result cannot be instantiated directly.'
end

.use_chunk_each=(value) ⇒ Object



63
64
65
66
67
# File 'lib/duckdb/result.rb', line 63

def use_chunk_each=(value)
  warn('`changing DuckDB::Result.use_chunk_each to false` will be deprecated.') if value == false

  @use_chunk_each = value
end

.use_chunk_each?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/duckdb/result.rb', line 69

def use_chunk_each?
  !!@use_chunk_each
end

Instance Method Details

#eachObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/duckdb/result.rb', line 74

def each
  if self.class.use_chunk_each?
    if streaming?
      return _chunk_stream unless block_given?

      _chunk_stream { |row| yield row }
    else
      return chunk_each unless block_given?

      chunk_each { |row| yield row }
    end
  else
    warn('this `each` behavior will be deprecated in the future. set `DuckDB::Result.use_chunk_each = true` to use new `each` behavior.')
    return to_enum { row_size } unless block_given?

    row_count.times do |row_index|
      yield row(row_index)
    end
  end
end

#enum_dictionary_values(col_index) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/duckdb/result.rb', line 109

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

#row(row_index) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/duckdb/result.rb', line 95

def row(row_index)
  warn("#{self.class}##{__method__} will be deprecated. set `DuckDB::Result.use_chunk_each = true`.")
  row = []
  column_count.times do |col_index|
    row << (_null?(row_index, col_index) ? nil : to_value(row_index, col_index))
  end
  row
end

#to_value(row_index, col_index) ⇒ Object



104
105
106
107
# File 'lib/duckdb/result.rb', line 104

def to_value(row_index, col_index)
  warn("#{self.class}##{__method__} will be deprecated. set `DuckDB::Result.use_chunk_each = true`.")
  send(TO_METHODS[_column_type(col_index)], row_index, col_index)
end