pg_query 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTQ5MzZjYzgxYTIxZGQ3MWU3N2ZiYzFlMzI5MDhmOTZjMmFjZDQ5ZQ==
4
+ ZWQ2ODM2MzRjNWVkODM4ZWJhMjliN2Y1Yjg4NzE4MzljYmUzM2ZkYw==
5
5
  data.tar.gz: !binary |-
6
- MzIwNTk4ODI3OWRjODA4MjI2ZWQxOGUyMjM1NGJjMGM5MzRlYmI3YQ==
6
+ MDMwMjdmODc1MjM1YzAwMTZhNDg4ZDk2Y2YxMjVhZWMzOGNlODNmNw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZTk5YzFhZmQyZjRkZTIwNDdhNDExMWViNzQwYjI1NDBlOGQyNzk1ZTE2MzRh
10
- OTlmNTQ3ZTc5MDRjNDQ4ZDRkMWVkOWZjMzY1MmZhMDUzZWM4YTM0NDZmODIw
11
- MDNkMDQ0ZmM1NzgzYzYwODFlZmRjZDk3ODQyZWMyM2Q2YjgwN2U=
9
+ MTAzODFhNjEyNWJmNmVhNzIxNGYwYWMzZWY0ZGE3YTAzOWYzOTMzZmVjZTA5
10
+ YWUyMGViNTgxMGIzOGU3NjRmZDMyZmUwMDBlNDUxMmZiYmYwZTMzNGY1MzE1
11
+ M2M3YTU3NGJkY2Y2N2I0NDI1NzIzZTEzY2ZlYTAyZWFiNTFmNGQ=
12
12
  data.tar.gz: !binary |-
13
- Nzk0NTY3MjE2NDE2OTZhOWYxNzAwOTNlZjE1Njk0MDdhNGYxNjU5NTgyZjI0
14
- ODhmYjQyZGMzN2ZlYTk4ZGY2OGZhYjMwNjU0YjhkYjJkYWFmNGNiZmU1NDVk
15
- MjY4Yzc2Y2E3NjRjYzA2MmEzNjg3NTkwZjA2MDBlMmIxMWQ1YmQ=
13
+ ZWI2YzI0Mjc5NzIzMDU1MGJiNzIyNzIzMGI1Y2UwMmI1MGE4NTk5ZThjYjE4
14
+ YTliZTkxMDM3MjM2OGQxODZjY2JhZGZkYTNkNWQ1MjMwNGVlMGM4OWEyMzc0
15
+ YTk4NmRmOTRmOTc0ZTJjOTQ3NDZiMGQxNGYxYjVkMWYzNzIyZWE=
data/lib/pg_query.rb CHANGED
@@ -2,4 +2,5 @@ require 'pg_query/version'
2
2
  require 'pg_query/parse_error'
3
3
 
4
4
  require 'pg_query/pg_query'
5
- require 'pg_query/parse'
5
+ require 'pg_query/parse'
6
+ require 'pg_query/parse_normalized'
@@ -0,0 +1,51 @@
1
+ class PgQuery
2
+ # Parses a query that has been normalized by pg_stat_statements
3
+ def self.parse_normalized(original_query)
4
+ # Transform ? into \uFFED
5
+ query = normalized_to_parseable_query(original_query)
6
+
7
+ # Parse it!
8
+ result = parse(query)
9
+
10
+ # Transform \uFFED references as if they were $0
11
+ parsed_to_normalized_parsetree!(result.parsetree)
12
+
13
+ PgQuery.new(original_query, result.parsetree, result.warnings)
14
+ end
15
+
16
+ protected
17
+ # The PostgreSQL parser doesn't understand pg_stat_statements replacement characters,
18
+ # change them into a fake column reference to an unusual unicode character \uFFED
19
+ def self.normalized_to_parseable_query(query)
20
+ regexps = [
21
+ 'INTERVAL ?',
22
+ /\$[0-9]+\?/,
23
+ '?.?',
24
+ /(?<!\\)\?/, # Replace all ?, unless they are escaped by a backslash
25
+ ]
26
+ regexps.each do |re|
27
+ query = query.gsub(re) {|m| "\uFFED" * m.size }
28
+ end
29
+ query
30
+ end
31
+
32
+ # Modifies the passed in parsetree to have paramrefs to $0 instead of columnref to \uFFED
33
+ def self.parsed_to_normalized_parsetree!(parsetree)
34
+ expressions = parsetree.dup
35
+ loop do
36
+ break unless expression = expressions.shift
37
+
38
+ if expression.is_a?(Array)
39
+ expressions += expression.compact
40
+ elsif expression.is_a?(Hash)
41
+ value = expression['COLUMNREF'] && expression['COLUMNREF']['fields']
42
+ if value && value.size == 1 && value[0].is_a?(String) && value[0].chars.to_a.uniq == ["\uFFED"]
43
+ expression.replace('PARAMREF' => {'number' => 0,
44
+ 'location' => expression['COLUMNREF']['location']})
45
+ else
46
+ expressions += expression.values.compact
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  class PgQuery
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Fittl
@@ -67,6 +67,7 @@ files:
67
67
  - lib/pg_query.rb
68
68
  - lib/pg_query/parse.rb
69
69
  - lib/pg_query/parse_error.rb
70
+ - lib/pg_query/parse_normalized.rb
70
71
  - lib/pg_query/version.rb
71
72
  homepage: http://github.com/pganalyze/pg_query
72
73
  licenses: