pghero 2.8.0 → 2.8.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: e754becdd218d70d2134bdc75529f30be8b7e5158e2e22bd7e89b326353e12fa
4
- data.tar.gz: dd0cd7fe10dc4201643f6c5f91d58060dbde78feab62ed98a6fb63ee17909666
3
+ metadata.gz: 3b4a596a016b5c33a99013818181ef967c26dcec092ee787b6629eac091ad121
4
+ data.tar.gz: 63fc2a949a62828a4537e54fb087c5a5b143bc88ab2753b4d777d4f793fb34ab
5
5
  SHA512:
6
- metadata.gz: b2b4685743c02cdc9bcc4f98225d7cf1de1efb824a4bda52b02baac22a8aa852755d19f1b343856da334c8aa29e0c27ebe2827be9dd5b6e8d8930e31373da2a2
7
- data.tar.gz: ba47c2eed1b11ba28ff2038b88543fd1c78a81c9188c96791dd8002c7928786fc178da9cc12cdf33c752e0243cacde2b1e569478150d988732f9042446be66dd
6
+ metadata.gz: beda0eb3c89c9a3928b9d998e7e9ae8858adda77873c9dc0d03836498a765c219815c6441b814d3b23da78f02f759d71737eb51236135cfb72c6372358043c37
7
+ data.tar.gz: 020de38d5b406ede495f44ad16ce6a5bde8ebb59c49027af4dc49afb88cf768bc20f3c064fe72070f2739eb2979d2b95d0f7cdbdd106851252e6c81f646d3e82
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 2.8.1 (2021-03-25)
2
+
3
+ - Added support for pg_query 2
4
+
1
5
  ## 2.8.0 (2021-03-14)
2
6
 
3
7
  - No longer show walsender in long running queries
@@ -201,36 +201,69 @@ module PgHero
201
201
  rescue PgQuery::ParseError
202
202
  return {error: "Parse error"}
203
203
  end
204
- return {error: "Unknown structure"} unless tree.size == 1
205
-
206
- tree = tree.first
207
-
208
- # pg_query 1.0.0
209
- tree = tree["RawStmt"]["stmt"] if tree["RawStmt"]
210
-
211
- table = parse_table(tree) rescue nil
212
- unless table
213
- error =
214
- case tree.keys.first
215
- when "InsertStmt"
216
- "INSERT statement"
217
- when "VariableSetStmt"
218
- "SET statement"
219
- when "SelectStmt"
220
- if (tree["SelectStmt"]["fromClause"].first["JoinExpr"] rescue false)
221
- "JOIN not supported yet"
204
+
205
+ if PgQuery::VERSION.to_i >= 2
206
+ return {error: "Unknown structure"} unless tree.stmts.size == 1
207
+
208
+ tree = tree.stmts.first.stmt
209
+
210
+ table = parse_table_v2(tree) rescue nil
211
+ unless table
212
+ error =
213
+ case tree.node
214
+ when :insert_stmt
215
+ "INSERT statement"
216
+ when :variable_set_stmt
217
+ "SET statement"
218
+ when :select_stmt
219
+ if (tree.select_stmt.from_clause.first.join_expr rescue false)
220
+ "JOIN not supported yet"
221
+ end
222
222
  end
223
- end
224
- return {error: error || "Unknown structure"}
225
- end
223
+ return {error: error || "Unknown structure"}
224
+ end
225
+
226
+ select = tree[tree.node.to_s]
227
+ where = (select.where_clause ? parse_where_v2(select.where_clause) : []) rescue nil
228
+ return {error: "Unknown structure"} unless where
229
+
230
+ sort = (select.sort_clause ? parse_sort_v2(select.sort_clause) : []) rescue []
231
+
232
+ {table: table, where: where, sort: sort}
233
+ else
234
+ # TODO remove support for pg_query < 2 in PgHero 3.0
235
+
236
+ return {error: "Unknown structure"} unless tree.size == 1
237
+
238
+ tree = tree.first
239
+
240
+ # pg_query 1.0.0
241
+ tree = tree["RawStmt"]["stmt"] if tree["RawStmt"]
242
+
243
+ table = parse_table(tree) rescue nil
244
+ unless table
245
+ error =
246
+ case tree.keys.first
247
+ when "InsertStmt"
248
+ "INSERT statement"
249
+ when "VariableSetStmt"
250
+ "SET statement"
251
+ when "SelectStmt"
252
+ if (tree["SelectStmt"]["fromClause"].first["JoinExpr"] rescue false)
253
+ "JOIN not supported yet"
254
+ end
255
+ end
256
+ return {error: error || "Unknown structure"}
257
+ end
226
258
 
227
- select = tree.values.first
228
- where = (select["whereClause"] ? parse_where(select["whereClause"]) : []) rescue nil
229
- return {error: "Unknown structure"} unless where
259
+ select = tree.values.first
260
+ where = (select["whereClause"] ? parse_where(select["whereClause"]) : []) rescue nil
261
+ return {error: "Unknown structure"} unless where
230
262
 
231
- sort = (select["sortClause"] ? parse_sort(select["sortClause"]) : []) rescue []
263
+ sort = (select["sortClause"] ? parse_sort(select["sortClause"]) : []) rescue []
232
264
 
233
- {table: table, where: where, sort: sort}
265
+ {table: table, where: where, sort: sort}
266
+ end
234
267
  end
235
268
 
236
269
  # TODO better row estimation
@@ -267,6 +300,17 @@ module PgHero
267
300
  end
268
301
  end
269
302
 
303
+ def parse_table_v2(tree)
304
+ case tree.node
305
+ when :select_stmt
306
+ tree.select_stmt.from_clause.first.range_var.relname
307
+ when :delete_stmt
308
+ tree.delete_stmt.relation.relname
309
+ when :update_stmt
310
+ tree.update_stmt.relation.relname
311
+ end
312
+ end
313
+
270
314
  def parse_table(tree)
271
315
  case tree.keys.first
272
316
  when "SelectStmt"
@@ -278,6 +322,26 @@ module PgHero
278
322
  end
279
323
  end
280
324
 
325
+ # TODO capture values
326
+ def parse_where_v2(tree)
327
+ aexpr = tree.a_expr
328
+
329
+ if tree.bool_expr
330
+ if tree.bool_expr.boolop == :AND_EXPR
331
+ tree.bool_expr.args.flat_map { |v| parse_where_v2(v) }
332
+ else
333
+ raise "Not Implemented"
334
+ end
335
+ elsif aexpr && ["=", "<>", ">", ">=", "<", "<=", "~~", "~~*", "BETWEEN"].include?(aexpr.name.first.string.str)
336
+ [{column: aexpr.lexpr.column_ref.fields.last.string.str, op: aexpr.name.first.string.str}]
337
+ elsif tree.null_test
338
+ op = tree.null_test.nulltesttype == :IS_NOT_NULL ? "not_null" : "null"
339
+ [{column: tree.null_test.arg.column_ref.fields.last.string.str, op: op}]
340
+ else
341
+ raise "Not Implemented"
342
+ end
343
+ end
344
+
281
345
  # TODO capture values
282
346
  def parse_where(tree)
283
347
  aexpr = tree["A_Expr"]
@@ -298,6 +362,15 @@ module PgHero
298
362
  end
299
363
  end
300
364
 
365
+ def parse_sort_v2(sort_clause)
366
+ sort_clause.map do |v|
367
+ {
368
+ column: v.sort_by.node.column_ref.fields.last.string.str,
369
+ direction: v.sort_by.sortby_dir == :SORTBY_DESC ? "desc" : "asc"
370
+ }
371
+ end
372
+ end
373
+
301
374
  def parse_sort(sort_clause)
302
375
  sort_clause.map do |v|
303
376
  {
@@ -1,3 +1,3 @@
1
1
  module PgHero
2
- VERSION = "2.8.0"
2
+ VERSION = "2.8.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pghero
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.0
4
+ version: 2.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-14 00:00:00.000000000 Z
11
+ date: 2021-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord