module ActiveRecord::ConnectionAdapters::SQLServer::CoreExt::Explain

Constants

SQLSERVER_STATEMENT_PREFIX
SQLSERVER_STATEMENT_REGEXP

Public Instance Methods

exec_explain(queries, options = []) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb, line 11
def exec_explain(queries, options = [])
  with_connection do |connection|
    return super(queries, options) unless connection.sqlserver?

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

    super(unprepared_queries, options)
  end
end

Private Instance Methods

unprepare_sqlserver_statement(sql, binds) click to toggle source

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.

# File lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb, line 28
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