Class: RDBI::Result::Driver::CSV

Inherits:
RDBI::Result::Driver show all
Defined in:
lib/rdbi/result.rb

Overview

This driver yields CSV:

dbh.execute("select foo, bar from my_table").fetch(:first, :CSV)

Yields the contents of columns foo and bar in CSV format (a String).

The fastercsv gem on 1.8 is used, which is the canonical csv library on 1.9. If you are using Ruby 1.8 and do not have this gem available and try to use this driver, the code will abort during driver construction.

Instance Method Summary collapse

Methods inherited from RDBI::Result::Driver

#convert_item, #convert_row

Constructor Details

#initialize(result, *args) ⇒ CSV

Returns a new instance of CSV.



345
346
347
348
349
350
351
352
353
# File 'lib/rdbi/result.rb', line 345

def initialize(result, *args)
  super
  if RUBY_VERSION =~ /^1.8/
    RDBI::Util.optional_require('fastercsv')
  else
    require 'csv'
  end
  # FIXME columns from schema deal maybe?
end

Instance Method Details

#fetch(row_count) ⇒ Object



355
356
357
358
359
360
361
# File 'lib/rdbi/result.rb', line 355

def fetch(row_count)
  csv_string = ""
  @result.raw_fetch(row_count).each do |row|
    csv_string << row.to_csv
  end
  return csv_string
end