require 'flydata-core/postgresql/pg_client' require 'flydata-core/table_def/postgresql_table_def' require 'flydata-core/table_def/value_conv' module Flydata module SourcePostgresql module QueryBasedSync class DiffQueryGenerator DEFAULT_LIMIT = 10000 FETCH_RECORDS_SQL = <= 0 colname = pk_columns[i] val = value_in_where(i, last_pks, binding_param_enabled) if str str = %Q|"#{colname}" > #{val} OR ("#{colname}" = #{val} AND (#{str}))| else str = %Q|"#{colname}" > #{val}| end i -= 1 end " AND (#{str})" end def value_in_where(index, last_pks, binding_param_enabled) if binding_param_enabled "$#{index + 1}" else %Q|'#{last_pks[index]}'| end end def fq_table_name(table, schema) !schema.to_s.empty? ? %Q|"#{schema}"."#{table}"| : %Q|"#{table}"| end # return a list of columns with necessary conversion as a string QUERY_VAL_CONV_RULES = Hash.new do |h, k| h[k] = case k when "bytea"; %Q['0x' || encode("%s", 'hex') AS "%s"] else %Q["%s"] end end VALUE_OVERRIDERS = Hash.new do |h, k| h[k] = case k when "bit", "varbit"; -> (v) { v ? '%d' % v.to_i(2) : nil } when "boolean"; -> (v) { FlydataCore::TableDef::PostgresqlTableDef::to_boolean(v) } when "money"; -> (v) { v ? FlydataCore::TableDef::ValueConv.strip_currency_format(v) : nil } else nil end end def column_list(columns, types) columns.each_with_index.collect{|c, i| QUERY_VAL_CONV_RULES[types[i]] % {column:c}}.join(", ") end def build_value_overriders(columns, types) columns.each_with_index.inject({}) do |h, (c, i)| overrider = VALUE_OVERRIDERS[types[i]] h[c] = overrider if overrider h end end end end end end