Class: ChDB::Statement

Inherits:
Object
  • Object
show all
Includes:
ParameterBinding, ResultHandler, SQLProcessor, Enumerable
Defined in:
lib/chdb/statement.rb

Overview

Represents a prepared SQL statement in the ChDB database. This class provides methods for executing SQL statements, binding parameters, and iterating over the result set.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResultHandler

done?, parse_output, step

Methods included from SQLProcessor

#escape, #process_sql

Methods included from ParameterBinding

#bind_param, #bind_params

Constructor Details

#initialize(db, sql_str) ⇒ Statement

Returns a new instance of Statement.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chdb/statement.rb', line 28

def initialize(db, sql_str)
  validate_inputs(db, sql_str)
  @sql = encode_sql(sql_str)
  @connection = db
  @executed = false
  @parsed = false
  @row_idx = 0
  @bind_vars = []
  @parsed_data = []
  @columns = []
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



26
27
28
# File 'lib/chdb/statement.rb', line 26

def columns
  @columns
end

#parsed_dataObject (readonly)

Returns the value of attribute parsed_data.



26
27
28
# File 'lib/chdb/statement.rb', line 26

def parsed_data
  @parsed_data
end

#resultObject (readonly)

Returns the value of attribute result.



26
27
28
# File 'lib/chdb/statement.rb', line 26

def result
  @result
end

Instance Method Details

#execute(*bind_vars) {|results| ... } ⇒ Object

Yields:

  • (results)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chdb/statement.rb', line 40

def execute(*bind_vars)
  reset! if @executed
  @executed = true

  bind_params(*bind_vars) unless bind_vars.empty?

  @processed_sql = process_sql

  results = @connection.build_result_set self
  @result = @connection.conn.query(@processed_sql, 'CSVWithNames')
  @result.output_format = 'CSVWithNames'

  yield results if block_given?
  results
end

#execute!(*bind_vars, &block) ⇒ Object



56
57
58
59
# File 'lib/chdb/statement.rb', line 56

def execute!(*bind_vars, &block)
  execute(*bind_vars)
  block ? each(&block) : to_a
end

#execute_with_format(*bind_vars, format) {|@result.buf| ... } ⇒ Object

Yields:



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chdb/statement.rb', line 61

def execute_with_format(*bind_vars, format)
  reset! if @executed
  @executed = true

  bind_params(*bind_vars) unless bind_vars.empty?

  @processed_sql = process_sql
  @result = @connection.conn.query(@processed_sql, format)

  yield @result.buf if block_given?
  @result.buf
end

#parseObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chdb/statement.rb', line 93

def parse
  return if @parsed

  if @result&.buf.to_s.empty?
    @columns = []
    @parsed_data = []
  else
    @columns, @parsed_data = ResultHandler.parse_output(@result.buf)
  end

  @parsed = true
  @results = nil
end

#reset!Object



74
75
76
77
78
79
80
81
82
# File 'lib/chdb/statement.rb', line 74

def reset!
  @executed = false
  @parsed = false
  @row_idx = 0
  @bind_vars.clear
  @parsed_data.clear
  @columns.clear
  @results = nil
end

#stepObject



84
85
86
87
88
89
90
91
# File 'lib/chdb/statement.rb', line 84

def step
  parse
  return nil if @row_idx >= @parsed_data.size

  current_row = @parsed_data[@row_idx]
  @row_idx += 1
  current_row
end