RowsLogger

This plugin offers rich information about result sets to AR logs.

Usage

Just enjoy Rails as usual.

Example

Consider some read operations like this.

Member.count
Member.find(:all)

That usually makes following log.

SQL (0.000300)   SELECT count(*) AS count_all FROM members
Member Load (0.000482)   SELECT * FROM members

RowsLogger appends information about rows count to the log.

SQL (0.000301) (1 Row)   SELECT count(*) AS count_all FROM members
Member Load (0.000415) (3 Rows)   SELECT * FROM members

For Developpers

This plugin modfies following methods.

'ConnectionAdapters::AbstractAdapter#log'
'ConnectionAdapters::AbstractAdapter#log_info'

<before> ConnectionAdapters::AbstractAdapter#log

--> log_info(sql, name, seconds)

<after> ConnectionAdapters::AbstractAdapter#log

--> log_info(sql, name, seconds, result = nil)
      --> log_result_info(result)
            --> ConcreteAdapter#count_result(result)

<an example for concrete adapter> ConnectionAdapters::MysqlAdapter#count_result

protected
def count_result(result)
  result.num_rows
end

Count Result Method

The ‘count_result’ method of Adapter class should return count of result set from ‘result’ object, where ‘result’ is an object generated by ‘log’ method. This is used as result information.

For exmaple, although this is nonsencial definition,

def count_result(result)
  0
end

this code always appends “(0 Rows)” to the log. The returned value is directly used even if it is not a numeric value. But no information will be appended in following cases.

1) when ‘count_result’ method returns nil 2) when ‘count_result’ method is not defined in current adapter

Note

‘count_result’ method should be defined as ‘protected’ or ‘public’ because we check whether it is implemented or not in current adapter by using ‘respond_to?’ method.

Supported Databases

Currently these databases are supported.

  • PostgreSQL

  • MySQL

For Other Databases

Put a new adapter file that contains ‘count_result’ method for your database adapter into adatpers directory.

Copyright © 2008 [email protected], released under the MIT license