Class: ScoutApm::Utils::SqlSanitizer
- Inherits:
-
Object
- Object
- ScoutApm::Utils::SqlSanitizer
- Defined in:
- lib/scout_apm/utils/sql_sanitizer.rb
Constant Summary collapse
- MULTIPLE_SPACES =
%r|\s+|.freeze
- MULTIPLE_QUESTIONS =
/\?(,\?)+/.freeze
- PSQL_VAR_INTERPOLATION =
%r|\[\[.*\]\]\s*\z|.freeze
- PSQL_REMOVE_STRINGS =
/'(?:[^']|'')*'/.freeze
- PSQL_REMOVE_JSON_STRINGS =
/:"(?:[^"]|"")*"/.freeze
- PSQL_REMOVE_INTEGERS =
/(?<!LIMIT )\b\d+\b/.freeze
- PSQL_AFTER_SELECT =
Should be everything between a FROM and a WHERE
/(?:SELECT\s+).*?(?:WHERE|FROM\z)/im.freeze
- PSQL_PLACEHOLDER =
/\$\d+/.freeze
- PSQL_IN_CLAUSE =
/IN\s+\(\?[^\)]*\)/.freeze
- PSQL_AFTER_FROM =
Should be everything between a FROM and a WHERE
/(?:FROM\s+).*?(?:WHERE|\z)/im.freeze
- PSQL_AFTER_FROM_AS =
Should be everything between a FROM and AS without WHERE
/(?:FROM\s+).*?(?:AS|\z)/im.freeze
- PSQL_AFTER_JOIN =
/(?:JOIN\s+).*?\z/im.freeze
- PSQL_AFTER_WHERE =
/(?:WHERE\s+).*?(?:SELECT|\z)/im.freeze
- PSQL_AFTER_SET =
/(?:SET\s+).*?(?:WHERE|\z)/im.freeze
- MYSQL_VAR_INTERPOLATION =
%r|\[\[.*\]\]\s*$|.freeze
- MYSQL_REMOVE_INTEGERS =
/(?<!LIMIT )\b\d+\b/.freeze
- MYSQL_REMOVE_SINGLE_QUOTE_STRINGS =
%r{'(?:\\'|[^']|'')*'}.freeze
- MYSQL_REMOVE_DOUBLE_QUOTE_STRINGS =
%r{"(?:\\"|[^"]|"")*"}.freeze
- MYSQL_IN_CLAUSE =
/IN\s+\(\?[^\)]*\)/.freeze
- SQLITE_VAR_INTERPOLATION =
%r|\[\[.*\]\]\s*$|.freeze
- SQLITE_REMOVE_STRINGS =
/'(?:[^']|'')*'/.freeze
- SQLITE_REMOVE_INTEGERS =
/(?<!LIMIT )\b\d+\b/.freeze
- SQLSERVER_REMOVE_EXECUTESQL =
> “EXEC sp_executesql N’SELECT [users].* FROM [users] WHERE (age > 50) ORDER BY [users]. ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY’, N’@0 int’, @0 = 10”
/EXEC sp_executesql (N')?/.freeze
- SQLSERVER_REMOVE_STRINGS =
/'(?:[^']|'')*'/.freeze
- SQLSERVER_REMOVE_INTEGERS =
/(?<!LIMIT )\b(?<!@)\d+\b/.freeze
- SQLSERVER_IN_CLAUSE =
/IN\s+\(\?[^\)]*\)/.freeze
Instance Attribute Summary collapse
-
#database_engine ⇒ Object
Returns the value of attribute database_engine.
Instance Method Summary collapse
-
#initialize(sql) ⇒ SqlSanitizer
constructor
A new instance of SqlSanitizer.
- #sql ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(sql) ⇒ SqlSanitizer
Returns a new instance of SqlSanitizer.
44 45 46 47 48 |
# File 'lib/scout_apm/utils/sql_sanitizer.rb', line 44 def initialize(sql) @raw_sql = sql @database_engine = ScoutApm::Agent.instance.context.environment.database_engine @sanitized = false # only sanitize once. end |
Instance Attribute Details
#database_engine ⇒ Object
Returns the value of attribute database_engine.
42 43 44 |
# File 'lib/scout_apm/utils/sql_sanitizer.rb', line 42 def database_engine @database_engine end |
Instance Method Details
#sql ⇒ Object
50 51 52 |
# File 'lib/scout_apm/utils/sql_sanitizer.rb', line 50 def sql @sql ||= scrubbed(@raw_sql.dup) # don't do this in initialize as it is extra work that isn't needed unless we have a slow transaction. end |
#to_s ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/scout_apm/utils/sql_sanitizer.rb', line 54 def to_s if @sanitized sql else @sanitized = true end case database_engine when :postgres then to_s_postgres when :mysql then to_s_mysql when :sqlite then to_s_sqlite when :sqlserver then to_s_sqlserver end end |