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

Constants

SQLSERVER_PARAM_MATCHER
SQLSERVER_STATEMENT_PREFIX

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 { |sql, bind| [unprepare_sqlserver_statement(sql), bind] }
  super(unprepared_queries)
end

Private Instance Methods

unprepare_sqlserver_statement(sql) 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 withe the quoted values.

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