Sha256: f28df5cee099eeec7a82bf1c75b546bb12482f390cdd815efa1020d9aede33b3

Contents?: true

Size: 1.76 KB

Versions: 13

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module ConnectionAdapters
    module SQLServer
      module CoreExt
        module Explain
          SQLSERVER_STATEMENT_PREFIX = "EXEC sp_executesql "
          SQLSERVER_STATEMENT_REGEXP = /N'(.+)', N'(.+)', (.+)/

          def exec_explain(queries, options = [])
            return super unless connection.adapter_name == "SQLServer"

            unprepared_queries = queries.map do |(sql, binds)|
              [unprepare_sqlserver_statement(sql, binds), binds]
            end
            super(unprepared_queries, options)
          end

          private

          # This is somewhat hacky, but it should reliably reformat our prepared sql statement
          # which uses sp_executesql to just the first argument, then unquote it. Likewise our
          # `sp_executesql` method should substitute the @n args with the quoted values.
          def unprepare_sqlserver_statement(sql, binds)
            return sql unless sql.start_with?(SQLSERVER_STATEMENT_PREFIX)

            executesql = sql.from(SQLSERVER_STATEMENT_PREFIX.length)
            executesql = executesql.match(SQLSERVER_STATEMENT_REGEXP).to_a[1]

            binds.each_with_index do |bind, index|

              value = if bind.is_a?(::ActiveModel::Attribute)  then
                connection.quote(bind.value_for_database)
              else
                connection.quote(bind)
              end
              executesql = executesql.sub("@#{index}", value)
            end

            executesql
          end
        end
      end
    end
  end
end

ActiveSupport.on_load(:active_record) do
  extend ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Explain
  ActiveRecord::Relation.include(ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Explain)
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
activerecord-sqlserver-adapter-7.1.9 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.8 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.7 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.6 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.5 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.4 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.3 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.2 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.1 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.0 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.0.rc2 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.0.rc1 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-7.1.0.beta1 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb