<commit_before>require 'sqlite_magic/version'
require 'sqlite3'

module SqliteMagic

  extend self

  class Error            < StandardError;end
  class DatabaseError    < Error;end
  class NoSuchTable      < Error;end

  class Connection
    attr_reader :database
    def initialize(db_loc='sqlite.db', options={})
      busy_timeout = options.delete(:busy_timeout)
      @database = SQLite3::Database.new(db_loc, options)
      @database.busy_timeout = busy_timeout if busy_timeout
    end

    def add_columns(tbl_name, col_names)
      existing_cols = database.table_info(tbl_name).map{ |c| c['name'] }
      missing_cols = col_names.map(&:to_s) - existing_cols
      missing_cols.each do |col_name|
        database.execute("ALTER TABLE #{tbl_name} ADD COLUMN '#{col_name}'")
      end
    end

    def close
      database.close
    end

    def commit
      database.commit
    end

    def create_table(tbl_name, col_names, unique_keys=nil)
      puts "Now creating new table: #{tbl_name}" if verbose?
      query = unique_keys ? "CREATE TABLE #{tbl_name} (#{format_field_names_as_string(col_names)}, UNIQUE (#{format_field_names_as_string(unique_keys)}))" :
                            "CREATE TABLE #{tbl_name} (#{format_field_names_as_string(col_names)})"
      database.execute query
      if unique_keys && !unique_keys.empty?
        query = "CREATE UNIQUE INDEX IF NOT EXISTS #{unique_keys.join('_')} " +
          "ON #{tbl_name} (#{format_field_names_as_string(unique_keys)})"
        database.execute query
      end
    end

    def execute(query,data=nil)
      raw_response = data ? database.execute2(query, data) : database.execute2(query)
      keys = raw_response.shift # get the keys
      raw_response.map{|e| Hash[keys.zip(e)] }
    rescue SQLite3::SQLException => e
      puts "Exception (#{e.inspect}) raised" if verbose?
      case e.message
      when /no such table/
        raise NoSuchTable.new(e.message)
      else
        raise e
      end
    end

    # This is an (expensive) convenience method to insert a row (for given unique keys), or if the row already exists<commit_msg>Quote index name so it work even if using an SQL keyword<commit_after>require 'sqlite_magic/version'
require 'sqlite3'

module SqliteMagic

  extend self

  class Error            < StandardError;end
  class DatabaseError    < Error;end
  class NoSuchTable      < Error;end

  class Connection
    attr_reader :database
    def initialize(db_loc='sqlite.db', options={})
      busy_timeout = options.delete(:busy_timeout)
      @database = SQLite3::Database.new(db_loc, options)
      @database.busy_timeout = busy_timeout if busy_timeout
    end

    def add_columns(tbl_name, col_names)
      existing_cols = database.table_info(tbl_name).map{ |c| c['name'] }
      missing_cols = col_names.map(&:to_s) - existing_cols
      missing_cols.each do |col_name|
        database.execute("ALTER TABLE #{tbl_name} ADD COLUMN '#{col_name}'")
      end
    end

    def close
      database.close
    end

    def commit
      database.commit
    end

    def create_table(tbl_name, col_names, unique_keys=nil)
      puts "Now creating new table: #{tbl_name}" if verbose?
      query = unique_keys ? "CREATE TABLE #{tbl_name} (#{format_field_names_as_string(col_names)}, UNIQUE (#{format_field_names_as_string(unique_keys)}))" :
                            "CREATE TABLE #{tbl_name} (#{format_field_names_as_string(col_names)})"
      database.execute query
      if unique_keys && !unique_keys.empty?
        query = "CREATE UNIQUE INDEX IF NOT EXISTS '#{unique_keys.join('_')}' " +
          "ON #{tbl_name} (#{format_field_names_as_string(unique_keys)})"
        database.execute query
      end
    end

    def execute(query,data=nil)
      raw_response = data ? database.execute2(query, data) : database.execute2(query)
      keys = raw_response.shift # get the keys
      raw_response.map{|e| Hash[keys.zip(e)] }
    rescue SQLite3::SQLException => e
      puts "Exception (#{e.inspect}) raised" if verbose?
      case e.message
      when /no such table/
        raise NoSuchTable.new(e.message)
      else
        raise e
      end
    end

    # This is an (expensive) convenience method to insert a row (for given unique keys), or if the row already exists
{'fraction_non_alphanumeric': 0.10011695906432748, 'fraction_numerical': 0.0023391812865497076, 'mean_word_length': 3.0339622641509436, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '2181855', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1311, 'n_words': 336}