<commit_before># encoding: utf-8

# Base class for Connector Providers
# that use FDW to import data through a foreign table

module CartoDB
  module Importer2
    class Connector
      class Provider
        def initialize(params = {})
          @params = Parameters.new(params, required: required_parameters + [:provider], optional: optional_parameters)
        end

        def errors
          @params.errors
        end

        def valid?
          errors.empty?
        end

        def validate!
          errors = self.errors
          raise InvalidParametersError.new(errors * "\n") if errors.present?
        end

        # Name of the table to be imported
        def table_name
          must_be_defined_in_derived_class
        end

        # SQL code to create the FDW server
        def create_server_command(_server_name)
          must_be_defined_in_derived_class
        end

        # SQL code to create the usermap for the user and postgres roles
        def create_usermap_command(_server_name, _username)
          must_be_defined_in_derived_class
        end

        # SQL code to create the foreign table used for importing
        def create_foreign_table_command(_server_name, _schema_name, _table_name, _foreign_prefix, _username)
          must_be_defined_in_derived_class
        end

        # SQL code to drop the FDW server
        def drop_server_command(server_name)
          fdw_drop_server server_name
        end

        # SQL code to drop the user mapping
        def drop_usermap_command(server_name, user)
          fdw_drop_usermap server_name, user
        end

        # SQL code to drop the foreign table
        def drop_foreign_table_command(schema_name, table_name)
          fdw_drop_foreign_table schema_name, table_name
        end

        # Parameters required by this connector provider
        def required_parameters
          must_be_defined_in_derived_class
        end

        # Optional parameters accepted by this connector provider
        def optional_parameters
          must_be_defined_in_derived_class
        end

        # Parameters accepted by this connector provider
        def accepted_parameters
          required_parameters + optional_parameters
        end

        def self.information
          # For convenience we'll use instance methods to provide the information
          # en each class. Otherwise all the information needed by such methods
          # would have to be defined in class methods too.
          test_provider = new({})
          {
            features: test_provider.features_information,
            parameters: test_provider.parameters_information
          }
        end

        def features_information
          must_be_defined_in_derived_class
        end

        def parameters_information
          # TODO: add templates with parameter descriptions
          info = {}
          required_parameters.each do |name|
            # TODO: description = load template for parameter name of @provider.name
            info[name.to_s] = {
              required: true,
              connection: false
            }
          end
          optional_parameters.each do |name|
            # TODO: description = load template for parameter name of @provider.name
            info[name.to_s] = {
              required: false,
              connection: false
            }
          end
          info
        end

        private

        include FdwSupport

        def must_be_defined_in_derived_class
          raise "Method #{caller_locations(1, 1)[0].label} must be defined in derived class"
        end

      end

    end
  end
end<commit_msg>Use NotImplementedError<commit_after># encoding: utf-8

# Base class for Connector Providers
# that use FDW to import data through a foreign table

module CartoDB
  module Importer2
    class Connector
      class Provider
        def initialize(params = {})
          @params = Parameters.new(params, required: required_parameters + [:provider], optional: optional_parameters)
        end

        def errors
          @params.errors
        end

        def valid?
          errors.empty?
        end

        def validate!
          errors = self.errors
          raise InvalidParametersError.new(errors * "\n") if errors.present?
        end

        # Name of the table to be imported
        def table_name
          must_be_defined_in_derived_class
        end

        # SQL code to create the FDW server
        def create_server_command(_server_name)
          must_be_defined_in_derived_class
        end

        # SQL code to create the usermap for the user and postgres roles
        def create_usermap_command(_server_name, _username)
          must_be_defined_in_derived_class
        end

        # SQL code to create the foreign table used for importing
        def create_foreign_table_command(_server_name, _schema_name, _table_name, _foreign_prefix, _username)
          must_be_defined_in_derived_class
        end

        # SQL code to drop the FDW server
        def drop_server_command(server_name)
          fdw_drop_server server_name
        end

        # SQL code to drop the user mapping
        def drop_usermap_command(server_name, user)
          fdw_drop_usermap server_name, user
        end

        # SQL code to drop the foreign table
        def drop_foreign_table_command(schema_name, table_name)
          fdw_drop_foreign_table schema_name, table_name
        end

        # Parameters required by this connector provider
        def required_parameters
          must_be_defined_in_derived_class
        end

        # Optional parameters accepted by this connector provider
        def optional_parameters
          must_be_defined_in_derived_class
        end

        # Parameters accepted by this connector provider
        def accepted_parameters
          required_parameters + optional_parameters
        end

        def self.information
          # For convenience we'll use instance methods to provide the information
          # en each class. Otherwise all the information needed by such methods
          # would have to be defined in class methods too.
          test_provider = new({})
          {
            features: test_provider.features_information,
            parameters: test_provider.parameters_information
          }
        end

        def features_information
          must_be_defined_in_derived_class
        end

        def parameters_information
          # TODO: add templates with parameter descriptions
          info = {}
          required_parameters.each do |name|
            # TODO: description = load template for parameter name of @provider.name
            info[name.to_s] = {
              required: true,
              connection: false
            }
          end
          optional_parameters.each do |name|
            # TODO: description = load template for parameter name of @provider.name
            info[name.to_s] = {
              required: false,
              connection: false
            }
          end
          info
        end

        private

        include FdwSupport

        def must_be_defined_in_derived_class
          raise NotImplementedError, "Method #{caller_locations(1, 1)[0].label} must be defined in derived class"
        end

      end

    end
  end
end
{'fraction_non_alphanumeric': 0.03938939621098542, 'fraction_numerical': 0.001362954886193267, 'mean_word_length': 1.9720534629404618, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2401283', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1771, 'n_words': 587}