Skip to content

Commit

Permalink
Merge pull request #504 from airbrake/sql-filter-grouping-fix
Browse files Browse the repository at this point in the history
filters/sql: apply more aggressive grouping
  • Loading branch information
kyrylo authored Oct 1, 2019
2 parents 2a69b81 + 350b910 commit e6ea54c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Airbrake Ruby Changelog

### master

* Improved grouping of some SQL queries
([#504](https://github.com/airbrake/airbrake-ruby/pull/504))

### [v4.6.0][v4.6.0] (August 5, 2019)

* Added the `query_stats` option that configures SQL performance
Expand Down
5 changes: 5 additions & 0 deletions lib/airbrake-ruby/filters/sql_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class SqlFilter
# rubocop:enable Metrics/LineLength
}.freeze

# @return [Regexp] the regexp that is applied after the feature regexps
# were used
POST_FILTER = /(?<=[values|in ]\().+(?=\))/i

# @return [Hash{Symbol=>Array<Symbol>}] a set of features that corresponds
# to a certain dialect
DIALECT_FEATURES = {
Expand Down Expand Up @@ -96,6 +100,7 @@ def call(resource)
return unless resource.respond_to?(:query)

q = resource.query.gsub(@regexp, FILTERED)
q.gsub!(POST_FILTER, FILTERED) if q =~ POST_FILTER
q = ERROR_MSG if UNMATCHED_PAIR[@dialect] =~ q
resource.query = q
end
Expand Down
12 changes: 8 additions & 4 deletions spec/filters/sql_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,19 @@
dialects: %i[postgres]
}, {
input: "INSERT INTO `X` values(\"test\",0, 1 , 2, 'test')",
output: "INSERT INTO `X` values(?,?, ? , ?, ?)",
output: "INSERT INTO `X` values(?)",
dialects: %i[mysql]
}, {
input: "INSERT INTO `X` values(\"test\",0, 1 , 2, 'test')",
output: "INSERT INTO `X` values(?)",
dialects: %i[mysql]
}, {
input: "SELECT c11.col1, c22.col2 FROM table c11, table c22 WHERE value='nothing'",
output: "SELECT c11.col1, c22.col2 FROM table c11, table c22 WHERE value=?",
dialects: ALL_DIALECTS
}, {
input: "INSERT INTO X VALUES(1, 23456, 123.456, 99+100)",
output: "INSERT INTO X VALUES(?, ?, ?, ?+?)",
output: "INSERT INTO X VALUES(?)",
dialects: ALL_DIALECTS
}, {
input: "SELECT * FROM table WHERE name=\"foo\" AND value=\"don't\"",
Expand Down Expand Up @@ -117,7 +121,7 @@
dialects: ALL_DIALECTS
}, {
input: "INSERT INTO X values('', 'a''b c',0, 1 , 'd''e f''s h')",
output: "INSERT INTO X values(?, ?,?, ? , ?)",
output: "INSERT INTO X values(?)",
dialects: ALL_DIALECTS
}, {
input: "SELECT * FROM t WHERE -- '\n bar='baz' -- '",
Expand Down Expand Up @@ -153,7 +157,7 @@
dialects: %i[postgres]
}, {
input: "INSERT INTO \"foo\" (\"bar\", \"baz\", \"qux\") VALUES ($1, $2, $3) RETURNING \"id\"",
output: "INSERT INTO \"foo\" (\"bar\", \"baz\", \"qux\") VALUES ($?, $?, $?) RETURNING \"id\"",
output: "INSERT INTO \"foo\" (?) RETURNING \"id\"",
dialects: %i[postgres]
}, {
input: "select * from foo where bar = 'some\\tthing' and baz = 10",
Expand Down

0 comments on commit e6ea54c

Please sign in to comment.