Class: RdbmsSampler::Sample

Inherits:
Object
  • Object
show all
Defined in:
lib/rdbms_sampler/sample.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sample

Returns a new instance of Sample.



8
9
10
11
12
13
14
15
16
# File 'lib/rdbms_sampler/sample.rb', line 8

def initialize(options ={})
  @connection = options[:conn]
  @rows_per_table = options[:rows_per_table] || 1000
  @table_samples = {}
  @schemas = options[:schemas]
  @computed = false
  @connection.execute 'SET SESSION TRANSACTION READ ONLY, ISOLATION LEVEL REPEATABLE READ'
  @connection.execute 'START TRANSACTION'
end

Instance Method Details

#compute!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rdbms_sampler/sample.rb', line 18

def compute!
  quoted_schema_names = @schemas.collect do |name|
    @connection.quote_table_name(name)
  end
  warn "Discovering tables in databases: #{quoted_schema_names.to_sentence}..."
  tables_without_views.each do |schema_name, table_name|
    table_sample = TableSample.new(@connection, schema_name, table_name, @rows_per_table)
    @table_samples[table_sample.identifier] = table_sample
  end
  return warn 'No tables found!' unless @table_samples.count > 0
  warn "Sampling #{@table_samples.count} tables..."
  @table_samples.values.map &:sample!
  warn 'Ensuring referential integrity...'
  begin
    new_dependencies = 0
    @table_samples.values.each do |table_sample|
      newly_added = table_sample.ensure_referential_integrity(self)
      if newly_added > 0
        new_dependencies += newly_added
        warn "  Expanded sample with #{newly_added} new rows referenced from table #{table_sample.quoted_name}"
      end
    end
    warn " Discovered #{new_dependencies} new dependencies" if new_dependencies > 0
  end while new_dependencies > 0
  warn 'Referential integrity obtained'

  warn 'Final sample contains:'
  @table_samples.values.each do |table_sample|
    warn "  #{table_sample.size} row(s) from `#{table_sample.identifier}`"
  end
  @computed = true
end

#table_sample_for_dependency(dependency) ⇒ TableSample

Parameters:

Returns:



53
54
55
56
# File 'lib/rdbms_sampler/sample.rb', line 53

def table_sample_for_dependency(dependency)
  raise "Table sample for [#{dependency.identifier}] not found" unless @table_samples.include? dependency.identifier
  @table_samples[dependency.identifier]
end

#to_sqlObject



58
59
60
61
# File 'lib/rdbms_sampler/sample.rb', line 58

def to_sql
  compute! unless @computed
  @table_samples.values.collect(&:to_sql) * "\n"
end