<commit_before>module QueryGenerator

  class GeneratedQuery < ActiveRecord::Base
    unloadable if Rails.env.development? #Don't cache this class in development environment, even if in gem

    validates_presence_of :name
    validates_presence_of :main_model

    serialize :models, Array
    serialize :associations, Hash
    serialize :model_offsets, Hash
    serialize :columns, Array

    def join_method
      :joins
    end

    # Returns an array of string which contains all column names
    # for output
    #--------------------------------------------------------------
    def table_header_columns
      output_columns.map {|oc| [oc.full_column_name.sub(".", "_"), oc.name]}
    end

    def sql
      sql = main_model_object.view_sql(:all, build_query(main_model_object, :order_by => true))
      sql = sql.gsub(/SELECT (.*) FROM/, "SELECT #{build_columns} FROM")
    end

    def build_columns
      cols = []
      output_columns.each do |qc|
        cols << "#{qc.full_column_name} AS `#{qc.full_column_name}`"
      end
      cols.join(", ")
    end

    def build_query(record, options = {})
      query = {}
      joins = joins_for(record, options[:pretty_inspect])
      query[join_method] = joins if joins && joins.any?
      query[:group] = "#{record.table_name}.#{record.primary_key}"
      query[:order] = order_by if options[:order_by] && order_by.present?
      query[:limit] = options[:limit] if options[:limit]
      query
    end
    
    # Calculates the row amount this query will produce
    # This is pure SQL and can be used to confirm that the correct
    # amount of rows is returned by .execute
    #--------------------------------------------------------------
    def count
      query = {}
      joins = joins_for(main_model_object, false)
      query[join_method] = joins if joins && joins.any?
      query[:group] = "#{main_model_object.table_name}.#{main_model_object.primary_key}"
      main_model_object.count(query).values.sum
    end

    def execute(options = {})
      main_model_object.conneciton

      #query = build_query(main_model_object, options)
      #main_records = main_model_object.find(:all, query)
      #
      #rows = []
      #main_records.each do |record|
      #  record_rows = build_rows_for(record)
      #  record_rows = record_rows.reject {|r| r.include?(nil)}<commit_msg>IMPORTANT: Switched from using pure active-record to own SQL<commit_after>module QueryGenerator

  class GeneratedQuery < ActiveRecord::Base
    unloadable if Rails.env.development? #Don't cache this class in development environment, even if in gem

    validates_presence_of :name
    validates_presence_of :main_model

    serialize :models, Array
    serialize :associations, Hash
    serialize :model_offsets, Hash
    serialize :columns, Array

    def join_method
      :joins
    end

    # Returns an array of string which contains all column names
    # for output
    #--------------------------------------------------------------
    def table_header_columns
      output_columns.map {|oc| [oc.full_column_name.sub(".", "_"), oc.name]}
    end

    def sql(what = build_columns)
      sql = main_model_object.view_sql(:all, build_query(main_model_object, :order_by => true))
      sql = sql.gsub(/SELECT (.*) FROM/, "SELECT #{what} FROM")
    end

    def build_columns
      cols = []
      output_columns.each do |qc|
        cols << "#{qc.full_column_name} AS `#{qc.full_column_name}`"
      end
      cols.join(", ")
    end

    def build_query(record, options = {})
      query = {}
      joins = joins_for(record, options[:pretty_inspect])
      query[join_method] = joins if joins && joins.any?
      query[:order] = order_by if options[:order_by] && order_by.present?
      query[:limit] = options[:limit] if options[:limit]
      query
    end
    
    # Calculates the row amount this query will produce
    # This is pure SQL and can be used to confirm that the correct
    # amount of rows is returned by .execute
    #--------------------------------------------------------------
    def count
      main_model_object.connection.select_all(sql("COUNT(*)")).first.values.first.to_i
    end

    def execute(options = {})
      joined_records = main_model_object.connection.select_all(sql)
      rows = []
      joined_records.each do |jr|
        row = Array.new(output_columns.size)
        output_columns.each_index do |index|
          row[index] = jr[output_columns[index].full_column_name]
        end
        rows << row
      end

      rows


      #query = build_query(main_model_object, options)
      #main_records = main_model_object.find(:all, query)
      #
      #rows = []
      #main_records.each do |record|
      #  record_rows = build_rows_for(record)
      #  record_rows = record_rows.reject {|r| r.include?(nil)}
{'fraction_non_alphanumeric': 0.15079034941763728, 'fraction_numerical': 0.0, 'mean_word_length': 2.41305890702626, 'pattern_counts': {'":': 0, '<': 11, '<?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': '968449', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1398, 'n_words': 356}