pg_query 2.2.0 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27667deffca50f6c8efc514a644d951e5129e5a7ff5fdf472ad39501adda12f0
4
- data.tar.gz: 3398f333de65ba7fdc637d9746805525b4a22933cbd9d6889c1d68643a780dca
3
+ metadata.gz: 9625ed6fd4d8f67673ee922f191996d2c157addf6d97aa092af682a21bf96ce7
4
+ data.tar.gz: 9a20dedc0420e5a7f2162c642c2ddee60cd98fd6bbfeff1b1affc181f6dcbc27
5
5
  SHA512:
6
- metadata.gz: c30bfcab9a142efc9c0fa8f12a18c157daa2074baf5b792cb832c2a214676f62aa558be69de2d5a0e15f87e3b4818b78d733844ada15fd0e96ba35fbe3b075da
7
- data.tar.gz: 1292be5ffdf9021997514e2a8175a4fae4058fbf4ece99c1a49de09b2490d9244947d35b46ecb0574c32a61d4cbb86eee518372b8b20496d47fd86ae264b8489
6
+ metadata.gz: 7f4633dd45e1530cebc92731315a238f22e4111ba23e10ea6991b6466f444ce2b974ffd858aaf5c93c5b6597e004e0b2334ac1832c24851956e34dd629fe58ff
7
+ data.tar.gz: 36d225dc15a58db4336cdbdd571a385308403b93d5507ec25b0deb5179be87b8a00fb029b1e5b77bdf62f79861fedd39ba350a50897bcfec4d61e266b2cdf639
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  * ...
6
6
 
7
+ ## 2.2.1 2022-01-20
8
+
9
+ * Detect tables used in the query of a PREPARE statement ([#273](https://github.com/pganalyze/pg_query/pull/273))
10
+ * Expose recursive walk functionality via walk! ([#268](https://github.com/pganalyze/pg_query/pull/268))
11
+ * Retain schema in name when parsing out functions ([#272](https://github.com/pganalyze/pg_query/pull/272))
7
12
 
8
13
  ## 2.2.0 2022-11-02
9
14
 
data/README.md CHANGED
@@ -72,6 +72,8 @@ PgQuery.parse("SELECT 1")
72
72
 
73
73
  ### Modifying a parsed query and turning it into SQL again
74
74
 
75
+ This is a simple example for `deparse`, for more complex modification, use `walk!`.
76
+
75
77
  ```ruby
76
78
  parsed_query = PgQuery.parse("SELECT * FROM users")
77
79
 
@@ -145,6 +147,36 @@ PgQuery.scan('SELECT 1 --comment')
145
147
  []]
146
148
  ```
147
149
 
150
+ ### Walking the parse tree
151
+
152
+ For generalized use, PgQuery provides `walk!` as a means to recursively work with the parsed query.
153
+
154
+ This can be used to create a bespoke pretty printer:
155
+
156
+ ```ruby
157
+ parsed_query = PgQuery.parse "SELECT * FROM tbl"
158
+ parsed_query.walk! { |node, k, v, location| puts k }
159
+ ```
160
+
161
+ More usefully, this can be used to rewrite a query. For example:
162
+
163
+ ```ruby
164
+ parsed_query.walk! do |node, k, v, location| puts k
165
+ next unless k.eql?(:range_var) || k.eql?(:relation)
166
+ next if v.relname.nil?
167
+ v.relname = "X_" + v.relname
168
+ end
169
+
170
+ parsed_query.deparse
171
+ ```
172
+
173
+ There are some caveats, and limitations, in this example.
174
+
175
+ First, some of the tree nodes are frozen. You can replace them, but you cannot modify in place.
176
+
177
+ Second, table rewriting is a bit more nuanced than this example. While this will rewrite the table names, it will
178
+ not correctly handle all CTEs, or rewrite columns with explicit table names.
179
+
148
180
  ## Differences from Upstream PostgreSQL
149
181
 
150
182
  This gem is based on [libpg_query](https://github.com/pganalyze/libpg_query),
@@ -216,7 +216,7 @@ module PgQuery
216
216
  when :OBJECT_FUNCTION
217
217
  # Only one function can be dropped in a statement
218
218
  obj = statement.drop_stmt.objects[0].object_with_args
219
- @functions << { function: obj.objname[0].string.str, type: :ddl }
219
+ @functions << { function: obj.objname.map { |f| f.string.str }.join('.'), type: :ddl }
220
220
  end
221
221
  when :grant_stmt
222
222
  objects = statement.grant_stmt.objects
@@ -235,18 +235,20 @@ module PgQuery
235
235
  statements << statement.explain_stmt.query
236
236
  when :create_function_stmt
237
237
  @functions << {
238
- function: statement.create_function_stmt.funcname[0].string.str,
238
+ function: statement.create_function_stmt.funcname.map { |f| f.string.str }.join('.'),
239
239
  type: :ddl
240
240
  }
241
241
  when :rename_stmt
242
242
  if statement.rename_stmt.rename_type == :OBJECT_FUNCTION
243
- original_name = statement.rename_stmt.object.object_with_args.objname[0].string.str
243
+ original_name = statement.rename_stmt.object.object_with_args.objname.map { |f| f.string.str }.join('.')
244
244
  new_name = statement.rename_stmt.newname
245
245
  @functions += [
246
246
  { function: original_name, type: :ddl },
247
247
  { function: new_name, type: :ddl }
248
248
  ]
249
249
  end
250
+ when :prepare_stmt
251
+ statements << statement.prepare_stmt.query
250
252
  end
251
253
  end
252
254
 
@@ -1,5 +1,11 @@
1
1
  module PgQuery
2
2
  class ParserResult
3
+ def walk!
4
+ treewalker!(@tree) do |parent_node, parent_field, node, location|
5
+ yield(parent_node, parent_field, node, location)
6
+ end
7
+ end
8
+
3
9
  private
4
10
 
5
11
  def treewalker!(tree) # rubocop:disable Metrics/CyclomaticComplexity
@@ -1,3 +1,3 @@
1
1
  module PgQuery
2
- VERSION = '2.2.0'.freeze
2
+ VERSION = '2.2.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Fittl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-03 00:00:00.000000000 Z
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -585,7 +585,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
585
585
  - !ruby/object:Gem::Version
586
586
  version: '0'
587
587
  requirements: []
588
- rubygems_version: 3.1.6
588
+ rubygems_version: 3.0.3
589
589
  signing_key:
590
590
  specification_version: 4
591
591
  summary: PostgreSQL query parsing and normalization library