<commit_before>module Netzke::Basepack::DataAdapters
  class SequelAdapter < AbstractAdapter
    def self.for_class?(model_class)
      model_class <= Sequel::Model
    end

    def get_records(params, columns=[])
    end

    def count_records(params, columns=[])
    end

    def map_type type
      @typemap ||= {
      }
      @typemap[type]
    end

    def get_assoc_property_type assoc_name, prop_name
    end

    # like get_assoc_property_type but for non-association columns
    def get_property_type column
    end

    def column_virtual? c
    end

    # Returns options for comboboxes in grids/forms
    def combobox_options_for_column(column, method_options = {})
    end

    def foreign_key_for assoc_name
    end

    # Returns the model class for association columns
    def klass_for assoc_name
    end

    def destroy(ids)
    end

    def find_record(id)
    end

    # Build a hash of foreign keys and the associated model
    def hash_fk_model
    end

    def move_records(params)
    end

    # Needed for seed and tests
    def last
    end

    def destroy_all
    end

    private

    def apply_column_filters(relation, column_filter)
    end

  end

end<commit_msg>implemented get_records method for Sequel<commit_after>module Netzke::Basepack::DataAdapters
  class SequelAdapter < AbstractAdapter
    def self.for_class?(model_class)
      model_class <= Sequel::Model
    end

    def get_records(params, columns=[])
      dataset = @model_class

      graphed=[]

      # Parses and applies grid column filters
      #
      # Example column grid data:
      #
      #     {"0" => {
      #       "data" => {
      #         "type" => "numeric",
      #         "comparison" => "gt",
      #         "value" => 10 },
      #       "field" => "id"
      #     },
      #     "1" => {
      #       "data" => {
      #         "type" => "string",
      #         "value" => "pizza"
      #       },
      #       "field" => "food_name"
      #     }}
      #

      if params[:filter]
        # these are still JSON-encoded due to the migration to Ext.direct
        column_filter=JSON.parse(params[:filter])

        column_filter.each do |v|
          field = v["field"]
          assoc, method = field.split('__')
          if method
            # when filtering on association's columns, we need to graph for LEFT OUTER JOIN
            dataset = dataset.eager_graph assoc.to_sym unless graphed.include? assoc.to_sym
            graphed << assoc.to_sym
          end

          value = v["value"]
          type = v["type"]
          op = v["comparison"]

          if type == "string"
            # strings are always LIKEd (case-insensitive)
            dataset = dataset.filter field.to_sym.ilike("%#{value}%")
          else
            if type == "date"
              # convert value to the DB date
              value.match /(\d\d)\/(\d\d)\/(\d\d\d\d)/
              value = "#{$3}-#{$1}-#{$2}"
            end
            # if it's NOT an association column, we need to qualify column name with model's table_name
            qualified_column_name = method ? field.to_sym : field.to_sym.qualify(@model_class.table_name)
            case op
            when 'eq'
              dataset = dataset.filter qualified_column_name => value
            when 'lt'
              dataset = dataset.filter ":column < #{value}", :column => qualified_column_name
            when 'gt'
              dataset = dataset.filter ":column > #{value}", :column => qualified_column_name
            end
          end
        end
      end

      # sorting
      if params[:sort] && sort_params = params[:sort]
        sort_params.each do |sort_param|
          assoc, method = sort_param["property"].split("__")
          dir = sort_param["direction"].downcase

          # if a sorting scope is set, call the scope with the given direction
          column = columns.detect { |c| c[:name] == sort_param["property"] }
          if column.try(:'has_key?', :sorting_scope)
            dataset = dataset.send(column[:sorting_scope].to_sym, dir.to_sym)
          else
            if method # sorting on associations column
              # graph the association for LEFT OUTER JOIN
              dataset = dataset.eager_graph(assoc.to_sym) unless graphed.include? assoc.to_sym
              graphed << assoc.to_sym
            end
            # coincidentally, netzkes convention of specifying association's attributes
            # i.e. "author__name" on Book matches sequel's convention
            # so we can just pass symbolized property here
            dataset = dataset.order(sort_param["property"].to_sym)
          end
        end
      end

      # eager load the associations indicated by columns,
      # but only if we didn't eager_graph them before (for ordering/filtering)
      # because this saves a ID IN query
      columns.each do |column|
        if column[:name].index('__')
          assoc, _ = column[:name].split('__')
          dataset = dataset.eager(assoc.to_sym) unless graphed.include? assoc.to_sym
        end
      end

      # apply paging
      if params[:limit]
        if params[:start]
          dataset = dataset.limit params[:limit], params[:start]
        else
          dataset = dataset.limit params[:limit]
        end
      end
      # apply scope
      dataset = dataset.extend_with(params[:scope]) if params[:scope]

      dataset.all
    end

    def count_records(params, columns=[])
    end

    def map_type type
      @typemap ||= {
      }
      @typemap[type]
    end

    def get_assoc_property_type assoc_name, prop_name
    end

    # like get_assoc_property_type but for non-association columns
    def get_property_type column
    end

    def column_virtual? c
    end

    # Returns options for comboboxes in grids/forms
    def combobox_options_for_column(column, method_options = {})
    end

    def foreign_key_for assoc_name
    end

    # Returns the model class for association columns
    def klass_for assoc_name
    end

    def destroy(ids)
    end

    def find_record(id)
    end

    # Build a hash of foreign keys and the associated model
    def hash_fk_model
    end

    def move_records(params)
    end

    # Needed for seed and tests
    def last
    end

    def destroy_all
    end

  end

end
{'fraction_non_alphanumeric': 0.0878754171301446, 'fraction_numerical': 0.0011123470522803114, 'mean_word_length': 1.991444866920152, 'pattern_counts': {'":': 2, '<': 12, '<?xml version=': 0, '>': 18, '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': '5006575', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1786, 'n_words': 516}