require 'flydata-core/errors' require 'flydata-core/postgresql/config' require 'pg' module FlydataCore module Postgresql class CompatibilityChecker def initialize(option = {}) option ||= {} @option = option.merge FlydataCore::Postgresql::Config.build_db_opts(option) end def do_check(option = @option, &block) result = block.call create_query(option) check_result(result, option) end # Override #def create_query(option = @option) #end # Override #def validate_result(result, option = @option) #end end class PostgresqlCompatibilityChecker < CompatibilityChecker def do_check(option = @option, &block) query = create_query(option) result = if block block.call query else exec_query(query) end check_result(result, option) end def exec_query(query) begin client = PGconn.connect(FlydataCore::Postgresql::Config.opts_for_pg(@option)) client.exec(query) ensure client.close rescue nil if client end end def schema_and_table_in_query(option = @option) schema = if option[:schema].to_s.strip.empty? "select current_schema" else "'#{option[:schema]}'" end { schema_name: schema, table_names: option[:tables].collect{|tn| "'#{tn}'"}.join(',') } end end class TableExistenceChecker < PostgresqlCompatibilityChecker TABLE_EXISTENCE_CHECK_QUERY_TMPLT = <