Class: Flydata::SourcePostgresql::QueryBasedSync::DiffQueryGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/source_postgresql/query_based_sync/diff_query_generator.rb

Constant Summary collapse

DEFAULT_LIMIT =
10000
FETCH_RECORDS_SQL =
<<EOS
SELECT %{columns} FROM %{table_name} WHERE %{where_clause} ORDER BY %{order_by_clause} LIMIT %{max_rows};
EOS
TO_SID_WHERE_CLAUSE =
"txid_visible_in_snapshot(xmin::TEXT::BIGINT, '%{to_sid}')"
FROM_SID_WHERE_CLAUSE =
"AND NOT txid_visible_in_snapshot(xmin::TEXT::BIGINT, '%{from_sid}')"

Instance Method Summary collapse

Constructor Details

#initialize(table, schema, query_cond = {}) ⇒ DiffQueryGenerator

Returns a new instance of DiffQueryGenerator.


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flydata/source_postgresql/query_based_sync/diff_query_generator.rb', line 20

def initialize(table, schema, query_cond = {})
  @table = table
  @schema = schema

  @columns = query_cond[:columns] or raise ArgumentError, "columns is required"
  @from_sid = query_cond[:from_sid]
  @to_sid = query_cond[:to_sid] or raise ArgumentError, "to_sid is required"
  @pk_columns = query_cond[:pk_columns] or raise ArgumentError, "pk_columns is required"
  @last_pks = query_cond[:last_pks]
  @limit = query_cond[:limit] || DEFAULT_LIMIT
end

Instance Method Details

#build_query(binding_param_enabled = true) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/flydata/source_postgresql/query_based_sync/diff_query_generator.rb', line 32

def build_query(binding_param_enabled = true)
  from_sid_where_clause = if @from_sid
                            FROM_SID_WHERE_CLAUSE % {from_sid: @from_sid}
                          else
                            nil
                          end

  column_names, types = @columns.inject([[],[]]) do |arys, h|
    arys[0] << h[:column]; arys[1] << h[:type].gsub(/\(.+?\)/,''); arys
  end
  columns = column_list(column_names, types)

  query = FETCH_RECORDS_SQL % {
    columns: columns,
    table_name: fq_table_name(@table, @schema),
    where_clause: build_where_clause(@from_sid, @to_sid, @pk_columns, @last_pks, binding_param_enabled),
    order_by_clause: build_order_by_clause(@pk_columns),
    max_rows: @limit,
  }

  opts = {
    value_overriders: build_value_overriders(column_names, types)
  }

  if binding_param_enabled
    opts[:binding_params] = @last_pks
  end

  FlydataCore::Postgresql::PGQuery.new(query, opts)
end