Skip to content

Commit

Permalink
Add Sqls extension
Browse files Browse the repository at this point in the history
  • Loading branch information
aguynamedryan committed Apr 3, 2024
1 parent 83ada23 commit 95e7c21
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/sequel/extensions/sqls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen-string-literal: true
#
# The sqls extension will record each SQL statement sent to the
# database
#
# DB.extension :sqls
# DB[:table]
# DB.sqls # => ["SELECT * FROM table LIMIT 1"]
#
# Related module: Sequel::Sqls

module Sequel
module Sqls
attr_reader :sqls

# Record SQL statements when logging query.
def log_connection_yield(sql, conn, args=nil)
@sqls_mutex.synchronize{sqls.push(sql)}
super
end

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

Database.register_extension(:sqls, Sqls)
end

0 comments on commit 95e7c21

Please sign in to comment.