Sha256: b0256d8641e446762e5445b18375105053776101579a31af0eaafa161d570854

Contents?: true

Size: 1.5 KB

Versions: 19

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
          # `sp_executesql` 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

19 entries across 19 versions & 2 rubygems

Version Path
activerecord-sqlserver-adapter-4.2.18 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.17 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.16 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter_new-4.2.15 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.15 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.13 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.12 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.11 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.10 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.9 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.8 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.6 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.5 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.4 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.3 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.2 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.1 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.0 lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb
activerecord-sqlserver-adapter-4.2.0.pre lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb