<commit_before>require_relative '../database'

# Represents a query for the SQLite3 database.
class Query < Database
  @distinct = false

  # Allows getting of the results instance variable.
  def result
    @result
  end

  # Allows getting of the errors encountered in the last execute.
  def errors
    @errors
  end

  # Creates a query. Requires at least a table name.
  # @param table [String] A table name.
  # @option type [Symbol] The type of operation this query is for. Defaults to select.
  def initialize(table, type = :select)
    @table = table # Table name
    @type = type # Type of query (select, insert, etc.)
  end

  # Adds a WHERE clause to the statement. Can be called multiple times for one query to add more conditions.
  # @param condition [String, Array] Either a string that follows SQL convention or an array of such
  # with interpolation.
  #   Example: .where(["user_id = ?", user_id])
  def where(condition)
    @conditions ||= []
    if condition.is_a? String
      @conditions << condition
    else
      condition[0].gsub!("?", "%s")
      @conditions << condition[0] % condition[1..-1]
    end
  end

  # The accessor for @conditions so we can check if it's nil or not before plunking it into a query
  # when resolved.
  def conditions
    if @conditions.nil? or @conditions.empty?
      ["1 = 1"]
    else
      @conditions
    end
  end

  # Sets the fields that we'll be operating on in the table.
  # @param fields [Array] An array containing all the field names.
  def fields=(fields)
    @fields = fields
  end

  # Sets the values that we'll be inserting. Only for queries of type :insert.
  # @param values [Array] An array containing all the values.
  def values=(values)
    values.map! { |value|
      if value.is_a? String
        "\"%s\"" % value
      else
        value
      end
    }
    @values = values
  end

  # Marks this query as requiring the DISTINCT modifier.
  def distinct
    @distinct = true
  end

  # Resolves the Query object to an executable format.
  def resolve
    case @type
    when :select
      "SELECT %{distinct}%{fields} FROM %{table} WHERE %{conditions};" % {
        table:      @table,
        fields:     if @fields.nil? then "id,*" else @fields.join(",") end,
        conditions: conditions.join(" AND "),
        distinct:   if @distinct then " DISTINCT" else "" end,
      }
    when :insert
      "INSERT INTO %{table} (%{fields}) VALUES (%{values});" % {
        table:  @table,
        fields: @fields.join(","),
        values: @values.join(",").dump
      }<commit_msg>Individual dumps<commit_after>require_relative '../database'

# Represents a query for the SQLite3 database.
class Query < Database
  @distinct = false

  # Allows getting of the results instance variable.
  def result
    @result
  end

  # Allows getting of the errors encountered in the last execute.
  def errors
    @errors
  end

  # Creates a query. Requires at least a table name.
  # @param table [String] A table name.
  # @option type [Symbol] The type of operation this query is for. Defaults to select.
  def initialize(table, type = :select)
    @table = table # Table name
    @type = type # Type of query (select, insert, etc.)
  end

  # Adds a WHERE clause to the statement. Can be called multiple times for one query to add more conditions.
  # @param condition [String, Array] Either a string that follows SQL convention or an array of such
  # with interpolation.
  #   Example: .where(["user_id = ?", user_id])
  def where(condition)
    @conditions ||= []
    if condition.is_a? String
      @conditions << condition
    else
      condition[0].gsub!("?", "%s")
      @conditions << condition[0] % condition[1..-1]
    end
  end

  # The accessor for @conditions so we can check if it's nil or not before plunking it into a query
  # when resolved.
  def conditions
    if @conditions.nil? or @conditions.empty?
      ["1 = 1"]
    else
      @conditions
    end
  end

  # Sets the fields that we'll be operating on in the table.
  # @param fields [Array] An array containing all the field names.
  def fields=(fields)
    @fields = fields
  end

  # Sets the values that we'll be inserting. Only for queries of type :insert.
  # @param values [Array] An array containing all the values.
  def values=(values)
    values.map! { |value|
      if value.is_a? String
        "\"%s\"" % value
      else
        value
      end
    }
    @values = values
  end

  # Marks this query as requiring the DISTINCT modifier.
  def distinct
    @distinct = true
  end

  # Resolves the Query object to an executable format.
  def resolve
    case @type
    when :select
      "SELECT %{distinct}%{fields} FROM %{table} WHERE %{conditions};" % {
        table:      @table,
        fields:     if @fields.nil? then "id,*" else @fields.join(",") end,
        conditions: conditions.join(" AND "),
        distinct:   if @distinct then " DISTINCT" else "" end,
      }
    when :insert
      "INSERT INTO %{table} (%{fields}) VALUES (%{values});" % {
        table:  @table,
        fields: @fields.join(","),
        values: @values.map { |x| x.dump }.join(",")
      }
{'fraction_non_alphanumeric': 0.10558040054442933, 'fraction_numerical': 0.0027221466070386936, 'mean_word_length': 2.625088090204369, 'pattern_counts': {'":': 0, '<': 13, '<?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': '730505', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1496, 'n_words': 612}