Class: RDBI::Driver::SQLite3::Cursor

Inherits:
Cursor
  • Object
show all
Defined in:
lib/rdbi/driver/sqlite3.rb

Overview

Because SQLite3’s ruby implementation does not support everything that our cursor implementation does, some methods, when called, will fetch the entire result set. In the instance this is done, the resulting array is used for all future operations.

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ Cursor

Returns a new instance of Cursor.



108
109
110
111
# File 'lib/rdbi/driver/sqlite3.rb', line 108

def initialize(handle)
  super(handle)
  @index = 0
end

Instance Method Details

#[](index) ⇒ Object



176
177
178
179
# File 'lib/rdbi/driver/sqlite3.rb', line 176

def [](index)
  coerce_to_array
  @array_handle[index]
end

#affected_countObject



137
138
139
140
# File 'lib/rdbi/driver/sqlite3.rb', line 137

def affected_count
  # sqlite3-ruby does not support affected counts
  0
end

#allObject



166
167
168
169
170
171
172
173
174
# File 'lib/rdbi/driver/sqlite3.rb', line 166

def all
  if rewindable_result
    rewind
    coerce_to_array
    @array_handle
  else
    @handle.to_a
  end
end

#coerce_to_arrayObject



205
206
207
208
209
210
211
# File 'lib/rdbi/driver/sqlite3.rb', line 205

def coerce_to_array
  unless @coerced 
    rewind
    @array_handle = @handle.to_a
    @coerced = true
  end
end

#empty?Boolean

Returns:

  • (Boolean)


196
197
198
199
# File 'lib/rdbi/driver/sqlite3.rb', line 196

def empty?
  coerce_to_array
  @array_handle.empty?
end

#fetch(count = 1) ⇒ Object



113
114
115
116
117
118
# File 'lib/rdbi/driver/sqlite3.rb', line 113

def fetch(count=1)
  return [] if last_row?
  a = []
  count.times { a.push(next_row) }
  return a
end

#finishObject



201
202
203
# File 'lib/rdbi/driver/sqlite3.rb', line 201

def finish
  @handle.close unless @handle.closed?
end

#firstObject



142
143
144
145
146
147
148
# File 'lib/rdbi/driver/sqlite3.rb', line 142

def first
  if @array_handle
    @array_handle.first
  else
    @handle.first
  end
end

#lastObject



150
151
152
153
# File 'lib/rdbi/driver/sqlite3.rb', line 150

def last
  coerce_to_array
  @array_handle[-1]
end

#last_row?Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
187
# File 'lib/rdbi/driver/sqlite3.rb', line 181

def last_row?
  if @array_handle
    @index == @array_handle.size
  else
    @handle.eof?
  end
end

#next_rowObject



120
121
122
123
124
125
126
127
128
129
# File 'lib/rdbi/driver/sqlite3.rb', line 120

def next_row
  val = if @array_handle
          @array_handle[@index]
        else
          @handle.next
        end

  @index += 1
  val
end

#restObject



155
156
157
158
159
160
161
162
163
164
# File 'lib/rdbi/driver/sqlite3.rb', line 155

def rest
  if rewindable_result
    rewind
    coerce_to_array
    oindex, @index = @index, @array_handle.size
    @array_handle[oindex, @index]
  else
    @handle.to_a
  end
end

#result_countObject



131
132
133
134
135
# File 'lib/rdbi/driver/sqlite3.rb', line 131

def result_count
  return 0 unless rewindable_result
  coerce_to_array
  @array_handle.size
end

#rewindObject

Raises:

  • (StandardError)


189
190
191
192
193
194
# File 'lib/rdbi/driver/sqlite3.rb', line 189

def rewind
  # FIXME better exception
  raise StandardError, "rewindable_result is not true" unless rewindable_result
  @index = 0
  @handle.reset unless @handle.closed?
end