gitlab-pg_query 1.3.1 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (480) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +217 -99
  3. data/README.md +92 -69
  4. data/Rakefile +85 -5
  5. data/ext/pg_query/extconf.rb +3 -40
  6. data/ext/pg_query/guc-file.c +0 -0
  7. data/ext/pg_query/include/access/amapi.h +246 -0
  8. data/ext/pg_query/include/access/attmap.h +52 -0
  9. data/ext/pg_query/include/access/attnum.h +64 -0
  10. data/ext/pg_query/include/access/clog.h +61 -0
  11. data/ext/pg_query/include/access/commit_ts.h +77 -0
  12. data/ext/pg_query/include/access/detoast.h +92 -0
  13. data/ext/pg_query/include/access/genam.h +228 -0
  14. data/ext/pg_query/include/access/gin.h +78 -0
  15. data/ext/pg_query/include/access/htup.h +89 -0
  16. data/ext/pg_query/include/access/htup_details.h +819 -0
  17. data/ext/pg_query/include/access/itup.h +161 -0
  18. data/ext/pg_query/include/access/parallel.h +82 -0
  19. data/ext/pg_query/include/access/printtup.h +35 -0
  20. data/ext/pg_query/include/access/relation.h +28 -0
  21. data/ext/pg_query/include/access/relscan.h +176 -0
  22. data/ext/pg_query/include/access/rmgr.h +35 -0
  23. data/ext/pg_query/include/access/rmgrlist.h +49 -0
  24. data/ext/pg_query/include/access/sdir.h +58 -0
  25. data/ext/pg_query/include/access/skey.h +151 -0
  26. data/ext/pg_query/include/access/stratnum.h +83 -0
  27. data/ext/pg_query/include/access/sysattr.h +29 -0
  28. data/ext/pg_query/include/access/table.h +27 -0
  29. data/ext/pg_query/include/access/tableam.h +1825 -0
  30. data/ext/pg_query/include/access/transam.h +265 -0
  31. data/ext/pg_query/include/access/tupconvert.h +51 -0
  32. data/ext/pg_query/include/access/tupdesc.h +154 -0
  33. data/ext/pg_query/include/access/tupmacs.h +247 -0
  34. data/ext/pg_query/include/access/twophase.h +61 -0
  35. data/ext/pg_query/include/access/xact.h +463 -0
  36. data/ext/pg_query/include/access/xlog.h +398 -0
  37. data/ext/pg_query/include/access/xlog_internal.h +330 -0
  38. data/ext/pg_query/include/access/xlogdefs.h +109 -0
  39. data/ext/pg_query/include/access/xloginsert.h +64 -0
  40. data/ext/pg_query/include/access/xlogreader.h +327 -0
  41. data/ext/pg_query/include/access/xlogrecord.h +227 -0
  42. data/ext/pg_query/include/bootstrap/bootstrap.h +62 -0
  43. data/ext/pg_query/include/c.h +1322 -0
  44. data/ext/pg_query/include/catalog/catalog.h +42 -0
  45. data/ext/pg_query/include/catalog/catversion.h +58 -0
  46. data/ext/pg_query/include/catalog/dependency.h +275 -0
  47. data/ext/pg_query/include/catalog/genbki.h +64 -0
  48. data/ext/pg_query/include/catalog/index.h +199 -0
  49. data/ext/pg_query/include/catalog/indexing.h +366 -0
  50. data/ext/pg_query/include/catalog/namespace.h +188 -0
  51. data/ext/pg_query/include/catalog/objectaccess.h +197 -0
  52. data/ext/pg_query/include/catalog/objectaddress.h +84 -0
  53. data/ext/pg_query/include/catalog/pg_aggregate.h +176 -0
  54. data/ext/pg_query/include/catalog/pg_aggregate_d.h +77 -0
  55. data/ext/pg_query/include/catalog/pg_am.h +60 -0
  56. data/ext/pg_query/include/catalog/pg_am_d.h +45 -0
  57. data/ext/pg_query/include/catalog/pg_attribute.h +204 -0
  58. data/ext/pg_query/include/catalog/pg_attribute_d.h +59 -0
  59. data/ext/pg_query/include/catalog/pg_authid.h +58 -0
  60. data/ext/pg_query/include/catalog/pg_authid_d.h +49 -0
  61. data/ext/pg_query/include/catalog/pg_class.h +200 -0
  62. data/ext/pg_query/include/catalog/pg_class_d.h +103 -0
  63. data/ext/pg_query/include/catalog/pg_collation.h +73 -0
  64. data/ext/pg_query/include/catalog/pg_collation_d.h +45 -0
  65. data/ext/pg_query/include/catalog/pg_constraint.h +247 -0
  66. data/ext/pg_query/include/catalog/pg_constraint_d.h +67 -0
  67. data/ext/pg_query/include/catalog/pg_control.h +250 -0
  68. data/ext/pg_query/include/catalog/pg_conversion.h +72 -0
  69. data/ext/pg_query/include/catalog/pg_conversion_d.h +35 -0
  70. data/ext/pg_query/include/catalog/pg_depend.h +73 -0
  71. data/ext/pg_query/include/catalog/pg_depend_d.h +34 -0
  72. data/ext/pg_query/include/catalog/pg_event_trigger.h +51 -0
  73. data/ext/pg_query/include/catalog/pg_event_trigger_d.h +34 -0
  74. data/ext/pg_query/include/catalog/pg_index.h +80 -0
  75. data/ext/pg_query/include/catalog/pg_index_d.h +56 -0
  76. data/ext/pg_query/include/catalog/pg_language.h +67 -0
  77. data/ext/pg_query/include/catalog/pg_language_d.h +39 -0
  78. data/ext/pg_query/include/catalog/pg_namespace.h +59 -0
  79. data/ext/pg_query/include/catalog/pg_namespace_d.h +34 -0
  80. data/ext/pg_query/include/catalog/pg_opclass.h +85 -0
  81. data/ext/pg_query/include/catalog/pg_opclass_d.h +49 -0
  82. data/ext/pg_query/include/catalog/pg_operator.h +102 -0
  83. data/ext/pg_query/include/catalog/pg_operator_d.h +106 -0
  84. data/ext/pg_query/include/catalog/pg_opfamily.h +60 -0
  85. data/ext/pg_query/include/catalog/pg_opfamily_d.h +47 -0
  86. data/ext/pg_query/include/catalog/pg_partitioned_table.h +63 -0
  87. data/ext/pg_query/include/catalog/pg_partitioned_table_d.h +35 -0
  88. data/ext/pg_query/include/catalog/pg_proc.h +211 -0
  89. data/ext/pg_query/include/catalog/pg_proc_d.h +99 -0
  90. data/ext/pg_query/include/catalog/pg_publication.h +115 -0
  91. data/ext/pg_query/include/catalog/pg_publication_d.h +36 -0
  92. data/ext/pg_query/include/catalog/pg_replication_origin.h +57 -0
  93. data/ext/pg_query/include/catalog/pg_replication_origin_d.h +29 -0
  94. data/ext/pg_query/include/catalog/pg_statistic.h +275 -0
  95. data/ext/pg_query/include/catalog/pg_statistic_d.h +194 -0
  96. data/ext/pg_query/include/catalog/pg_statistic_ext.h +74 -0
  97. data/ext/pg_query/include/catalog/pg_statistic_ext_d.h +40 -0
  98. data/ext/pg_query/include/catalog/pg_transform.h +45 -0
  99. data/ext/pg_query/include/catalog/pg_transform_d.h +32 -0
  100. data/ext/pg_query/include/catalog/pg_trigger.h +137 -0
  101. data/ext/pg_query/include/catalog/pg_trigger_d.h +106 -0
  102. data/ext/pg_query/include/catalog/pg_ts_config.h +50 -0
  103. data/ext/pg_query/include/catalog/pg_ts_config_d.h +32 -0
  104. data/ext/pg_query/include/catalog/pg_ts_dict.h +54 -0
  105. data/ext/pg_query/include/catalog/pg_ts_dict_d.h +33 -0
  106. data/ext/pg_query/include/catalog/pg_ts_parser.h +57 -0
  107. data/ext/pg_query/include/catalog/pg_ts_parser_d.h +35 -0
  108. data/ext/pg_query/include/catalog/pg_ts_template.h +48 -0
  109. data/ext/pg_query/include/catalog/pg_ts_template_d.h +32 -0
  110. data/ext/pg_query/include/catalog/pg_type.h +372 -0
  111. data/ext/pg_query/include/catalog/pg_type_d.h +285 -0
  112. data/ext/pg_query/include/catalog/storage.h +48 -0
  113. data/ext/pg_query/include/commands/async.h +54 -0
  114. data/ext/pg_query/include/commands/dbcommands.h +35 -0
  115. data/ext/pg_query/include/commands/defrem.h +173 -0
  116. data/ext/pg_query/include/commands/event_trigger.h +88 -0
  117. data/ext/pg_query/include/commands/explain.h +127 -0
  118. data/ext/pg_query/include/commands/prepare.h +61 -0
  119. data/ext/pg_query/include/commands/tablespace.h +67 -0
  120. data/ext/pg_query/include/commands/trigger.h +277 -0
  121. data/ext/pg_query/include/commands/user.h +37 -0
  122. data/ext/pg_query/include/commands/vacuum.h +293 -0
  123. data/ext/pg_query/include/commands/variable.h +38 -0
  124. data/ext/pg_query/include/common/file_perm.h +56 -0
  125. data/ext/pg_query/include/common/hashfn.h +104 -0
  126. data/ext/pg_query/include/common/ip.h +37 -0
  127. data/ext/pg_query/include/common/keywords.h +33 -0
  128. data/ext/pg_query/include/common/kwlookup.h +44 -0
  129. data/ext/pg_query/include/common/relpath.h +90 -0
  130. data/ext/pg_query/include/common/string.h +19 -0
  131. data/ext/pg_query/include/common/unicode_combining_table.h +196 -0
  132. data/ext/pg_query/include/datatype/timestamp.h +197 -0
  133. data/ext/pg_query/include/executor/execdesc.h +70 -0
  134. data/ext/pg_query/include/executor/executor.h +614 -0
  135. data/ext/pg_query/include/executor/functions.h +41 -0
  136. data/ext/pg_query/include/executor/instrument.h +101 -0
  137. data/ext/pg_query/include/executor/spi.h +175 -0
  138. data/ext/pg_query/include/executor/tablefunc.h +67 -0
  139. data/ext/pg_query/include/executor/tuptable.h +487 -0
  140. data/ext/pg_query/include/fmgr.h +775 -0
  141. data/ext/pg_query/include/funcapi.h +348 -0
  142. data/ext/pg_query/include/getaddrinfo.h +162 -0
  143. data/ext/pg_query/include/jit/jit.h +105 -0
  144. data/ext/pg_query/include/kwlist_d.h +1072 -0
  145. data/ext/pg_query/include/lib/ilist.h +727 -0
  146. data/ext/pg_query/include/lib/pairingheap.h +102 -0
  147. data/ext/pg_query/include/lib/simplehash.h +1059 -0
  148. data/ext/pg_query/include/lib/stringinfo.h +161 -0
  149. data/ext/pg_query/include/libpq/auth.h +29 -0
  150. data/ext/pg_query/include/libpq/crypt.h +46 -0
  151. data/ext/pg_query/include/libpq/hba.h +140 -0
  152. data/ext/pg_query/include/libpq/libpq-be.h +326 -0
  153. data/ext/pg_query/include/libpq/libpq.h +133 -0
  154. data/ext/pg_query/include/libpq/pqcomm.h +208 -0
  155. data/ext/pg_query/include/libpq/pqformat.h +210 -0
  156. data/ext/pg_query/include/libpq/pqsignal.h +42 -0
  157. data/ext/pg_query/include/mb/pg_wchar.h +672 -0
  158. data/ext/pg_query/include/mb/stringinfo_mb.h +24 -0
  159. data/ext/pg_query/include/miscadmin.h +476 -0
  160. data/ext/pg_query/include/nodes/bitmapset.h +122 -0
  161. data/ext/pg_query/include/nodes/execnodes.h +2520 -0
  162. data/ext/pg_query/include/nodes/extensible.h +160 -0
  163. data/ext/pg_query/include/nodes/lockoptions.h +61 -0
  164. data/ext/pg_query/include/nodes/makefuncs.h +108 -0
  165. data/ext/pg_query/include/nodes/memnodes.h +108 -0
  166. data/ext/pg_query/include/nodes/nodeFuncs.h +162 -0
  167. data/ext/pg_query/include/nodes/nodes.h +842 -0
  168. data/ext/pg_query/include/nodes/params.h +170 -0
  169. data/ext/pg_query/include/nodes/parsenodes.h +3579 -0
  170. data/ext/pg_query/include/nodes/pathnodes.h +2556 -0
  171. data/ext/pg_query/include/nodes/pg_list.h +605 -0
  172. data/ext/pg_query/include/nodes/plannodes.h +1251 -0
  173. data/ext/pg_query/include/nodes/primnodes.h +1541 -0
  174. data/ext/pg_query/include/nodes/print.h +34 -0
  175. data/ext/pg_query/include/nodes/tidbitmap.h +75 -0
  176. data/ext/pg_query/include/nodes/value.h +61 -0
  177. data/ext/pg_query/include/optimizer/cost.h +206 -0
  178. data/ext/pg_query/include/optimizer/geqo.h +88 -0
  179. data/ext/pg_query/include/optimizer/geqo_gene.h +45 -0
  180. data/ext/pg_query/include/optimizer/optimizer.h +199 -0
  181. data/ext/pg_query/include/optimizer/paths.h +249 -0
  182. data/ext/pg_query/include/optimizer/planmain.h +119 -0
  183. data/ext/pg_query/include/parser/analyze.h +49 -0
  184. data/ext/pg_query/include/parser/gram.h +1067 -0
  185. data/ext/pg_query/include/parser/gramparse.h +75 -0
  186. data/ext/pg_query/include/parser/kwlist.h +477 -0
  187. data/ext/pg_query/include/parser/parse_agg.h +68 -0
  188. data/ext/pg_query/include/parser/parse_clause.h +54 -0
  189. data/ext/pg_query/include/parser/parse_coerce.h +97 -0
  190. data/ext/pg_query/include/parser/parse_collate.h +27 -0
  191. data/ext/pg_query/include/parser/parse_expr.h +26 -0
  192. data/ext/pg_query/include/parser/parse_func.h +73 -0
  193. data/ext/pg_query/include/parser/parse_node.h +327 -0
  194. data/ext/pg_query/include/parser/parse_oper.h +67 -0
  195. data/ext/pg_query/include/parser/parse_relation.h +123 -0
  196. data/ext/pg_query/include/parser/parse_target.h +46 -0
  197. data/ext/pg_query/include/parser/parse_type.h +60 -0
  198. data/ext/pg_query/include/parser/parser.h +41 -0
  199. data/ext/pg_query/include/parser/parsetree.h +61 -0
  200. data/ext/pg_query/include/parser/scanner.h +152 -0
  201. data/ext/pg_query/include/parser/scansup.h +30 -0
  202. data/ext/pg_query/include/partitioning/partdefs.h +26 -0
  203. data/ext/pg_query/include/pg_config.h +989 -0
  204. data/ext/pg_query/include/pg_config_ext.h +8 -0
  205. data/ext/pg_query/include/pg_config_manual.h +350 -0
  206. data/ext/pg_query/include/pg_config_os.h +8 -0
  207. data/ext/pg_query/include/pg_getopt.h +56 -0
  208. data/ext/pg_query/include/pg_query.h +121 -0
  209. data/ext/pg_query/include/pg_query_enum_defs.c +2454 -0
  210. data/ext/pg_query/include/pg_query_fingerprint_conds.c +875 -0
  211. data/ext/pg_query/include/pg_query_fingerprint_defs.c +12413 -0
  212. data/ext/pg_query/include/pg_query_json_helper.c +61 -0
  213. data/ext/pg_query/include/pg_query_outfuncs_conds.c +686 -0
  214. data/ext/pg_query/include/pg_query_outfuncs_defs.c +2437 -0
  215. data/ext/pg_query/include/pg_query_readfuncs_conds.c +222 -0
  216. data/ext/pg_query/include/pg_query_readfuncs_defs.c +2878 -0
  217. data/ext/pg_query/include/pg_trace.h +17 -0
  218. data/ext/pg_query/include/pgstat.h +1487 -0
  219. data/ext/pg_query/include/pgtime.h +84 -0
  220. data/ext/pg_query/include/pl_gram.h +385 -0
  221. data/ext/pg_query/include/pl_reserved_kwlist.h +52 -0
  222. data/ext/pg_query/include/pl_reserved_kwlist_d.h +114 -0
  223. data/ext/pg_query/include/pl_unreserved_kwlist.h +112 -0
  224. data/ext/pg_query/include/pl_unreserved_kwlist_d.h +246 -0
  225. data/ext/pg_query/include/plerrcodes.h +990 -0
  226. data/ext/pg_query/include/plpgsql.h +1347 -0
  227. data/ext/pg_query/include/port.h +524 -0
  228. data/ext/pg_query/include/port/atomics.h +524 -0
  229. data/ext/pg_query/include/port/atomics/arch-arm.h +26 -0
  230. data/ext/pg_query/include/port/atomics/arch-ppc.h +254 -0
  231. data/ext/pg_query/include/port/atomics/arch-x86.h +252 -0
  232. data/ext/pg_query/include/port/atomics/fallback.h +170 -0
  233. data/ext/pg_query/include/port/atomics/generic-gcc.h +286 -0
  234. data/ext/pg_query/include/port/atomics/generic.h +401 -0
  235. data/ext/pg_query/include/port/pg_bitutils.h +226 -0
  236. data/ext/pg_query/include/port/pg_bswap.h +161 -0
  237. data/ext/pg_query/include/port/pg_crc32c.h +101 -0
  238. data/ext/pg_query/include/portability/instr_time.h +256 -0
  239. data/ext/pg_query/include/postgres.h +764 -0
  240. data/ext/pg_query/include/postgres_ext.h +74 -0
  241. data/ext/pg_query/include/postmaster/autovacuum.h +83 -0
  242. data/ext/pg_query/include/postmaster/bgworker.h +161 -0
  243. data/ext/pg_query/include/postmaster/bgworker_internals.h +64 -0
  244. data/ext/pg_query/include/postmaster/bgwriter.h +45 -0
  245. data/ext/pg_query/include/postmaster/fork_process.h +17 -0
  246. data/ext/pg_query/include/postmaster/interrupt.h +32 -0
  247. data/ext/pg_query/include/postmaster/pgarch.h +39 -0
  248. data/ext/pg_query/include/postmaster/postmaster.h +77 -0
  249. data/ext/pg_query/include/postmaster/syslogger.h +98 -0
  250. data/ext/pg_query/include/postmaster/walwriter.h +21 -0
  251. data/ext/pg_query/include/protobuf-c.h +1106 -0
  252. data/ext/pg_query/include/protobuf-c/protobuf-c.h +1106 -0
  253. data/ext/pg_query/include/protobuf/pg_query.pb-c.h +10846 -0
  254. data/ext/pg_query/include/protobuf/pg_query.pb.h +124718 -0
  255. data/ext/pg_query/include/regex/regex.h +184 -0
  256. data/ext/pg_query/include/replication/logicallauncher.h +31 -0
  257. data/ext/pg_query/include/replication/logicalproto.h +110 -0
  258. data/ext/pg_query/include/replication/logicalworker.h +19 -0
  259. data/ext/pg_query/include/replication/origin.h +73 -0
  260. data/ext/pg_query/include/replication/reorderbuffer.h +467 -0
  261. data/ext/pg_query/include/replication/slot.h +219 -0
  262. data/ext/pg_query/include/replication/syncrep.h +115 -0
  263. data/ext/pg_query/include/replication/walreceiver.h +340 -0
  264. data/ext/pg_query/include/replication/walsender.h +74 -0
  265. data/ext/pg_query/include/rewrite/prs2lock.h +46 -0
  266. data/ext/pg_query/include/rewrite/rewriteHandler.h +40 -0
  267. data/ext/pg_query/include/rewrite/rewriteManip.h +87 -0
  268. data/ext/pg_query/include/rewrite/rewriteSupport.h +26 -0
  269. data/ext/pg_query/include/storage/backendid.h +37 -0
  270. data/ext/pg_query/include/storage/block.h +121 -0
  271. data/ext/pg_query/include/storage/buf.h +46 -0
  272. data/ext/pg_query/include/storage/bufmgr.h +292 -0
  273. data/ext/pg_query/include/storage/bufpage.h +459 -0
  274. data/ext/pg_query/include/storage/condition_variable.h +62 -0
  275. data/ext/pg_query/include/storage/dsm.h +61 -0
  276. data/ext/pg_query/include/storage/dsm_impl.h +75 -0
  277. data/ext/pg_query/include/storage/fd.h +168 -0
  278. data/ext/pg_query/include/storage/ipc.h +81 -0
  279. data/ext/pg_query/include/storage/item.h +19 -0
  280. data/ext/pg_query/include/storage/itemid.h +184 -0
  281. data/ext/pg_query/include/storage/itemptr.h +206 -0
  282. data/ext/pg_query/include/storage/large_object.h +100 -0
  283. data/ext/pg_query/include/storage/latch.h +190 -0
  284. data/ext/pg_query/include/storage/lmgr.h +114 -0
  285. data/ext/pg_query/include/storage/lock.h +612 -0
  286. data/ext/pg_query/include/storage/lockdefs.h +59 -0
  287. data/ext/pg_query/include/storage/lwlock.h +232 -0
  288. data/ext/pg_query/include/storage/lwlocknames.h +51 -0
  289. data/ext/pg_query/include/storage/off.h +57 -0
  290. data/ext/pg_query/include/storage/pg_sema.h +61 -0
  291. data/ext/pg_query/include/storage/pg_shmem.h +90 -0
  292. data/ext/pg_query/include/storage/pmsignal.h +94 -0
  293. data/ext/pg_query/include/storage/predicate.h +87 -0
  294. data/ext/pg_query/include/storage/proc.h +333 -0
  295. data/ext/pg_query/include/storage/proclist_types.h +51 -0
  296. data/ext/pg_query/include/storage/procsignal.h +75 -0
  297. data/ext/pg_query/include/storage/relfilenode.h +99 -0
  298. data/ext/pg_query/include/storage/s_lock.h +1047 -0
  299. data/ext/pg_query/include/storage/sharedfileset.h +45 -0
  300. data/ext/pg_query/include/storage/shm_mq.h +85 -0
  301. data/ext/pg_query/include/storage/shm_toc.h +58 -0
  302. data/ext/pg_query/include/storage/shmem.h +81 -0
  303. data/ext/pg_query/include/storage/sinval.h +153 -0
  304. data/ext/pg_query/include/storage/sinvaladt.h +43 -0
  305. data/ext/pg_query/include/storage/smgr.h +109 -0
  306. data/ext/pg_query/include/storage/spin.h +77 -0
  307. data/ext/pg_query/include/storage/standby.h +91 -0
  308. data/ext/pg_query/include/storage/standbydefs.h +74 -0
  309. data/ext/pg_query/include/storage/sync.h +62 -0
  310. data/ext/pg_query/include/tcop/cmdtag.h +58 -0
  311. data/ext/pg_query/include/tcop/cmdtaglist.h +217 -0
  312. data/ext/pg_query/include/tcop/deparse_utility.h +108 -0
  313. data/ext/pg_query/include/tcop/dest.h +149 -0
  314. data/ext/pg_query/include/tcop/fastpath.h +21 -0
  315. data/ext/pg_query/include/tcop/pquery.h +45 -0
  316. data/ext/pg_query/include/tcop/tcopprot.h +89 -0
  317. data/ext/pg_query/include/tcop/utility.h +108 -0
  318. data/ext/pg_query/include/tsearch/ts_cache.h +98 -0
  319. data/ext/pg_query/include/utils/acl.h +312 -0
  320. data/ext/pg_query/include/utils/aclchk_internal.h +45 -0
  321. data/ext/pg_query/include/utils/array.h +458 -0
  322. data/ext/pg_query/include/utils/builtins.h +127 -0
  323. data/ext/pg_query/include/utils/bytea.h +27 -0
  324. data/ext/pg_query/include/utils/catcache.h +231 -0
  325. data/ext/pg_query/include/utils/date.h +90 -0
  326. data/ext/pg_query/include/utils/datetime.h +343 -0
  327. data/ext/pg_query/include/utils/datum.h +68 -0
  328. data/ext/pg_query/include/utils/dsa.h +123 -0
  329. data/ext/pg_query/include/utils/dynahash.h +19 -0
  330. data/ext/pg_query/include/utils/elog.h +439 -0
  331. data/ext/pg_query/include/utils/errcodes.h +352 -0
  332. data/ext/pg_query/include/utils/expandeddatum.h +159 -0
  333. data/ext/pg_query/include/utils/expandedrecord.h +231 -0
  334. data/ext/pg_query/include/utils/float.h +356 -0
  335. data/ext/pg_query/include/utils/fmgroids.h +2657 -0
  336. data/ext/pg_query/include/utils/fmgrprotos.h +2646 -0
  337. data/ext/pg_query/include/utils/fmgrtab.h +48 -0
  338. data/ext/pg_query/include/utils/guc.h +443 -0
  339. data/ext/pg_query/include/utils/guc_tables.h +272 -0
  340. data/ext/pg_query/include/utils/hsearch.h +149 -0
  341. data/ext/pg_query/include/utils/inval.h +64 -0
  342. data/ext/pg_query/include/utils/lsyscache.h +197 -0
  343. data/ext/pg_query/include/utils/memdebug.h +82 -0
  344. data/ext/pg_query/include/utils/memutils.h +225 -0
  345. data/ext/pg_query/include/utils/numeric.h +76 -0
  346. data/ext/pg_query/include/utils/palloc.h +136 -0
  347. data/ext/pg_query/include/utils/partcache.h +102 -0
  348. data/ext/pg_query/include/utils/pg_locale.h +119 -0
  349. data/ext/pg_query/include/utils/pg_lsn.h +29 -0
  350. data/ext/pg_query/include/utils/pidfile.h +56 -0
  351. data/ext/pg_query/include/utils/plancache.h +235 -0
  352. data/ext/pg_query/include/utils/portal.h +241 -0
  353. data/ext/pg_query/include/utils/probes.h +114 -0
  354. data/ext/pg_query/include/utils/ps_status.h +25 -0
  355. data/ext/pg_query/include/utils/queryenvironment.h +74 -0
  356. data/ext/pg_query/include/utils/regproc.h +28 -0
  357. data/ext/pg_query/include/utils/rel.h +644 -0
  358. data/ext/pg_query/include/utils/relcache.h +151 -0
  359. data/ext/pg_query/include/utils/reltrigger.h +81 -0
  360. data/ext/pg_query/include/utils/resowner.h +86 -0
  361. data/ext/pg_query/include/utils/rls.h +50 -0
  362. data/ext/pg_query/include/utils/ruleutils.h +44 -0
  363. data/ext/pg_query/include/utils/sharedtuplestore.h +61 -0
  364. data/ext/pg_query/include/utils/snapmgr.h +158 -0
  365. data/ext/pg_query/include/utils/snapshot.h +206 -0
  366. data/ext/pg_query/include/utils/sortsupport.h +276 -0
  367. data/ext/pg_query/include/utils/syscache.h +219 -0
  368. data/ext/pg_query/include/utils/timeout.h +88 -0
  369. data/ext/pg_query/include/utils/timestamp.h +116 -0
  370. data/ext/pg_query/include/utils/tuplesort.h +277 -0
  371. data/ext/pg_query/include/utils/tuplestore.h +91 -0
  372. data/ext/pg_query/include/utils/typcache.h +202 -0
  373. data/ext/pg_query/include/utils/tzparser.h +39 -0
  374. data/ext/pg_query/include/utils/varlena.h +39 -0
  375. data/ext/pg_query/include/utils/xml.h +84 -0
  376. data/ext/pg_query/include/xxhash.h +5445 -0
  377. data/ext/pg_query/include/xxhash/xxhash.h +5445 -0
  378. data/ext/pg_query/pg_query.c +104 -0
  379. data/ext/pg_query/pg_query.pb-c.c +37628 -0
  380. data/ext/pg_query/pg_query_deparse.c +9959 -0
  381. data/ext/pg_query/pg_query_fingerprint.c +295 -0
  382. data/ext/pg_query/pg_query_fingerprint.h +8 -0
  383. data/ext/pg_query/pg_query_internal.h +24 -0
  384. data/ext/pg_query/pg_query_json_plpgsql.c +738 -0
  385. data/ext/pg_query/pg_query_json_plpgsql.h +9 -0
  386. data/ext/pg_query/pg_query_normalize.c +439 -0
  387. data/ext/pg_query/pg_query_outfuncs.h +10 -0
  388. data/ext/pg_query/pg_query_outfuncs_json.c +297 -0
  389. data/ext/pg_query/pg_query_outfuncs_protobuf.c +237 -0
  390. data/ext/pg_query/pg_query_parse.c +148 -0
  391. data/ext/pg_query/pg_query_parse_plpgsql.c +460 -0
  392. data/ext/pg_query/pg_query_readfuncs.h +11 -0
  393. data/ext/pg_query/pg_query_readfuncs_protobuf.c +142 -0
  394. data/ext/pg_query/pg_query_ruby.c +108 -12
  395. data/ext/pg_query/pg_query_scan.c +173 -0
  396. data/ext/pg_query/pg_query_split.c +221 -0
  397. data/ext/pg_query/protobuf-c.c +3660 -0
  398. data/ext/pg_query/src_backend_catalog_namespace.c +1051 -0
  399. data/ext/pg_query/src_backend_catalog_pg_proc.c +142 -0
  400. data/ext/pg_query/src_backend_commands_define.c +117 -0
  401. data/ext/pg_query/src_backend_libpq_pqcomm.c +651 -0
  402. data/ext/pg_query/src_backend_nodes_bitmapset.c +513 -0
  403. data/ext/pg_query/src_backend_nodes_copyfuncs.c +6013 -0
  404. data/ext/pg_query/src_backend_nodes_equalfuncs.c +4003 -0
  405. data/ext/pg_query/src_backend_nodes_extensible.c +99 -0
  406. data/ext/pg_query/src_backend_nodes_list.c +922 -0
  407. data/ext/pg_query/src_backend_nodes_makefuncs.c +417 -0
  408. data/ext/pg_query/src_backend_nodes_nodeFuncs.c +1363 -0
  409. data/ext/pg_query/src_backend_nodes_value.c +84 -0
  410. data/ext/pg_query/src_backend_parser_gram.c +47456 -0
  411. data/ext/pg_query/src_backend_parser_parse_expr.c +313 -0
  412. data/ext/pg_query/src_backend_parser_parser.c +497 -0
  413. data/ext/pg_query/src_backend_parser_scan.c +7091 -0
  414. data/ext/pg_query/src_backend_parser_scansup.c +160 -0
  415. data/ext/pg_query/src_backend_postmaster_postmaster.c +2230 -0
  416. data/ext/pg_query/src_backend_storage_ipc_ipc.c +192 -0
  417. data/ext/pg_query/src_backend_storage_lmgr_s_lock.c +370 -0
  418. data/ext/pg_query/src_backend_tcop_postgres.c +776 -0
  419. data/ext/pg_query/src_backend_utils_adt_datum.c +326 -0
  420. data/ext/pg_query/src_backend_utils_adt_expandeddatum.c +98 -0
  421. data/ext/pg_query/src_backend_utils_adt_format_type.c +136 -0
  422. data/ext/pg_query/src_backend_utils_adt_ruleutils.c +1683 -0
  423. data/ext/pg_query/src_backend_utils_error_assert.c +74 -0
  424. data/ext/pg_query/src_backend_utils_error_elog.c +1748 -0
  425. data/ext/pg_query/src_backend_utils_fmgr_fmgr.c +570 -0
  426. data/ext/pg_query/src_backend_utils_hash_dynahash.c +1086 -0
  427. data/ext/pg_query/src_backend_utils_init_globals.c +168 -0
  428. data/ext/pg_query/src_backend_utils_mb_mbutils.c +839 -0
  429. data/ext/pg_query/src_backend_utils_misc_guc.c +1831 -0
  430. data/ext/pg_query/src_backend_utils_mmgr_aset.c +1560 -0
  431. data/ext/pg_query/src_backend_utils_mmgr_mcxt.c +1006 -0
  432. data/ext/pg_query/src_common_encnames.c +158 -0
  433. data/ext/pg_query/src_common_keywords.c +39 -0
  434. data/ext/pg_query/src_common_kwlist_d.h +1081 -0
  435. data/ext/pg_query/src_common_kwlookup.c +91 -0
  436. data/ext/pg_query/src_common_psprintf.c +158 -0
  437. data/ext/pg_query/src_common_string.c +86 -0
  438. data/ext/pg_query/src_common_stringinfo.c +336 -0
  439. data/ext/pg_query/src_common_wchar.c +1651 -0
  440. data/ext/pg_query/src_pl_plpgsql_src_pl_comp.c +1133 -0
  441. data/ext/pg_query/src_pl_plpgsql_src_pl_funcs.c +877 -0
  442. data/ext/pg_query/src_pl_plpgsql_src_pl_gram.c +6533 -0
  443. data/ext/pg_query/src_pl_plpgsql_src_pl_handler.c +107 -0
  444. data/ext/pg_query/src_pl_plpgsql_src_pl_reserved_kwlist_d.h +123 -0
  445. data/ext/pg_query/src_pl_plpgsql_src_pl_scanner.c +671 -0
  446. data/ext/pg_query/src_pl_plpgsql_src_pl_unreserved_kwlist_d.h +255 -0
  447. data/ext/pg_query/src_port_erand48.c +127 -0
  448. data/ext/pg_query/src_port_pg_bitutils.c +246 -0
  449. data/ext/pg_query/src_port_pgsleep.c +69 -0
  450. data/ext/pg_query/src_port_pgstrcasecmp.c +83 -0
  451. data/ext/pg_query/src_port_qsort.c +240 -0
  452. data/ext/pg_query/src_port_random.c +31 -0
  453. data/ext/pg_query/src_port_snprintf.c +1449 -0
  454. data/ext/pg_query/src_port_strerror.c +324 -0
  455. data/ext/pg_query/src_port_strnlen.c +39 -0
  456. data/ext/pg_query/xxhash.c +43 -0
  457. data/lib/pg_query.rb +7 -4
  458. data/lib/pg_query/constants.rb +21 -0
  459. data/lib/pg_query/deparse.rb +15 -1581
  460. data/lib/pg_query/filter_columns.rb +88 -85
  461. data/lib/pg_query/fingerprint.rb +122 -87
  462. data/lib/pg_query/json_field_names.rb +1402 -0
  463. data/lib/pg_query/node.rb +31 -0
  464. data/lib/pg_query/param_refs.rb +42 -37
  465. data/lib/pg_query/parse.rb +220 -203
  466. data/lib/pg_query/parse_error.rb +1 -1
  467. data/lib/pg_query/pg_query_pb.rb +3211 -0
  468. data/lib/pg_query/scan.rb +23 -0
  469. data/lib/pg_query/treewalker.rb +24 -40
  470. data/lib/pg_query/truncate.rb +71 -42
  471. data/lib/pg_query/version.rb +2 -2
  472. metadata +472 -11
  473. data/ext/pg_query/pg_query_ruby.h +0 -10
  474. data/lib/pg_query/deep_dup.rb +0 -16
  475. data/lib/pg_query/deparse/alter_table.rb +0 -42
  476. data/lib/pg_query/deparse/interval.rb +0 -105
  477. data/lib/pg_query/deparse/keywords.rb +0 -159
  478. data/lib/pg_query/deparse/rename.rb +0 -41
  479. data/lib/pg_query/legacy_parsetree.rb +0 -109
  480. data/lib/pg_query/node_types.rb +0 -296
@@ -0,0 +1,295 @@
1
+ // Ensure we have asprintf's definition on glibc-based platforms to avoid compiler warnings
2
+ #define _GNU_SOURCE
3
+ #include <stdio.h>
4
+
5
+ #include "pg_query.h"
6
+ #include "pg_query_internal.h"
7
+ #include "pg_query_fingerprint.h"
8
+
9
+ #include "postgres.h"
10
+ #include "xxhash/xxhash.h"
11
+ #include "lib/ilist.h"
12
+
13
+ #include "parser/parser.h"
14
+ #include "parser/scanner.h"
15
+ #include "parser/scansup.h"
16
+
17
+ #include "nodes/parsenodes.h"
18
+ #include "nodes/value.h"
19
+
20
+ #include <unistd.h>
21
+ #include <fcntl.h>
22
+
23
+ // Definitions
24
+
25
+ typedef struct FingerprintContext
26
+ {
27
+ XXH3_state_t *xxh_state;
28
+
29
+ bool write_tokens;
30
+ dlist_head tokens;
31
+ } FingerprintContext;
32
+
33
+ typedef struct FingerprintListContext
34
+ {
35
+ XXH64_hash_t hash;
36
+ size_t list_pos;
37
+ } FingerprintListContext;
38
+
39
+ typedef struct FingerprintToken
40
+ {
41
+ char *str;
42
+ dlist_node list_node;
43
+ } FingerprintToken;
44
+
45
+ static void _fingerprintNode(FingerprintContext *ctx, const void *obj, const void *parent, char *parent_field_name, unsigned int depth);
46
+ static void _fingerprintInitContext(FingerprintContext *ctx, bool write_tokens);
47
+ static void _fingerprintFreeContext(FingerprintContext *ctx);
48
+
49
+ #define PG_QUERY_FINGERPRINT_VERSION 3
50
+
51
+ // Implementations
52
+
53
+ static void
54
+ _fingerprintString(FingerprintContext *ctx, const char *str)
55
+ {
56
+ if (ctx->xxh_state != NULL) {
57
+ XXH3_64bits_update(ctx->xxh_state, str, strlen(str));
58
+ }
59
+
60
+ if (ctx->write_tokens) {
61
+ FingerprintToken *token = palloc0(sizeof(FingerprintToken));
62
+ token->str = pstrdup(str);
63
+ dlist_push_tail(&ctx->tokens, &token->list_node);
64
+ }
65
+ }
66
+
67
+ static void
68
+ _fingerprintInteger(FingerprintContext *ctx, const Value *node)
69
+ {
70
+ if (node->val.ival != 0) {
71
+ _fingerprintString(ctx, "Integer");
72
+ _fingerprintString(ctx, "ival");
73
+ char buffer[50];
74
+ sprintf(buffer, "%d", node->val.ival);
75
+ _fingerprintString(ctx, buffer);
76
+ }
77
+ }
78
+
79
+ static void
80
+ _fingerprintFloat(FingerprintContext *ctx, const Value *node)
81
+ {
82
+ if (node->val.str != NULL) {
83
+ _fingerprintString(ctx, "Float");
84
+ _fingerprintString(ctx, "str");
85
+ _fingerprintString(ctx, node->val.str);
86
+ }
87
+ }
88
+
89
+ static void
90
+ _fingerprintBitString(FingerprintContext *ctx, const Value *node)
91
+ {
92
+ if (node->val.str != NULL) {
93
+ _fingerprintString(ctx, "BitString");
94
+ _fingerprintString(ctx, "str");
95
+ _fingerprintString(ctx, node->val.str);
96
+ }
97
+ }
98
+
99
+ static int compareFingerprintListContext(const void *a, const void *b)
100
+ {
101
+ FingerprintListContext *ca = *(FingerprintListContext**) a;
102
+ FingerprintListContext *cb = *(FingerprintListContext**) b;
103
+ if (ca->hash > cb->hash)
104
+ return 1;
105
+ else if (ca->hash < cb->hash)
106
+ return -1;
107
+ return 0;
108
+ }
109
+
110
+ static void
111
+ _fingerprintList(FingerprintContext *ctx, const List *node, const void *parent, char *field_name, unsigned int depth)
112
+ {
113
+ if (field_name != NULL && (strcmp(field_name, "fromClause") == 0 || strcmp(field_name, "targetList") == 0 ||
114
+ strcmp(field_name, "cols") == 0 || strcmp(field_name, "rexpr") == 0 || strcmp(field_name, "valuesLists") == 0 ||
115
+ strcmp(field_name, "args") == 0)) {
116
+
117
+ FingerprintListContext** listCtxArr = palloc0(node->length * sizeof(FingerprintListContext*));
118
+ size_t listCtxCount = 0;
119
+ const ListCell *lc;
120
+
121
+ foreach(lc, node)
122
+ {
123
+ FingerprintContext subCtx;
124
+ FingerprintListContext* listCtx = palloc0(sizeof(FingerprintListContext));
125
+
126
+ _fingerprintInitContext(&subCtx, false);
127
+ _fingerprintNode(&subCtx, lfirst(lc), parent, field_name, depth + 1);
128
+ listCtx->hash = XXH3_64bits_digest(subCtx.xxh_state);
129
+ listCtx->list_pos = listCtxCount;
130
+ _fingerprintFreeContext(&subCtx);
131
+
132
+ listCtxArr[listCtxCount] = listCtx;
133
+ listCtxCount += 1;
134
+ }
135
+
136
+ pg_qsort(listCtxArr, listCtxCount, sizeof(FingerprintListContext*), compareFingerprintListContext);
137
+
138
+ for (size_t i = 0; i < listCtxCount; i++)
139
+ {
140
+ if (i > 0 && listCtxArr[i - 1]->hash == listCtxArr[i]->hash)
141
+ continue; // Ignore duplicates
142
+
143
+ _fingerprintNode(ctx, lfirst(list_nth_cell(node, listCtxArr[i]->list_pos)), parent, field_name, depth + 1);
144
+ }
145
+ } else {
146
+ const ListCell *lc;
147
+
148
+ foreach(lc, node)
149
+ {
150
+ _fingerprintNode(ctx, lfirst(lc), parent, field_name, depth + 1);
151
+
152
+ lnext(node, lc);
153
+ }
154
+ }
155
+ }
156
+
157
+ static void
158
+ _fingerprintInitContext(FingerprintContext *ctx, bool write_tokens) {
159
+ ctx->xxh_state = XXH3_createState();
160
+ if (ctx->xxh_state == NULL) abort();
161
+ if (XXH3_64bits_reset_withSeed(ctx->xxh_state, PG_QUERY_FINGERPRINT_VERSION) == XXH_ERROR) abort();
162
+
163
+ if (write_tokens) {
164
+ ctx->write_tokens = true;
165
+ dlist_init(&ctx->tokens);
166
+ } else {
167
+ ctx->write_tokens = false;
168
+ }
169
+ }
170
+
171
+ static void
172
+ _fingerprintFreeContext(FingerprintContext *ctx) {
173
+ XXH3_freeState(ctx->xxh_state);
174
+ }
175
+
176
+ #include "pg_query_enum_defs.c"
177
+ #include "pg_query_fingerprint_defs.c"
178
+
179
+ void
180
+ _fingerprintNode(FingerprintContext *ctx, const void *obj, const void *parent, char *field_name, unsigned int depth)
181
+ {
182
+ // Some queries are overly complex in their parsetree - lets consistently cut them off at 100 nodes deep
183
+ if (depth >= 100) {
184
+ return;
185
+ }
186
+
187
+ if (obj == NULL)
188
+ {
189
+ return; // Ignore
190
+ }
191
+
192
+ switch (nodeTag(obj))
193
+ {
194
+ case T_List:
195
+ _fingerprintList(ctx, obj, parent, field_name, depth);
196
+ break;
197
+ case T_Integer:
198
+ _fingerprintInteger(ctx, obj);
199
+ break;
200
+ case T_Float:
201
+ _fingerprintFloat(ctx, obj);
202
+ break;
203
+ case T_String:
204
+ _fingerprintString(ctx, "String");
205
+ _fingerprintString(ctx, "str");
206
+ _fingerprintString(ctx, ((Value*) obj)->val.str);
207
+ break;
208
+ case T_BitString:
209
+ _fingerprintBitString(ctx, obj);
210
+ break;
211
+
212
+ #include "pg_query_fingerprint_conds.c"
213
+
214
+ default:
215
+ elog(WARNING, "could not fingerprint unrecognized node type: %d",
216
+ (int) nodeTag(obj));
217
+
218
+ return;
219
+ }
220
+ }
221
+
222
+ PgQueryFingerprintResult pg_query_fingerprint_with_opts(const char* input, bool printTokens)
223
+ {
224
+ MemoryContext ctx = NULL;
225
+ PgQueryInternalParsetreeAndError parsetree_and_error;
226
+ PgQueryFingerprintResult result = {0};
227
+
228
+ ctx = pg_query_enter_memory_context();
229
+
230
+ parsetree_and_error = pg_query_raw_parse(input);
231
+
232
+ // These are all malloc-ed and will survive exiting the memory context, the caller is responsible to free them now
233
+ result.stderr_buffer = parsetree_and_error.stderr_buffer;
234
+ result.error = parsetree_and_error.error;
235
+
236
+ if (parsetree_and_error.tree != NULL || result.error == NULL) {
237
+ FingerprintContext ctx;
238
+ XXH64_canonical_t chash;
239
+
240
+ _fingerprintInitContext(&ctx, printTokens);
241
+
242
+ if (parsetree_and_error.tree != NULL) {
243
+ _fingerprintNode(&ctx, parsetree_and_error.tree, NULL, NULL, 0);
244
+ }
245
+
246
+ if (printTokens) {
247
+ dlist_iter iter;
248
+
249
+ printf("[");
250
+
251
+ dlist_foreach(iter, &ctx.tokens)
252
+ {
253
+ FingerprintToken *token = dlist_container(FingerprintToken, list_node, iter.cur);
254
+
255
+ printf("\"%s\", ", token->str);
256
+ }
257
+
258
+ printf("]\n");
259
+ }
260
+
261
+ result.fingerprint = XXH3_64bits_digest(ctx.xxh_state);
262
+ _fingerprintFreeContext(&ctx);
263
+
264
+ XXH64_canonicalFromHash(&chash, result.fingerprint);
265
+ int err = asprintf(&result.fingerprint_str, "%02x%02x%02x%02x%02x%02x%02x%02x",
266
+ chash.digest[0], chash.digest[1], chash.digest[2], chash.digest[3],
267
+ chash.digest[4], chash.digest[5], chash.digest[6], chash.digest[7]);
268
+ if (err == -1) {
269
+ PgQueryError* error = malloc(sizeof(PgQueryError));
270
+ error->message = strdup("Failed to output fingerprint string due to asprintf failure");
271
+ result.error = error;
272
+ }
273
+ }
274
+
275
+ pg_query_exit_memory_context(ctx);
276
+
277
+ return result;
278
+ }
279
+
280
+ PgQueryFingerprintResult pg_query_fingerprint(const char* input)
281
+ {
282
+ return pg_query_fingerprint_with_opts(input, false);
283
+ }
284
+
285
+ void pg_query_free_fingerprint_result(PgQueryFingerprintResult result)
286
+ {
287
+ if (result.error) {
288
+ free(result.error->message);
289
+ free(result.error->filename);
290
+ free(result.error);
291
+ }
292
+
293
+ free(result.fingerprint_str);
294
+ free(result.stderr_buffer);
295
+ }
@@ -0,0 +1,8 @@
1
+ #ifndef PG_QUERY_FINGERPRINT_H
2
+ #define PG_QUERY_FINGERPRINT_H
3
+
4
+ #include <stdbool.h>
5
+
6
+ PgQueryFingerprintResult pg_query_fingerprint_with_opts(const char* input, bool printTokens);
7
+
8
+ #endif
@@ -0,0 +1,24 @@
1
+ #ifndef PG_QUERY_INTERNAL_H
2
+ #define PG_QUERY_INTERNAL_H
3
+
4
+ #include "postgres.h"
5
+ #include "utils/memutils.h"
6
+ #include "nodes/pg_list.h"
7
+
8
+ #define STDERR_BUFFER_LEN 4096
9
+ #define DEBUG
10
+
11
+ typedef struct {
12
+ List *tree;
13
+ char* stderr_buffer;
14
+ PgQueryError* error;
15
+ } PgQueryInternalParsetreeAndError;
16
+
17
+ PgQueryInternalParsetreeAndError pg_query_raw_parse(const char* input);
18
+
19
+ void pg_query_free_error(PgQueryError *error);
20
+
21
+ MemoryContext pg_query_enter_memory_context();
22
+ void pg_query_exit_memory_context(MemoryContext ctx);
23
+
24
+ #endif
@@ -0,0 +1,738 @@
1
+ #include "pg_query.h"
2
+ #include "pg_query_json_plpgsql.h"
3
+
4
+ #include "pg_query_json_helper.c"
5
+
6
+ /* Write the label for the node type */
7
+ #define WRITE_NODE_TYPE(nodelabel) \
8
+ appendStringInfoString(out, "\"" nodelabel "\":{")
9
+
10
+ /* Write an integer field */
11
+ #define WRITE_INT_FIELD(outname, outname_json, fldname) \
12
+ if (node->fldname != 0) { \
13
+ appendStringInfo(out, "\"" CppAsString(outname_json) "\":%d,", node->fldname); \
14
+ }
15
+
16
+ /* Write a long-integer field */
17
+ #define WRITE_LONG_FIELD(outname, outname_json, fldname) \
18
+ if (node->fldname != 0) { \
19
+ appendStringInfo(out, "\"" CppAsString(outname_json) "\":%ld,", node->fldname); \
20
+ }
21
+
22
+ /* Write an enumerated-type field as an integer code */
23
+ #define WRITE_ENUM_FIELD(outname, outname_json, fldname) \
24
+ appendStringInfo(out, "\"" CppAsString(outname_json) "\":%d,", \
25
+ (int) node->fldname)
26
+
27
+ /* Write a boolean field */
28
+ #define WRITE_BOOL_FIELD(outname, outname_json, fldname) \
29
+ if (node->fldname) { \
30
+ appendStringInfo(out, "\"" CppAsString(outname_json) "\":%s,", \
31
+ booltostr(node->fldname)); \
32
+ }
33
+
34
+ /* Write a character-string (possibly NULL) field */
35
+ #define WRITE_STRING_FIELD(outname, outname_json, fldname) \
36
+ if (node->fldname != NULL) { \
37
+ appendStringInfo(out, "\"" CppAsString(outname_json) "\":"); \
38
+ _outToken(out, node->fldname); \
39
+ appendStringInfo(out, ","); \
40
+ }
41
+
42
+ #define WRITE_INT_VALUE(fldname, value) \
43
+ if (value != 0) { \
44
+ appendStringInfo(out, "\"" CppAsString(fldname) "\":%d,", value); \
45
+ }
46
+
47
+ #define WRITE_STRING_VALUE(fldname, value) \
48
+ if (true) { \
49
+ appendStringInfo(out, "\"" CppAsString(fldname) "\":"); \
50
+ _outToken(out, value); \
51
+ appendStringInfo(out, ","); \
52
+ }
53
+
54
+ #define WRITE_OBJ_FIELD(fldname, outfunc) \
55
+ if (node->fldname != NULL) { \
56
+ appendStringInfo(out, "\"" CppAsString(fldname) "\":{"); \
57
+ outfunc(out, node->fldname); \
58
+ removeTrailingDelimiter(out); \
59
+ appendStringInfo(out, "}},"); \
60
+ }
61
+
62
+ #define WRITE_LIST_FIELD(fldname, fldtype, outfunc) \
63
+ if (node->fldname != NULL) { \
64
+ ListCell *lc; \
65
+ appendStringInfo(out, "\"" CppAsString(fldname) "\":["); \
66
+ foreach(lc, node->fldname) { \
67
+ appendStringInfoString(out, "{"); \
68
+ outfunc(out, (fldtype *) lfirst(lc)); \
69
+ removeTrailingDelimiter(out); \
70
+ appendStringInfoString(out, "}},"); \
71
+ } \
72
+ removeTrailingDelimiter(out); \
73
+ appendStringInfoString(out, "],"); \
74
+ }
75
+
76
+ #define WRITE_STATEMENTS_FIELD(fldname) \
77
+ if (node->fldname != NULL) { \
78
+ ListCell *lc; \
79
+ appendStringInfo(out, "\"" CppAsString(fldname) "\":["); \
80
+ foreach(lc, node->fldname) { \
81
+ dump_stmt(out, (PLpgSQL_stmt *) lfirst(lc)); \
82
+ } \
83
+ removeTrailingDelimiter(out); \
84
+ appendStringInfoString(out, "],"); \
85
+ }
86
+
87
+ #define WRITE_EXPR_FIELD(fldname) WRITE_OBJ_FIELD(fldname, dump_expr)
88
+ #define WRITE_BLOCK_FIELD(fldname) WRITE_OBJ_FIELD(fldname, dump_block)
89
+ #define WRITE_RECORD_FIELD(fldname) WRITE_OBJ_FIELD(fldname, dump_record)
90
+ #define WRITE_ROW_FIELD(fldname) WRITE_OBJ_FIELD(fldname, dump_row)
91
+ #define WRITE_VAR_FIELD(fldname) WRITE_OBJ_FIELD(fldname, dump_var)
92
+ #define WRITE_VARIABLE_FIELD(fldname) WRITE_OBJ_FIELD(fldname, dump_variable);
93
+
94
+ static void dump_record(StringInfo out, PLpgSQL_rec *stmt);
95
+ static void dump_row(StringInfo out, PLpgSQL_row *stmt);
96
+ static void dump_var(StringInfo out, PLpgSQL_var *stmt);
97
+ static void dump_variable(StringInfo out, PLpgSQL_variable *stmt);
98
+ static void dump_record_field(StringInfo out, PLpgSQL_recfield *node);
99
+ static void dump_array_elem(StringInfo out, PLpgSQL_arrayelem *node);
100
+ static void dump_stmt(StringInfo out, PLpgSQL_stmt *stmt);
101
+ static void dump_block(StringInfo out, PLpgSQL_stmt_block *block);
102
+ static void dump_exception_block(StringInfo out, PLpgSQL_exception_block *node);
103
+ static void dump_assign(StringInfo out, PLpgSQL_stmt_assign *stmt);
104
+ static void dump_if(StringInfo out, PLpgSQL_stmt_if *stmt);
105
+ static void dump_if_elsif(StringInfo out, PLpgSQL_if_elsif *node);
106
+ static void dump_case(StringInfo out, PLpgSQL_stmt_case *stmt);
107
+ static void dump_case_when(StringInfo out, PLpgSQL_case_when *node);
108
+ static void dump_loop(StringInfo out, PLpgSQL_stmt_loop *stmt);
109
+ static void dump_while(StringInfo out, PLpgSQL_stmt_while *stmt);
110
+ static void dump_fori(StringInfo out, PLpgSQL_stmt_fori *stmt);
111
+ static void dump_fors(StringInfo out, PLpgSQL_stmt_fors *stmt);
112
+ static void dump_forc(StringInfo out, PLpgSQL_stmt_forc *stmt);
113
+ static void dump_foreach_a(StringInfo out, PLpgSQL_stmt_foreach_a *stmt);
114
+ static void dump_exit(StringInfo out, PLpgSQL_stmt_exit *stmt);
115
+ static void dump_return(StringInfo out, PLpgSQL_stmt_return *stmt);
116
+ static void dump_return_next(StringInfo out, PLpgSQL_stmt_return_next *stmt);
117
+ static void dump_return_query(StringInfo out, PLpgSQL_stmt_return_query *stmt);
118
+ static void dump_raise(StringInfo out, PLpgSQL_stmt_raise *stmt);
119
+ static void dump_raise_option(StringInfo out, PLpgSQL_raise_option *node);
120
+ static void dump_execsql(StringInfo out, PLpgSQL_stmt_execsql *stmt);
121
+ static void dump_dynexecute(StringInfo out, PLpgSQL_stmt_dynexecute *stmt);
122
+ static void dump_dynfors(StringInfo out, PLpgSQL_stmt_dynfors *stmt);
123
+ static void dump_getdiag(StringInfo out, PLpgSQL_stmt_getdiag *stmt);
124
+ static void dump_getdiag_item(StringInfo out, PLpgSQL_diag_item *node);
125
+ static void dump_open(StringInfo out, PLpgSQL_stmt_open *stmt);
126
+ static void dump_fetch(StringInfo out, PLpgSQL_stmt_fetch *stmt);
127
+ static void dump_close(StringInfo out, PLpgSQL_stmt_close *stmt);
128
+ static void dump_perform(StringInfo out, PLpgSQL_stmt_perform *stmt);
129
+ static void dump_expr(StringInfo out, PLpgSQL_expr *expr);
130
+ static void dump_function(StringInfo out, PLpgSQL_function *func);
131
+ static void dump_exception(StringInfo out, PLpgSQL_exception *node);
132
+ static void dump_condition(StringInfo out, PLpgSQL_condition *node);
133
+ static void dump_type(StringInfo out, PLpgSQL_type *node);
134
+
135
+ static void
136
+ dump_stmt(StringInfo out, PLpgSQL_stmt *node)
137
+ {
138
+ appendStringInfoChar(out, '{');
139
+ switch (node->cmd_type)
140
+ {
141
+ case PLPGSQL_STMT_BLOCK:
142
+ dump_block(out, (PLpgSQL_stmt_block *) node);
143
+ break;
144
+ case PLPGSQL_STMT_ASSIGN:
145
+ dump_assign(out, (PLpgSQL_stmt_assign *) node);
146
+ break;
147
+ case PLPGSQL_STMT_IF:
148
+ dump_if(out, (PLpgSQL_stmt_if *) node);
149
+ break;
150
+ case PLPGSQL_STMT_CASE:
151
+ dump_case(out, (PLpgSQL_stmt_case *) node);
152
+ break;
153
+ case PLPGSQL_STMT_LOOP:
154
+ dump_loop(out, (PLpgSQL_stmt_loop *) node);
155
+ break;
156
+ case PLPGSQL_STMT_WHILE:
157
+ dump_while(out, (PLpgSQL_stmt_while *) node);
158
+ break;
159
+ case PLPGSQL_STMT_FORI:
160
+ dump_fori(out, (PLpgSQL_stmt_fori *) node);
161
+ break;
162
+ case PLPGSQL_STMT_FORS:
163
+ dump_fors(out, (PLpgSQL_stmt_fors *) node);
164
+ break;
165
+ case PLPGSQL_STMT_FORC:
166
+ dump_forc(out, (PLpgSQL_stmt_forc *) node);
167
+ break;
168
+ case PLPGSQL_STMT_FOREACH_A:
169
+ dump_foreach_a(out, (PLpgSQL_stmt_foreach_a *) node);
170
+ break;
171
+ case PLPGSQL_STMT_EXIT:
172
+ dump_exit(out, (PLpgSQL_stmt_exit *) node);
173
+ break;
174
+ case PLPGSQL_STMT_RETURN:
175
+ dump_return(out, (PLpgSQL_stmt_return *) node);
176
+ break;
177
+ case PLPGSQL_STMT_RETURN_NEXT:
178
+ dump_return_next(out, (PLpgSQL_stmt_return_next *) node);
179
+ break;
180
+ case PLPGSQL_STMT_RETURN_QUERY:
181
+ dump_return_query(out, (PLpgSQL_stmt_return_query *) node);
182
+ break;
183
+ case PLPGSQL_STMT_RAISE:
184
+ dump_raise(out, (PLpgSQL_stmt_raise *) node);
185
+ break;
186
+ case PLPGSQL_STMT_EXECSQL:
187
+ dump_execsql(out, (PLpgSQL_stmt_execsql *) node);
188
+ break;
189
+ case PLPGSQL_STMT_DYNEXECUTE:
190
+ dump_dynexecute(out, (PLpgSQL_stmt_dynexecute *) node);
191
+ break;
192
+ case PLPGSQL_STMT_DYNFORS:
193
+ dump_dynfors(out, (PLpgSQL_stmt_dynfors *) node);
194
+ break;
195
+ case PLPGSQL_STMT_GETDIAG:
196
+ dump_getdiag(out, (PLpgSQL_stmt_getdiag *) node);
197
+ break;
198
+ case PLPGSQL_STMT_OPEN:
199
+ dump_open(out, (PLpgSQL_stmt_open *) node);
200
+ break;
201
+ case PLPGSQL_STMT_FETCH:
202
+ dump_fetch(out, (PLpgSQL_stmt_fetch *) node);
203
+ break;
204
+ case PLPGSQL_STMT_CLOSE:
205
+ dump_close(out, (PLpgSQL_stmt_close *) node);
206
+ break;
207
+ case PLPGSQL_STMT_PERFORM:
208
+ dump_perform(out, (PLpgSQL_stmt_perform *) node);
209
+ break;
210
+ default:
211
+ elog(ERROR, "unrecognized cmd_type: %d", node->cmd_type);
212
+ break;
213
+ }
214
+ removeTrailingDelimiter(out);
215
+ appendStringInfoString(out, "}},");
216
+ }
217
+
218
+ static void
219
+ dump_block(StringInfo out, PLpgSQL_stmt_block *node)
220
+ {
221
+ WRITE_NODE_TYPE("PLpgSQL_stmt_block");
222
+
223
+ WRITE_INT_FIELD(lineno, lineno, lineno);
224
+ WRITE_STRING_FIELD(label, label, label);
225
+ WRITE_STATEMENTS_FIELD(body);
226
+ WRITE_OBJ_FIELD(exceptions, dump_exception_block);
227
+
228
+ removeTrailingDelimiter(out);
229
+ }
230
+
231
+ static void
232
+ dump_exception_block(StringInfo out, PLpgSQL_exception_block *node)
233
+ {
234
+ WRITE_NODE_TYPE("PLpgSQL_exception_block");
235
+
236
+ WRITE_LIST_FIELD(exc_list, PLpgSQL_exception, dump_exception);
237
+ }
238
+
239
+ static void
240
+ dump_exception(StringInfo out, PLpgSQL_exception *node)
241
+ {
242
+ PLpgSQL_condition *cond;
243
+
244
+ WRITE_NODE_TYPE("PLpgSQL_exception");
245
+
246
+ appendStringInfo(out, "\"conditions\":[");
247
+ for (cond = node->conditions; cond; cond = cond->next)
248
+ {
249
+ appendStringInfoString(out, "{");
250
+ dump_condition(out, cond);
251
+ removeTrailingDelimiter(out);
252
+ appendStringInfoString(out, "}},");
253
+ }
254
+ removeTrailingDelimiter(out);
255
+ appendStringInfoString(out, "],");
256
+
257
+ WRITE_STATEMENTS_FIELD(action);
258
+ }
259
+
260
+ static void
261
+ dump_condition(StringInfo out, PLpgSQL_condition *node)
262
+ {
263
+ WRITE_NODE_TYPE("PLpgSQL_condition");
264
+
265
+ WRITE_STRING_FIELD(condname, condname, condname);
266
+ }
267
+
268
+ static void
269
+ dump_assign(StringInfo out, PLpgSQL_stmt_assign *node)
270
+ {
271
+ WRITE_NODE_TYPE("PLpgSQL_stmt_assign");
272
+
273
+ WRITE_INT_FIELD(lineno, lineno, lineno);
274
+ WRITE_INT_FIELD(varno, varno, varno);
275
+ WRITE_EXPR_FIELD(expr);
276
+ }
277
+
278
+ static void
279
+ dump_if(StringInfo out, PLpgSQL_stmt_if *node)
280
+ {
281
+ WRITE_NODE_TYPE("PLpgSQL_stmt_if");
282
+
283
+ WRITE_INT_FIELD(lineno, lineno, lineno);
284
+ WRITE_EXPR_FIELD(cond);
285
+ WRITE_STATEMENTS_FIELD(then_body);
286
+ WRITE_LIST_FIELD(elsif_list, PLpgSQL_if_elsif, dump_if_elsif);
287
+ WRITE_STATEMENTS_FIELD(else_body);
288
+ }
289
+
290
+ static void
291
+ dump_if_elsif(StringInfo out, PLpgSQL_if_elsif *node)
292
+ {
293
+ WRITE_NODE_TYPE("PLpgSQL_if_elsif");
294
+
295
+ WRITE_INT_FIELD(lineno, lineno, lineno);
296
+ WRITE_EXPR_FIELD(cond);
297
+ WRITE_STATEMENTS_FIELD(stmts);
298
+ }
299
+
300
+ static void
301
+ dump_case(StringInfo out, PLpgSQL_stmt_case *node)
302
+ {
303
+ ListCell *l;
304
+
305
+ WRITE_NODE_TYPE("PLpgSQL_stmt_case");
306
+
307
+ WRITE_INT_FIELD(lineno, lineno, lineno);
308
+ WRITE_EXPR_FIELD(t_expr);
309
+ WRITE_INT_FIELD(t_varno, t_varno, t_varno);
310
+ WRITE_LIST_FIELD(case_when_list, PLpgSQL_case_when, dump_case_when);
311
+ WRITE_BOOL_FIELD(have_else, have_else, have_else);
312
+ WRITE_STATEMENTS_FIELD(else_stmts);
313
+ }
314
+
315
+ static void
316
+ dump_case_when(StringInfo out, PLpgSQL_case_when *node)
317
+ {
318
+ WRITE_NODE_TYPE("PLpgSQL_case_when");
319
+
320
+ WRITE_INT_FIELD(lineno, lineno, lineno);
321
+ WRITE_EXPR_FIELD(expr);
322
+ WRITE_STATEMENTS_FIELD(stmts);
323
+ }
324
+
325
+ static void
326
+ dump_loop(StringInfo out, PLpgSQL_stmt_loop *node)
327
+ {
328
+ WRITE_NODE_TYPE("PLpgSQL_stmt_loop");
329
+
330
+ WRITE_INT_FIELD(lineno, lineno, lineno);
331
+ WRITE_STRING_FIELD(label, label, label);
332
+ WRITE_STATEMENTS_FIELD(body);
333
+ }
334
+
335
+ static void
336
+ dump_while(StringInfo out, PLpgSQL_stmt_while *node)
337
+ {
338
+ WRITE_NODE_TYPE("PLpgSQL_stmt_while");
339
+
340
+ WRITE_INT_FIELD(lineno, lineno, lineno);
341
+ WRITE_STRING_FIELD(label, label, label);
342
+ WRITE_EXPR_FIELD(cond);
343
+ WRITE_STATEMENTS_FIELD(body);
344
+ }
345
+
346
+ /* FOR statement with integer loopvar */
347
+ static void
348
+ dump_fori(StringInfo out, PLpgSQL_stmt_fori *node)
349
+ {
350
+ WRITE_NODE_TYPE("PLpgSQL_stmt_fori");
351
+
352
+ WRITE_INT_FIELD(lineno, lineno, lineno);
353
+ WRITE_STRING_FIELD(label, label, label);
354
+ WRITE_VAR_FIELD(var);
355
+ WRITE_EXPR_FIELD(lower);
356
+ WRITE_EXPR_FIELD(upper);
357
+ WRITE_EXPR_FIELD(step);
358
+ WRITE_BOOL_FIELD(reverse, reverse, reverse);
359
+ WRITE_STATEMENTS_FIELD(body);
360
+ }
361
+
362
+ static void
363
+ dump_fors(StringInfo out, PLpgSQL_stmt_fors *node)
364
+ {
365
+ WRITE_NODE_TYPE("PLpgSQL_stmt_fors");
366
+
367
+ WRITE_INT_FIELD(lineno, lineno, lineno);
368
+ WRITE_STRING_FIELD(label, label, label);
369
+ WRITE_VARIABLE_FIELD(var);
370
+ WRITE_STATEMENTS_FIELD(body);
371
+ WRITE_EXPR_FIELD(query);
372
+ }
373
+
374
+ static void
375
+ dump_forc(StringInfo out, PLpgSQL_stmt_forc *node)
376
+ {
377
+ WRITE_NODE_TYPE("PLpgSQL_stmt_forc");
378
+
379
+ WRITE_INT_FIELD(lineno, lineno, lineno);
380
+ WRITE_STRING_FIELD(label, label, label);
381
+ WRITE_VARIABLE_FIELD(var);
382
+ WRITE_STATEMENTS_FIELD(body);
383
+ WRITE_INT_FIELD(curvar, curvar, curvar);
384
+ WRITE_EXPR_FIELD(argquery);
385
+ }
386
+
387
+ static void
388
+ dump_foreach_a(StringInfo out, PLpgSQL_stmt_foreach_a *node)
389
+ {
390
+ WRITE_NODE_TYPE("PLpgSQL_stmt_foreach_a");
391
+
392
+ WRITE_INT_FIELD(lineno, lineno, lineno);
393
+ WRITE_STRING_FIELD(label, label, label);
394
+ WRITE_INT_FIELD(varno, varno, varno);
395
+ WRITE_INT_FIELD(slice, slice, slice);
396
+ WRITE_EXPR_FIELD(expr);
397
+ WRITE_STATEMENTS_FIELD(body);
398
+ }
399
+
400
+ static void
401
+ dump_open(StringInfo out, PLpgSQL_stmt_open *node)
402
+ {
403
+ WRITE_NODE_TYPE("PLpgSQL_stmt_open");
404
+
405
+ WRITE_INT_FIELD(lineno, lineno, lineno);
406
+ WRITE_INT_FIELD(curvar, curvar, curvar);
407
+ WRITE_INT_FIELD(cursor_options, cursor_options, cursor_options);
408
+ WRITE_EXPR_FIELD(argquery);
409
+ WRITE_EXPR_FIELD(query);
410
+ WRITE_EXPR_FIELD(dynquery);
411
+ WRITE_LIST_FIELD(params, PLpgSQL_expr, dump_expr);
412
+ }
413
+
414
+ static void
415
+ dump_fetch(StringInfo out, PLpgSQL_stmt_fetch *node)
416
+ {
417
+ WRITE_NODE_TYPE("PLpgSQL_stmt_fetch");
418
+
419
+ WRITE_INT_FIELD(lineno, lineno, lineno);
420
+ WRITE_VARIABLE_FIELD(target);
421
+ WRITE_INT_FIELD(curvar, curvar, curvar);
422
+ WRITE_ENUM_FIELD(direction, direction, direction);
423
+ WRITE_LONG_FIELD(how_many, how_many, how_many);
424
+ WRITE_EXPR_FIELD(expr);
425
+ WRITE_BOOL_FIELD(is_move, is_move, is_move);
426
+ WRITE_BOOL_FIELD(returns_multiple_rows, returns_multiple_rows, returns_multiple_rows);
427
+ }
428
+
429
+ static void
430
+ dump_close(StringInfo out, PLpgSQL_stmt_close *node)
431
+ {
432
+ WRITE_NODE_TYPE("PLpgSQL_stmt_close");
433
+
434
+ WRITE_INT_FIELD(lineno, lineno, lineno);
435
+ WRITE_INT_FIELD(curvar, curvar, curvar);
436
+ }
437
+
438
+ static void
439
+ dump_perform(StringInfo out, PLpgSQL_stmt_perform *node)
440
+ {
441
+ WRITE_NODE_TYPE("PLpgSQL_stmt_perform");
442
+
443
+ WRITE_INT_FIELD(lineno, lineno, lineno);
444
+ WRITE_EXPR_FIELD(expr);
445
+ }
446
+
447
+ static void
448
+ dump_exit(StringInfo out, PLpgSQL_stmt_exit *node)
449
+ {
450
+ WRITE_NODE_TYPE("PLpgSQL_stmt_exit");
451
+
452
+ WRITE_INT_FIELD(lineno, lineno, lineno);
453
+ WRITE_BOOL_FIELD(is_exit, is_exit, is_exit);
454
+ WRITE_STRING_FIELD(label, label, label);
455
+ WRITE_EXPR_FIELD(cond);
456
+ }
457
+
458
+ static void
459
+ dump_return(StringInfo out, PLpgSQL_stmt_return *node)
460
+ {
461
+ WRITE_NODE_TYPE("PLpgSQL_stmt_return");
462
+
463
+ WRITE_INT_FIELD(lineno, lineno, lineno);
464
+ WRITE_EXPR_FIELD(expr);
465
+ //WRITE_INT_FIELD(retvarno);
466
+ }
467
+
468
+ static void
469
+ dump_return_next(StringInfo out, PLpgSQL_stmt_return_next *node)
470
+ {
471
+ WRITE_NODE_TYPE("PLpgSQL_stmt_return_next");
472
+
473
+ WRITE_INT_FIELD(lineno, lineno, lineno);
474
+ WRITE_EXPR_FIELD(expr);
475
+ //WRITE_INT_FIELD(retvarno);
476
+ }
477
+
478
+ static void
479
+ dump_return_query(StringInfo out, PLpgSQL_stmt_return_query *node)
480
+ {
481
+ WRITE_NODE_TYPE("PLpgSQL_stmt_return_query");
482
+
483
+ WRITE_INT_FIELD(lineno, lineno, lineno);
484
+ WRITE_EXPR_FIELD(query);
485
+ WRITE_EXPR_FIELD(dynquery);
486
+ WRITE_LIST_FIELD(params, PLpgSQL_expr, dump_expr);
487
+ }
488
+
489
+ static void
490
+ dump_raise(StringInfo out, PLpgSQL_stmt_raise *node)
491
+ {
492
+ WRITE_NODE_TYPE("PLpgSQL_stmt_raise");
493
+
494
+ WRITE_INT_FIELD(lineno, lineno, lineno);
495
+ WRITE_INT_FIELD(elog_level, elog_level, elog_level);
496
+ WRITE_STRING_FIELD(condname, condname, condname);
497
+ WRITE_STRING_FIELD(message, message, message);
498
+ WRITE_LIST_FIELD(params, PLpgSQL_expr, dump_expr);
499
+ WRITE_LIST_FIELD(options, PLpgSQL_raise_option, dump_raise_option);
500
+ }
501
+
502
+ static void
503
+ dump_raise_option(StringInfo out, PLpgSQL_raise_option *node)
504
+ {
505
+ WRITE_NODE_TYPE("PLpgSQL_raise_option");
506
+
507
+ WRITE_ENUM_FIELD(opt_type, opt_type, opt_type);
508
+ WRITE_EXPR_FIELD(expr);
509
+ }
510
+
511
+ static void
512
+ dump_execsql(StringInfo out, PLpgSQL_stmt_execsql *node)
513
+ {
514
+ WRITE_NODE_TYPE("PLpgSQL_stmt_execsql");
515
+
516
+ WRITE_INT_FIELD(lineno, lineno, lineno);
517
+ WRITE_EXPR_FIELD(sqlstmt);
518
+ //WRITE_BOOL_FIELD(mod_stmt); // This is only populated when executing the function
519
+ WRITE_BOOL_FIELD(into, into, into);
520
+ WRITE_BOOL_FIELD(strict, strict, strict);
521
+ WRITE_VARIABLE_FIELD(target);
522
+ }
523
+
524
+ static void
525
+ dump_dynexecute(StringInfo out, PLpgSQL_stmt_dynexecute *node)
526
+ {
527
+ WRITE_NODE_TYPE("PLpgSQL_stmt_dynexecute");
528
+
529
+ WRITE_INT_FIELD(lineno, lineno, lineno);
530
+ WRITE_EXPR_FIELD(query);
531
+ WRITE_BOOL_FIELD(into, into, into);
532
+ WRITE_BOOL_FIELD(strict, strict, strict);
533
+ WRITE_VARIABLE_FIELD(target);
534
+ WRITE_LIST_FIELD(params, PLpgSQL_expr, dump_expr);
535
+ }
536
+
537
+ static void
538
+ dump_dynfors(StringInfo out, PLpgSQL_stmt_dynfors *node)
539
+ {
540
+ WRITE_NODE_TYPE("PLpgSQL_stmt_dynfors");
541
+
542
+ WRITE_INT_FIELD(lineno, lineno, lineno);
543
+ WRITE_STRING_FIELD(label, label, label);
544
+ WRITE_VARIABLE_FIELD(var);
545
+ WRITE_STATEMENTS_FIELD(body);
546
+ WRITE_EXPR_FIELD(query);
547
+ WRITE_LIST_FIELD(params, PLpgSQL_expr, dump_expr);
548
+ }
549
+
550
+ static void
551
+ dump_getdiag(StringInfo out, PLpgSQL_stmt_getdiag *node)
552
+ {
553
+ WRITE_NODE_TYPE("PLpgSQL_stmt_getdiag");
554
+
555
+ WRITE_INT_FIELD(lineno, lineno, lineno);
556
+ WRITE_BOOL_FIELD(is_stacked, is_stacked, is_stacked);
557
+ WRITE_LIST_FIELD(diag_items, PLpgSQL_diag_item, dump_getdiag_item);
558
+ }
559
+
560
+ static void
561
+ dump_getdiag_item(StringInfo out, PLpgSQL_diag_item *node)
562
+ {
563
+ WRITE_NODE_TYPE("PLpgSQL_diag_item");
564
+
565
+ WRITE_STRING_VALUE(kind, plpgsql_getdiag_kindname(node->kind));
566
+ WRITE_INT_FIELD(target, target, target);
567
+ }
568
+
569
+ static void
570
+ dump_expr(StringInfo out, PLpgSQL_expr *node)
571
+ {
572
+ WRITE_NODE_TYPE("PLpgSQL_expr");
573
+
574
+ WRITE_STRING_FIELD(query, query, query);
575
+ }
576
+
577
+ static void
578
+ dump_function(StringInfo out, PLpgSQL_function *node)
579
+ {
580
+ int i;
581
+ PLpgSQL_datum *d;
582
+
583
+ WRITE_NODE_TYPE("PLpgSQL_function");
584
+ WRITE_INT_FIELD(new_varno, new_varno, new_varno);
585
+ WRITE_INT_FIELD(old_varno, old_varno, old_varno);
586
+
587
+ appendStringInfoString(out, "\"datums\":");
588
+ appendStringInfoChar(out, '[');
589
+ for (i = 0; i < node->ndatums; i++)
590
+ {
591
+ appendStringInfoChar(out, '{');
592
+ d = node->datums[i];
593
+
594
+ switch (d->dtype)
595
+ {
596
+ case PLPGSQL_DTYPE_VAR:
597
+ dump_var(out, (PLpgSQL_var *) d);
598
+ break;
599
+ case PLPGSQL_DTYPE_ROW:
600
+ dump_row(out, (PLpgSQL_row *) d);
601
+ break;
602
+ case PLPGSQL_DTYPE_REC:
603
+ dump_record(out, (PLpgSQL_rec *) d);
604
+ break;
605
+ case PLPGSQL_DTYPE_RECFIELD:
606
+ dump_record_field(out, (PLpgSQL_recfield *) d);
607
+ break;
608
+ case PLPGSQL_DTYPE_ARRAYELEM:
609
+ dump_array_elem(out, (PLpgSQL_arrayelem *) d);
610
+ break;
611
+ default:
612
+ elog(WARNING, "could not dump unrecognized dtype: %d",
613
+ (int) d->dtype);
614
+ }
615
+ removeTrailingDelimiter(out);
616
+ appendStringInfoString(out, "}},");
617
+ }
618
+ removeTrailingDelimiter(out);
619
+ appendStringInfoString(out, "],");
620
+
621
+ WRITE_BLOCK_FIELD(action);
622
+ }
623
+
624
+ static void
625
+ dump_var(StringInfo out, PLpgSQL_var *node)
626
+ {
627
+ WRITE_NODE_TYPE("PLpgSQL_var");
628
+
629
+ WRITE_STRING_FIELD(refname, refname, refname);
630
+ WRITE_INT_FIELD(lineno, lineno, lineno);
631
+ WRITE_OBJ_FIELD(datatype, dump_type);
632
+ WRITE_BOOL_FIELD(isconst, isconst, isconst);
633
+ WRITE_BOOL_FIELD(notnull, notnull, notnull);
634
+ WRITE_EXPR_FIELD(default_val);
635
+ WRITE_EXPR_FIELD(cursor_explicit_expr);
636
+ WRITE_INT_FIELD(cursor_explicit_argrow, cursor_explicit_argrow, cursor_explicit_argrow);
637
+ WRITE_INT_FIELD(cursor_options, cursor_options, cursor_options);
638
+ }
639
+
640
+ static void
641
+ dump_variable(StringInfo out, PLpgSQL_variable *node)
642
+ {
643
+ switch (node->dtype)
644
+ {
645
+ case PLPGSQL_DTYPE_REC:
646
+ dump_record(out, (PLpgSQL_rec *) node);
647
+ break;
648
+ case PLPGSQL_DTYPE_VAR:
649
+ dump_var(out, (PLpgSQL_var *) node);
650
+ break;
651
+ case PLPGSQL_DTYPE_ROW:
652
+ dump_row(out, (PLpgSQL_row *) node);
653
+ break;
654
+ default:
655
+ elog(ERROR, "unrecognized variable type: %d", node->dtype);
656
+ break;
657
+ }
658
+ }
659
+
660
+ static void
661
+ dump_type(StringInfo out, PLpgSQL_type *node)
662
+ {
663
+ WRITE_NODE_TYPE("PLpgSQL_type");
664
+
665
+ WRITE_STRING_FIELD(typname, typname, typname);
666
+ }
667
+
668
+ static void
669
+ dump_row(StringInfo out, PLpgSQL_row *node)
670
+ {
671
+ int i = 0;
672
+
673
+ WRITE_NODE_TYPE("PLpgSQL_row");
674
+
675
+ WRITE_STRING_FIELD(refname, refname, refname);
676
+ WRITE_INT_FIELD(lineno, lineno, lineno);
677
+
678
+ appendStringInfoString(out, "\"fields\":");
679
+ appendStringInfoChar(out, '[');
680
+
681
+ for (i = 0; i < node->nfields; i++)
682
+ {
683
+ if (node->fieldnames[i]) {
684
+ appendStringInfoChar(out, '{');
685
+ WRITE_STRING_VALUE(name, node->fieldnames[i]);
686
+ WRITE_INT_VALUE(varno, node->varnos[i]);
687
+ removeTrailingDelimiter(out);
688
+ appendStringInfoString(out, "},");
689
+ } else {
690
+ appendStringInfoString(out, "null,");
691
+ }
692
+ }
693
+ removeTrailingDelimiter(out);
694
+
695
+ appendStringInfoString(out, "],");
696
+ }
697
+
698
+ static void
699
+ dump_record(StringInfo out, PLpgSQL_rec *node) {
700
+ WRITE_NODE_TYPE("PLpgSQL_rec");
701
+
702
+ WRITE_STRING_FIELD(refname, refname, refname);
703
+ WRITE_INT_FIELD(dno, dno, dno);
704
+ WRITE_INT_FIELD(lineno, lineno, lineno);
705
+ }
706
+
707
+ static void
708
+ dump_record_field(StringInfo out, PLpgSQL_recfield *node) {
709
+ WRITE_NODE_TYPE("PLpgSQL_recfield");
710
+
711
+ WRITE_STRING_FIELD(fieldname, fieldname, fieldname);
712
+ WRITE_INT_FIELD(recparentno, recparentno, recparentno);
713
+ }
714
+
715
+ static void
716
+ dump_array_elem(StringInfo out, PLpgSQL_arrayelem *node) {
717
+ WRITE_NODE_TYPE("PLpgSQL_arrayelem");
718
+
719
+ WRITE_EXPR_FIELD(subscript);
720
+ WRITE_INT_FIELD(arrayparentno, arrayparentno, arrayparentno);
721
+ }
722
+
723
+ char *
724
+ plpgsqlToJSON(PLpgSQL_function *func)
725
+ {
726
+ StringInfoData str;
727
+
728
+ initStringInfo(&str);
729
+
730
+ appendStringInfoChar(&str, '{');
731
+
732
+ dump_function(&str, func);
733
+
734
+ removeTrailingDelimiter(&str);
735
+ appendStringInfoString(&str, "}}");
736
+
737
+ return str.data;
738
+ }