|
1 | 1 | module ARTest |
2 | 2 | module SQLServer |
3 | 3 |
|
4 | | - extend self |
| 4 | + module SqlCounterSqlserver |
5 | 5 |
|
6 | | - attr_accessor :sql_counter_listenter |
| 6 | + # Only return the log vs. log_all |
| 7 | + def capture_sql_ss |
| 8 | + ActiveRecord::SQLCounter.clear_log |
| 9 | + yield |
| 10 | + ActiveRecord::SQLCounter.log.dup |
| 11 | + end |
7 | 12 |
|
8 | | - def ignored_sql |
9 | | - [ /SELECT SCOPE_IDENTITY/, |
10 | | - /INFORMATION_SCHEMA\.(TABLES|VIEWS|COLUMNS)/, |
11 | | - /SELECT @@version/, |
12 | | - /SELECT @@TRANCOUNT/, |
13 | | - /(BEGIN|COMMIT|ROLLBACK|SAVE) TRANSACTION/, |
14 | | - /SELECT CAST\(.* AS .*\) AS value/ ] |
15 | 13 | end |
16 | 14 |
|
17 | | - def sql_counter_listenters |
18 | | - ActiveSupport::Notifications.notifier.listeners_for('sql.active_record').select do |listener| |
19 | | - listener.inspect =~ /ActiveRecord::SQLCounter/ |
20 | | - end |
21 | | - end |
| 15 | + ignored_sql = [ |
| 16 | + /INFORMATION_SCHEMA\.(TABLES|VIEWS|COLUMNS)/im, |
| 17 | + /SELECT @@version/, |
| 18 | + /SELECT @@TRANCOUNT/, |
| 19 | + /(BEGIN|COMMIT|ROLLBACK|SAVE) TRANSACTION/, |
| 20 | + /SELECT CAST\(.* AS .*\) AS value/ |
| 21 | + ] |
22 | 22 |
|
23 | | - def sql_counter_listenters_unsubscribe |
24 | | - sql_counter_listenters.each { |listener| ActiveSupport::Notifications.unsubscribe(listener) } |
25 | | - end |
| 23 | + sqlcounter = ObjectSpace.each_object(ActiveRecord::SQLCounter).to_a.first |
| 24 | + sqlcounter.instance_variable_set :@ignore, Regexp.union(ignored_sql.push(sqlcounter.ignore)) |
26 | 25 |
|
27 | 26 | end |
28 | 27 | end |
29 | | - |
30 | | -ActiveRecord::SQLCounter.ignored_sql.concat ARTest::SQLServer.ignored_sql |
31 | | -ARTest::SQLServer.sql_counter_listenters_unsubscribe |
32 | | -ARTest::SQLServer.sql_counter_listenter = ActiveSupport::Notifications.subscribe 'sql.active_record', ActiveRecord::SQLCounter.new |
0 commit comments