Class: CsvFastImporter::Database::Queryable

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_fast_importer/database/queryable.rb

Overview

Inherit from this class to create new custom database implementation Do not forget to call .identifier_quote_character

Direct Known Subclasses

Mysql, Postgres

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Queryable

Returns a new instance of Queryable.



8
9
10
# File 'lib/csv_fast_importer/database/queryable.rb', line 8

def initialize(connection)
  @connection = connection
end

Class Method Details

.identifier_quote_character(character) ⇒ Object

Character used around identifiers (table or column name) to handle special characters



13
14
15
16
17
# File 'lib/csv_fast_importer/database/queryable.rb', line 13

def self.identifier_quote_character(character)
  define_method "identify" do |identifier|
    character + identifier + character
  end
end

Instance Method Details

#connectionObject



23
24
25
# File 'lib/csv_fast_importer/database/queryable.rb', line 23

def connection
  @connection.raw_connection
end

#delete_all(table) ⇒ Object



41
42
43
# File 'lib/csv_fast_importer/database/queryable.rb', line 41

def delete_all(table)
  execute "DELETE FROM #{identify(table)}"
end

#execute(query) ⇒ Object



27
28
29
# File 'lib/csv_fast_importer/database/queryable.rb', line 27

def execute(query)
  @connection.execute query
end

#identify(table_or_column) ⇒ Object



19
20
21
# File 'lib/csv_fast_importer/database/queryable.rb', line 19

def identify(table_or_column)
  raise '#identify method not available. #identifier_quote_character is certainly missing'
end

#query(query) ⇒ Object



31
32
33
# File 'lib/csv_fast_importer/database/queryable.rb', line 31

def query(query)
  @connection.select_value query
end

#transactionObject



35
36
37
38
39
# File 'lib/csv_fast_importer/database/queryable.rb', line 35

def transaction
  @connection.transaction do
    yield
  end
end

#truncate(table) ⇒ Object



45
46
47
# File 'lib/csv_fast_importer/database/queryable.rb', line 45

def truncate(table)
  execute "TRUNCATE TABLE #{identify(table)}"
end