<commit_before>module Carto
  class FederatedTablesService
    def initialize(user:)
      @user = user
      @superuser_db_connection = @user.in_database(as: :superuser)
      @user_db_connection = @user.in_database()
    end

    # Federated Servers

    def list_servers(pagination)
      pagination[:offset] = (pagination[:page] - 1) * pagination[:per_page]
      @user_db_connection[
        select_federated_servers_query(pagination)
      ].all
    end

    def count_servers
      @user_db_connection[count_federated_servers_query].first[:count]
    end

    def register_server(attributes)
      @superuser_db_connection[register_federated_server_query(attributes)].first
      grant_access_to_federated_server(
        federated_server_name: attributes[:federated_server_name],
        db_role: @user.database_username
       )
      get_server(federated_server_name: attributes[:federated_server_name])
    end

    def grant_access_to_federated_server(federated_server_name:, db_role:)
      @superuser_db_connection[
        grant_access_to_federated_server_query(
          federated_server_name: federated_server_name,
          db_role: db_role
        )
      ].all
    end

    def get_server(federated_server_name:)
      @user_db_connection[get_federated_server_query(federated_server_name: federated_server_name)].first
    end

    def update_server(federated_server_name, attributes)
      unregister_server(federated_server_name: federated_server_name)
      attributes[:federated_server_name] = federated_server_name
      register_server(attributes)
      get_server(federated_server_name: attributes[:federated_server_name])
    end

    def unregister_server(federated_server_name:)
      @superuser_db_connection[
        unregister_federated_server_query(federated_server_name: federated_server_name)
      ].first
    end

    def revoke_access_to_federated_server(federated_server_name:, db_role:)
      @superuser_db_connection[
        revoke_access_to_federated_server_query(
          federated_server_name: federated_server_name,
          db_role: db_role
        )
      ].all
    end

    # Remote Schemas

    def list_remote_schemas(federated_server_name, pagination)
      pagination[:offset] = (pagination[:page] - 1) * pagination[:per_page]
      @user_db_connection[
        select_remote_schemas_query(federated_server_name, pagination)
      ].all
    end

    def count_remote_schemas(federated_server_name)
      @user_db_connection[
        count_remote_schemas_query(federated_server_name)
      ].first[:count]
    end

    # Remote Tables

    def list_remote_tables(federated_server_name, remote_schema_name, pagination)
      pagination[:offset] = (pagination[:page] - 1) * pagination[:per_page]
      remote_tables = @user_db_connection[
        select_remote_tables_query(federated_server_name, remote_schema_name, pagination)
      ].all
      remote_tables.each do |remote_table|
        remote_table[:columns] = JSON.parse(remote_table[:columns], symbolize_names: true)
      end
      return remote_tables
    end

    def count_remote_tables(federated_server_name, remote_schema_name)
      @user_db_connection[
        count_remote_tables_query(federated_server_name, remote_schema_name)
      ].first[:count]
    end

    def register_table(attributes)
      @user_db_connection[register_remote_table_query(attributes)].first
      get_remote_table(
        federated_server_name: attributes[:federated_server_name],
        remote_schema_name: attributes[:remote_schema_name],
        remote_table_name: attributes[:remote_table_name]
      )
    end

    def get_remote_table(federated_server_name:, remote_schema_name:, remote_table_name:)
      remote_table = @user_db_connection[
        get_remote_table_query(
          federated_server_name: federated_server_name,
          remote_schema_name: remote_schema_name,
          remote_table_name: remote_table_name
        )
      ].first
      unless remote_table.nil?
        remote_table[:columns] = JSON.parse(remote_table[:columns], symbolize_names: true)
      end
      return remote_table
    end

    def update_table(attributes)
      unregister_table(
        federated_server_name: attributes[:federated_server_name],
        remote_schema_name: attributes[:remote_schema_name],
        remote_table_name: attributes[:remote_table_name]
      )
      register_table(attributes)
      get_remote_table(
        federated_server_name: attributes[:federated_server_name],
        remote_schema_name: attributes[:remote_schema_name],
        remote_table_name: attributes[:remote_table_name]
      )
    end

    def unregister_table(federated_server_name:, remote_schema_name:, remote_table_name:)
      @user_db_connection[
        unregister_remote_table_query(
          federated_server_name: federated_server_name,
          remote_schema_name: remote_schema_name,
          remote_table_name: remote_table_name
        )
      ].first
    end

    private

    def select_federated_servers_query(pagination)
      %{
        SELECT * FROM (#{select_servers_query}) AS federated_servers
        ORDER BY #{pagination[:order]} #{pagination[:direction]}
        LIMIT #{pagination[:per_page]}
        OFFSET #{pagination[:offset]}
      }.squish<commit_msg>Use keyword arguments<commit_after>module Carto
  class FederatedTablesService
    def initialize(user:)
      @user = user
      @superuser_db_connection = @user.in_database(as: :superuser)
      @user_db_connection = @user.in_database()
    end

    # Federated Servers

    def list_servers(order:, direction:, per_page:, page:)
      offset = (page - 1) * per_page
      @user_db_connection[
        select_federated_servers_query(order: order, direction: direction, per_page: per_page, offset: offset)
      ].all
    end

    def count_servers
      @user_db_connection[count_federated_servers_query].first[:count]
    end

    def register_server(attributes)
      @superuser_db_connection[register_federated_server_query(attributes)].first
      grant_access_to_federated_server(
        federated_server_name: attributes[:federated_server_name],
        db_role: @user.database_username
       )
      get_server(federated_server_name: attributes[:federated_server_name])
    end

    def grant_access_to_federated_server(federated_server_name:, db_role:)
      @superuser_db_connection[
        grant_access_to_federated_server_query(
          federated_server_name: federated_server_name,
          db_role: db_role
        )
      ].all
    end

    def get_server(federated_server_name:)
      @user_db_connection[get_federated_server_query(federated_server_name: federated_server_name)].first
    end

    def update_server(federated_server_name, attributes)
      unregister_server(federated_server_name: federated_server_name)
      attributes[:federated_server_name] = federated_server_name
      register_server(attributes)
      get_server(federated_server_name: attributes[:federated_server_name])
    end

    def unregister_server(federated_server_name:)
      @superuser_db_connection[
        unregister_federated_server_query(federated_server_name: federated_server_name)
      ].first
    end

    def revoke_access_to_federated_server(federated_server_name:, db_role:)
      @superuser_db_connection[
        revoke_access_to_federated_server_query(
          federated_server_name: federated_server_name,
          db_role: db_role
        )
      ].all
    end

    # Remote Schemas

    def list_remote_schemas(federated_server_name, pagination)
      pagination[:offset] = (pagination[:page] - 1) * pagination[:per_page]
      @user_db_connection[
        select_remote_schemas_query(federated_server_name, pagination)
      ].all
    end

    def count_remote_schemas(federated_server_name)
      @user_db_connection[
        count_remote_schemas_query(federated_server_name)
      ].first[:count]
    end

    # Remote Tables

    def list_remote_tables(federated_server_name, remote_schema_name, pagination)
      pagination[:offset] = (pagination[:page] - 1) * pagination[:per_page]
      remote_tables = @user_db_connection[
        select_remote_tables_query(federated_server_name, remote_schema_name, pagination)
      ].all
      remote_tables.each do |remote_table|
        remote_table[:columns] = JSON.parse(remote_table[:columns], symbolize_names: true)
      end
      return remote_tables
    end

    def count_remote_tables(federated_server_name, remote_schema_name)
      @user_db_connection[
        count_remote_tables_query(federated_server_name, remote_schema_name)
      ].first[:count]
    end

    def register_table(attributes)
      @user_db_connection[register_remote_table_query(attributes)].first
      get_remote_table(
        federated_server_name: attributes[:federated_server_name],
        remote_schema_name: attributes[:remote_schema_name],
        remote_table_name: attributes[:remote_table_name]
      )
    end

    def get_remote_table(federated_server_name:, remote_schema_name:, remote_table_name:)
      remote_table = @user_db_connection[
        get_remote_table_query(
          federated_server_name: federated_server_name,
          remote_schema_name: remote_schema_name,
          remote_table_name: remote_table_name
        )
      ].first
      unless remote_table.nil?
        remote_table[:columns] = JSON.parse(remote_table[:columns], symbolize_names: true)
      end
      return remote_table
    end

    def update_table(attributes)
      unregister_table(
        federated_server_name: attributes[:federated_server_name],
        remote_schema_name: attributes[:remote_schema_name],
        remote_table_name: attributes[:remote_table_name]
      )
      register_table(attributes)
      get_remote_table(
        federated_server_name: attributes[:federated_server_name],
        remote_schema_name: attributes[:remote_schema_name],
        remote_table_name: attributes[:remote_table_name]
      )
    end

    def unregister_table(federated_server_name:, remote_schema_name:, remote_table_name:)
      @user_db_connection[
        unregister_remote_table_query(
          federated_server_name: federated_server_name,
          remote_schema_name: remote_schema_name,
          remote_table_name: remote_table_name
        )
      ].first
    end

    private

    def select_federated_servers_query(order:, direction:, per_page:, offset:)
      %{
        SELECT * FROM (#{select_servers_query}) AS federated_servers
        ORDER BY #{order} #{direction}
        LIMIT #{per_page}
        OFFSET #{offset}
      }.squish
{'fraction_non_alphanumeric': 0.07182580827599208, 'fraction_numerical': 0.0005655575454802526, 'mean_word_length': 3.707187222715173, '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': '6951759', 'n_tokens_mistral': 3314, 'n_tokens_neox': 3165, 'n_words': 464}