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

Constants

SQLSERVER_STATEMENT_PREFIX
SQLSERVER_STATEMENT_REGEXP

Public Instance Methods

exec_explain(queries) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb, line 10
def exec_explain(queries)
  unprepared_queries = queries.map do |(sql, binds)|
    [unprepare_sqlserver_statement(sql, binds), binds]
  end
  super(unprepared_queries)
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 statment which uses sp_executesql to just the first argument, then unquote it. Likewise our `sp_executesql` method should substitude the @n args with the quoted values.

# File lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb, line 22
def unprepare_sqlserver_statement(sql, binds)
  return sql unless sql.starts_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 = connection.quote(bind)
    executesql = executesql.sub("@#{index}", value)
  end

  executesql
end