# frozen_string_literal: truerequire'bigdecimal'moduleDuckDB# 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# endclassResultincludeEnumerableRETURN_TYPES=%i[invalid changed_rows nothing query_result].freezealiascolumn_sizecolumn_countclass<<selfdefnewraiseDuckDB::Error,'DuckDB::Result cannot be instantiated directly.'endenddefeach(&)return_chunk_streamunlessblock_given?_chunk_stream(&)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 # => :nothingdefreturn_typei=_return_typeraiseDuckDB::Error,"Unknown return type: #{i}"ifi>=RETURN_TYPES.sizeRETURN_TYPES[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 # => :createdefstatement_typei=_statement_typeConverter::IntToSym.statement_type_to_sym(i)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', 'πΎπΎΦ Ι π']defenum_dictionary_values(col_index)values=[]_enum_dictionary_size(col_index).timesdo|i|values<<_enum_dictionary_value(col_index,i)endvaluesendendend