Class: DataImport::Adapters::Sequel

Inherits:
Object
  • Object
show all
Defined in:
lib/data-import/adapters/sequel.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dbObject (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(options = {})
  ::Sequel.identifier_output_method = :to_s
  self.new ::Sequel.connect(options)
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, options = {})
  sql = @db.from(table)
  sql = sql.select(*options[:columns]) unless options[:columns].nil?
  sql = sql.distinct if options[: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, options = {}, &block)
  if options[:primary_key].nil? || !numeric_column?(table, options[:primary_key])
    each_row_without_batches table, options, &block
  else
    each_row_in_batches table, options, &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, options = {}, &block)
  personen = @db.from(table)
  max = maximum_value(table, options[:primary_key]) || 0
  lower_bound = 0
  batch_size = 1000
  while (lower_bound <= max) do
    upper_bound = lower_bound + batch_size - 1
    sql = personen.filter(options[:primary_key] => lower_bound..upper_bound)
    sql = sql.select(*options[:columns]) unless options[:columns].nil?
    sql = sql.distinct if options[:distinct]
    sql = sql.order(*options[:order]) unless options[: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, options = {}, &block)
  sql = @db.from(table)
  sql = sql.select(*options[:columns]) unless options[:columns].nil?
  sql = sql.distinct if options[:distinct]
  sql = sql.order(*options[:order]) unless options[: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

Returns:

  • (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