Class: Drizzle::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/drizzle/result.rb

Overview

a result set from a drizzle query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res_ptr) ⇒ Result

creates a result set instance This result set does not buffer any results until instructed to do so. Results can be fully buffered or buffered at the row level.

parameters

* res_ptr   FFI memory pointer to a drizzle_result_t object


22
23
24
25
26
27
# File 'lib/drizzle/result.rb', line 22

def initialize(res_ptr)
  @columns, @rows = [], []
  @res_ptr = res_ptr
  @affected_rows = LibDrizzle.drizzle_result_affected_rows(@res_ptr)
  @insert_id = LibDrizzle.drizzle_result_insert_id(@res_ptr)
end

Instance Attribute Details

#affected_rowsObject (readonly)

Returns the value of attribute affected_rows.



10
11
12
# File 'lib/drizzle/result.rb', line 10

def affected_rows
  @affected_rows
end

#columnsObject (readonly)

Returns the value of attribute columns.



10
11
12
# File 'lib/drizzle/result.rb', line 10

def columns
  @columns
end

#insert_idObject (readonly)

Returns the value of attribute insert_id.



10
11
12
# File 'lib/drizzle/result.rb', line 10

def insert_id
  @insert_id
end

#rowsObject (readonly)

Returns the value of attribute rows.



10
11
12
# File 'lib/drizzle/result.rb', line 10

def rows
  @rows
end

Instance Method Details

#buffer_resultObject

buffer all rows and columns and copy them into ruby arrays Free the FFI pointer afterwards



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/drizzle/result.rb', line 33

def buffer_result()
  ret = LibDrizzle.drizzle_result_buffer(@res_ptr)
  if LibDrizzle::ReturnCode[ret] != LibDrizzle::ReturnCode[:DRIZZLE_RETURN_OK]
    LibDrizzle.drizzle_result_free(@res_ptr)
  end

  loop do
    col_ptr = LibDrizzle.drizzle_column_next(@res_ptr)
    break if col_ptr.null?
    @columns << LibDrizzle.drizzle_column_name(col_ptr).to_sym
  end

  loop do
    row_ptr = LibDrizzle.drizzle_row_next(@res_ptr)
    break if row_ptr.null?
    @rows << row_ptr.get_array_of_string(0, @columns.size)
  end

  LibDrizzle.drizzle_result_free(@res_ptr)
end

#buffer_rowObject

buffer an individual row.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/drizzle/result.rb', line 57

def buffer_row()
  # if the columns have not been read for this result
  # set yet, then we need to do that here. If this is not
  # performed here, we will receive a bad packet error
  read_columns if @columns.empty?
  ret_ptr = FFI::MemoryPointer.new(:int)
  row_ptr = LibDrizzle.drizzle_row_buffer(@res_ptr, ret_ptr)
  if LibDrizzle::ReturnCode[ret_ptr.get_int(0)] != :DRIZZLE_RETURN_OK
    LibDrizzle.drizzle_result_free(@res_ptr)
  end
  if row_ptr.null?
    LibDrizzle.drizzle_result_free(@res_ptr)
    return nil
  end
  num_of_cols = LibDrizzle.drizzle_result_column_count(@res_ptr)
  row = row_ptr.get_array_of_string(0, @columns.size)
end

#eachObject



87
88
89
90
91
# File 'lib/drizzle/result.rb', line 87

def each
  @rows.each do |row|
    yield row if block_given?
  end
end

#read_columnsObject

buffer all columns for this result set



78
79
80
81
82
83
84
85
# File 'lib/drizzle/result.rb', line 78

def read_columns
  ret = LibDrizzle.drizzle_column_buffer(@res_ptr)
  loop do
    col_ptr = LibDrizzle.drizzle_column_next(@res_ptr)
    break if col_ptr.null?
    @columns << LibDrizzle.drizzle_column_name(col_ptr).to_sym
  end
end