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

RETURN_TYPES =
%i[invalid changed_rows nothing query_result].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newObject

Raises:



33
34
35
# File 'lib/duckdb/result.rb', line 33

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

.use_chunk_each=(value) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/duckdb/result.rb', line 37

def use_chunk_each=(value)
  raise('`changing DuckDB::Result.use_chunk_each to false` was deprecated.') unless value

  warn('`DuckDB::Result.use_chunk_each=` will be deprecated.')

  true
end

.use_chunk_each?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/duckdb/result.rb', line 45

def use_chunk_each?
  warn('`DuckDB::Result.use_chunk_each?` will be deprecated.')
  true
end

Instance Method Details

#eachObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/duckdb/result.rb', line 51

def 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
end

#enum_dictionary_values(col_index) ⇒ Object



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

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

#return_typeObject

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

Raises:



71
72
73
74
75
76
# File 'lib/duckdb/result.rb', line 71

def return_type
  i = _return_type
  raise DuckDB::Error, "Unknown return type: #{i}" if i >= RETURN_TYPES.size

  RETURN_TYPES[i]
end

#statement_typeObject

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


89
90
91
92
# File 'lib/duckdb/result.rb', line 89

def statement_type
  i = _statement_type
  Converter::IntToSym.statement_type_to_sym(i)
end