relq 1.0.0

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.
Files changed (305) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +862 -0
  3. package/dist/addons/buffer.js +1869 -0
  4. package/dist/addons/pg-cursor.js +1425 -0
  5. package/dist/addons/pg-format.js +2248 -0
  6. package/dist/addons/pg.js +4790 -0
  7. package/dist/bin/relq.js +2 -0
  8. package/dist/cjs/cache/index.cjs +9 -0
  9. package/dist/cjs/cache/query-cache.cjs +311 -0
  10. package/dist/cjs/cli/commands/add.cjs +82 -0
  11. package/dist/cjs/cli/commands/commit.cjs +145 -0
  12. package/dist/cjs/cli/commands/diff.cjs +84 -0
  13. package/dist/cjs/cli/commands/export.cjs +333 -0
  14. package/dist/cjs/cli/commands/fetch.cjs +59 -0
  15. package/dist/cjs/cli/commands/generate.cjs +242 -0
  16. package/dist/cjs/cli/commands/history.cjs +165 -0
  17. package/dist/cjs/cli/commands/import.cjs +524 -0
  18. package/dist/cjs/cli/commands/init.cjs +437 -0
  19. package/dist/cjs/cli/commands/introspect.cjs +142 -0
  20. package/dist/cjs/cli/commands/log.cjs +62 -0
  21. package/dist/cjs/cli/commands/migrate.cjs +167 -0
  22. package/dist/cjs/cli/commands/pull.cjs +410 -0
  23. package/dist/cjs/cli/commands/push.cjs +165 -0
  24. package/dist/cjs/cli/commands/rollback.cjs +169 -0
  25. package/dist/cjs/cli/commands/status.cjs +110 -0
  26. package/dist/cjs/cli/commands/sync.cjs +79 -0
  27. package/dist/cjs/cli/index.cjs +275 -0
  28. package/dist/cjs/cli/utils/change-tracker.cjs +446 -0
  29. package/dist/cjs/cli/utils/commit-manager.cjs +239 -0
  30. package/dist/cjs/cli/utils/config-loader.cjs +127 -0
  31. package/dist/cjs/cli/utils/env-loader.cjs +62 -0
  32. package/dist/cjs/cli/utils/fast-introspect.cjs +398 -0
  33. package/dist/cjs/cli/utils/git-utils.cjs +404 -0
  34. package/dist/cjs/cli/utils/migration-generator.cjs +269 -0
  35. package/dist/cjs/cli/utils/relqignore.cjs +114 -0
  36. package/dist/cjs/cli/utils/repo-manager.cjs +515 -0
  37. package/dist/cjs/cli/utils/schema-comparator.cjs +313 -0
  38. package/dist/cjs/cli/utils/schema-diff.cjs +284 -0
  39. package/dist/cjs/cli/utils/schema-hash.cjs +108 -0
  40. package/dist/cjs/cli/utils/schema-introspect.cjs +455 -0
  41. package/dist/cjs/cli/utils/snapshot-manager.cjs +223 -0
  42. package/dist/cjs/cli/utils/spinner.cjs +108 -0
  43. package/dist/cjs/cli/utils/sql-generator.cjs +520 -0
  44. package/dist/cjs/cli/utils/sql-parser.cjs +999 -0
  45. package/dist/cjs/cli/utils/type-generator.cjs +2061 -0
  46. package/dist/cjs/condition/array-condition-builder.cjs +503 -0
  47. package/dist/cjs/condition/array-numeric-condition-builder.cjs +186 -0
  48. package/dist/cjs/condition/array-specialized-condition-builder.cjs +206 -0
  49. package/dist/cjs/condition/array-string-condition-builder.cjs +146 -0
  50. package/dist/cjs/condition/base-condition-builder.cjs +2 -0
  51. package/dist/cjs/condition/condition-collector.cjs +284 -0
  52. package/dist/cjs/condition/fulltext-condition-builder.cjs +61 -0
  53. package/dist/cjs/condition/geometric-condition-builder.cjs +208 -0
  54. package/dist/cjs/condition/index.cjs +25 -0
  55. package/dist/cjs/condition/jsonb-condition-builder.cjs +160 -0
  56. package/dist/cjs/condition/network-condition-builder.cjs +230 -0
  57. package/dist/cjs/condition/range-condition-builder.cjs +82 -0
  58. package/dist/cjs/config/config.cjs +190 -0
  59. package/dist/cjs/config/index.cjs +9 -0
  60. package/dist/cjs/constants/pg-values.cjs +68 -0
  61. package/dist/cjs/copy/copy-builder.cjs +316 -0
  62. package/dist/cjs/copy/index.cjs +6 -0
  63. package/dist/cjs/core/query-builder.cjs +440 -0
  64. package/dist/cjs/core/relq-client.cjs +1831 -0
  65. package/dist/cjs/core/typed-kuery-client.cjs +2 -0
  66. package/dist/cjs/count/count-builder.cjs +88 -0
  67. package/dist/cjs/count/index.cjs +5 -0
  68. package/dist/cjs/cte/cte-builder.cjs +89 -0
  69. package/dist/cjs/cte/index.cjs +5 -0
  70. package/dist/cjs/ddl/function.cjs +48 -0
  71. package/dist/cjs/ddl/index.cjs +7 -0
  72. package/dist/cjs/ddl/sql.cjs +54 -0
  73. package/dist/cjs/delete/delete-builder.cjs +135 -0
  74. package/dist/cjs/delete/index.cjs +5 -0
  75. package/dist/cjs/errors/relq-errors.cjs +329 -0
  76. package/dist/cjs/examples/fulltext-search-test.cjs +122 -0
  77. package/dist/cjs/explain/explain-builder.cjs +99 -0
  78. package/dist/cjs/explain/index.cjs +5 -0
  79. package/dist/cjs/function/create-function-builder.cjs +196 -0
  80. package/dist/cjs/function/index.cjs +6 -0
  81. package/dist/cjs/functions/advanced-functions.cjs +241 -0
  82. package/dist/cjs/functions/case-builder.cjs +66 -0
  83. package/dist/cjs/functions/geometric-functions.cjs +104 -0
  84. package/dist/cjs/functions/index.cjs +184 -0
  85. package/dist/cjs/functions/network-functions.cjs +86 -0
  86. package/dist/cjs/functions/sql-functions.cjs +431 -0
  87. package/dist/cjs/index.cjs +164 -0
  88. package/dist/cjs/indexing/create-index-builder.cjs +187 -0
  89. package/dist/cjs/indexing/drop-index-builder.cjs +89 -0
  90. package/dist/cjs/indexing/index-types.cjs +2 -0
  91. package/dist/cjs/indexing/index.cjs +8 -0
  92. package/dist/cjs/insert/conflict-builder.cjs +173 -0
  93. package/dist/cjs/insert/index.cjs +5 -0
  94. package/dist/cjs/insert/insert-builder.cjs +254 -0
  95. package/dist/cjs/introspect/index.cjs +229 -0
  96. package/dist/cjs/maintenance/index.cjs +6 -0
  97. package/dist/cjs/maintenance/vacuum-builder.cjs +166 -0
  98. package/dist/cjs/pubsub/index.cjs +7 -0
  99. package/dist/cjs/pubsub/listen-notify-builder.cjs +57 -0
  100. package/dist/cjs/pubsub/listener-connection.cjs +180 -0
  101. package/dist/cjs/raw/index.cjs +5 -0
  102. package/dist/cjs/raw/raw-query-builder.cjs +27 -0
  103. package/dist/cjs/schema/index.cjs +15 -0
  104. package/dist/cjs/schema/schema-builder.cjs +1167 -0
  105. package/dist/cjs/schema-builder.cjs +21 -0
  106. package/dist/cjs/schema-definition/column-types.cjs +829 -0
  107. package/dist/cjs/schema-definition/index.cjs +62 -0
  108. package/dist/cjs/schema-definition/introspection.cjs +620 -0
  109. package/dist/cjs/schema-definition/partitions.cjs +129 -0
  110. package/dist/cjs/schema-definition/pg-enum.cjs +76 -0
  111. package/dist/cjs/schema-definition/pg-function.cjs +91 -0
  112. package/dist/cjs/schema-definition/pg-sequence.cjs +56 -0
  113. package/dist/cjs/schema-definition/pg-trigger.cjs +108 -0
  114. package/dist/cjs/schema-definition/relations.cjs +98 -0
  115. package/dist/cjs/schema-definition/sql-expressions.cjs +202 -0
  116. package/dist/cjs/schema-definition/table-definition.cjs +636 -0
  117. package/dist/cjs/select/aggregate-builder.cjs +179 -0
  118. package/dist/cjs/select/index.cjs +5 -0
  119. package/dist/cjs/select/select-builder.cjs +233 -0
  120. package/dist/cjs/sequence/index.cjs +7 -0
  121. package/dist/cjs/sequence/sequence-builder.cjs +264 -0
  122. package/dist/cjs/table/alter-table-builder.cjs +146 -0
  123. package/dist/cjs/table/constraint-builder.cjs +102 -0
  124. package/dist/cjs/table/create-table-builder.cjs +248 -0
  125. package/dist/cjs/table/index.cjs +17 -0
  126. package/dist/cjs/table/partition-builder.cjs +131 -0
  127. package/dist/cjs/table/truncate-builder.cjs +70 -0
  128. package/dist/cjs/transaction/index.cjs +6 -0
  129. package/dist/cjs/transaction/transaction-builder.cjs +78 -0
  130. package/dist/cjs/trigger/create-trigger-builder.cjs +174 -0
  131. package/dist/cjs/trigger/index.cjs +6 -0
  132. package/dist/cjs/types/aggregate-types.cjs +2 -0
  133. package/dist/cjs/types/config-types.cjs +40 -0
  134. package/dist/cjs/types/inference-types.cjs +18 -0
  135. package/dist/cjs/types/pagination-types.cjs +7 -0
  136. package/dist/cjs/types/result-types.cjs +2 -0
  137. package/dist/cjs/types/schema-types.cjs +2 -0
  138. package/dist/cjs/types/subscription-types.cjs +2 -0
  139. package/dist/cjs/types.cjs +2 -0
  140. package/dist/cjs/update/array-update-builder.cjs +205 -0
  141. package/dist/cjs/update/index.cjs +13 -0
  142. package/dist/cjs/update/update-builder.cjs +195 -0
  143. package/dist/cjs/utils/case-converter.cjs +58 -0
  144. package/dist/cjs/utils/environment-detection.cjs +120 -0
  145. package/dist/cjs/utils/index.cjs +10 -0
  146. package/dist/cjs/utils/pool-defaults.cjs +106 -0
  147. package/dist/cjs/utils/type-coercion.cjs +118 -0
  148. package/dist/cjs/view/create-view-builder.cjs +180 -0
  149. package/dist/cjs/view/index.cjs +7 -0
  150. package/dist/cjs/window/index.cjs +5 -0
  151. package/dist/cjs/window/window-builder.cjs +80 -0
  152. package/dist/config.cjs +1 -0
  153. package/dist/config.d.ts +655 -0
  154. package/dist/config.js +1 -0
  155. package/dist/esm/cache/index.js +1 -0
  156. package/dist/esm/cache/query-cache.js +303 -0
  157. package/dist/esm/cli/commands/add.js +78 -0
  158. package/dist/esm/cli/commands/commit.js +109 -0
  159. package/dist/esm/cli/commands/diff.js +81 -0
  160. package/dist/esm/cli/commands/export.js +297 -0
  161. package/dist/esm/cli/commands/fetch.js +56 -0
  162. package/dist/esm/cli/commands/generate.js +206 -0
  163. package/dist/esm/cli/commands/history.js +129 -0
  164. package/dist/esm/cli/commands/import.js +488 -0
  165. package/dist/esm/cli/commands/init.js +401 -0
  166. package/dist/esm/cli/commands/introspect.js +106 -0
  167. package/dist/esm/cli/commands/log.js +59 -0
  168. package/dist/esm/cli/commands/migrate.js +131 -0
  169. package/dist/esm/cli/commands/pull.js +374 -0
  170. package/dist/esm/cli/commands/push.js +129 -0
  171. package/dist/esm/cli/commands/rollback.js +133 -0
  172. package/dist/esm/cli/commands/status.js +107 -0
  173. package/dist/esm/cli/commands/sync.js +76 -0
  174. package/dist/esm/cli/index.js +240 -0
  175. package/dist/esm/cli/utils/change-tracker.js +405 -0
  176. package/dist/esm/cli/utils/commit-manager.js +191 -0
  177. package/dist/esm/cli/utils/config-loader.js +86 -0
  178. package/dist/esm/cli/utils/env-loader.js +57 -0
  179. package/dist/esm/cli/utils/fast-introspect.js +362 -0
  180. package/dist/esm/cli/utils/git-utils.js +347 -0
  181. package/dist/esm/cli/utils/migration-generator.js +263 -0
  182. package/dist/esm/cli/utils/relqignore.js +74 -0
  183. package/dist/esm/cli/utils/repo-manager.js +444 -0
  184. package/dist/esm/cli/utils/schema-comparator.js +307 -0
  185. package/dist/esm/cli/utils/schema-diff.js +276 -0
  186. package/dist/esm/cli/utils/schema-hash.js +69 -0
  187. package/dist/esm/cli/utils/schema-introspect.js +418 -0
  188. package/dist/esm/cli/utils/snapshot-manager.js +179 -0
  189. package/dist/esm/cli/utils/spinner.js +101 -0
  190. package/dist/esm/cli/utils/sql-generator.js +504 -0
  191. package/dist/esm/cli/utils/sql-parser.js +992 -0
  192. package/dist/esm/cli/utils/type-generator.js +2058 -0
  193. package/dist/esm/condition/array-condition-builder.js +495 -0
  194. package/dist/esm/condition/array-numeric-condition-builder.js +182 -0
  195. package/dist/esm/condition/array-specialized-condition-builder.js +200 -0
  196. package/dist/esm/condition/array-string-condition-builder.js +142 -0
  197. package/dist/esm/condition/base-condition-builder.js +1 -0
  198. package/dist/esm/condition/condition-collector.js +275 -0
  199. package/dist/esm/condition/fulltext-condition-builder.js +53 -0
  200. package/dist/esm/condition/geometric-condition-builder.js +200 -0
  201. package/dist/esm/condition/index.js +7 -0
  202. package/dist/esm/condition/jsonb-condition-builder.js +152 -0
  203. package/dist/esm/condition/network-condition-builder.js +222 -0
  204. package/dist/esm/condition/range-condition-builder.js +74 -0
  205. package/dist/esm/config/config.js +150 -0
  206. package/dist/esm/config/index.js +1 -0
  207. package/dist/esm/constants/pg-values.js +63 -0
  208. package/dist/esm/copy/copy-builder.js +308 -0
  209. package/dist/esm/copy/index.js +1 -0
  210. package/dist/esm/core/query-builder.js +426 -0
  211. package/dist/esm/core/relq-client.js +1791 -0
  212. package/dist/esm/core/typed-kuery-client.js +1 -0
  213. package/dist/esm/count/count-builder.js +81 -0
  214. package/dist/esm/count/index.js +1 -0
  215. package/dist/esm/cte/cte-builder.js +82 -0
  216. package/dist/esm/cte/index.js +1 -0
  217. package/dist/esm/ddl/function.js +45 -0
  218. package/dist/esm/ddl/index.js +2 -0
  219. package/dist/esm/ddl/sql.js +51 -0
  220. package/dist/esm/delete/delete-builder.js +128 -0
  221. package/dist/esm/delete/index.js +1 -0
  222. package/dist/esm/errors/relq-errors.js +310 -0
  223. package/dist/esm/examples/fulltext-search-test.js +117 -0
  224. package/dist/esm/explain/explain-builder.js +95 -0
  225. package/dist/esm/explain/index.js +1 -0
  226. package/dist/esm/function/create-function-builder.js +188 -0
  227. package/dist/esm/function/index.js +1 -0
  228. package/dist/esm/functions/advanced-functions.js +231 -0
  229. package/dist/esm/functions/case-builder.js +58 -0
  230. package/dist/esm/functions/geometric-functions.js +97 -0
  231. package/dist/esm/functions/index.js +171 -0
  232. package/dist/esm/functions/network-functions.js +79 -0
  233. package/dist/esm/functions/sql-functions.js +421 -0
  234. package/dist/esm/index.js +34 -0
  235. package/dist/esm/indexing/create-index-builder.js +180 -0
  236. package/dist/esm/indexing/drop-index-builder.js +81 -0
  237. package/dist/esm/indexing/index-types.js +1 -0
  238. package/dist/esm/indexing/index.js +2 -0
  239. package/dist/esm/insert/conflict-builder.js +162 -0
  240. package/dist/esm/insert/index.js +1 -0
  241. package/dist/esm/insert/insert-builder.js +247 -0
  242. package/dist/esm/introspect/index.js +224 -0
  243. package/dist/esm/maintenance/index.js +1 -0
  244. package/dist/esm/maintenance/vacuum-builder.js +158 -0
  245. package/dist/esm/pubsub/index.js +1 -0
  246. package/dist/esm/pubsub/listen-notify-builder.js +48 -0
  247. package/dist/esm/pubsub/listener-connection.js +173 -0
  248. package/dist/esm/raw/index.js +1 -0
  249. package/dist/esm/raw/raw-query-builder.js +20 -0
  250. package/dist/esm/schema/index.js +1 -0
  251. package/dist/esm/schema/schema-builder.js +1150 -0
  252. package/dist/esm/schema-builder.js +2 -0
  253. package/dist/esm/schema-definition/column-types.js +738 -0
  254. package/dist/esm/schema-definition/index.js +10 -0
  255. package/dist/esm/schema-definition/introspection.js +614 -0
  256. package/dist/esm/schema-definition/partitions.js +123 -0
  257. package/dist/esm/schema-definition/pg-enum.js +70 -0
  258. package/dist/esm/schema-definition/pg-function.js +85 -0
  259. package/dist/esm/schema-definition/pg-sequence.js +50 -0
  260. package/dist/esm/schema-definition/pg-trigger.js +102 -0
  261. package/dist/esm/schema-definition/relations.js +90 -0
  262. package/dist/esm/schema-definition/sql-expressions.js +193 -0
  263. package/dist/esm/schema-definition/table-definition.js +630 -0
  264. package/dist/esm/select/aggregate-builder.js +172 -0
  265. package/dist/esm/select/index.js +1 -0
  266. package/dist/esm/select/select-builder.js +226 -0
  267. package/dist/esm/sequence/index.js +1 -0
  268. package/dist/esm/sequence/sequence-builder.js +255 -0
  269. package/dist/esm/table/alter-table-builder.js +138 -0
  270. package/dist/esm/table/constraint-builder.js +95 -0
  271. package/dist/esm/table/create-table-builder.js +241 -0
  272. package/dist/esm/table/index.js +5 -0
  273. package/dist/esm/table/partition-builder.js +121 -0
  274. package/dist/esm/table/truncate-builder.js +63 -0
  275. package/dist/esm/transaction/index.js +1 -0
  276. package/dist/esm/transaction/transaction-builder.js +70 -0
  277. package/dist/esm/trigger/create-trigger-builder.js +166 -0
  278. package/dist/esm/trigger/index.js +1 -0
  279. package/dist/esm/types/aggregate-types.js +1 -0
  280. package/dist/esm/types/config-types.js +36 -0
  281. package/dist/esm/types/inference-types.js +12 -0
  282. package/dist/esm/types/pagination-types.js +4 -0
  283. package/dist/esm/types/result-types.js +1 -0
  284. package/dist/esm/types/schema-types.js +1 -0
  285. package/dist/esm/types/subscription-types.js +1 -0
  286. package/dist/esm/types.js +1 -0
  287. package/dist/esm/update/array-update-builder.js +192 -0
  288. package/dist/esm/update/index.js +2 -0
  289. package/dist/esm/update/update-builder.js +188 -0
  290. package/dist/esm/utils/case-converter.js +55 -0
  291. package/dist/esm/utils/environment-detection.js +113 -0
  292. package/dist/esm/utils/index.js +2 -0
  293. package/dist/esm/utils/pool-defaults.js +100 -0
  294. package/dist/esm/utils/type-coercion.js +110 -0
  295. package/dist/esm/view/create-view-builder.js +171 -0
  296. package/dist/esm/view/index.js +1 -0
  297. package/dist/esm/window/index.js +1 -0
  298. package/dist/esm/window/window-builder.js +73 -0
  299. package/dist/index.cjs +1 -0
  300. package/dist/index.d.ts +10341 -0
  301. package/dist/index.js +1 -0
  302. package/dist/schema-builder.cjs +1 -0
  303. package/dist/schema-builder.d.ts +2272 -0
  304. package/dist/schema-builder.js +1 -0
  305. package/package.json +55 -0
@@ -0,0 +1,520 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.quoteIdentifier = quoteIdentifier;
4
+ exports.generateExtensionSQL = generateExtensionSQL;
5
+ exports.generateEnumSQL = generateEnumSQL;
6
+ exports.generateDomainSQL = generateDomainSQL;
7
+ exports.generateCompositeTypeSQL = generateCompositeTypeSQL;
8
+ exports.generateSequenceSQL = generateSequenceSQL;
9
+ exports.generateTableSQL = generateTableSQL;
10
+ exports.generatePartitionSQL = generatePartitionSQL;
11
+ exports.generateIndexSQL = generateIndexSQL;
12
+ exports.generateAlterTableConstraintSQL = generateAlterTableConstraintSQL;
13
+ exports.generateFunctionSQL = generateFunctionSQL;
14
+ exports.generateTriggerSQL = generateTriggerSQL;
15
+ exports.generateCommentSQL = generateCommentSQL;
16
+ exports.generateFullSchemaSQL = generateFullSchemaSQL;
17
+ const RESERVED_WORDS = new Set([
18
+ 'user', 'order', 'check', 'table', 'index', 'column', 'constraint',
19
+ 'primary', 'foreign', 'key', 'references', 'unique', 'default',
20
+ 'null', 'not', 'and', 'or', 'in', 'like', 'between', 'case',
21
+ 'when', 'then', 'else', 'end', 'select', 'from', 'where', 'group',
22
+ 'having', 'order', 'by', 'limit', 'offset', 'join', 'left', 'right',
23
+ 'inner', 'outer', 'cross', 'on', 'as', 'into', 'values', 'insert',
24
+ 'update', 'delete', 'create', 'alter', 'drop', 'grant', 'revoke',
25
+ 'all', 'any', 'some', 'exists', 'true', 'false', 'boolean', 'int',
26
+ 'integer', 'text', 'varchar', 'char', 'timestamp', 'date', 'time',
27
+ 'interval', 'numeric', 'decimal', 'real', 'float', 'double', 'serial',
28
+ 'bigserial', 'smallserial', 'uuid', 'json', 'jsonb', 'array', 'type',
29
+ 'enum', 'domain', 'function', 'trigger', 'procedure', 'view', 'sequence',
30
+ 'schema', 'database', 'role', 'grant', 'privileges', 'public', 'admin',
31
+ 'analyze', 'using', 'cast', 'collate', 'returning', 'with', 'recursive',
32
+ 'abort', 'access', 'action', 'add', 'after', 'aggregate', 'also',
33
+ 'always', 'analyse', 'assertion', 'assignment', 'asymmetric', 'at',
34
+ 'attribute', 'authorization', 'backward', 'before', 'begin', 'both',
35
+ 'cache', 'called', 'cascade', 'cascaded', 'catalog', 'chain',
36
+ 'characteristics', 'checkpoint', 'class', 'close', 'cluster',
37
+ 'comment', 'comments', 'commit', 'committed', 'concurrently',
38
+ 'configuration', 'connection', 'constraints', 'content', 'continue',
39
+ 'conversion', 'copy', 'cost', 'current', 'current_catalog',
40
+ 'current_date', 'current_role', 'current_schema', 'current_time',
41
+ 'current_timestamp', 'current_user', 'cursor', 'cycle', 'data',
42
+ 'day', 'deallocate', 'declare', 'defaults', 'deferrable', 'deferred',
43
+ 'definer', 'delimiter', 'delimiters', 'depends', 'desc', 'detach',
44
+ 'dictionary', 'disable', 'discard', 'distinct', 'document', 'each',
45
+ 'enable', 'encoding', 'encrypted', 'escape', 'event', 'except',
46
+ 'exclude', 'excluding', 'exclusive', 'execute', 'explain', 'expression',
47
+ 'extension', 'external', 'extract', 'fetch', 'filter', 'first',
48
+ 'following', 'force', 'forward', 'freeze', 'full', 'generated',
49
+ 'global', 'granted', 'greatest', 'handler', 'header', 'hold',
50
+ 'hour', 'identity', 'if', 'ilike', 'immediate', 'immutable', 'implicit',
51
+ 'import', 'include', 'including', 'increment', 'indexes', 'inherit',
52
+ 'inherits', 'initially', 'inline', 'input', 'insensitive', 'instead',
53
+ 'invoker', 'is', 'isnull', 'isolation', 'label', 'language', 'large',
54
+ 'last', 'lateral', 'lc_collate', 'lc_ctype', 'leading', 'leakproof',
55
+ 'least', 'level', 'listen', 'load', 'local', 'localtime',
56
+ 'localtimestamp', 'location', 'lock', 'locked', 'logged', 'mapping',
57
+ 'match', 'materialized', 'maxvalue', 'method', 'minute', 'minvalue',
58
+ 'mode', 'month', 'move', 'name', 'names', 'national', 'natural',
59
+ 'nchar', 'new', 'next', 'no', 'none', 'normalize', 'nothing',
60
+ 'notify', 'notnull', 'nowait', 'nullif', 'nulls', 'object', 'of',
61
+ 'off', 'oids', 'old', 'only', 'operator', 'option', 'options',
62
+ 'ordinality', 'others', 'out', 'over', 'overlaps', 'overlay',
63
+ 'overriding', 'owned', 'owner', 'parallel', 'parser', 'partial',
64
+ 'partition', 'passing', 'password', 'placing', 'plans', 'policy',
65
+ 'position', 'preceding', 'prepare', 'prepared', 'preserve', 'prior',
66
+ 'privileges', 'procedural', 'program', 'publication', 'quote',
67
+ 'range', 'read', 'reassign', 'recheck', 'refresh', 'reindex',
68
+ 'relative', 'release', 'rename', 'repeatable', 'replace', 'replica',
69
+ 'reset', 'restart', 'restrict', 'result', 'return', 'returns',
70
+ 'rollback', 'rollup', 'routine', 'row', 'rows', 'rule', 'savepoint',
71
+ 'scroll', 'search', 'second', 'security', 'serializable', 'server',
72
+ 'session', 'session_user', 'set', 'setof', 'sets', 'share', 'show',
73
+ 'similar', 'simple', 'skip', 'snapshot', 'sql', 'stable', 'standalone',
74
+ 'start', 'statement', 'statistics', 'stdin', 'stdout', 'storage',
75
+ 'stored', 'strict', 'strip', 'subscription', 'substring', 'support',
76
+ 'symmetric', 'sysid', 'system', 'tables', 'tablespace', 'temp',
77
+ 'template', 'temporary', 'ties', 'trailing', 'transaction',
78
+ 'transform', 'treat', 'trim', 'truncate', 'trusted', 'types',
79
+ 'uescape', 'unbounded', 'uncommitted', 'unencrypted', 'union',
80
+ 'unknown', 'unlisten', 'unlogged', 'until', 'vacuum', 'valid',
81
+ 'validate', 'validator', 'value', 'varying', 'verbose', 'version',
82
+ 'volatile', 'whitespace', 'window', 'within', 'without', 'work',
83
+ 'wrapper', 'write', 'xml', 'xmlattributes', 'xmlconcat', 'xmlelement',
84
+ 'xmlexists', 'xmlforest', 'xmlnamespaces', 'xmlparse', 'xmlpi',
85
+ 'xmlroot', 'xmlserialize', 'xmltable', 'year', 'yes', 'zone',
86
+ ]);
87
+ function quoteIdentifier(name) {
88
+ const lowerName = name.toLowerCase();
89
+ if (RESERVED_WORDS.has(lowerName)) {
90
+ return `"${name}"`;
91
+ }
92
+ if (name !== lowerName || /^[0-9]/.test(name)) {
93
+ return `"${name}"`;
94
+ }
95
+ if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
96
+ return `"${name}"`;
97
+ }
98
+ return name;
99
+ }
100
+ function quoteColumnRef(name) {
101
+ const lowerName = name.toLowerCase();
102
+ if (RESERVED_WORDS.has(lowerName)) {
103
+ return `"${name}"`;
104
+ }
105
+ if (!/^[a-z_][a-z0-9_]*$/.test(name)) {
106
+ return `"${name}"`;
107
+ }
108
+ return name;
109
+ }
110
+ function generateExtensionSQL(extensionName) {
111
+ const quotedName = extensionName.includes('-') ? `"${extensionName}"` : extensionName;
112
+ return `CREATE EXTENSION IF NOT EXISTS ${quotedName};`;
113
+ }
114
+ function generateEnumSQL(enumInfo) {
115
+ const quotedName = quoteIdentifier(enumInfo.name);
116
+ const quotedValues = enumInfo.values.map(v => `'${v.replace(/'/g, "''")}'`).join(', ');
117
+ return `CREATE TYPE ${quotedName} AS ENUM (${quotedValues});`;
118
+ }
119
+ function generateDomainSQL(domain) {
120
+ const parts = [];
121
+ parts.push(`CREATE DOMAIN ${quoteIdentifier(domain.name)} AS ${domain.baseType}`);
122
+ if (domain.defaultValue) {
123
+ parts.push(`DEFAULT ${domain.defaultValue}`);
124
+ }
125
+ if (domain.isNotNull) {
126
+ parts.push('NOT NULL');
127
+ }
128
+ if (domain.checkExpression) {
129
+ const check = domain.checkExpression.trim();
130
+ if (check.startsWith('(') && check.endsWith(')')) {
131
+ parts.push(`CHECK ${check}`);
132
+ }
133
+ else {
134
+ parts.push(`CHECK (${check})`);
135
+ }
136
+ }
137
+ return parts.join('\n ') + ';';
138
+ }
139
+ function generateCompositeTypeSQL(compositeType) {
140
+ const lines = [];
141
+ lines.push(`CREATE TYPE ${quoteIdentifier(compositeType.name)} AS (`);
142
+ const attrLines = compositeType.attributes.map((attr, idx) => {
143
+ const isLast = idx === compositeType.attributes.length - 1;
144
+ return ` ${quoteIdentifier(attr.name).padEnd(20)} ${attr.type}${isLast ? '' : ','}`;
145
+ });
146
+ lines.push(...attrLines);
147
+ lines.push(');');
148
+ return lines.join('\n');
149
+ }
150
+ function generateSequenceSQL(seq) {
151
+ const parts = [];
152
+ parts.push(`CREATE SEQUENCE ${quoteIdentifier(seq.name)}`);
153
+ if (seq.dataType && seq.dataType !== 'bigint') {
154
+ parts.push(`AS ${seq.dataType}`);
155
+ }
156
+ if (seq.start !== undefined && seq.start !== 1) {
157
+ parts.push(`START ${seq.start}`);
158
+ }
159
+ if (seq.increment !== undefined && seq.increment !== 1) {
160
+ parts.push(`INCREMENT ${seq.increment}`);
161
+ }
162
+ if (seq.minValue !== undefined && seq.minValue !== null) {
163
+ parts.push(`MINVALUE ${seq.minValue}`);
164
+ }
165
+ else if (seq.minValue === null) {
166
+ parts.push('NO MINVALUE');
167
+ }
168
+ if (seq.maxValue !== undefined && seq.maxValue !== null) {
169
+ parts.push(`MAXVALUE ${seq.maxValue}`);
170
+ }
171
+ else if (seq.maxValue === null) {
172
+ parts.push('NO MAXVALUE');
173
+ }
174
+ if (seq.cache !== undefined && seq.cache !== 1) {
175
+ parts.push(`CACHE ${seq.cache}`);
176
+ }
177
+ if (seq.cycle) {
178
+ parts.push('CYCLE');
179
+ }
180
+ if (seq.ownedBy) {
181
+ parts.push(`OWNED BY ${seq.ownedBy}`);
182
+ }
183
+ return parts.join(' ') + ';';
184
+ }
185
+ function generateTableSQL(table, options = {}) {
186
+ const { includeConstraints = true } = options;
187
+ const lines = [];
188
+ lines.push(`CREATE TABLE ${quoteIdentifier(table.name)} (`);
189
+ const columnLines = [];
190
+ for (const col of table.columns) {
191
+ columnLines.push(generateColumnSQL(col));
192
+ }
193
+ if (includeConstraints && table.constraints) {
194
+ for (const constraint of table.constraints) {
195
+ const constraintSQL = generateInlineConstraintSQL(constraint);
196
+ if (constraintSQL) {
197
+ columnLines.push(constraintSQL);
198
+ }
199
+ }
200
+ }
201
+ lines.push(columnLines.map((l, i) => {
202
+ const isLast = i === columnLines.length - 1;
203
+ return ` ${l}${isLast ? '' : ','}`;
204
+ }).join('\n'));
205
+ if (table.isPartitioned && table.partitionType && table.partitionKey) {
206
+ lines.push(`) PARTITION BY ${table.partitionType.toUpperCase()} (${table.partitionKey});`);
207
+ }
208
+ else {
209
+ lines.push(');');
210
+ }
211
+ return lines.join('\n');
212
+ }
213
+ function generateColumnSQL(col) {
214
+ const parts = [];
215
+ parts.push(`${quoteIdentifier(col.name).padEnd(28)}${col.dataType}`);
216
+ if (col.dataType.endsWith('[]')) {
217
+ }
218
+ if (col.isGenerated && col.generationExpression) {
219
+ parts.push(`GENERATED ALWAYS AS (${col.generationExpression}) STORED`);
220
+ return parts.join(' ');
221
+ }
222
+ if (col.identityGeneration) {
223
+ parts.push(`GENERATED ${col.identityGeneration} AS IDENTITY`);
224
+ }
225
+ if (!col.isNullable && !col.isPrimaryKey) {
226
+ parts.push('NOT NULL');
227
+ }
228
+ if (col.defaultValue !== null && col.defaultValue !== undefined && !col.isGenerated) {
229
+ parts.push(`DEFAULT ${col.defaultValue}`);
230
+ }
231
+ if (col.isUnique && !col.isPrimaryKey) {
232
+ parts.push('UNIQUE');
233
+ }
234
+ if (col.references) {
235
+ const ref = col.references;
236
+ let refStr = `REFERENCES ${quoteIdentifier(ref.table)}(${quoteIdentifier(ref.column)})`;
237
+ if (ref.onDelete && ref.onDelete !== 'NO ACTION') {
238
+ refStr += ` ON DELETE ${ref.onDelete}`;
239
+ }
240
+ if (ref.onUpdate && ref.onUpdate !== 'NO ACTION') {
241
+ refStr += ` ON UPDATE ${ref.onUpdate}`;
242
+ }
243
+ parts.push(refStr);
244
+ }
245
+ if (col.check) {
246
+ parts.push(`CHECK (${col.check})`);
247
+ }
248
+ return parts.join(' ');
249
+ }
250
+ function generateInlineConstraintSQL(constraint) {
251
+ if (constraint.definition) {
252
+ const def = constraint.definition.trim();
253
+ if (def.toUpperCase().startsWith('CONSTRAINT')) {
254
+ return def;
255
+ }
256
+ if (constraint.name) {
257
+ return `CONSTRAINT ${quoteIdentifier(constraint.name)} ${def}`;
258
+ }
259
+ return def;
260
+ }
261
+ const parts = [];
262
+ if (constraint.name) {
263
+ parts.push(`CONSTRAINT ${quoteIdentifier(constraint.name)}`);
264
+ }
265
+ switch (constraint.type) {
266
+ case 'PRIMARY KEY':
267
+ if (constraint.columns && constraint.columns.length > 0) {
268
+ parts.push(`PRIMARY KEY (${constraint.columns.map(c => quoteIdentifier(c)).join(', ')})`);
269
+ }
270
+ else {
271
+ return null;
272
+ }
273
+ break;
274
+ case 'UNIQUE':
275
+ if (constraint.columns && constraint.columns.length > 0) {
276
+ parts.push(`UNIQUE (${constraint.columns.map(c => quoteIdentifier(c)).join(', ')})`);
277
+ }
278
+ else {
279
+ return null;
280
+ }
281
+ break;
282
+ case 'FOREIGN KEY':
283
+ if (constraint.columns && constraint.referencedTable && constraint.referencedColumns) {
284
+ parts.push(`FOREIGN KEY (${constraint.columns.map(c => quoteIdentifier(c)).join(', ')})`);
285
+ parts.push(`REFERENCES ${quoteIdentifier(constraint.referencedTable)}(${constraint.referencedColumns.map(c => quoteIdentifier(c)).join(', ')})`);
286
+ }
287
+ else {
288
+ return null;
289
+ }
290
+ break;
291
+ case 'CHECK':
292
+ if (constraint.checkExpression) {
293
+ parts.push(`CHECK (${constraint.checkExpression})`);
294
+ }
295
+ else {
296
+ return null;
297
+ }
298
+ break;
299
+ case 'EXCLUDE':
300
+ return null;
301
+ default:
302
+ return null;
303
+ }
304
+ return parts.join(' ');
305
+ }
306
+ function generatePartitionSQL(partition) {
307
+ const parts = [];
308
+ parts.push(`CREATE TABLE ${quoteIdentifier(partition.name)} PARTITION OF ${quoteIdentifier(partition.parentTable)}`);
309
+ if (partition.partitionBound) {
310
+ const bound = partition.partitionBound.trim();
311
+ if (bound.toUpperCase().startsWith('FOR VALUES') || bound.toUpperCase() === 'DEFAULT') {
312
+ parts.push(bound);
313
+ }
314
+ else {
315
+ parts.push(`FOR VALUES ${bound}`);
316
+ }
317
+ }
318
+ return parts.join(' ') + ';';
319
+ }
320
+ function generateIndexSQL(index, tableName) {
321
+ if (index.definition) {
322
+ let def = index.definition.trim();
323
+ if (!def.endsWith(';')) {
324
+ def += ';';
325
+ }
326
+ return def;
327
+ }
328
+ const parts = [];
329
+ parts.push('CREATE');
330
+ if (index.isUnique) {
331
+ parts.push('UNIQUE');
332
+ }
333
+ parts.push('INDEX');
334
+ parts.push(quoteIdentifier(index.name));
335
+ parts.push('ON');
336
+ parts.push(quoteIdentifier(tableName));
337
+ if (index.type && index.type.toLowerCase() !== 'btree') {
338
+ parts.push(`USING ${index.type.toUpperCase()}`);
339
+ }
340
+ if (index.expression) {
341
+ parts.push(`(${index.expression})`);
342
+ }
343
+ else if (index.columns && index.columns.length > 0) {
344
+ const cols = index.columns.map(c => quoteColumnRef(c)).join(', ');
345
+ parts.push(`(${cols})`);
346
+ }
347
+ if (index.whereClause) {
348
+ parts.push(`WHERE ${index.whereClause}`);
349
+ }
350
+ return parts.join(' ') + ';';
351
+ }
352
+ function generateAlterTableConstraintSQL(tableName, constraint) {
353
+ const constraintDef = generateInlineConstraintSQL(constraint);
354
+ if (!constraintDef) {
355
+ return '';
356
+ }
357
+ return `ALTER TABLE ${quoteIdentifier(tableName)} ADD ${constraintDef};`;
358
+ }
359
+ function generateFunctionSQL(func) {
360
+ if (func.definition) {
361
+ let def = func.definition.trim();
362
+ if (!def.endsWith(';')) {
363
+ def += ';';
364
+ }
365
+ return def;
366
+ }
367
+ const parts = [];
368
+ parts.push(`CREATE OR REPLACE FUNCTION ${quoteIdentifier(func.name)}(`);
369
+ if (func.argTypes && func.argTypes.length > 0) {
370
+ parts.push(func.argTypes.join(', '));
371
+ }
372
+ parts.push(')');
373
+ if (func.returnType) {
374
+ parts.push(`RETURNS ${func.returnType}`);
375
+ }
376
+ if (func.language) {
377
+ parts.push(`LANGUAGE ${func.language}`);
378
+ }
379
+ if (func.volatility) {
380
+ parts.push(func.volatility.toUpperCase());
381
+ }
382
+ parts.push('AS $$ /* body not available */ $$;');
383
+ return parts.join('\n');
384
+ }
385
+ function generateTriggerSQL(trigger) {
386
+ if (trigger.definition) {
387
+ let def = trigger.definition.trim();
388
+ if (!def.endsWith(';')) {
389
+ def += ';';
390
+ }
391
+ return def;
392
+ }
393
+ const parts = [];
394
+ parts.push('CREATE TRIGGER');
395
+ parts.push(quoteIdentifier(trigger.name));
396
+ if (trigger.timing) {
397
+ parts.push(trigger.timing.toUpperCase());
398
+ }
399
+ if (trigger.event) {
400
+ parts.push(trigger.event.toUpperCase());
401
+ }
402
+ parts.push('ON');
403
+ parts.push(quoteIdentifier(trigger.tableName));
404
+ parts.push('FOR EACH ROW');
405
+ parts.push('EXECUTE FUNCTION');
406
+ parts.push(`${quoteIdentifier(trigger.functionName)}()`);
407
+ return parts.join(' ') + ';';
408
+ }
409
+ function generateCommentSQL(comment) {
410
+ const quotedComment = comment.comment.replace(/'/g, "''");
411
+ switch (comment.objectType.toUpperCase()) {
412
+ case 'TABLE':
413
+ return `COMMENT ON TABLE ${quoteIdentifier(comment.objectName)} IS '${quotedComment}';`;
414
+ case 'COLUMN':
415
+ return `COMMENT ON COLUMN ${quoteIdentifier(comment.objectName)}.${quoteIdentifier(comment.subObjectName || '')} IS '${quotedComment}';`;
416
+ case 'INDEX':
417
+ return `COMMENT ON INDEX ${quoteIdentifier(comment.objectName)} IS '${quotedComment}';`;
418
+ case 'FUNCTION':
419
+ case 'PROCEDURE':
420
+ return `COMMENT ON FUNCTION ${quoteIdentifier(comment.objectName)} IS '${quotedComment}';`;
421
+ case 'TRIGGER':
422
+ return `COMMENT ON TRIGGER ${quoteIdentifier(comment.objectName)} ON ${quoteIdentifier(comment.subObjectName || '')} IS '${quotedComment}';`;
423
+ case 'SEQUENCE':
424
+ return `COMMENT ON SEQUENCE ${quoteIdentifier(comment.objectName)} IS '${quotedComment}';`;
425
+ case 'TYPE':
426
+ return `COMMENT ON TYPE ${quoteIdentifier(comment.objectName)} IS '${quotedComment}';`;
427
+ case 'DOMAIN':
428
+ return `COMMENT ON DOMAIN ${quoteIdentifier(comment.objectName)} IS '${quotedComment}';`;
429
+ default:
430
+ return `COMMENT ON ${comment.objectType.toUpperCase()} ${quoteIdentifier(comment.objectName)} IS '${quotedComment}';`;
431
+ }
432
+ }
433
+ function generateFullSchemaSQL(schema, options = {}) {
434
+ const { includeExtensions = true, includeEnums = true, includeDomains = true, includeCompositeTypes = true, includeSequences = true, includeTables = true, includePartitions = true, includeIndexes = true, includeConstraints = true, includeFunctions = true, includeTriggers = true, headerComment, } = options;
435
+ const sections = [];
436
+ if (headerComment) {
437
+ sections.push(`-- ${headerComment}\n`);
438
+ }
439
+ if (includeExtensions && schema.extensions && schema.extensions.length > 0) {
440
+ sections.push('-- Extensions');
441
+ for (const ext of schema.extensions) {
442
+ sections.push(generateExtensionSQL(ext));
443
+ }
444
+ sections.push('');
445
+ }
446
+ if (includeEnums && schema.enums && schema.enums.length > 0) {
447
+ sections.push('-- Enums');
448
+ for (const enumInfo of schema.enums) {
449
+ sections.push(generateEnumSQL(enumInfo));
450
+ }
451
+ sections.push('');
452
+ }
453
+ if (includeDomains && schema.domains && schema.domains.length > 0) {
454
+ sections.push('-- Domains');
455
+ for (const domain of schema.domains) {
456
+ sections.push(generateDomainSQL(domain));
457
+ }
458
+ sections.push('');
459
+ }
460
+ if (includeCompositeTypes && schema.compositeTypes && schema.compositeTypes.length > 0) {
461
+ sections.push('-- Composite Types');
462
+ for (const type of schema.compositeTypes) {
463
+ sections.push(generateCompositeTypeSQL(type));
464
+ }
465
+ sections.push('');
466
+ }
467
+ if (includeSequences && schema.sequences && schema.sequences.length > 0) {
468
+ sections.push('-- Sequences');
469
+ for (const seq of schema.sequences) {
470
+ sections.push(generateSequenceSQL(seq));
471
+ }
472
+ sections.push('');
473
+ }
474
+ if (includeTables && schema.tables && schema.tables.length > 0) {
475
+ sections.push('-- Tables');
476
+ for (const table of schema.tables) {
477
+ sections.push(generateTableSQL(table, { includeConstraints }));
478
+ sections.push('');
479
+ }
480
+ }
481
+ if (includePartitions && schema.partitions && schema.partitions.length > 0) {
482
+ sections.push('-- Partitions');
483
+ for (const partition of schema.partitions) {
484
+ sections.push(generatePartitionSQL(partition));
485
+ }
486
+ sections.push('');
487
+ }
488
+ if (includeIndexes && schema.tables) {
489
+ const indexLines = [];
490
+ for (const table of schema.tables) {
491
+ if (table.indexes && table.indexes.length > 0) {
492
+ for (const index of table.indexes) {
493
+ if (!index.isPrimary) {
494
+ indexLines.push(generateIndexSQL(index, table.name));
495
+ }
496
+ }
497
+ }
498
+ }
499
+ if (indexLines.length > 0) {
500
+ sections.push('-- Indexes');
501
+ sections.push(indexLines.join('\n'));
502
+ sections.push('');
503
+ }
504
+ }
505
+ if (includeFunctions && schema.functions && schema.functions.length > 0) {
506
+ sections.push('-- Functions');
507
+ for (const func of schema.functions) {
508
+ sections.push(generateFunctionSQL(func));
509
+ sections.push('');
510
+ }
511
+ }
512
+ if (includeTriggers && schema.triggers && schema.triggers.length > 0) {
513
+ sections.push('-- Triggers');
514
+ for (const trigger of schema.triggers) {
515
+ sections.push(generateTriggerSQL(trigger));
516
+ }
517
+ sections.push('');
518
+ }
519
+ return sections.join('\n').trim() + '\n';
520
+ }