Sha256: d29bdc5c60ab31bcef1289048f9adcf7e81ba06761f06f0e1975074528bc6dbb

Contents?: true

Size: 1.5 KB

Versions: 13

Compression:

Stored size: 1.5 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    module Sqlserver
      module CoreExt
        module Explain
          SQLSERVER_STATEMENT_PREFIX = 'EXEC sp_executesql '
          SQLSERVER_PARAM_MATCHER = /@\d+ =/

          def exec_explain(queries)
            unprepared_queries = queries.map { |sql, bind| [unprepare_sqlserver_statement(sql), bind] }
            super(unprepared_queries)
          end

          private

          # This is somewhat hacky, but it should reliably reformat our prepared sql statment
          # which uses sp_executesql to just the first argument, then unquote it. Likewise our
          # do_exec_query method should substitude the @n args withe the quoted values.
          def unprepare_sqlserver_statement(sql)
            if sql.starts_with?(SQLSERVER_STATEMENT_PREFIX)
              executesql = sql.from(SQLSERVER_STATEMENT_PREFIX.length)
              executesql_args = executesql.split(', ')
              found_args = executesql_args.reject! { |arg| arg =~ SQLSERVER_PARAM_MATCHER }
              executesql_args.pop if found_args && executesql_args.many?
              executesql = executesql_args.join(', ').strip.match(/N'(.*)'/m)[1]
              Utils.unquote_string(executesql)
            else
              sql
            end
          end
        end
      end
    end
  end
end

ActiveRecord::Base.extend ActiveRecord::ConnectionAdapters::Sqlserver::CoreExt::Explain
ActiveRecord::Relation.send :include, ActiveRecord::ConnectionAdapters::Sqlserver::CoreExt::Explain

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
activerecord-sqlserver-adapter-4.1.8 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.0.4 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.1.7 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.0.3 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.1.6 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.0.2 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.1.5 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.1.4 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.0.1 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.1.2 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.1.1 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.1.0 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.0.0 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb