effect-qb 0.12.3

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 (81) hide show
  1. package/README.md +1294 -0
  2. package/dist/mysql.js +57575 -0
  3. package/dist/postgres.js +6303 -0
  4. package/package.json +42 -0
  5. package/src/internal/aggregation-validation.ts +57 -0
  6. package/src/internal/case-analysis.ts +50 -0
  7. package/src/internal/coercion-analysis.ts +30 -0
  8. package/src/internal/coercion-errors.ts +29 -0
  9. package/src/internal/coercion-kind.ts +32 -0
  10. package/src/internal/coercion-normalize.ts +7 -0
  11. package/src/internal/coercion-rules.ts +25 -0
  12. package/src/internal/column-state.ts +453 -0
  13. package/src/internal/column.ts +417 -0
  14. package/src/internal/datatypes/define.ts +44 -0
  15. package/src/internal/datatypes/lookup.ts +280 -0
  16. package/src/internal/datatypes/shape.ts +72 -0
  17. package/src/internal/derived-table.ts +149 -0
  18. package/src/internal/dialect.ts +30 -0
  19. package/src/internal/executor.ts +390 -0
  20. package/src/internal/expression-ast.ts +349 -0
  21. package/src/internal/expression.ts +325 -0
  22. package/src/internal/grouping-key.ts +82 -0
  23. package/src/internal/json/ast.ts +63 -0
  24. package/src/internal/json/errors.ts +13 -0
  25. package/src/internal/json/path.ts +227 -0
  26. package/src/internal/json/shape.ts +1 -0
  27. package/src/internal/json/types.ts +386 -0
  28. package/src/internal/mysql-dialect.ts +39 -0
  29. package/src/internal/mysql-renderer.ts +37 -0
  30. package/src/internal/plan.ts +64 -0
  31. package/src/internal/postgres-dialect.ts +34 -0
  32. package/src/internal/postgres-renderer.ts +40 -0
  33. package/src/internal/predicate-analysis.ts +71 -0
  34. package/src/internal/predicate-atom.ts +43 -0
  35. package/src/internal/predicate-branches.ts +40 -0
  36. package/src/internal/predicate-context.ts +279 -0
  37. package/src/internal/predicate-formula.ts +100 -0
  38. package/src/internal/predicate-key.ts +28 -0
  39. package/src/internal/predicate-nnf.ts +12 -0
  40. package/src/internal/predicate-normalize.ts +202 -0
  41. package/src/internal/projection-alias.ts +15 -0
  42. package/src/internal/projections.ts +101 -0
  43. package/src/internal/query-ast.ts +297 -0
  44. package/src/internal/query-factory.ts +6757 -0
  45. package/src/internal/query-requirements.ts +40 -0
  46. package/src/internal/query.ts +1590 -0
  47. package/src/internal/renderer.ts +102 -0
  48. package/src/internal/runtime-normalize.ts +344 -0
  49. package/src/internal/runtime-schema.ts +428 -0
  50. package/src/internal/runtime-value.ts +85 -0
  51. package/src/internal/schema-derivation.ts +131 -0
  52. package/src/internal/sql-expression-renderer.ts +1353 -0
  53. package/src/internal/table-options.ts +225 -0
  54. package/src/internal/table.ts +674 -0
  55. package/src/mysql/column.ts +30 -0
  56. package/src/mysql/datatypes/index.ts +6 -0
  57. package/src/mysql/datatypes/spec.ts +180 -0
  58. package/src/mysql/errors/catalog.ts +51662 -0
  59. package/src/mysql/errors/fields.ts +21 -0
  60. package/src/mysql/errors/index.ts +18 -0
  61. package/src/mysql/errors/normalize.ts +232 -0
  62. package/src/mysql/errors/requirements.ts +73 -0
  63. package/src/mysql/executor.ts +134 -0
  64. package/src/mysql/query.ts +189 -0
  65. package/src/mysql/renderer.ts +19 -0
  66. package/src/mysql/table.ts +157 -0
  67. package/src/mysql.ts +18 -0
  68. package/src/postgres/column.ts +20 -0
  69. package/src/postgres/datatypes/index.ts +8 -0
  70. package/src/postgres/datatypes/spec.ts +264 -0
  71. package/src/postgres/errors/catalog.ts +452 -0
  72. package/src/postgres/errors/fields.ts +48 -0
  73. package/src/postgres/errors/index.ts +4 -0
  74. package/src/postgres/errors/normalize.ts +209 -0
  75. package/src/postgres/errors/requirements.ts +65 -0
  76. package/src/postgres/errors/types.ts +38 -0
  77. package/src/postgres/executor.ts +131 -0
  78. package/src/postgres/query.ts +189 -0
  79. package/src/postgres/renderer.ts +29 -0
  80. package/src/postgres/table.ts +157 -0
  81. package/src/postgres.ts +18 -0
@@ -0,0 +1,452 @@
1
+ /**
2
+ * Official PostgreSQL SQLSTATE catalog from the current Appendix A docs.
3
+ * Source: https://www.postgresql.org/docs/current/errcodes-appendix.html
4
+ */
5
+
6
+ import type { PostgresErrorSemanticField } from "./fields.js"
7
+
8
+ export const postgresErrorClasses = {
9
+ "00": 'Successful Completion',
10
+ "01": 'Warning',
11
+ "02": 'No Data (this is also a warning class per the SQL standard)',
12
+ "03": 'SQL Statement Not Yet Complete',
13
+ "08": 'Connection Exception',
14
+ "09": 'Triggered Action Exception',
15
+ "0A": 'Feature Not Supported',
16
+ "0B": 'Invalid Transaction Initiation',
17
+ "0F": 'Locator Exception',
18
+ "0L": 'Invalid Grantor',
19
+ "0P": 'Invalid Role Specification',
20
+ "0Z": 'Diagnostics Exception',
21
+ "10": 'XQuery Error',
22
+ "20": 'Case Not Found',
23
+ "21": 'Cardinality Violation',
24
+ "22": 'Data Exception',
25
+ "23": 'Integrity Constraint Violation',
26
+ "24": 'Invalid Cursor State',
27
+ "25": 'Invalid Transaction State',
28
+ "26": 'Invalid SQL Statement Name',
29
+ "27": 'Triggered Data Change Violation',
30
+ "28": 'Invalid Authorization Specification',
31
+ "2B": 'Dependent Privilege Descriptors Still Exist',
32
+ "2D": 'Invalid Transaction Termination',
33
+ "2F": 'SQL Routine Exception',
34
+ "34": 'Invalid Cursor Name',
35
+ "38": 'External Routine Exception',
36
+ "39": 'External Routine Invocation Exception',
37
+ "3B": 'Savepoint Exception',
38
+ "3D": 'Invalid Catalog Name',
39
+ "3F": 'Invalid Schema Name',
40
+ "40": 'Transaction Rollback',
41
+ "42": 'Syntax Error or Access Rule Violation',
42
+ "44": 'WITH CHECK OPTION Violation',
43
+ "53": 'Insufficient Resources',
44
+ "54": 'Program Limit Exceeded',
45
+ "55": 'Object Not In Prerequisite State',
46
+ "57": 'Operator Intervention',
47
+ "58": 'System Error (errors external to PostgreSQL itself)',
48
+ "F0": 'Configuration File Error',
49
+ "HV": 'Foreign Data Wrapper Error (SQL/MED)',
50
+ "P0": 'PL/pgSQL Error',
51
+ "XX": 'Internal Error',
52
+ } as const
53
+
54
+ export type PostgresErrorClassCode = keyof typeof postgresErrorClasses
55
+ export type PostgresErrorClassName = (typeof postgresErrorClasses)[PostgresErrorClassCode]
56
+
57
+ export const postgresErrorTypes = {
58
+ "00": "successful-completion",
59
+ "01": "warning",
60
+ "02": "no-data",
61
+ "03": "sql-statement-not-yet-complete",
62
+ "08": "connection-exception",
63
+ "09": "triggered-action-exception",
64
+ "0A": "feature-not-supported",
65
+ "0B": "invalid-transaction-initiation",
66
+ "0F": "locator-exception",
67
+ "0L": "invalid-grantor",
68
+ "0P": "invalid-role-specification",
69
+ "0Z": "diagnostics-exception",
70
+ "10": "xquery-error",
71
+ "20": "case-not-found",
72
+ "21": "cardinality-violation",
73
+ "22": "data-exception",
74
+ "23": "integrity-constraint-violation",
75
+ "24": "invalid-cursor-state",
76
+ "25": "invalid-transaction-state",
77
+ "26": "invalid-sql-statement-name",
78
+ "27": "triggered-data-change-violation",
79
+ "28": "invalid-authorization-specification",
80
+ "2B": "dependent-privilege-descriptors-still-exist",
81
+ "2D": "invalid-transaction-termination",
82
+ "2F": "sql-routine-exception",
83
+ "34": "invalid-cursor-name",
84
+ "38": "external-routine-exception",
85
+ "39": "external-routine-invocation-exception",
86
+ "3B": "savepoint-exception",
87
+ "3D": "invalid-catalog-name",
88
+ "3F": "invalid-schema-name",
89
+ "40": "transaction-rollback",
90
+ "42": "syntax-error-or-access-rule-violation",
91
+ "44": "with-check-option-violation",
92
+ "53": "insufficient-resources",
93
+ "54": "program-limit-exceeded",
94
+ "55": "object-not-in-prerequisite-state",
95
+ "57": "operator-intervention",
96
+ "58": "system-error",
97
+ "F0": "configuration-file-error",
98
+ "HV": "foreign-data-wrapper-error",
99
+ "P0": "pl-pgsql-error",
100
+ "XX": "internal-error"
101
+ } as const
102
+
103
+ export type PostgresErrorType = (typeof postgresErrorTypes)[PostgresErrorClassCode]
104
+
105
+ export const postgresErrorConditions = {
106
+ "00000": "successful_completion",
107
+ "01000": "warning",
108
+ "0100C": "dynamic_result_sets_returned",
109
+ "01008": "implicit_zero_bit_padding",
110
+ "01003": "null_value_eliminated_in_set_function",
111
+ "01007": "privilege_not_granted",
112
+ "01006": "privilege_not_revoked",
113
+ "01004": "string_data_right_truncation",
114
+ "01P01": "deprecated_feature",
115
+ "02000": "no_data",
116
+ "02001": "no_additional_dynamic_result_sets_returned",
117
+ "03000": "sql_statement_not_yet_complete",
118
+ "08000": "connection_exception",
119
+ "08003": "connection_does_not_exist",
120
+ "08006": "connection_failure",
121
+ "08001": "sqlclient_unable_to_establish_sqlconnection",
122
+ "08004": "sqlserver_rejected_establishment_of_sqlconnection",
123
+ "08007": "transaction_resolution_unknown",
124
+ "08P01": "protocol_violation",
125
+ "09000": "triggered_action_exception",
126
+ "0A000": "feature_not_supported",
127
+ "0B000": "invalid_transaction_initiation",
128
+ "0F000": "locator_exception",
129
+ "0F001": "invalid_locator_specification",
130
+ "0L000": "invalid_grantor",
131
+ "0LP01": "invalid_grant_operation",
132
+ "0P000": "invalid_role_specification",
133
+ "0Z000": "diagnostics_exception",
134
+ "0Z002": "stacked_diagnostics_accessed_without_active_handler",
135
+ "10608": "invalid_argument_for_xquery",
136
+ "20000": "case_not_found",
137
+ "21000": "cardinality_violation",
138
+ "22000": "data_exception",
139
+ "2202E": "array_subscript_error",
140
+ "22021": "character_not_in_repertoire",
141
+ "22008": "datetime_field_overflow",
142
+ "22012": "division_by_zero",
143
+ "22005": "error_in_assignment",
144
+ "2200B": "escape_character_conflict",
145
+ "22022": "indicator_overflow",
146
+ "22015": "interval_field_overflow",
147
+ "2201E": "invalid_argument_for_logarithm",
148
+ "22014": "invalid_argument_for_ntile_function",
149
+ "22016": "invalid_argument_for_nth_value_function",
150
+ "2201F": "invalid_argument_for_power_function",
151
+ "2201G": "invalid_argument_for_width_bucket_function",
152
+ "22018": "invalid_character_value_for_cast",
153
+ "22007": "invalid_datetime_format",
154
+ "22019": "invalid_escape_character",
155
+ "2200D": "invalid_escape_octet",
156
+ "22025": "invalid_escape_sequence",
157
+ "22P06": "nonstandard_use_of_escape_character",
158
+ "22010": "invalid_indicator_parameter_value",
159
+ "22023": "invalid_parameter_value",
160
+ "22013": "invalid_preceding_or_following_size",
161
+ "2201B": "invalid_regular_expression",
162
+ "2201W": "invalid_row_count_in_limit_clause",
163
+ "2201X": "invalid_row_count_in_result_offset_clause",
164
+ "2202H": "invalid_tablesample_argument",
165
+ "2202G": "invalid_tablesample_repeat",
166
+ "22009": "invalid_time_zone_displacement_value",
167
+ "2200C": "invalid_use_of_escape_character",
168
+ "2200G": "most_specific_type_mismatch",
169
+ "22004": "null_value_not_allowed",
170
+ "22002": "null_value_no_indicator_parameter",
171
+ "22003": "numeric_value_out_of_range",
172
+ "2200H": "sequence_generator_limit_exceeded",
173
+ "22026": "string_data_length_mismatch",
174
+ "22001": "string_data_right_truncation",
175
+ "22011": "substring_error",
176
+ "22027": "trim_error",
177
+ "22024": "unterminated_c_string",
178
+ "2200F": "zero_length_character_string",
179
+ "22P01": "floating_point_exception",
180
+ "22P02": "invalid_text_representation",
181
+ "22P03": "invalid_binary_representation",
182
+ "22P04": "bad_copy_file_format",
183
+ "22P05": "untranslatable_character",
184
+ "2200L": "not_an_xml_document",
185
+ "2200M": "invalid_xml_document",
186
+ "2200N": "invalid_xml_content",
187
+ "2200S": "invalid_xml_comment",
188
+ "2200T": "invalid_xml_processing_instruction",
189
+ "22030": "duplicate_json_object_key_value",
190
+ "22031": "invalid_argument_for_sql_json_datetime_function",
191
+ "22032": "invalid_json_text",
192
+ "22033": "invalid_sql_json_subscript",
193
+ "22034": "more_than_one_sql_json_item",
194
+ "22035": "no_sql_json_item",
195
+ "22036": "non_numeric_sql_json_item",
196
+ "22037": "non_unique_keys_in_a_json_object",
197
+ "22038": "singleton_sql_json_item_required",
198
+ "22039": "sql_json_array_not_found",
199
+ "2203A": "sql_json_member_not_found",
200
+ "2203B": "sql_json_number_not_found",
201
+ "2203C": "sql_json_object_not_found",
202
+ "2203D": "too_many_json_array_elements",
203
+ "2203E": "too_many_json_object_members",
204
+ "2203F": "sql_json_scalar_required",
205
+ "2203G": "sql_json_item_cannot_be_cast_to_target_type",
206
+ "23000": "integrity_constraint_violation",
207
+ "23001": "restrict_violation",
208
+ "23502": "not_null_violation",
209
+ "23503": "foreign_key_violation",
210
+ "23505": "unique_violation",
211
+ "23514": "check_violation",
212
+ "23P01": "exclusion_violation",
213
+ "24000": "invalid_cursor_state",
214
+ "25000": "invalid_transaction_state",
215
+ "25001": "active_sql_transaction",
216
+ "25002": "branch_transaction_already_active",
217
+ "25008": "held_cursor_requires_same_isolation_level",
218
+ "25003": "inappropriate_access_mode_for_branch_transaction",
219
+ "25004": "inappropriate_isolation_level_for_branch_transaction",
220
+ "25005": "no_active_sql_transaction_for_branch_transaction",
221
+ "25006": "read_only_sql_transaction",
222
+ "25007": "schema_and_data_statement_mixing_not_supported",
223
+ "25P01": "no_active_sql_transaction",
224
+ "25P02": "in_failed_sql_transaction",
225
+ "25P03": "idle_in_transaction_session_timeout",
226
+ "25P04": "transaction_timeout",
227
+ "26000": "invalid_sql_statement_name",
228
+ "27000": "triggered_data_change_violation",
229
+ "28000": "invalid_authorization_specification",
230
+ "28P01": "invalid_password",
231
+ "2B000": "dependent_privilege_descriptors_still_exist",
232
+ "2BP01": "dependent_objects_still_exist",
233
+ "2D000": "invalid_transaction_termination",
234
+ "2F000": "sql_routine_exception",
235
+ "2F005": "function_executed_no_return_statement",
236
+ "2F002": "modifying_sql_data_not_permitted",
237
+ "2F003": "prohibited_sql_statement_attempted",
238
+ "2F004": "reading_sql_data_not_permitted",
239
+ "34000": "invalid_cursor_name",
240
+ "38000": "external_routine_exception",
241
+ "38001": "containing_sql_not_permitted",
242
+ "38002": "modifying_sql_data_not_permitted",
243
+ "38003": "prohibited_sql_statement_attempted",
244
+ "38004": "reading_sql_data_not_permitted",
245
+ "39000": "external_routine_invocation_exception",
246
+ "39001": "invalid_sqlstate_returned",
247
+ "39004": "null_value_not_allowed",
248
+ "39P01": "trigger_protocol_violated",
249
+ "39P02": "srf_protocol_violated",
250
+ "39P03": "event_trigger_protocol_violated",
251
+ "3B000": "savepoint_exception",
252
+ "3B001": "invalid_savepoint_specification",
253
+ "3D000": "invalid_catalog_name",
254
+ "3F000": "invalid_schema_name",
255
+ "40000": "transaction_rollback",
256
+ "40002": "transaction_integrity_constraint_violation",
257
+ "40001": "serialization_failure",
258
+ "40003": "statement_completion_unknown",
259
+ "40P01": "deadlock_detected",
260
+ "42000": "syntax_error_or_access_rule_violation",
261
+ "42601": "syntax_error",
262
+ "42501": "insufficient_privilege",
263
+ "42846": "cannot_coerce",
264
+ "42803": "grouping_error",
265
+ "42P20": "windowing_error",
266
+ "42P19": "invalid_recursion",
267
+ "42830": "invalid_foreign_key",
268
+ "42602": "invalid_name",
269
+ "42622": "name_too_long",
270
+ "42939": "reserved_name",
271
+ "42804": "datatype_mismatch",
272
+ "42P18": "indeterminate_datatype",
273
+ "42P21": "collation_mismatch",
274
+ "42P22": "indeterminate_collation",
275
+ "42809": "wrong_object_type",
276
+ "428C9": "generated_always",
277
+ "42703": "undefined_column",
278
+ "42883": "undefined_function",
279
+ "42P01": "undefined_table",
280
+ "42P02": "undefined_parameter",
281
+ "42704": "undefined_object",
282
+ "42701": "duplicate_column",
283
+ "42P03": "duplicate_cursor",
284
+ "42P04": "duplicate_database",
285
+ "42723": "duplicate_function",
286
+ "42P05": "duplicate_prepared_statement",
287
+ "42P06": "duplicate_schema",
288
+ "42P07": "duplicate_table",
289
+ "42712": "duplicate_alias",
290
+ "42710": "duplicate_object",
291
+ "42702": "ambiguous_column",
292
+ "42725": "ambiguous_function",
293
+ "42P08": "ambiguous_parameter",
294
+ "42P09": "ambiguous_alias",
295
+ "42P10": "invalid_column_reference",
296
+ "42611": "invalid_column_definition",
297
+ "42P11": "invalid_cursor_definition",
298
+ "42P12": "invalid_database_definition",
299
+ "42P13": "invalid_function_definition",
300
+ "42P14": "invalid_prepared_statement_definition",
301
+ "42P15": "invalid_schema_definition",
302
+ "42P16": "invalid_table_definition",
303
+ "42P17": "invalid_object_definition",
304
+ "44000": "with_check_option_violation",
305
+ "53000": "insufficient_resources",
306
+ "53100": "disk_full",
307
+ "53200": "out_of_memory",
308
+ "53300": "too_many_connections",
309
+ "53400": "configuration_limit_exceeded",
310
+ "54000": "program_limit_exceeded",
311
+ "54001": "statement_too_complex",
312
+ "54011": "too_many_columns",
313
+ "54023": "too_many_arguments",
314
+ "55000": "object_not_in_prerequisite_state",
315
+ "55006": "object_in_use",
316
+ "55P02": "cant_change_runtime_param",
317
+ "55P03": "lock_not_available",
318
+ "55P04": "unsafe_new_enum_value_usage",
319
+ "57000": "operator_intervention",
320
+ "57014": "query_canceled",
321
+ "57P01": "admin_shutdown",
322
+ "57P02": "crash_shutdown",
323
+ "57P03": "cannot_connect_now",
324
+ "57P04": "database_dropped",
325
+ "57P05": "idle_session_timeout",
326
+ "58000": "system_error",
327
+ "58030": "io_error",
328
+ "58P01": "undefined_file",
329
+ "58P02": "duplicate_file",
330
+ "58P03": "file_name_too_long",
331
+ "F0000": "config_file_error",
332
+ "F0001": "lock_file_exists",
333
+ "HV000": "fdw_error",
334
+ "HV005": "fdw_column_name_not_found",
335
+ "HV002": "fdw_dynamic_parameter_value_needed",
336
+ "HV010": "fdw_function_sequence_error",
337
+ "HV021": "fdw_inconsistent_descriptor_information",
338
+ "HV024": "fdw_invalid_attribute_value",
339
+ "HV007": "fdw_invalid_column_name",
340
+ "HV008": "fdw_invalid_column_number",
341
+ "HV004": "fdw_invalid_data_type",
342
+ "HV006": "fdw_invalid_data_type_descriptors",
343
+ "HV091": "fdw_invalid_descriptor_field_identifier",
344
+ "HV00B": "fdw_invalid_handle",
345
+ "HV00C": "fdw_invalid_option_index",
346
+ "HV00D": "fdw_invalid_option_name",
347
+ "HV090": "fdw_invalid_string_length_or_buffer_length",
348
+ "HV00A": "fdw_invalid_string_format",
349
+ "HV009": "fdw_invalid_use_of_null_pointer",
350
+ "HV014": "fdw_too_many_handles",
351
+ "HV001": "fdw_out_of_memory",
352
+ "HV00P": "fdw_no_schemas",
353
+ "HV00J": "fdw_option_name_not_found",
354
+ "HV00K": "fdw_reply_handle",
355
+ "HV00Q": "fdw_schema_not_found",
356
+ "HV00R": "fdw_table_not_found",
357
+ "HV00L": "fdw_unable_to_create_execution",
358
+ "HV00M": "fdw_unable_to_create_reply",
359
+ "HV00N": "fdw_unable_to_establish_connection",
360
+ "P0000": "plpgsql_error",
361
+ "P0001": "raise_exception",
362
+ "P0002": "no_data_found",
363
+ "P0003": "too_many_rows",
364
+ "P0004": "assert_failure",
365
+ "XX000": "internal_error",
366
+ "XX001": "data_corrupted",
367
+ "XX002": "index_corrupted",
368
+ } as const
369
+
370
+ export type PostgresSqlStateCode = keyof typeof postgresErrorConditions
371
+ export type PostgresConditionName = (typeof postgresErrorConditions)[PostgresSqlStateCode]
372
+
373
+ type KebabCase<S extends string> = S extends `${infer Head}_${infer Tail}`
374
+ ? `${Lowercase<Head>}-${KebabCase<Tail>}`
375
+ : Lowercase<S>
376
+
377
+ type PostgresErrorClassForCode<Code extends PostgresSqlStateCode> = {
378
+ readonly [ClassCode in PostgresErrorClassCode]:
379
+ Code extends `${ClassCode}${string}` ? ClassCode : never
380
+ }[PostgresErrorClassCode]
381
+
382
+ export type PostgresErrorTag<Code extends PostgresSqlStateCode> =
383
+ `@postgres/${(typeof postgresErrorTypes)[PostgresErrorClassForCode<Code>]}/${KebabCase<(typeof postgresErrorConditions)[Code]>}`
384
+
385
+ const semanticFieldOverrides: Partial<Record<PostgresSqlStateCode, readonly PostgresErrorSemanticField[]>> = {
386
+ "23502": ["schemaName", "tableName", "columnName", "detail"],
387
+ "23503": ["schemaName", "tableName", "constraintName", "detail"],
388
+ "23505": ["schemaName", "tableName", "constraintName", "detail"],
389
+ "23514": ["schemaName", "tableName", "constraintName", "detail"],
390
+ "23P01": ["schemaName", "tableName", "constraintName", "detail"],
391
+ "3D000": ["schemaName", "detail"],
392
+ "3F000": ["schemaName", "detail"],
393
+ "42P01": ["schemaName", "tableName", "position"],
394
+ "42703": ["schemaName", "tableName", "columnName", "position"],
395
+ "42704": ["detail", "position"],
396
+ "42883": ["dataTypeName", "position", "hint"],
397
+ "42P02": ["position", "detail"],
398
+ "42601": ["position", "detail", "hint"],
399
+ }
400
+
401
+ const inferSemanticFields = (code: PostgresSqlStateCode, condition: PostgresConditionName): readonly PostgresErrorSemanticField[] => {
402
+ const override = semanticFieldOverrides[code]
403
+ if (override) {
404
+ return override
405
+ }
406
+ const fields = new Set<PostgresErrorSemanticField>()
407
+ if (condition.includes("schema")) fields.add("schemaName")
408
+ if (condition.includes("table") || condition.includes("relation")) fields.add("tableName")
409
+ if (condition.includes("column")) fields.add("columnName")
410
+ if (condition.includes("datatype") || condition.includes("data_type") || condition.includes("cast")) fields.add("dataTypeName")
411
+ if (condition.includes("constraint") || condition.includes("foreign_key") || condition.includes("unique") || condition.includes("check_") || condition.includes("exclusion")) fields.add("constraintName")
412
+ if (condition.includes("syntax") || condition.includes("parse") || condition.includes("parameter") || condition.includes("function") || condition.includes("operator") || condition.includes("statement")) fields.add("position")
413
+ if (condition.includes("privilege") || condition.includes("permission") || condition.includes("authorization")) fields.add("hint")
414
+ if (code.startsWith("08") || code.startsWith("53") || code.startsWith("57") || code.startsWith("58") || code.startsWith("XX")) {
415
+ fields.add("detail")
416
+ }
417
+ return [...fields]
418
+ }
419
+
420
+ const toTag = <Code extends PostgresSqlStateCode>(
421
+ code: Code,
422
+ condition: (typeof postgresErrorConditions)[Code]
423
+ ): PostgresErrorTag<Code> =>
424
+ `@postgres/${postgresErrorTypes[code.slice(0, 2) as PostgresErrorClassCode]}/${condition.replaceAll("_", "-")}` as PostgresErrorTag<Code>
425
+
426
+ export type PostgresErrorDescriptor<Code extends PostgresSqlStateCode = PostgresSqlStateCode> = {
427
+ readonly code: Code
428
+ readonly condition: (typeof postgresErrorConditions)[Code]
429
+ readonly classCode: PostgresErrorClassForCode<Code>
430
+ readonly className: (typeof postgresErrorClasses)[PostgresErrorClassForCode<Code>]
431
+ readonly tag: PostgresErrorTag<Code>
432
+ readonly primaryFields: readonly PostgresErrorSemanticField[]
433
+ }
434
+
435
+ export const postgresErrorCatalog = Object.freeze(Object.fromEntries(
436
+ Object.entries(postgresErrorConditions).map(([code, condition]) => {
437
+ const classCode = code.slice(0, 2) as PostgresErrorClassCode
438
+ return [code, {
439
+ code,
440
+ condition,
441
+ classCode,
442
+ className: postgresErrorClasses[classCode],
443
+ tag: toTag(code as PostgresSqlStateCode, condition as PostgresConditionName),
444
+ primaryFields: inferSemanticFields(code as PostgresSqlStateCode, condition as PostgresConditionName)
445
+ }]
446
+ })
447
+ )) as unknown as { readonly [Code in PostgresSqlStateCode]: PostgresErrorDescriptor<Code> }
448
+
449
+ export const isPostgresSqlStateCode = (value: string): value is PostgresSqlStateCode => value in postgresErrorCatalog
450
+
451
+ export const getPostgresErrorDescriptor = <Code extends PostgresSqlStateCode>(code: Code): PostgresErrorDescriptor<Code> =>
452
+ postgresErrorCatalog[code]
@@ -0,0 +1,48 @@
1
+ /** Normalized semantic properties extracted from Postgres driver errors. */
2
+ export const postgresErrorSemanticFields = [
3
+ "severity",
4
+ "severityNonLocalized",
5
+ "detail",
6
+ "hint",
7
+ "position",
8
+ "internalPosition",
9
+ "internalQuery",
10
+ "where",
11
+ "schemaName",
12
+ "tableName",
13
+ "columnName",
14
+ "dataTypeName",
15
+ "constraintName",
16
+ "file",
17
+ "line",
18
+ "routine"
19
+ ] as const
20
+
21
+ /** Semantic field names that may be relevant for a specific SQLSTATE. */
22
+ export type PostgresErrorSemanticField = typeof postgresErrorSemanticFields[number]
23
+
24
+ /** Normalized wire/protocol fields commonly exposed by Postgres drivers. */
25
+ export interface PostgresErrorFields {
26
+ readonly severity?: string
27
+ readonly severityNonLocalized?: string
28
+ readonly detail?: string
29
+ readonly hint?: string
30
+ readonly position?: number
31
+ readonly internalPosition?: number
32
+ readonly internalQuery?: string
33
+ readonly where?: string
34
+ readonly schemaName?: string
35
+ readonly tableName?: string
36
+ readonly columnName?: string
37
+ readonly dataTypeName?: string
38
+ readonly constraintName?: string
39
+ readonly file?: string
40
+ readonly line?: number
41
+ readonly routine?: string
42
+ }
43
+
44
+ /** Render context attached when an error occurred during query execution. */
45
+ export interface PostgresQueryContext {
46
+ readonly sql: string
47
+ readonly params: readonly unknown[]
48
+ }
@@ -0,0 +1,4 @@
1
+ export * from "./catalog.js"
2
+ export * from "./fields.js"
3
+ export * from "./normalize.js"
4
+ export * from "./requirements.js"