Module: Sequel::SqlRecorder

Defined in:
lib/sequel/extensions/sql_recorder.rb

Overview

Extension module that adds SQL recording capabilities to Sequel databases. When included, it provides a sql_recorder method that returns an array of all SQL statements executed against the database.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sql_recorderArray<String> (readonly)

Returns the array of recorded SQL statements.

The array accumulates all SQL statements sent to the database since the extension was loaded or since the last time clear was called on the array.

Examples:

DB.extension :sql_recorder
DB[:users].all
DB.sql_recorder #=> ["SELECT * FROM users"]

Returns:

  • (Array<String>)

    array of SQL statement strings



51
52
53
# File 'lib/sequel/extensions/sql_recorder.rb', line 51

def sql_recorder
  @sql_recorder
end

Class Method Details

.extended(db) ⇒ Object

Initializes the SQL recording infrastructure when the extension is loaded.

Sets up the mutex for thread-safe access and initializes the SQL recording array. This method is automatically called when the extension is loaded via DB.extension :sql_recorder.

Parameters:

  • db (Sequel::Database)

    the database instance being extended



74
75
76
77
78
79
# File 'lib/sequel/extensions/sql_recorder.rb', line 74

def self.extended(db)
  db.instance_exec do
    @sql_recorder_mutex ||= Mutex.new
    @sql_recorder ||= []
  end
end

Instance Method Details

#log_connection_yield(sql, conn, args = nil) ⇒ Object

Intercepts SQL execution to record statements.

This method overrides Sequel’s log_connection_yield to capture each SQL statement in a thread-safe manner before delegating to the parent implementation.

Parameters:

  • sql (String)

    the SQL statement being executed

  • conn (Object)

    the database connection object

  • args (Object) (defaults to: nil)

    additional arguments (optional)

Returns:

  • (Object)

    result from the parent log_connection_yield method



62
63
64
65
# File 'lib/sequel/extensions/sql_recorder.rb', line 62

def log_connection_yield(sql, conn, args = nil)
  @sql_recorder_mutex.synchronize { sql_recorder.push(sql) }
  super
end