Class: DataImport::Adapters::Sequel
- Inherits:
-
Object
- Object
- DataImport::Adapters::Sequel
- Defined in:
- lib/data-import/adapters/sequel.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Class Method Summary collapse
Instance Method Summary collapse
- #count(table, options = {}) ⇒ Object
- #each_row(table, options = {}, &block) ⇒ Object
- #each_row_in_batches(table, options = {}, &block) ⇒ Object
- #each_row_without_batches(table, options = {}, &block) ⇒ Object
-
#initialize(db) ⇒ Sequel
constructor
A new instance of Sequel.
- #insert_row(table, row) ⇒ Object
- #maximum_value(table, column) ⇒ Object
- #numeric_column?(table, column) ⇒ Boolean
- #transaction(&block) ⇒ Object
- #truncate(table) ⇒ Object
- #unique_row(table, key) ⇒ Object
- #update_row(table, row) ⇒ Object
Constructor Details
#initialize(db) ⇒ Sequel
Returns a new instance of Sequel.
15 16 17 |
# File 'lib/data-import/adapters/sequel.rb', line 15 def initialize(db) @db = db end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
8 9 10 |
# File 'lib/data-import/adapters/sequel.rb', line 8 def db @db end |
Class Method Details
.connect(options = {}) ⇒ Object
10 11 12 13 |
# File 'lib/data-import/adapters/sequel.rb', line 10 def self.connect( = {}) ::Sequel.identifier_output_method = :to_s self.new ::Sequel.connect() end |
Instance Method Details
#count(table, options = {}) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/data-import/adapters/sequel.rb', line 69 def count(table, = {}) sql = @db.from(table) sql = sql.select(*[:columns]) unless [:columns].nil? sql = sql.distinct if [:distinct] sql.count end |
#each_row(table, options = {}, &block) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/data-import/adapters/sequel.rb', line 29 def each_row(table, = {}, &block) if [:primary_key].nil? || !numeric_column?(table, [:primary_key]) each_row_without_batches table, , &block else each_row_in_batches table, , &block end end |
#each_row_in_batches(table, options = {}, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/data-import/adapters/sequel.rb', line 47 def each_row_in_batches(table, = {}, &block) personen = @db.from(table) max = maximum_value(table, [:primary_key]) || 0 lower_bound = 0 batch_size = 1000 while (lower_bound <= max) do upper_bound = lower_bound + batch_size - 1 sql = personen.filter([:primary_key] => lower_bound..upper_bound) sql = sql.select(*[:columns]) unless [:columns].nil? sql = sql.distinct if [:distinct] sql = sql.order(*[:order]) unless [:order].nil? sql.each do |result| yield result if block_given? end unless sql.nil? lower_bound += batch_size end end |
#each_row_without_batches(table, options = {}, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/data-import/adapters/sequel.rb', line 37 def each_row_without_batches(table, = {}, &block) sql = @db.from(table) sql = sql.select(*[:columns]) unless [:columns].nil? sql = sql.distinct if [:distinct] sql = sql.order(*[:order]) unless [:order].nil? sql.each do |row| yield row if block_given? end end |
#insert_row(table, row) ⇒ Object
76 77 78 |
# File 'lib/data-import/adapters/sequel.rb', line 76 def insert_row(table, row) @db.from(table).insert(row) end |
#maximum_value(table, column) ⇒ Object
65 66 67 |
# File 'lib/data-import/adapters/sequel.rb', line 65 def maximum_value(table, column) @db.from(table).max(column) end |
#numeric_column?(table, column) ⇒ Boolean
85 86 87 88 |
# File 'lib/data-import/adapters/sequel.rb', line 85 def numeric_column?(table, column) column_definition = @db.schema(table).select{|c| c.first == column}.first column_definition[1][:type] == :integer unless column_definition.nil? end |
#transaction(&block) ⇒ Object
23 24 25 26 27 |
# File 'lib/data-import/adapters/sequel.rb', line 23 def transaction(&block) @db.transaction do yield block end end |
#truncate(table) ⇒ Object
19 20 21 |
# File 'lib/data-import/adapters/sequel.rb', line 19 def truncate(table) @db.from(table).delete end |
#unique_row(table, key) ⇒ Object
90 91 92 |
# File 'lib/data-import/adapters/sequel.rb', line 90 def unique_row(table, key) @db.from(table)[:id => key] end |
#update_row(table, row) ⇒ Object
80 81 82 83 |
# File 'lib/data-import/adapters/sequel.rb', line 80 def update_row(table, row) id = row.delete(:id) || row.delete('id') @db.from(table).filter(:id => id).update(row) end |