Sha256: b72bb284766fada2dfbfbd4e60424cc3d8546323415e055df8811f5549ad4178

Contents?: true

Size: 1.84 KB

Versions: 21

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require "active_record/connection_adapters/sqlserver/showplan/printer_table"
require "active_record/connection_adapters/sqlserver/showplan/printer_xml"

module ActiveRecord
  module ConnectionAdapters
    module SQLServer
      module Showplan
        OPTION_ALL  = "SHOWPLAN_ALL"
        OPTION_TEXT = "SHOWPLAN_TEXT"
        OPTION_XML  = "SHOWPLAN_XML"
        OPTIONS = [OPTION_ALL, OPTION_TEXT, OPTION_XML]

        def explain(arel, binds = [])
          sql = to_sql(arel)
          result = with_showplan_on { sp_executesql(sql, "EXPLAIN", binds) }
          printer = showplan_printer.new(result)
          printer.pp
        end

        protected

        def with_showplan_on
          set_showplan_option(true)
          yield
        ensure
          set_showplan_option(false)
        end

        def set_showplan_option(enable = true)
          sql = "SET #{showplan_option} #{enable ? 'ON' : 'OFF'}"
          raw_connection_do(sql)
        rescue Exception
          raise ActiveRecordError, "#{showplan_option} could not be turned #{enable ? 'ON' : 'OFF'}, perhaps you do not have SHOWPLAN permissions?"
        end

        def showplan_option
          (SQLServerAdapter.showplan_option || OPTION_ALL).tap do |opt|
            raise(ArgumentError, "Unknown SHOWPLAN option #{opt.inspect} found.") if OPTIONS.exclude?(opt)
          end
        end

        def showplan_all?
          showplan_option == OPTION_ALL
        end

        def showplan_text?
          showplan_option == OPTION_TEXT
        end

        def showplan_xml?
          showplan_option == OPTION_XML
        end

        def showplan_printer
          case showplan_option
          when OPTION_XML then PrinterXml
          when OPTION_ALL, OPTION_TEXT then PrinterTable
          else PrinterTable
          end
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
activerecord-sqlserver-adapter-7.0.7 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.6 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.5.1 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.5.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.4.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.3.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.0.3 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.2.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.1.3.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.1.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.0.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-7.0.0.0.rc1 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.1.2.1 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.1.2.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.1.1.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.1.0.0 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.1.0.0.rc1 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.0.2 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.0.1 lib/active_record/connection_adapters/sqlserver/showplan.rb
activerecord-sqlserver-adapter-6.0.0 lib/active_record/connection_adapters/sqlserver/showplan.rb