Sha256: 38c7d0dd6dd6427bf8a7b2686e25316ce2e63dfa9f69044d2c9fdfc097aed4fb

Contents?: true

Size: 924 Bytes

Versions: 1

Compression:

Stored size: 924 Bytes

Contents

require 'sqlite3'

module CsvQuery
  # Wraps a SQLite in-memory database with a single table named csv.
  class Database
    attr_reader :database

    def initialize(csv)
      @database = SQLite3::Database.new(':memory:')
      @columns = csv.headers
      create_table(@columns)
    end

    def import_data_from_csv(csv)
      columns = csv.headers

      sql = "INSERT INTO csv VALUES (#{(['?'] * columns.size).join(',')})"
      statement = database.prepare(sql)

      csv.each do |row|
        statement.execute(row.fields)
      end
    end

    # Returns the results of sql. First row of the resultset contains the column names
    def query(sql)
      database.execute2(sql)
    end

    private

    def create_table(column_names)
      column_definitions = column_names.collect { |name| "\"#{name}\" VARCHAR(255)" }
      database.execute "CREATE TABLE csv (#{column_definitions.join(", ")})"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_query-1.0.4 lib/csv_query/database.rb