Sha256: c5e418f3baaa0a48320ded00c5a46b31d209eab57373bd3eb1977948c5a69b58

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "active_record_simple_execute/version"

require "active_support/lazy_load_hooks"

ActiveSupport.on_load(:active_record) do

  ActiveRecord::ConnectionAdapters::DatabaseStatements.module_eval do
    def simple_execute(sql_str, **sql_vars)
      readonly = sql_str.strip.downcase.start_with?("select ")

      if readonly
        if Rails::VERSION::STRING.to_f >= 7.1
          sanitized_sql = Arel.sql(sql_str, **sql_vars)
        else
          sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, **sql_vars])
        end
        query_result = select_all(sanitized_sql)
      else
        # Must use sanitize_sql_array, since Arel.sql is not yet compatible with `exec_query` or `execute`, https://github.com/rails/rails/pull/53740

        sanitized_sql = ActiveRecord::Base.sanitize_sql_array([sql_str, **sql_vars])
        query_result = exec_query(sanitized_sql)
      end

      records = query_result.to_a

      return records
    end
  end

  ActiveRecord::Base.class_eval do
    def self.simple_execute(sql_str, **sql_vars)
      self.connection.simple_execute(sql_str, **sql_vars)
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_simple_execute-1.1.0 lib/active_record_simple_execute.rb