Class: RDBI::Driver::MySQL::Cursor

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

Overview

Due to mysql statement handles and result sets being tightly coupled, RDBI::Database#execute may require a full fetch of the result set for any of this to work.

If you must use execute, use the block form, which will wait to close any statement handles. Performance will differ sharply.

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ Cursor

Returns a new instance of Cursor.



217
218
219
220
# File 'lib/rdbi/driver/mysql.rb', line 217

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

Instance Method Details

#[](index) ⇒ Object



301
302
303
# File 'lib/rdbi/driver/mysql.rb', line 301

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

#affected_countObject



248
249
250
251
252
253
254
# File 'lib/rdbi/driver/mysql.rb', line 248

def affected_count
  if @array_handle
    0
  else
    @handle.affected_rows
  end
end

#allObject



293
294
295
296
297
298
299
# File 'lib/rdbi/driver/mysql.rb', line 293

def all
  if @array_handle
    fetch_range(0, @array_handle.size)
  else
    fetch_range(0, @handle.num_rows)
  end
end

#coerce_to_arrayObject



332
333
334
335
336
337
338
339
340
# File 'lib/rdbi/driver/mysql.rb', line 332

def coerce_to_array
  unless @array_handle
    @array_handle = []
    begin
      @handle.num_rows.times { @array_handle.push(@handle.fetch) }
    rescue
    end
  end
end

#empty?Boolean

Returns:

  • (Boolean)


320
321
322
323
324
325
326
# File 'lib/rdbi/driver/mysql.rb', line 320

def empty?
  if @array_handle
    @array_handle.empty?
  else
    @handle.num_rows == 0
  end
end

#fetch(count = 1) ⇒ Object



222
223
224
225
226
227
# File 'lib/rdbi/driver/mysql.rb', line 222

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

#finishObject



328
329
330
# File 'lib/rdbi/driver/mysql.rb', line 328

def finish
  @handle.free_result
end

#firstObject



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rdbi/driver/mysql.rb', line 256

def first
  if @array_handle
    @array_handle.first
  else
    cur = @handle.row_tell
    @handle.data_seek(0)
    res = @handle.fetch
    @handle.row_seek(cur)
    return res
  end
end

#lastObject



268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rdbi/driver/mysql.rb', line 268

def last
  if @array_handle
    @array_handle.last
  else
    #raise RDBI::Cursor::NotRewindableError, "last requires a rewindable result in mysql"
    cur = @handle.row_tell
    @handle.data_seek(@handle.num_rows - 1)
    res = @handle.fetch
    @handle.row_seek(cur)
    return res
  end
end

#last_row?Boolean

Returns:

  • (Boolean)


305
306
307
308
309
310
311
# File 'lib/rdbi/driver/mysql.rb', line 305

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

#next_rowObject



229
230
231
232
233
234
235
236
237
238
# File 'lib/rdbi/driver/mysql.rb', line 229

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

  @index += 1
  val
end

#restObject



281
282
283
284
285
286
287
288
289
290
291
# File 'lib/rdbi/driver/mysql.rb', line 281

def rest
  oindex = nil

  if @array_handle
    oindex, @index = @index, @array_handle.size
  else
    oindex, @index = @index, @handle.num_rows
  end

  fetch_range(oindex, @index)
end

#result_countObject



240
241
242
243
244
245
246
# File 'lib/rdbi/driver/mysql.rb', line 240

def result_count
  if @array_handle
    @array_handle.size
  else
    @handle.num_rows
  end
end

#rewindObject



313
314
315
316
317
318
# File 'lib/rdbi/driver/mysql.rb', line 313

def rewind
  @index = 0
  unless @array_handle
    @handle.data_seek(0)
  end
end