qubu 0.6.0 → 0.6.1

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 (65) hide show
  1. package/README.md +112 -0
  2. package/dist/codegen.d.mts +1 -1
  3. package/dist/codegen.mjs +1 -1
  4. package/dist/{column-r1Y4ivwt.mjs → column-BzN8KFJa.mjs} +39 -2
  5. package/dist/{column-Da37jYSD.mjs → column-CFvSbil0.mjs} +1 -1
  6. package/dist/{complete-types-CY0KbzNw.d.mts → complete-types-CNMWBWap.d.mts} +1 -1
  7. package/dist/{constraints-YGyNPQ_z.mjs → constraints-DM_tarXc.mjs} +2 -2
  8. package/dist/core.d.mts +1 -1
  9. package/dist/core.mjs +2 -3
  10. package/dist/diagnostics-I9vVtXkc.mjs +40 -0
  11. package/dist/diff.d.mts +2 -2
  12. package/dist/expressions-BCjc08zw.mjs +129 -0
  13. package/dist/{index-B2rZf3-2.d.mts → index-CGui70hi.d.mts} +2 -2
  14. package/dist/index.d.mts +2 -2
  15. package/dist/index.mjs +58 -15
  16. package/dist/introspection/mysql.d.mts +26 -0
  17. package/dist/introspection/mysql.mjs +1145 -0
  18. package/dist/introspection/postgres.d.mts +40 -0
  19. package/dist/introspection/postgres.mjs +1554 -0
  20. package/dist/introspection/sqlite.d.mts +15 -0
  21. package/dist/introspection/sqlite.mjs +986 -0
  22. package/dist/introspection.d.mts +3 -78
  23. package/dist/introspection.mjs +5 -3683
  24. package/dist/mysql.d.mts +1 -1
  25. package/dist/mysql.mjs +2 -2
  26. package/dist/{on-conflict-DZQ85f1t.mjs → on-conflict-CnaY5qso.mjs} +76 -4
  27. package/dist/postgres-Dey7QXPL.mjs +69 -0
  28. package/dist/postgres.d.mts +3 -3
  29. package/dist/postgres.mjs +3 -52
  30. package/dist/registry-oWDiqD7i.mjs +127 -0
  31. package/dist/{relational-CxnLCqZQ.mjs → relational-DSAJ-l58.mjs} +1 -2
  32. package/dist/schema.d.mts +1 -1
  33. package/dist/schema.mjs +7 -6
  34. package/dist/{serialize-BN07IK0v.mjs → serialize-CE-gw5_s.mjs} +3 -3
  35. package/dist/{serialize-CEIIlWhC.d.mts → serialize-OvXCLzjm.d.mts} +1 -1
  36. package/dist/snapshot/mysql.d.mts +2 -2
  37. package/dist/snapshot/mysql.mjs +28 -28
  38. package/dist/snapshot/postgres.d.mts +4 -4
  39. package/dist/snapshot/postgres.mjs +21 -21
  40. package/dist/snapshot/sqlite.d.mts +4 -4
  41. package/dist/snapshot/sqlite.mjs +27 -27
  42. package/dist/{snapshot-Xam8-q0j.mjs → snapshot-DgsOhf_8.mjs} +4 -42
  43. package/dist/snapshot.d.mts +4 -4
  44. package/dist/snapshot.mjs +1 -1
  45. package/dist/{source-SqrKWjFJ.mjs → source-BDuUXmAk.mjs} +2 -2
  46. package/dist/sqlite.d.mts +1 -1
  47. package/dist/sqlite.mjs +6 -6
  48. package/dist/{table-BwflqeAj.mjs → table-C1QGNe4P.mjs} +3 -3
  49. package/dist/{types-CTCqtFlS.d.mts → types-BEn0N_al.d.mts} +1 -1
  50. package/dist/{types-BIJsj2fJ.mjs → types-BLNRatG_.mjs} +2 -4
  51. package/dist/{types-C0VkiwpR.d.mts → types-DUe6eeI0.d.mts} +57 -28
  52. package/docs/guides/compose-queries.md +22 -0
  53. package/docs/guides/drizzle.md +11 -11
  54. package/docs/guides/mutations.md +89 -0
  55. package/docs/guides/valtio-sync.md +113 -0
  56. package/docs/migrations/index.md +50 -12
  57. package/docs/migrations/operations.md +20 -6
  58. package/docs/reference/supported-surface.md +17 -6
  59. package/docs/schema/code-generation.md +3 -2
  60. package/docs/schema/introspection.md +3 -2
  61. package/docs/sql-semantic-types.md +8 -0
  62. package/package.json +13 -1
  63. package/dist/registry-BRcUuazJ.mjs +0 -256
  64. package/dist/standard-DfcZEVOj.mjs +0 -12
  65. package/dist/value-D14I_XgL.mjs +0 -29
@@ -0,0 +1,1145 @@
1
+ import { n as createIntrospectionDiagnostic } from "../diagnostics-I9vVtXkc.mjs";
2
+ //#region src/introspection/mysql.ts
3
+ const mysqlServerQuery = `SELECT VERSION() AS version, @@version_comment AS version_comment`;
4
+ const mysqlTablesQuery = `
5
+ SELECT TABLE_NAME AS table_name, TABLE_TYPE AS table_type,
6
+ ENGINE AS engine, TABLE_COLLATION AS table_collation,
7
+ CREATE_OPTIONS AS create_options, TABLE_COMMENT AS table_comment
8
+ FROM INFORMATION_SCHEMA.TABLES
9
+ WHERE TABLE_SCHEMA = ?
10
+ ORDER BY TABLE_NAME
11
+ `;
12
+ const mysqlColumnsQuery = `
13
+ SELECT TABLE_NAME AS table_name, COLUMN_NAME AS column_name,
14
+ ORDINAL_POSITION AS ordinal_position, COLUMN_TYPE AS column_type,
15
+ DATA_TYPE AS data_type,
16
+ IS_NULLABLE AS is_nullable, COLUMN_DEFAULT AS column_default,
17
+ EXTRA AS extra, GENERATION_EXPRESSION AS generation_expression,
18
+ CHARACTER_SET_NAME AS character_set_name,
19
+ COLLATION_NAME AS collation_name, COLUMN_COMMENT AS column_comment
20
+ FROM INFORMATION_SCHEMA.COLUMNS
21
+ WHERE TABLE_SCHEMA = ?
22
+ ORDER BY TABLE_NAME, ORDINAL_POSITION
23
+ `;
24
+ const mysqlKeyUsageQuery = `
25
+ SELECT kcu.TABLE_NAME AS table_name, kcu.CONSTRAINT_NAME AS constraint_name,
26
+ tc.CONSTRAINT_TYPE AS constraint_type, tc.ENFORCED AS enforced,
27
+ kcu.COLUMN_NAME AS column_name, kcu.ORDINAL_POSITION AS ordinal_position,
28
+ kcu.REFERENCED_TABLE_NAME AS referenced_table_name,
29
+ kcu.REFERENCED_COLUMN_NAME AS referenced_column_name,
30
+ rc.UPDATE_RULE AS update_rule, rc.DELETE_RULE AS delete_rule,
31
+ rc.MATCH_OPTION AS match_option
32
+ FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
33
+ JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
34
+ ON tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
35
+ AND tc.TABLE_NAME = kcu.TABLE_NAME
36
+ AND tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
37
+ LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
38
+ ON rc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
39
+ AND rc.TABLE_NAME = kcu.TABLE_NAME
40
+ AND rc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
41
+ WHERE kcu.CONSTRAINT_SCHEMA = ?
42
+ ORDER BY kcu.TABLE_NAME, kcu.CONSTRAINT_NAME, kcu.ORDINAL_POSITION
43
+ `;
44
+ const mysqlChecksQuery = `
45
+ SELECT tc.TABLE_NAME AS table_name, tc.CONSTRAINT_NAME AS constraint_name,
46
+ tc.ENFORCED AS enforced, cc.CHECK_CLAUSE AS check_clause
47
+ FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
48
+ JOIN INFORMATION_SCHEMA.CHECK_CONSTRAINTS cc
49
+ ON cc.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA
50
+ AND cc.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
51
+ WHERE tc.CONSTRAINT_SCHEMA = ? AND tc.CONSTRAINT_TYPE = 'CHECK'
52
+ ORDER BY tc.TABLE_NAME, tc.CONSTRAINT_NAME
53
+ `;
54
+ const mysqlStatisticsQuery = `
55
+ SELECT TABLE_NAME AS table_name, INDEX_NAME AS index_name,
56
+ NON_UNIQUE AS non_unique, SEQ_IN_INDEX AS seq_in_index,
57
+ COLUMN_NAME AS column_name, COLLATION AS collation,
58
+ INDEX_TYPE AS index_type, EXPRESSION AS expression,
59
+ SUB_PART AS sub_part, IS_VISIBLE AS is_visible,
60
+ COMMENT AS comment, INDEX_COMMENT AS index_comment
61
+ FROM INFORMATION_SCHEMA.STATISTICS
62
+ WHERE TABLE_SCHEMA = ?
63
+ ORDER BY TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX
64
+ `;
65
+ /** View definitions and execution properties for the selected database. */
66
+ const mysqlViewsQuery = `
67
+ SELECT TABLE_NAME AS table_name, VIEW_DEFINITION AS view_definition,
68
+ CHECK_OPTION AS check_option, IS_UPDATABLE AS is_updatable,
69
+ SECURITY_TYPE AS security_type, DEFINER AS definer
70
+ FROM INFORMATION_SCHEMA.VIEWS
71
+ WHERE TABLE_SCHEMA = ?
72
+ ORDER BY TABLE_NAME
73
+ `;
74
+ /** Stored function and procedure declarations for the selected database. */
75
+ const mysqlRoutinesQuery = `
76
+ SELECT ROUTINE_NAME AS routine_name, ROUTINE_TYPE AS routine_type,
77
+ DATA_TYPE AS data_type, DTD_IDENTIFIER AS dtd_identifier,
78
+ ROUTINE_BODY AS routine_body,
79
+ ROUTINE_DEFINITION AS routine_definition,
80
+ EXTERNAL_LANGUAGE AS external_language,
81
+ SQL_DATA_ACCESS AS sql_data_access,
82
+ IS_DETERMINISTIC AS is_deterministic,
83
+ SECURITY_TYPE AS security_type, SQL_MODE AS sql_mode,
84
+ ROUTINE_COMMENT AS routine_comment
85
+ FROM INFORMATION_SCHEMA.ROUTINES
86
+ WHERE ROUTINE_SCHEMA = ?
87
+ ORDER BY ROUTINE_NAME, ROUTINE_TYPE
88
+ `;
89
+ /** Parameter rows, including the ordinal zero return row for functions. */
90
+ const mysqlRoutineParametersQuery = `
91
+ SELECT SPECIFIC_NAME AS routine_name, ORDINAL_POSITION AS ordinal_position,
92
+ PARAMETER_MODE AS parameter_mode, PARAMETER_NAME AS parameter_name,
93
+ DATA_TYPE AS data_type, DTD_IDENTIFIER AS dtd_identifier
94
+ FROM INFORMATION_SCHEMA.PARAMETERS
95
+ WHERE SPECIFIC_SCHEMA = ?
96
+ ORDER BY SPECIFIC_NAME, ORDINAL_POSITION
97
+ `;
98
+ /** Trigger bodies and their table/event metadata. */
99
+ const mysqlTriggersQuery = `
100
+ SELECT TRIGGER_NAME AS trigger_name, EVENT_MANIPULATION AS event_manipulation,
101
+ EVENT_OBJECT_TABLE AS table_name, ACTION_CONDITION AS action_condition,
102
+ ACTION_STATEMENT AS action_statement,
103
+ ACTION_ORIENTATION AS action_orientation,
104
+ ACTION_TIMING AS action_timing, ACTION_ORDER AS action_order,
105
+ DEFINER AS definer, SQL_MODE AS sql_mode
106
+ FROM INFORMATION_SCHEMA.TRIGGERS
107
+ WHERE TRIGGER_SCHEMA = ?
108
+ ORDER BY TRIGGER_NAME, ACTION_ORDER
109
+ `;
110
+ /** Partition and subpartition declarations for tables in the selected database. */
111
+ const mysqlPartitionsQuery = `
112
+ SELECT TABLE_NAME AS table_name, PARTITION_NAME AS partition_name,
113
+ SUBPARTITION_NAME AS subpartition_name,
114
+ PARTITION_ORDINAL_POSITION AS partition_ordinal_position,
115
+ SUBPARTITION_ORDINAL_POSITION AS subpartition_ordinal_position,
116
+ PARTITION_METHOD AS partition_method,
117
+ SUBPARTITION_METHOD AS subpartition_method,
118
+ PARTITION_EXPRESSION AS partition_expression,
119
+ SUBPARTITION_EXPRESSION AS subpartition_expression,
120
+ PARTITION_DESCRIPTION AS partition_description,
121
+ PARTITION_COMMENT AS partition_comment,
122
+ TABLESPACE_NAME AS tablespace_name
123
+ FROM INFORMATION_SCHEMA.PARTITIONS
124
+ WHERE TABLE_SCHEMA = ? AND PARTITION_NAME IS NOT NULL
125
+ ORDER BY TABLE_NAME, PARTITION_ORDINAL_POSITION, SUBPARTITION_ORDINAL_POSITION
126
+ `;
127
+ /** Collations used by tables or columns in the selected database. */
128
+ const mysqlCollationsQuery = `
129
+ SELECT c.COLLATION_NAME AS collation_name,
130
+ c.CHARACTER_SET_NAME AS character_set_name,
131
+ c.ID AS collation_id, c.IS_DEFAULT AS is_default,
132
+ c.IS_COMPILED AS is_compiled, c.SORTLEN AS sort_length,
133
+ c.PAD_ATTRIBUTE AS pad_attribute
134
+ FROM INFORMATION_SCHEMA.COLLATIONS c
135
+ JOIN (
136
+ SELECT DISTINCT TABLE_COLLATION AS collation_name
137
+ FROM INFORMATION_SCHEMA.TABLES
138
+ WHERE TABLE_SCHEMA = ? AND TABLE_COLLATION IS NOT NULL
139
+ UNION
140
+ SELECT DISTINCT COLLATION_NAME AS collation_name
141
+ FROM INFORMATION_SCHEMA.COLUMNS
142
+ WHERE TABLE_SCHEMA = ? AND COLLATION_NAME IS NOT NULL
143
+ ) used ON used.collation_name = c.COLLATION_NAME
144
+ ORDER BY c.COLLATION_NAME
145
+ `;
146
+ /** Scheduled events are retained as opaque MySQL objects, not migration input. */
147
+ const mysqlEventsQuery = `
148
+ SELECT EVENT_NAME AS event_name, EVENT_TYPE AS event_type,
149
+ STATUS AS status, EVENT_DEFINITION AS event_definition,
150
+ EVENT_BODY AS event_body, EXECUTE_AT AS execute_at,
151
+ INTERVAL_VALUE AS interval_value, INTERVAL_FIELD AS interval_field,
152
+ EVENT_COMMENT AS event_comment, DEFINER AS definer
153
+ FROM INFORMATION_SCHEMA.EVENTS
154
+ WHERE EVENT_SCHEMA = ?
155
+ ORDER BY EVENT_NAME
156
+ `;
157
+ /** Read one MySQL database into the normalized catalog contract. */
158
+ async function readCatalog(connection, options) {
159
+ const diagnostics = [];
160
+ if (connection.dialect !== "mysql") {
161
+ diagnostics.push(createIntrospectionDiagnostic({
162
+ severity: "error",
163
+ code: "dialect-mismatch",
164
+ message: "MySQL catalog reading requires a MySQL CatalogConnection",
165
+ path: ["connection", "dialect"]
166
+ }));
167
+ return emptyCatalog(options.namespace, diagnostics);
168
+ }
169
+ const server = serverInfo((await query(connection, mysqlServerQuery, [], options, diagnostics, "server"))[0], diagnostics);
170
+ const tableRows = await query(connection, mysqlTablesQuery, [options.namespace], options, diagnostics, "tables");
171
+ const tables = tableRows.filter((row) => text(row.table_type) === "BASE TABLE").map((row) => table(row, options.namespace));
172
+ const deferredObjects = [];
173
+ const opaqueObjects = [];
174
+ const views = [];
175
+ const comments = [];
176
+ const tableByName = new Map(tables.map((item) => [item.physicalName, item]));
177
+ const viewRows = await query(connection, mysqlViewsQuery, [options.namespace], options, diagnostics, "views");
178
+ const viewRowsByName = new Map(viewRows.map((row) => [text(row.table_name) ?? "", row]));
179
+ for (const row of tableRows.filter((currentRow) => text(currentRow.table_type) !== "BASE TABLE")) {
180
+ const kind = text(row.table_type)?.toUpperCase();
181
+ if (kind === "VIEW" || kind === "SYSTEM VIEW") {
182
+ const mapped = viewObject(row, viewRowsByName.get(text(row.table_name) ?? ""), options.namespace, diagnostics);
183
+ if (mapped.kind === "deferred-object") deferredObjects.push(mapped);
184
+ else views.push(mapped);
185
+ } else {
186
+ const mapped = deferred(row, options.namespace);
187
+ deferredObjects.push(mapped);
188
+ diagnostics.push(createIntrospectionDiagnostic({
189
+ severity: "warning",
190
+ code: "unmodeled-object",
191
+ message: `MySQL object ${mapped.physicalName} (${kind ?? "unknown"}) is retained as a deferred record`,
192
+ path: ["deferredObjects", mapped.physicalName],
193
+ physicalReference: mapped.reference,
194
+ remediation: "Inspect the deferred record before using it as migration input."
195
+ }));
196
+ }
197
+ }
198
+ const relationByName = /* @__PURE__ */ new Map();
199
+ for (const currentTable of tables) relationByName.set(currentTable.physicalName, {
200
+ kind: "table",
201
+ id: currentTable.id
202
+ });
203
+ for (const view of views) relationByName.set(view.physicalName, {
204
+ kind: "view",
205
+ id: view.id
206
+ });
207
+ const columnRows = await query(connection, mysqlColumnsQuery, [options.namespace], options, diagnostics, "columns");
208
+ for (const currentTable of tables) currentTable.columns = columnRows.filter((row) => text(row.table_name) === currentTable.physicalName).map((row) => column(row, currentTable, options.namespace));
209
+ for (const view of views) view.columns = columnRows.filter((row) => text(row.table_name) === view.physicalName).map((row) => column(row, view, options.namespace));
210
+ const keyRows = await query(connection, mysqlKeyUsageQuery, [options.namespace], options, diagnostics, "constraints");
211
+ for (const currentTable of tables) currentTable.constraints = constraints(currentTable, keyRows.filter((row) => text(row.table_name) === currentTable.physicalName), tableByName, options.namespace, diagnostics);
212
+ const checkRows = await query(connection, mysqlChecksQuery, [options.namespace], options, diagnostics, "checks");
213
+ const constraintsByTable = /* @__PURE__ */ new Map();
214
+ for (const currentTable of tables) constraintsByTable.set(currentTable.physicalName, [...currentTable.constraints]);
215
+ for (const row of checkRows) {
216
+ const currentTable = tableByName.get(text(row.table_name) ?? "");
217
+ if (!currentTable) continue;
218
+ const physicalName = text(row.constraint_name) ?? `check_${currentTable.physicalName}`;
219
+ const check = {
220
+ kind: "check",
221
+ id: stableId(physicalName),
222
+ identitySource: "physical-name",
223
+ physicalName,
224
+ expression: sql(text(row.check_clause) ?? "true", options.namespace, currentTable, physicalName),
225
+ dialect: text(row.enforced)?.toUpperCase() === "NO" ? {
226
+ dialect: "mysql",
227
+ version: 1,
228
+ data: { enforced: false }
229
+ } : void 0
230
+ };
231
+ constraintsByTable.get(currentTable.physicalName)?.push(check);
232
+ }
233
+ for (const currentTable of tables) currentTable.constraints = constraintsByTable.get(currentTable.physicalName) ?? [];
234
+ const statRows = await query(connection, mysqlStatisticsQuery, [options.namespace], options, diagnostics, "statistics");
235
+ const grouped = /* @__PURE__ */ new Map();
236
+ for (const row of statRows) {
237
+ const key = `${text(row.table_name) ?? ""}\u0000${text(row.index_name) ?? ""}`;
238
+ const group = grouped.get(key) ?? [];
239
+ group.push(row);
240
+ grouped.set(key, group);
241
+ }
242
+ for (const rows of grouped.values()) {
243
+ const currentTable = tableByName.get(text(rows[0].table_name) ?? "");
244
+ if (!currentTable) continue;
245
+ const mappedIndex = mapIndex(rows, currentTable, options.namespace, diagnostics);
246
+ if (mappedIndex) currentTable.indexes = [...currentTable.indexes, mappedIndex];
247
+ }
248
+ const routineRows = await query(connection, mysqlRoutinesQuery, [options.namespace], options, diagnostics, "routines");
249
+ const routineParameterRows = await query(connection, mysqlRoutineParametersQuery, [options.namespace], options, diagnostics, "routine-parameters");
250
+ const routineParameters = /* @__PURE__ */ new Map();
251
+ for (const row of routineParameterRows) {
252
+ const name = text(row.routine_name) ?? "unknown";
253
+ const group = routineParameters.get(name) ?? [];
254
+ group.push(row);
255
+ routineParameters.set(name, group);
256
+ }
257
+ const routines = routineRows.map((row) => {
258
+ const routine = routineObject(row, routineParameters.get(text(row.routine_name) ?? "") ?? [], options.namespace);
259
+ if (routine.comment) comments.push(routine.comment);
260
+ relationByName.set(routine.physicalName, {
261
+ kind: "routine",
262
+ id: routine.id
263
+ });
264
+ return routine;
265
+ });
266
+ const triggerRows = await query(connection, mysqlTriggersQuery, [options.namespace], options, diagnostics, "triggers");
267
+ const triggers = [];
268
+ for (const rows of groupRows(triggerRows, (row) => text(row.trigger_name) ?? "unknown")) {
269
+ const mapped = triggerObject(rows, relationByName, options.namespace, diagnostics);
270
+ if (mapped.kind === "trigger") triggers.push(mapped);
271
+ else deferredObjects.push(mapped);
272
+ }
273
+ const partitionRows = await query(connection, mysqlPartitionsQuery, [options.namespace], options, diagnostics, "partitions");
274
+ const partitions = [];
275
+ for (const row of partitionRows) {
276
+ const mapped = partitionObject(row, tableByName, options.namespace, diagnostics);
277
+ if (mapped.kind === "partition") {
278
+ partitions.push(mapped);
279
+ if (mapped.comment) comments.push(mapped.comment);
280
+ } else deferredObjects.push(mapped);
281
+ }
282
+ const collationRows = await query(connection, mysqlCollationsQuery, [options.namespace, options.namespace], options, diagnostics, "collations");
283
+ const usedCollations = new Set([...tableRows.map((row) => text(row.table_collation)), ...columnRows.map((row) => text(row.collation_name))].filter((value) => value !== void 0));
284
+ const collations = collationRows.filter((row) => {
285
+ const name = text(row.collation_name);
286
+ return name !== void 0 && usedCollations.has(name);
287
+ }).map((row) => collationObject(row, options.namespace));
288
+ const eventRows = await query(connection, mysqlEventsQuery, [options.namespace], options, diagnostics, "events");
289
+ for (const row of eventRows) {
290
+ const event = opaqueEvent(row, options.namespace);
291
+ opaqueObjects.push(event);
292
+ diagnostics.push(createIntrospectionDiagnostic({
293
+ severity: "warning",
294
+ code: "unmodeled-object",
295
+ message: `MySQL event ${event.physicalName} is retained as opaque data`,
296
+ path: ["opaqueObjects", event.physicalName],
297
+ physicalReference: event.reference,
298
+ remediation: "Inspect the event definition before treating it as migration input."
299
+ }));
300
+ const eventComment = text(row.event_comment);
301
+ if (eventComment !== void 0 && eventComment !== "") comments.push(objectComment({
302
+ kind: "opaque-object",
303
+ id: event.id
304
+ }, eventComment, event.reference, options.namespace));
305
+ }
306
+ for (const currentTable of tables) {
307
+ const tableComment = text(tableRows.find((row) => text(row.table_name) === currentTable.physicalName)?.table_comment);
308
+ if (tableComment !== void 0 && tableComment !== "") comments.push(objectComment({
309
+ kind: "table",
310
+ id: currentTable.id
311
+ }, tableComment, currentTable.reference, options.namespace));
312
+ for (const currentColumn of currentTable.columns) {
313
+ const columnComment = text(columnRows.find((row) => text(row.table_name) === currentTable.physicalName && text(row.column_name) === currentColumn.physicalName)?.column_comment);
314
+ if (columnComment !== void 0 && columnComment !== "") comments.push(objectComment({
315
+ kind: "column",
316
+ id: currentColumn.id
317
+ }, columnComment, currentColumn.reference, options.namespace));
318
+ }
319
+ }
320
+ for (const view of views) {
321
+ const viewComment = text(tableRows.find((row) => text(row.table_name) === view.physicalName)?.table_comment);
322
+ if (viewComment !== void 0 && viewComment !== "") comments.push(objectComment({
323
+ kind: view.kind,
324
+ id: view.id
325
+ }, viewComment, view.reference, options.namespace));
326
+ for (const currentColumn of view.columns) {
327
+ const columnComment = text(columnRows.find((row) => text(row.table_name) === view.physicalName && text(row.column_name) === currentColumn.physicalName)?.column_comment);
328
+ if (columnComment !== void 0 && columnComment !== "") comments.push(objectComment({
329
+ kind: "column",
330
+ id: currentColumn.id
331
+ }, columnComment, currentColumn.reference, options.namespace));
332
+ }
333
+ }
334
+ const capabilities = {
335
+ generatedColumns: server.capabilities.generatedColumns,
336
+ identityMetadata: server.capabilities.identityMetadata,
337
+ checkConstraints: server.capabilities.checkConstraints,
338
+ checkConstraintEnforcement: server.capabilities.checkConstraintEnforcement,
339
+ expressionDecompilation: server.capabilities.expressionDecompilation,
340
+ indexExpressions: server.capabilities.indexExpressions,
341
+ indexPredicates: server.capabilities.indexPredicates,
342
+ indexIncludedColumns: server.capabilities.indexIncludedColumns,
343
+ namespaces: server.capabilities.namespaces,
344
+ visibility: server.capabilities.visibility
345
+ };
346
+ const serverCapabilities = {
347
+ ...server.capabilities,
348
+ views: true,
349
+ materializedViews: false,
350
+ sequences: false,
351
+ enums: false,
352
+ domains: false,
353
+ collations: true,
354
+ routines: true,
355
+ triggers: true,
356
+ partitions: true,
357
+ policies: false,
358
+ extensions: false,
359
+ comments: true,
360
+ ownership: false,
361
+ scheduledEvents: true,
362
+ tableEngines: true,
363
+ generatedColumnModes: true,
364
+ selectedNamespace: options.namespace,
365
+ productFamily: server.product === "mariadb" ? "mariadb" : "mysql8"
366
+ };
367
+ const namespace = {
368
+ kind: "mysql-database",
369
+ name: options.namespace,
370
+ reference: reference("namespace", options.namespace, options.namespace, "INFORMATION_SCHEMA.SCHEMATA", "SCHEMA_NAME"),
371
+ dialect: extension({
372
+ selectedNamespace: options.namespace,
373
+ views: true,
374
+ materializedViews: false,
375
+ sequences: false,
376
+ routines: true,
377
+ triggers: true,
378
+ partitions: true,
379
+ collations: true,
380
+ comments: true,
381
+ ownership: false,
382
+ policies: false,
383
+ extensions: false
384
+ })
385
+ };
386
+ return Object.freeze({
387
+ dialect: "mysql",
388
+ server: {
389
+ ...server,
390
+ capabilities: serverCapabilities
391
+ },
392
+ namespace,
393
+ tables: Object.freeze(tables),
394
+ views: Object.freeze(views),
395
+ collations: Object.freeze(collations),
396
+ routines: Object.freeze(routines),
397
+ triggers: Object.freeze(triggers),
398
+ partitions: Object.freeze(partitions),
399
+ deferredObjects: Object.freeze(deferredObjects),
400
+ opaqueObjects: Object.freeze(opaqueObjects),
401
+ comments: Object.freeze(comments),
402
+ ownership: Object.freeze([]),
403
+ capabilities,
404
+ diagnostics: Object.freeze(diagnostics)
405
+ });
406
+ }
407
+ async function query(connection, textValue, parameters, options, diagnostics, operation) {
408
+ try {
409
+ return await connection.query({
410
+ text: textValue,
411
+ parameters
412
+ }, { signal: options.signal });
413
+ } catch {
414
+ diagnostics.push(createIntrospectionDiagnostic({
415
+ severity: "error",
416
+ code: "query-failed",
417
+ message: `MySQL catalog query failed while reading ${operation}`,
418
+ path: [operation],
419
+ remediation: "Check Information Schema permissions and the selected database."
420
+ }));
421
+ return [];
422
+ }
423
+ }
424
+ function serverInfo(row, diagnostics) {
425
+ const rawVersion = text(row?.version) ?? "unknown";
426
+ const comment = text(row?.version_comment) ?? "";
427
+ const mariadb = /mariadb/i.test(rawVersion) || /mariadb/i.test(comment);
428
+ const parts = rawVersion.match(/(\d+)\.(\d+)(?:\.(\d+))?/);
429
+ const major = parts ? Number(parts[1]) : void 0;
430
+ const minor = parts ? Number(parts[2]) : void 0;
431
+ if (mariadb) diagnostics.push(createIntrospectionDiagnostic({
432
+ severity: "error",
433
+ code: "unsupported-product",
434
+ message: "MariaDB requires a dedicated catalog adapter and is not treated as MySQL",
435
+ path: ["server", "product"]
436
+ }));
437
+ const supported = !mariadb && major === 8 && ((minor ?? 0) > 0 || (minor ?? 0) === 0 && Number(parts?.[3] ?? 0) >= 16);
438
+ if (!supported && !mariadb) diagnostics.push(createIntrospectionDiagnostic({
439
+ severity: "error",
440
+ code: "unsupported-server",
441
+ message: "MySQL introspection requires MySQL 8.0 or newer",
442
+ path: ["server", "version"]
443
+ }));
444
+ return {
445
+ product: mariadb ? "mariadb" : "mysql",
446
+ rawVersion,
447
+ parsedVersion: major === void 0 ? void 0 : {
448
+ major,
449
+ minor,
450
+ patch: parts?.[3] ? Number(parts[3]) : void 0
451
+ },
452
+ capabilities: {
453
+ generatedColumns: supported,
454
+ identityMetadata: supported,
455
+ checkConstraints: supported,
456
+ checkConstraintEnforcement: supported ? "enforced" : "unknown",
457
+ expressionDecompilation: false,
458
+ indexExpressions: supported,
459
+ indexPredicates: false,
460
+ indexIncludedColumns: false,
461
+ namespaces: true,
462
+ visibility: "complete",
463
+ mysql8: supported
464
+ }
465
+ };
466
+ }
467
+ function table(row, namespace) {
468
+ const physicalName = text(row.table_name) ?? "unnamed_table";
469
+ return {
470
+ kind: "table",
471
+ id: stableId(physicalName),
472
+ identitySource: "physical-name",
473
+ physicalName,
474
+ reference: reference("table", physicalName, namespace, "INFORMATION_SCHEMA.TABLES", "TABLE_NAME"),
475
+ columns: [],
476
+ constraints: [],
477
+ indexes: [],
478
+ dialect: extension({
479
+ ...text(row.engine) === void 0 ? {} : { engine: text(row.engine) },
480
+ ...text(row.table_collation) === void 0 ? {} : { collation: text(row.table_collation) },
481
+ ...text(row.create_options) === void 0 ? {} : { createOptions: text(row.create_options) }
482
+ })
483
+ };
484
+ }
485
+ function deferred(row, namespace) {
486
+ const physicalName = text(row.table_name) ?? "unnamed_object";
487
+ return {
488
+ kind: "deferred-object",
489
+ id: stableId(`deferred:${text(row.table_type) ?? "other"}:${physicalName}`),
490
+ identitySource: "physical-name",
491
+ objectKind: text(row.table_type)?.toUpperCase() === "VIEW" || text(row.table_type)?.toUpperCase() === "SYSTEM VIEW" ? "view" : "other",
492
+ physicalName,
493
+ reference: reference("deferred-object", physicalName, namespace, "INFORMATION_SCHEMA.TABLES", "TABLE_NAME"),
494
+ dialect: extension({
495
+ ...text(row.table_type) === void 0 ? {} : { tableType: text(row.table_type) },
496
+ ...text(row.engine) === void 0 ? {} : { engine: text(row.engine) }
497
+ })
498
+ };
499
+ }
500
+ function column(row, table, namespace) {
501
+ const physicalName = text(row.column_name) ?? "unnamed_column";
502
+ const extra = text(row.extra) ?? "";
503
+ const generationExpression = text(row.generation_expression);
504
+ const generated = generationExpression !== void 0 && (/GENERATED/i.test(extra) || generationExpression !== "") ? {
505
+ kind: "generated",
506
+ mode: /VIRTUAL/i.test(extra) ? "virtual" : /STORED/i.test(extra) ? "stored" : "unknown",
507
+ expression: sql(generationExpression, namespace, table, physicalName)
508
+ } : void 0;
509
+ const identity = /auto_increment/i.test(extra) ? {
510
+ kind: "identity",
511
+ generation: "by-default",
512
+ options: {},
513
+ dialect: {
514
+ dialect: "mysql",
515
+ version: 1,
516
+ data: { autoIncrement: true }
517
+ }
518
+ } : void 0;
519
+ const onUpdateMatch = extra.match(/on update\s+(.+)$/i);
520
+ const defaultText = text(row.column_default);
521
+ const defaultValue = literal(defaultText, text(row.data_type));
522
+ return {
523
+ kind: "column",
524
+ id: stableId(physicalName),
525
+ identitySource: "physical-name",
526
+ physicalName,
527
+ ordinalPosition: number(row.ordinal_position) ?? 0,
528
+ nullable: text(row.is_nullable)?.toUpperCase() !== "NO",
529
+ storage: { nativeType: text(row.column_type) ?? "unknown" },
530
+ default: defaultValue !== void 0 ? {
531
+ kind: "literal",
532
+ value: defaultValue
533
+ } : defaultText !== void 0 ? {
534
+ kind: "expression",
535
+ expression: sql(defaultText, namespace, table, physicalName)
536
+ } : void 0,
537
+ generated,
538
+ identity,
539
+ onUpdate: onUpdateMatch?.[1] ? sql(onUpdateMatch[1], namespace, table, physicalName) : void 0,
540
+ dialect: extension({
541
+ ...text(row.data_type) === void 0 ? {} : { dataType: text(row.data_type) },
542
+ ...text(row.character_set_name) === void 0 ? {} : { characterSet: text(row.character_set_name) },
543
+ ...text(row.collation_name) === void 0 ? {} : { collation: text(row.collation_name) },
544
+ ...extra === "" ? {} : { extra }
545
+ }),
546
+ reference: reference("column", physicalName, namespace, "INFORMATION_SCHEMA.COLUMNS", "COLUMN_NAME")
547
+ };
548
+ }
549
+ function constraints(table, rows, tableByName, namespace, diagnostics) {
550
+ const grouped = /* @__PURE__ */ new Map();
551
+ for (const row of rows) {
552
+ const key = text(row.constraint_name) ?? "unknown";
553
+ const group = grouped.get(key) ?? [];
554
+ group.push(row);
555
+ grouped.set(key, group);
556
+ }
557
+ const result = [];
558
+ for (const [physicalName, group] of grouped) {
559
+ const first = group[0];
560
+ const columns = group.sort((left, right) => (number(left.ordinal_position) ?? 0) - (number(right.ordinal_position) ?? 0)).map((row) => text(row.column_name) ?? "unknown");
561
+ const common = {
562
+ id: stableId(physicalName),
563
+ identitySource: "physical-name",
564
+ physicalName,
565
+ reference: reference("constraint", physicalName, namespace, "INFORMATION_SCHEMA.TABLE_CONSTRAINTS", "CONSTRAINT_NAME")
566
+ };
567
+ const type = text(first.constraint_type);
568
+ if (type === "PRIMARY KEY") {
569
+ result.push({
570
+ kind: "primary-key",
571
+ ...common,
572
+ columns
573
+ });
574
+ continue;
575
+ }
576
+ if (type === "UNIQUE") {
577
+ result.push({
578
+ kind: "unique",
579
+ ...common,
580
+ columns,
581
+ nulls: "distinct"
582
+ });
583
+ continue;
584
+ }
585
+ if (type !== "FOREIGN KEY") continue;
586
+ const targetTableName = text(first.referenced_table_name) ?? "unknown";
587
+ if (!tableByName.has(targetTableName)) diagnostics.push(createIntrospectionDiagnostic({
588
+ severity: "error",
589
+ code: "unresolved-reference",
590
+ message: `MySQL foreign key target ${targetTableName} was not found`,
591
+ path: [
592
+ table.id,
593
+ "constraints",
594
+ physicalName
595
+ ]
596
+ }));
597
+ result.push({
598
+ kind: "foreign-key",
599
+ ...common,
600
+ columns,
601
+ target: {
602
+ table: targetTableName,
603
+ columns: group.map((row) => text(row.referenced_column_name) ?? "unknown")
604
+ },
605
+ onUpdate: action(first.update_rule),
606
+ onDelete: action(first.delete_rule),
607
+ match: match(first.match_option),
608
+ dialect: text(first.enforced)?.toUpperCase() === "NO" ? {
609
+ dialect: "mysql",
610
+ version: 1,
611
+ data: { enforced: false }
612
+ } : void 0
613
+ });
614
+ }
615
+ return result;
616
+ }
617
+ function mapIndex(rows, table, namespace, diagnostics) {
618
+ const first = rows[0];
619
+ const physicalName = text(first.index_name);
620
+ if (!physicalName) return;
621
+ const terms = [];
622
+ for (const row of rows) {
623
+ const expression = text(row.expression);
624
+ const columnName = text(row.column_name);
625
+ if (!expression && !columnName) {
626
+ diagnostics.push(createIntrospectionDiagnostic({
627
+ severity: "error",
628
+ code: "unsupported-feature",
629
+ message: `MySQL index ${physicalName} has an unrepresented term`,
630
+ path: [
631
+ table.id,
632
+ "indexes",
633
+ physicalName
634
+ ]
635
+ }));
636
+ continue;
637
+ }
638
+ terms.push(expression ? {
639
+ kind: "expression",
640
+ expression: sql(expression, namespace, table, physicalName),
641
+ position: number(row.seq_in_index) ?? 0
642
+ } : {
643
+ kind: "column",
644
+ column: columnName,
645
+ position: number(row.seq_in_index) ?? 0,
646
+ direction: text(row.collation) === "D" ? "DESC" : "ASC",
647
+ prefixLength: row.sub_part === null || row.sub_part === void 0 ? void 0 : {
648
+ kind: "literal",
649
+ value: number(row.sub_part) ?? text(row.sub_part) ?? ""
650
+ }
651
+ });
652
+ if (row.sub_part !== null && row.sub_part !== void 0) diagnostics.push(createIntrospectionDiagnostic({
653
+ severity: "warning",
654
+ code: "lossy-mapping",
655
+ message: `MySQL index prefix length for ${physicalName} is retained only in catalog rows`,
656
+ path: [
657
+ table.id,
658
+ "indexes",
659
+ physicalName
660
+ ]
661
+ }));
662
+ }
663
+ return {
664
+ kind: "index",
665
+ id: stableId(physicalName),
666
+ identitySource: "physical-name",
667
+ physicalName,
668
+ unique: number(first.non_unique) === 0,
669
+ terms,
670
+ method: text(first.index_type),
671
+ dialect: extension({
672
+ ...text(first.index_type) === void 0 ? {} : { indexType: text(first.index_type) },
673
+ ...text(first.is_visible) === void 0 ? {} : { visible: text(first.is_visible).toUpperCase() !== "NO" },
674
+ ...text(first.comment) === void 0 ? {} : { comment: text(first.comment) },
675
+ ...text(first.index_comment) === void 0 ? {} : { indexComment: text(first.index_comment) }
676
+ }),
677
+ reference: reference("index", physicalName, namespace, "INFORMATION_SCHEMA.STATISTICS", "INDEX_NAME")
678
+ };
679
+ }
680
+ function viewObject(tableRow, viewRow, namespace, diagnostics) {
681
+ const physicalName = text(tableRow.table_name) ?? "unnamed_view";
682
+ const physicalReference = reference("view", physicalName, namespace, "INFORMATION_SCHEMA.VIEWS", "TABLE_NAME");
683
+ const definition = text(viewRow?.view_definition);
684
+ if (definition === void 0 || definition.trim() === "") {
685
+ diagnostics.push(createIntrospectionDiagnostic({
686
+ severity: "error",
687
+ code: "expression-parse-failed",
688
+ message: `MySQL view ${physicalName} has no recoverable definition`,
689
+ path: [
690
+ "views",
691
+ physicalName,
692
+ "definition"
693
+ ],
694
+ physicalReference,
695
+ remediation: "Grant the metadata privilege needed to read INFORMATION_SCHEMA.VIEWS.VIEW_DEFINITION."
696
+ }));
697
+ return {
698
+ kind: "deferred-object",
699
+ id: stableId(`view:${physicalName}`),
700
+ identitySource: "physical-name",
701
+ objectKind: "view",
702
+ physicalName,
703
+ reference: physicalReference,
704
+ dialect: extension({
705
+ reason: "definition-unavailable",
706
+ ...text(viewRow?.definer) === void 0 ? {} : { definer: text(viewRow?.definer) }
707
+ })
708
+ };
709
+ }
710
+ const checkOption = normalizeCheckOption(viewRow?.check_option);
711
+ const securityType = text(viewRow?.security_type)?.toUpperCase();
712
+ return {
713
+ kind: "view",
714
+ id: stableId(physicalName),
715
+ identitySource: "physical-name",
716
+ physicalName,
717
+ columns: [],
718
+ definition: sql(definition, namespace, {
719
+ kind: "view",
720
+ physicalName,
721
+ reference: physicalReference
722
+ }, physicalName),
723
+ ...checkOption === void 0 ? {} : { checkOption },
724
+ ...securityType === "INVOKER" ? { securityInvoker: true } : {},
725
+ reference: physicalReference,
726
+ provenance: {
727
+ kind: "catalog",
728
+ dialect: "mysql",
729
+ reference: physicalReference
730
+ },
731
+ dialect: extension({
732
+ ...text(viewRow?.is_updatable) === void 0 ? {} : { isUpdatable: text(viewRow?.is_updatable) },
733
+ ...securityType === void 0 ? {} : { securityType },
734
+ ...text(viewRow?.definer) === void 0 ? {} : { definer: text(viewRow?.definer) }
735
+ })
736
+ };
737
+ }
738
+ function routineObject(row, parameterRows, namespace) {
739
+ const physicalName = text(row.routine_name) ?? "unnamed_routine";
740
+ const physicalReference = reference("routine", physicalName, namespace, "INFORMATION_SCHEMA.ROUTINES", "ROUTINE_NAME");
741
+ const parameters = parameterRows.filter((row) => (number(row.ordinal_position) ?? 0) > 0).sort((left, right) => (number(left.ordinal_position) ?? 0) - (number(right.ordinal_position) ?? 0)).map(routineParameter);
742
+ const definition = text(row.routine_definition);
743
+ const returnType = text(row.data_type);
744
+ const routine = {
745
+ kind: "routine",
746
+ id: stableId(physicalName),
747
+ identitySource: "physical-name",
748
+ physicalName,
749
+ routineKind: text(row.routine_type)?.toUpperCase() === "FUNCTION" ? "function" : text(row.routine_type)?.toUpperCase() === "PROCEDURE" ? "procedure" : "unknown",
750
+ parameters,
751
+ ...returnType === void 0 || returnType.toUpperCase() === "VOID" ? {} : { returnType: storage(row.data_type, row.dtd_identifier) },
752
+ ...text(row.external_language) === void 0 && text(row.routine_body) === void 0 ? {} : { language: text(row.external_language) ?? text(row.routine_body) ?? "unknown" },
753
+ ...definition === void 0 ? {} : { body: sql(definition, namespace, {
754
+ kind: "routine",
755
+ physicalName,
756
+ reference: physicalReference
757
+ }, physicalName) },
758
+ volatility: "unknown",
759
+ ...text(row.security_type) === void 0 ? {} : { security: routineSecurity(row.security_type) },
760
+ reference: physicalReference,
761
+ provenance: {
762
+ kind: "catalog",
763
+ dialect: "mysql",
764
+ reference: physicalReference
765
+ },
766
+ dialect: extension({
767
+ ...text(row.is_deterministic) === void 0 ? {} : { deterministic: text(row.is_deterministic).toUpperCase() === "YES" },
768
+ ...text(row.sql_data_access) === void 0 ? {} : { sqlDataAccess: text(row.sql_data_access) },
769
+ ...text(row.sql_mode) === void 0 ? {} : { sqlMode: text(row.sql_mode) }
770
+ })
771
+ };
772
+ const commentText = text(row.routine_comment);
773
+ if (commentText === void 0 || commentText === "") return routine;
774
+ return {
775
+ ...routine,
776
+ comment: objectComment({
777
+ kind: "routine",
778
+ id: routine.id
779
+ }, commentText, physicalReference, namespace)
780
+ };
781
+ }
782
+ function routineParameter(row) {
783
+ const parameterName = text(row.parameter_name);
784
+ return {
785
+ ...parameterName === void 0 ? {} : { name: parameterName },
786
+ ...routineParameterMode(row.parameter_mode) === void 0 ? {} : { mode: routineParameterMode(row.parameter_mode) },
787
+ storage: storage(row.data_type, row.dtd_identifier),
788
+ ordinalPosition: number(row.ordinal_position) ?? 0
789
+ };
790
+ }
791
+ function triggerObject(rows, relations, namespace, diagnostics) {
792
+ const first = rows[0];
793
+ const physicalName = text(first.trigger_name) ?? "unnamed_trigger";
794
+ const physicalReference = reference("trigger", physicalName, namespace, "INFORMATION_SCHEMA.TRIGGERS", "TRIGGER_NAME");
795
+ const tableName = text(first.table_name);
796
+ const target = tableName === void 0 ? void 0 : relations.get(tableName);
797
+ const body = text(first.action_statement);
798
+ if (target === void 0 || body === void 0 || body.trim() === "") {
799
+ if (target === void 0) diagnostics.push(createIntrospectionDiagnostic({
800
+ severity: "error",
801
+ code: "unresolved-reference",
802
+ message: `MySQL trigger ${physicalName} target ${tableName ?? "unknown"} was not found`,
803
+ path: [
804
+ "triggers",
805
+ physicalName,
806
+ "table"
807
+ ],
808
+ physicalReference
809
+ }));
810
+ if (body === void 0 || body.trim() === "") diagnostics.push(createIntrospectionDiagnostic({
811
+ severity: "error",
812
+ code: "expression-parse-failed",
813
+ message: `MySQL trigger ${physicalName} has no recoverable body`,
814
+ path: [
815
+ "triggers",
816
+ physicalName,
817
+ "body"
818
+ ],
819
+ physicalReference
820
+ }));
821
+ return {
822
+ kind: "deferred-object",
823
+ id: stableId(`trigger:${physicalName}`),
824
+ identitySource: "physical-name",
825
+ objectKind: "trigger",
826
+ physicalName,
827
+ reference: physicalReference,
828
+ dialect: extension({
829
+ ...tableName === void 0 ? {} : { tableName },
830
+ ...body === void 0 ? {} : { actionStatement: body }
831
+ })
832
+ };
833
+ }
834
+ const timing = triggerTiming(first.action_timing);
835
+ const events = [...new Set(rows.map((row) => triggerEvent(row.event_manipulation)).filter((value) => value !== void 0))];
836
+ return {
837
+ kind: "trigger",
838
+ id: stableId(physicalName),
839
+ identitySource: "physical-name",
840
+ physicalName,
841
+ table: target,
842
+ timing,
843
+ events,
844
+ orientation: triggerOrientation(first.action_orientation),
845
+ ...text(first.action_condition) === void 0 ? {} : { condition: sql(text(first.action_condition), namespace, {
846
+ kind: "trigger",
847
+ physicalName,
848
+ reference: physicalReference
849
+ }, physicalName) },
850
+ body: sql(body, namespace, {
851
+ kind: "trigger",
852
+ physicalName,
853
+ reference: physicalReference
854
+ }, physicalName),
855
+ reference: physicalReference,
856
+ provenance: {
857
+ kind: "catalog",
858
+ dialect: "mysql",
859
+ reference: physicalReference
860
+ },
861
+ dialect: extension({
862
+ ...text(first.definer) === void 0 ? {} : { definer: text(first.definer) },
863
+ ...text(first.sql_mode) === void 0 ? {} : { sqlMode: text(first.sql_mode) },
864
+ ...number(first.action_order) === void 0 ? {} : { actionOrder: number(first.action_order) }
865
+ })
866
+ };
867
+ }
868
+ function partitionObject(row, tables, namespace, diagnostics) {
869
+ const tableName = text(row.table_name) ?? "unknown";
870
+ const partitionName = text(row.partition_name);
871
+ const subpartitionName = text(row.subpartition_name);
872
+ const physicalName = partitionName === void 0 ? tableName : subpartitionName === void 0 ? partitionName : `${partitionName}/${subpartitionName}`;
873
+ const physicalReference = reference("partition", physicalName, namespace, "INFORMATION_SCHEMA.PARTITIONS", "PARTITION_NAME");
874
+ const parent = tables.get(tableName);
875
+ if (parent === void 0) {
876
+ diagnostics.push(createIntrospectionDiagnostic({
877
+ severity: "error",
878
+ code: "unresolved-reference",
879
+ message: `MySQL partition ${physicalName} parent table ${tableName} was not found`,
880
+ path: [
881
+ "partitions",
882
+ physicalName,
883
+ "parent"
884
+ ],
885
+ physicalReference
886
+ }));
887
+ return {
888
+ kind: "deferred-object",
889
+ id: stableId(`partition:${tableName}:${physicalName}`),
890
+ identitySource: "physical-name",
891
+ objectKind: "partition",
892
+ physicalName,
893
+ reference: physicalReference,
894
+ dialect: extension({ tableName })
895
+ };
896
+ }
897
+ const method = text(row.subpartition_method) ?? text(row.partition_method) ?? "UNKNOWN";
898
+ const strategy = partitionStrategy(method);
899
+ if (strategy === "unknown") diagnostics.push(createIntrospectionDiagnostic({
900
+ severity: "warning",
901
+ code: "unsupported-feature",
902
+ message: `MySQL partition method ${method} is retained with an unknown normalized strategy`,
903
+ path: [
904
+ "partitions",
905
+ physicalName,
906
+ "strategy"
907
+ ],
908
+ physicalReference
909
+ }));
910
+ const boundText = text(row.partition_description) ?? text(row.subpartition_expression) ?? text(row.partition_expression);
911
+ const expression = text(row.partition_expression);
912
+ const partition = {
913
+ kind: "partition",
914
+ id: stableId(`partition:${tableName}:${physicalName}`),
915
+ identitySource: "physical-name",
916
+ physicalName,
917
+ parent: {
918
+ kind: "table",
919
+ id: parent.id
920
+ },
921
+ strategy,
922
+ ...expression === void 0 ? {} : { keyColumns: partitionKeyColumns(expression) },
923
+ ...boundText === void 0 ? {} : { bound: sql(boundText, namespace, parent, physicalName) },
924
+ reference: physicalReference,
925
+ dialect: extension({
926
+ ...text(row.partition_method) === void 0 ? {} : { partitionMethod: text(row.partition_method) },
927
+ ...text(row.subpartition_method) === void 0 ? {} : { subpartitionMethod: text(row.subpartition_method) },
928
+ ...text(row.subpartition_name) === void 0 ? {} : { subpartitionName: text(row.subpartition_name) },
929
+ ...text(row.tablespace_name) === void 0 ? {} : { tablespace: text(row.tablespace_name) }
930
+ })
931
+ };
932
+ const commentText = text(row.partition_comment);
933
+ if (commentText === void 0 || commentText === "") return partition;
934
+ return {
935
+ ...partition,
936
+ comment: objectComment({
937
+ kind: "partition",
938
+ id: partition.id
939
+ }, commentText, physicalReference, namespace)
940
+ };
941
+ }
942
+ function collationObject(row, namespace) {
943
+ const physicalName = text(row.collation_name) ?? "unknown_collation";
944
+ return {
945
+ kind: "collation",
946
+ id: stableId(physicalName),
947
+ identitySource: "physical-name",
948
+ physicalName,
949
+ reference: reference("collation", physicalName, namespace, "INFORMATION_SCHEMA.COLLATIONS", "COLLATION_NAME"),
950
+ dialect: extension({
951
+ ...text(row.character_set_name) === void 0 ? {} : { characterSet: text(row.character_set_name) },
952
+ ...number(row.collation_id) === void 0 ? {} : { id: number(row.collation_id) },
953
+ ...text(row.is_default) === void 0 ? {} : { isDefault: text(row.is_default) === "Yes" },
954
+ ...text(row.is_compiled) === void 0 ? {} : { isCompiled: text(row.is_compiled) === "Yes" },
955
+ ...number(row.sort_length) === void 0 ? {} : { sortLength: number(row.sort_length) },
956
+ ...text(row.pad_attribute) === void 0 ? {} : { padAttribute: text(row.pad_attribute) }
957
+ })
958
+ };
959
+ }
960
+ function opaqueEvent(row, namespace) {
961
+ const physicalName = text(row.event_name) ?? "unnamed_event";
962
+ const physicalReference = reference("opaque-object", physicalName, namespace, "INFORMATION_SCHEMA.EVENTS", "EVENT_NAME");
963
+ const definition = text(row.event_definition) ?? text(row.event_body);
964
+ return {
965
+ kind: "opaque-object",
966
+ id: stableId(`event:${physicalName}`),
967
+ identitySource: "physical-name",
968
+ objectKind: "event",
969
+ physicalName,
970
+ data: {
971
+ ...text(row.event_type) === void 0 ? {} : { eventType: text(row.event_type) },
972
+ ...text(row.status) === void 0 ? {} : { status: text(row.status) },
973
+ ...text(row.execute_at) === void 0 ? {} : { executeAt: text(row.execute_at) },
974
+ ...text(row.interval_value) === void 0 ? {} : { intervalValue: text(row.interval_value) },
975
+ ...text(row.interval_field) === void 0 ? {} : { intervalField: text(row.interval_field) },
976
+ ...text(row.definer) === void 0 ? {} : { definer: text(row.definer) }
977
+ },
978
+ ...definition === void 0 ? {} : { sql: sql(definition, namespace, {
979
+ kind: "opaque-object",
980
+ physicalName,
981
+ reference: physicalReference
982
+ }, physicalName) },
983
+ reference: physicalReference,
984
+ provenance: {
985
+ kind: "catalog",
986
+ dialect: "mysql",
987
+ reference: physicalReference
988
+ },
989
+ dialect: extension({ objectKind: "event" })
990
+ };
991
+ }
992
+ function objectComment(object, comment, physicalReference, namespace) {
993
+ return {
994
+ kind: "comment",
995
+ id: stableId(`${object.kind}_${object.id}_comment`),
996
+ object,
997
+ text: comment,
998
+ reference: physicalReference,
999
+ provenance: {
1000
+ kind: "catalog",
1001
+ dialect: "mysql",
1002
+ reference: physicalReference,
1003
+ path: ["namespace", namespace]
1004
+ }
1005
+ };
1006
+ }
1007
+ function groupRows(rows, keyOf) {
1008
+ const groups = /* @__PURE__ */ new Map();
1009
+ for (const row of rows) {
1010
+ const key = keyOf(row);
1011
+ const group = groups.get(key) ?? [];
1012
+ group.push(row);
1013
+ groups.set(key, group);
1014
+ }
1015
+ return [...groups.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([, group]) => group);
1016
+ }
1017
+ function extension(data) {
1018
+ return {
1019
+ dialect: "mysql",
1020
+ version: 1,
1021
+ data
1022
+ };
1023
+ }
1024
+ function storage(dataType, declaration) {
1025
+ return { nativeType: text(declaration) ?? text(dataType) ?? "unknown" };
1026
+ }
1027
+ function normalizeCheckOption(value) {
1028
+ const normalized = text(value)?.toLowerCase();
1029
+ return normalized === "none" || normalized === void 0 ? normalized === void 0 ? void 0 : "none" : normalized === "local" ? "local" : normalized === "cascaded" ? "cascaded" : void 0;
1030
+ }
1031
+ function routineSecurity(value) {
1032
+ return text(value)?.toLowerCase() === "invoker" ? "invoker" : text(value)?.toLowerCase() === "definer" ? "definer" : "unknown";
1033
+ }
1034
+ function routineParameterMode(value) {
1035
+ const normalized = text(value)?.toLowerCase();
1036
+ return normalized === "in" ? "in" : normalized === "out" ? "out" : normalized === "inout" ? "inout" : void 0;
1037
+ }
1038
+ function triggerTiming(value) {
1039
+ const normalized = text(value)?.toLowerCase();
1040
+ return normalized === "before" ? "before" : normalized === "after" ? "after" : "unknown";
1041
+ }
1042
+ function triggerEvent(value) {
1043
+ const normalized = text(value)?.toLowerCase();
1044
+ return normalized === "insert" || normalized === "update" || normalized === "delete" ? normalized : void 0;
1045
+ }
1046
+ function triggerOrientation(value) {
1047
+ const normalized = text(value)?.toLowerCase();
1048
+ return normalized === "row" ? "row" : normalized === "statement" ? "statement" : void 0;
1049
+ }
1050
+ function partitionStrategy(value) {
1051
+ const normalized = value.toLowerCase();
1052
+ return normalized === "range" ? "range" : normalized === "list" ? "list" : normalized === "hash" ? "hash" : "unknown";
1053
+ }
1054
+ function partitionKeyColumns(expression) {
1055
+ return expression.split(",").map((value) => value.trim()).filter((value) => /^[A-Za-z_][A-Za-z0-9_$]*$/.test(value));
1056
+ }
1057
+ function sql(textValue, namespace, table, name) {
1058
+ return {
1059
+ kind: "sql",
1060
+ dialect: "mysql",
1061
+ text: textValue,
1062
+ provenance: {
1063
+ kind: "catalog",
1064
+ dialect: "mysql",
1065
+ reference: table.reference ?? reference(table.kind, name, namespace, "INFORMATION_SCHEMA", "TABLE_NAME", table.physicalName)
1066
+ }
1067
+ };
1068
+ }
1069
+ function reference(kind, name, namespace, relation, key, value = name) {
1070
+ return {
1071
+ kind,
1072
+ name,
1073
+ namespace,
1074
+ catalog: {
1075
+ relation,
1076
+ key,
1077
+ value
1078
+ }
1079
+ };
1080
+ }
1081
+ function literal(value, dataType) {
1082
+ if (value === void 0) return;
1083
+ if (value.toUpperCase() === "NULL") return null;
1084
+ if (value.toUpperCase() === "TRUE") return true;
1085
+ if (value.toUpperCase() === "FALSE") return false;
1086
+ if (/^-?(?:\d+\.?\d*|\.\d+)$/.test(value)) return Number(value);
1087
+ if (/^'(?:''|[^'])*'$/.test(value)) return value.slice(1, -1).replace(/''/g, "'");
1088
+ if (value === "" || /^(?:char|varchar|text|tinytext|mediumtext|longtext|enum|set)$/i.test(dataType ?? "")) return value;
1089
+ }
1090
+ function emptyCatalog(namespace, diagnostics) {
1091
+ return {
1092
+ dialect: "mysql",
1093
+ server: {
1094
+ product: "mysql",
1095
+ rawVersion: "unknown",
1096
+ capabilities: {
1097
+ generatedColumns: false,
1098
+ identityMetadata: false,
1099
+ checkConstraints: false,
1100
+ checkConstraintEnforcement: "unknown",
1101
+ expressionDecompilation: false,
1102
+ indexExpressions: false,
1103
+ indexPredicates: false,
1104
+ indexIncludedColumns: false,
1105
+ namespaces: true,
1106
+ visibility: "unknown"
1107
+ }
1108
+ },
1109
+ namespace: {
1110
+ kind: "mysql-database",
1111
+ name: namespace
1112
+ },
1113
+ tables: [],
1114
+ deferredObjects: [],
1115
+ diagnostics
1116
+ };
1117
+ }
1118
+ function stableId(value) {
1119
+ if (value && !/[.\\\u0000-\u001f\u007f]/.test(value)) return value;
1120
+ let hash = 2166136261;
1121
+ for (const character of value) {
1122
+ hash ^= character.charCodeAt(0);
1123
+ hash = Math.imul(hash, 16777619);
1124
+ }
1125
+ return `introspected_${(hash >>> 0).toString(16)}`;
1126
+ }
1127
+ function text(value) {
1128
+ return value === null || value === void 0 ? void 0 : String(value);
1129
+ }
1130
+ function number(value) {
1131
+ if (typeof value === "number" && Number.isFinite(value)) return value;
1132
+ if (typeof value === "string" && value.trim() !== "") {
1133
+ const result = Number(value);
1134
+ return Number.isFinite(result) ? result : void 0;
1135
+ }
1136
+ }
1137
+ function action(value) {
1138
+ const normalized = text(value)?.toLowerCase();
1139
+ return normalized === "cascade" ? "cascade" : normalized === "restrict" ? "restrict" : normalized === "set null" ? "set-null" : normalized === "set default" ? "set-default" : "no-action";
1140
+ }
1141
+ function match(value) {
1142
+ return text(value)?.toLowerCase() === "full" ? "full" : "simple";
1143
+ }
1144
+ //#endregion
1145
+ export { mysqlChecksQuery, mysqlCollationsQuery, mysqlColumnsQuery, mysqlEventsQuery, mysqlKeyUsageQuery, mysqlPartitionsQuery, mysqlRoutineParametersQuery, mysqlRoutinesQuery, mysqlServerQuery, mysqlStatisticsQuery, mysqlTablesQuery, mysqlTriggersQuery, mysqlViewsQuery, readCatalog };