@zz1996/dbhub-dameng 0.1.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.
@@ -0,0 +1,527 @@
1
+ import {
2
+ extractAffectedRows,
3
+ parseQueryResults
4
+ } from "./chunk-RTB262PR.js";
5
+ import {
6
+ quoteIdentifier
7
+ } from "./chunk-IPK7BYBL.js";
8
+ import {
9
+ SQLRowLimiter
10
+ } from "./chunk-ZNQTMARG.js";
11
+ import {
12
+ ConnectorRegistry,
13
+ SafeURL,
14
+ obfuscateDSNPassword,
15
+ splitSQLStatements
16
+ } from "./chunk-SQA2ISDE.js";
17
+
18
+ // src/connectors/mysql/index.ts
19
+ import mysql from "mysql2/promise";
20
+ var MySQLDSNParser = class {
21
+ async parse(dsn, config) {
22
+ const connectionTimeoutSeconds = config?.connectionTimeoutSeconds;
23
+ const timezone = config?.timezone;
24
+ if (!this.isValidDSN(dsn)) {
25
+ const obfuscatedDSN = obfuscateDSNPassword(dsn);
26
+ const expectedFormat = this.getSampleDSN();
27
+ throw new Error(
28
+ `Invalid MySQL DSN format.
29
+ Provided: ${obfuscatedDSN}
30
+ Expected: ${expectedFormat}`
31
+ );
32
+ }
33
+ try {
34
+ const url = new SafeURL(dsn);
35
+ const config2 = {
36
+ host: url.hostname,
37
+ port: url.port ? parseInt(url.port) : 3306,
38
+ database: url.pathname ? url.pathname.substring(1) : "",
39
+ // Remove leading '/' if exists
40
+ user: url.username,
41
+ password: url.password,
42
+ multipleStatements: true,
43
+ // Enable native multi-statement support
44
+ supportBigNumbers: true
45
+ // Return BIGINT as string when value exceeds Number.MAX_SAFE_INTEGER
46
+ };
47
+ url.forEachSearchParam((value, key) => {
48
+ if (key === "sslmode") {
49
+ if (value === "disable") {
50
+ config2.ssl = void 0;
51
+ } else if (value === "require") {
52
+ config2.ssl = { rejectUnauthorized: false };
53
+ } else {
54
+ config2.ssl = {};
55
+ }
56
+ }
57
+ });
58
+ if (connectionTimeoutSeconds !== void 0) {
59
+ config2.connectTimeout = connectionTimeoutSeconds * 1e3;
60
+ }
61
+ if (timezone !== void 0) {
62
+ config2.timezone = timezone;
63
+ }
64
+ if (url.password && url.password.includes("X-Amz-Credential")) {
65
+ config2.authPlugins = {
66
+ mysql_clear_password: () => () => {
67
+ return Buffer.from(url.password + "\0");
68
+ }
69
+ };
70
+ if (config2.ssl === void 0) {
71
+ config2.ssl = { rejectUnauthorized: false };
72
+ }
73
+ }
74
+ return config2;
75
+ } catch (error) {
76
+ throw new Error(
77
+ `Failed to parse MySQL DSN: ${error instanceof Error ? error.message : String(error)}`
78
+ );
79
+ }
80
+ }
81
+ getSampleDSN() {
82
+ return "mysql://root:password@localhost:3306/mysql?sslmode=require";
83
+ }
84
+ isValidDSN(dsn) {
85
+ try {
86
+ return dsn.startsWith("mysql://");
87
+ } catch (error) {
88
+ return false;
89
+ }
90
+ }
91
+ };
92
+ var MySQLConnector = class _MySQLConnector {
93
+ constructor() {
94
+ this.id = "mysql";
95
+ this.name = "MySQL";
96
+ this.dsnParser = new MySQLDSNParser();
97
+ this.pool = null;
98
+ // Source ID is set by ConnectorManager after cloning
99
+ this.sourceId = "default";
100
+ }
101
+ getId() {
102
+ return this.sourceId;
103
+ }
104
+ clone() {
105
+ return new _MySQLConnector();
106
+ }
107
+ async connect(dsn, initScript, config) {
108
+ try {
109
+ const connectionOptions = await this.dsnParser.parse(dsn, config);
110
+ this.pool = mysql.createPool(connectionOptions);
111
+ if (config?.queryTimeoutSeconds !== void 0) {
112
+ this.queryTimeoutMs = config.queryTimeoutSeconds * 1e3;
113
+ }
114
+ const [rows] = await this.pool.query("SELECT 1");
115
+ } catch (err) {
116
+ console.error("Failed to connect to MySQL database:", err);
117
+ throw err;
118
+ }
119
+ }
120
+ async disconnect() {
121
+ if (this.pool) {
122
+ await this.pool.end();
123
+ this.pool = null;
124
+ }
125
+ }
126
+ async getSchemas() {
127
+ if (!this.pool) {
128
+ throw new Error("Not connected to database");
129
+ }
130
+ try {
131
+ const [rows] = await this.pool.query(`
132
+ SELECT SCHEMA_NAME
133
+ FROM INFORMATION_SCHEMA.SCHEMATA
134
+ WHERE SCHEMA_NAME NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
135
+ ORDER BY SCHEMA_NAME
136
+ `);
137
+ return rows.map((row) => row.SCHEMA_NAME);
138
+ } catch (error) {
139
+ console.error("Error getting schemas:", error);
140
+ throw error;
141
+ }
142
+ }
143
+ async getTables(schema) {
144
+ if (!this.pool) {
145
+ throw new Error("Not connected to database");
146
+ }
147
+ try {
148
+ const schemaClause = schema ? "WHERE TABLE_SCHEMA = ?" : "WHERE TABLE_SCHEMA = DATABASE()";
149
+ const queryParams = schema ? [schema] : [];
150
+ const [rows] = await this.pool.query(
151
+ `
152
+ SELECT TABLE_NAME
153
+ FROM INFORMATION_SCHEMA.TABLES
154
+ ${schemaClause}
155
+ AND TABLE_TYPE = 'BASE TABLE'
156
+ ORDER BY TABLE_NAME
157
+ `,
158
+ queryParams
159
+ );
160
+ return rows.map((row) => row.TABLE_NAME);
161
+ } catch (error) {
162
+ console.error("Error getting tables:", error);
163
+ throw error;
164
+ }
165
+ }
166
+ async getViews(schema) {
167
+ if (!this.pool) {
168
+ throw new Error("Not connected to database");
169
+ }
170
+ try {
171
+ const schemaClause = schema ? "WHERE TABLE_SCHEMA = ?" : "WHERE TABLE_SCHEMA = DATABASE()";
172
+ const queryParams = schema ? [schema] : [];
173
+ const [rows] = await this.pool.query(
174
+ `
175
+ SELECT TABLE_NAME
176
+ FROM INFORMATION_SCHEMA.TABLES
177
+ ${schemaClause}
178
+ AND TABLE_TYPE = 'VIEW'
179
+ ORDER BY TABLE_NAME
180
+ `,
181
+ queryParams
182
+ );
183
+ return rows.map((row) => row.TABLE_NAME);
184
+ } catch (error) {
185
+ console.error("Error getting views:", error);
186
+ throw error;
187
+ }
188
+ }
189
+ async tableExists(tableName, schema) {
190
+ if (!this.pool) {
191
+ throw new Error("Not connected to database");
192
+ }
193
+ try {
194
+ const schemaClause = schema ? "WHERE TABLE_SCHEMA = ?" : "WHERE TABLE_SCHEMA = DATABASE()";
195
+ const queryParams = schema ? [schema, tableName] : [tableName];
196
+ const [rows] = await this.pool.query(
197
+ `
198
+ SELECT COUNT(*) AS COUNT
199
+ FROM INFORMATION_SCHEMA.TABLES
200
+ ${schemaClause}
201
+ AND TABLE_NAME = ?
202
+ `,
203
+ queryParams
204
+ );
205
+ return rows[0].COUNT > 0;
206
+ } catch (error) {
207
+ console.error("Error checking if table exists:", error);
208
+ throw error;
209
+ }
210
+ }
211
+ async getTableIndexes(tableName, schema) {
212
+ if (!this.pool) {
213
+ throw new Error("Not connected to database");
214
+ }
215
+ try {
216
+ const schemaClause = schema ? "TABLE_SCHEMA = ?" : "TABLE_SCHEMA = DATABASE()";
217
+ const queryParams = schema ? [schema, tableName] : [tableName];
218
+ const [indexRows] = await this.pool.query(
219
+ `
220
+ SELECT
221
+ INDEX_NAME,
222
+ COLUMN_NAME,
223
+ NON_UNIQUE,
224
+ SEQ_IN_INDEX
225
+ FROM
226
+ INFORMATION_SCHEMA.STATISTICS
227
+ WHERE
228
+ ${schemaClause}
229
+ AND TABLE_NAME = ?
230
+ ORDER BY
231
+ INDEX_NAME,
232
+ SEQ_IN_INDEX
233
+ `,
234
+ queryParams
235
+ );
236
+ const indexMap = /* @__PURE__ */ new Map();
237
+ for (const row of indexRows) {
238
+ const indexName = row.INDEX_NAME;
239
+ const columnName = row.COLUMN_NAME;
240
+ const isUnique = row.NON_UNIQUE === 0;
241
+ const isPrimary = indexName === "PRIMARY";
242
+ if (!indexMap.has(indexName)) {
243
+ indexMap.set(indexName, {
244
+ columns: [],
245
+ is_unique: isUnique,
246
+ is_primary: isPrimary
247
+ });
248
+ }
249
+ const indexInfo = indexMap.get(indexName);
250
+ indexInfo.columns.push(columnName);
251
+ }
252
+ const results = [];
253
+ indexMap.forEach((indexInfo, indexName) => {
254
+ results.push({
255
+ index_name: indexName,
256
+ column_names: indexInfo.columns,
257
+ is_unique: indexInfo.is_unique,
258
+ is_primary: indexInfo.is_primary
259
+ });
260
+ });
261
+ return results;
262
+ } catch (error) {
263
+ console.error("Error getting table indexes:", error);
264
+ throw error;
265
+ }
266
+ }
267
+ async getTableSchema(tableName, schema) {
268
+ if (!this.pool) {
269
+ throw new Error("Not connected to database");
270
+ }
271
+ try {
272
+ const schemaClause = schema ? "WHERE TABLE_SCHEMA = ?" : "WHERE TABLE_SCHEMA = DATABASE()";
273
+ const queryParams = schema ? [schema, tableName] : [tableName];
274
+ const [rows] = await this.pool.query(
275
+ `
276
+ SELECT
277
+ COLUMN_NAME as column_name,
278
+ DATA_TYPE as data_type,
279
+ IS_NULLABLE as is_nullable,
280
+ COLUMN_DEFAULT as column_default,
281
+ COLUMN_COMMENT as description
282
+ FROM INFORMATION_SCHEMA.COLUMNS
283
+ ${schemaClause}
284
+ AND TABLE_NAME = ?
285
+ ORDER BY ORDINAL_POSITION
286
+ `,
287
+ queryParams
288
+ );
289
+ return rows.map((row) => ({
290
+ ...row,
291
+ description: row.description || null
292
+ }));
293
+ } catch (error) {
294
+ console.error("Error getting table schema:", error);
295
+ throw error;
296
+ }
297
+ }
298
+ async getTableComment(tableName, schema) {
299
+ if (!this.pool) {
300
+ throw new Error("Not connected to database");
301
+ }
302
+ try {
303
+ const schemaClause = schema ? "WHERE TABLE_SCHEMA = ?" : "WHERE TABLE_SCHEMA = DATABASE()";
304
+ const queryParams = schema ? [schema, tableName] : [tableName];
305
+ const [rows] = await this.pool.query(
306
+ `
307
+ SELECT TABLE_COMMENT
308
+ FROM INFORMATION_SCHEMA.TABLES
309
+ ${schemaClause}
310
+ AND TABLE_NAME = ?
311
+ `,
312
+ queryParams
313
+ );
314
+ if (rows.length > 0) {
315
+ return rows[0].TABLE_COMMENT || null;
316
+ }
317
+ return null;
318
+ } catch (error) {
319
+ return null;
320
+ }
321
+ }
322
+ async getStoredProcedures(schema, routineType) {
323
+ if (!this.pool) {
324
+ throw new Error("Not connected to database");
325
+ }
326
+ try {
327
+ const schemaClause = schema ? "WHERE ROUTINE_SCHEMA = ?" : "WHERE ROUTINE_SCHEMA = DATABASE()";
328
+ const queryParams = schema ? [schema] : [];
329
+ let typeFilter = "";
330
+ if (routineType === "function") {
331
+ typeFilter = " AND ROUTINE_TYPE = 'FUNCTION'";
332
+ } else if (routineType === "procedure") {
333
+ typeFilter = " AND ROUTINE_TYPE = 'PROCEDURE'";
334
+ }
335
+ const [rows] = await this.pool.query(
336
+ `
337
+ SELECT ROUTINE_NAME
338
+ FROM INFORMATION_SCHEMA.ROUTINES
339
+ ${schemaClause}${typeFilter}
340
+ ORDER BY ROUTINE_NAME
341
+ `,
342
+ queryParams
343
+ );
344
+ return rows.map((row) => row.ROUTINE_NAME);
345
+ } catch (error) {
346
+ console.error("Error getting stored procedures:", error);
347
+ throw error;
348
+ }
349
+ }
350
+ async getStoredProcedureDetail(procedureName, schema) {
351
+ if (!this.pool) {
352
+ throw new Error("Not connected to database");
353
+ }
354
+ try {
355
+ const schemaClause = schema ? "WHERE r.ROUTINE_SCHEMA = ?" : "WHERE r.ROUTINE_SCHEMA = DATABASE()";
356
+ const queryParams = schema ? [schema, procedureName] : [procedureName];
357
+ const [rows] = await this.pool.query(
358
+ `
359
+ SELECT
360
+ r.ROUTINE_NAME AS procedure_name,
361
+ CASE
362
+ WHEN r.ROUTINE_TYPE = 'PROCEDURE' THEN 'procedure'
363
+ ELSE 'function'
364
+ END AS procedure_type,
365
+ LOWER(r.ROUTINE_TYPE) AS routine_type,
366
+ r.ROUTINE_DEFINITION,
367
+ r.DTD_IDENTIFIER AS return_type,
368
+ (
369
+ SELECT GROUP_CONCAT(
370
+ CONCAT(p.PARAMETER_NAME, ' ', p.PARAMETER_MODE, ' ', p.DATA_TYPE)
371
+ ORDER BY p.ORDINAL_POSITION
372
+ SEPARATOR ', '
373
+ )
374
+ FROM INFORMATION_SCHEMA.PARAMETERS p
375
+ WHERE p.SPECIFIC_SCHEMA = r.ROUTINE_SCHEMA
376
+ AND p.SPECIFIC_NAME = r.ROUTINE_NAME
377
+ AND p.PARAMETER_NAME IS NOT NULL
378
+ ) AS parameter_list
379
+ FROM INFORMATION_SCHEMA.ROUTINES r
380
+ ${schemaClause}
381
+ AND r.ROUTINE_NAME = ?
382
+ `,
383
+ queryParams
384
+ );
385
+ if (rows.length === 0) {
386
+ const schemaName = schema || "current schema";
387
+ throw new Error(`Stored procedure '${procedureName}' not found in ${schemaName}`);
388
+ }
389
+ const procedure = rows[0];
390
+ let definition = procedure.ROUTINE_DEFINITION;
391
+ try {
392
+ const schemaValue = schema || await this.getCurrentSchema();
393
+ const quotedSchema = quoteIdentifier(schemaValue, "mysql");
394
+ const quotedProcName = quoteIdentifier(procedureName, "mysql");
395
+ if (procedure.procedure_type === "procedure") {
396
+ try {
397
+ const [defRows] = await this.pool.query(`
398
+ SHOW CREATE PROCEDURE ${quotedSchema}.${quotedProcName}
399
+ `);
400
+ if (defRows && defRows.length > 0) {
401
+ definition = defRows[0]["Create Procedure"];
402
+ }
403
+ } catch (err) {
404
+ console.error(`Error getting procedure definition with SHOW CREATE: ${err}`);
405
+ }
406
+ } else {
407
+ try {
408
+ const [defRows] = await this.pool.query(`
409
+ SHOW CREATE FUNCTION ${quotedSchema}.${quotedProcName}
410
+ `);
411
+ if (defRows && defRows.length > 0) {
412
+ definition = defRows[0]["Create Function"];
413
+ }
414
+ } catch (innerErr) {
415
+ console.error(`Error getting function definition with SHOW CREATE: ${innerErr}`);
416
+ }
417
+ }
418
+ if (!definition) {
419
+ const [bodyRows] = await this.pool.query(
420
+ `
421
+ SELECT ROUTINE_DEFINITION, ROUTINE_BODY
422
+ FROM INFORMATION_SCHEMA.ROUTINES
423
+ WHERE ROUTINE_SCHEMA = ? AND ROUTINE_NAME = ?
424
+ `,
425
+ [schemaValue, procedureName]
426
+ );
427
+ if (bodyRows && bodyRows.length > 0) {
428
+ if (bodyRows[0].ROUTINE_DEFINITION) {
429
+ definition = bodyRows[0].ROUTINE_DEFINITION;
430
+ } else if (bodyRows[0].ROUTINE_BODY) {
431
+ definition = bodyRows[0].ROUTINE_BODY;
432
+ }
433
+ }
434
+ }
435
+ } catch (error) {
436
+ console.error(`Error getting procedure/function details: ${error}`);
437
+ }
438
+ return {
439
+ procedure_name: procedure.procedure_name,
440
+ procedure_type: procedure.procedure_type,
441
+ language: "sql",
442
+ // MySQL procedures are generally in SQL
443
+ parameter_list: procedure.parameter_list || "",
444
+ return_type: procedure.routine_type === "function" ? procedure.return_type : void 0,
445
+ definition: definition || void 0
446
+ };
447
+ } catch (error) {
448
+ console.error("Error getting stored procedure detail:", error);
449
+ throw error;
450
+ }
451
+ }
452
+ // Helper method to get current schema (database) name
453
+ async getCurrentSchema() {
454
+ const [rows] = await this.pool.query("SELECT DATABASE() AS DB");
455
+ return rows[0].DB;
456
+ }
457
+ /**
458
+ * Default search scope = the database named in the DSN. DATABASE() returns
459
+ * null when the connection was opened without a database, in which case
460
+ * callers fall back to the full server-wide schema list.
461
+ */
462
+ async getDefaultSchema() {
463
+ if (!this.pool) {
464
+ throw new Error("Not connected to database");
465
+ }
466
+ const [rows] = await this.pool.query("SELECT DATABASE() AS DB");
467
+ return rows[0]?.DB ?? null;
468
+ }
469
+ async executeSQL(sql, options, parameters) {
470
+ if (!this.pool) {
471
+ throw new Error("Not connected to database");
472
+ }
473
+ const conn = await this.pool.getConnection();
474
+ try {
475
+ if (options.readonly) {
476
+ await conn.query("START TRANSACTION READ ONLY");
477
+ }
478
+ let processedSQL = sql;
479
+ if (options.maxRows) {
480
+ const statements = splitSQLStatements(sql, "mysql");
481
+ const processedStatements = statements.map(
482
+ (statement) => SQLRowLimiter.applyMaxRows(statement, options.maxRows)
483
+ );
484
+ processedSQL = processedStatements.join("; ");
485
+ if (sql.trim().endsWith(";")) {
486
+ processedSQL += ";";
487
+ }
488
+ }
489
+ let results;
490
+ if (parameters && parameters.length > 0) {
491
+ try {
492
+ results = await conn.query({ sql: processedSQL, timeout: this.queryTimeoutMs }, parameters);
493
+ } catch (error) {
494
+ console.error(`[MySQL executeSQL] ERROR: ${error.message}`);
495
+ console.error(`[MySQL executeSQL] SQL: ${processedSQL}`);
496
+ console.error(`[MySQL executeSQL] Parameters: ${JSON.stringify(parameters)}`);
497
+ throw error;
498
+ }
499
+ } else {
500
+ results = await conn.query({ sql: processedSQL, timeout: this.queryTimeoutMs });
501
+ }
502
+ const [firstResult] = results;
503
+ const rows = parseQueryResults(firstResult);
504
+ const rowCount = extractAffectedRows(firstResult);
505
+ if (options.readonly) {
506
+ await conn.query("COMMIT");
507
+ }
508
+ return { rows, rowCount };
509
+ } catch (error) {
510
+ if (options.readonly) {
511
+ try {
512
+ await conn.query("ROLLBACK");
513
+ } catch {
514
+ }
515
+ }
516
+ console.error("Error executing query:", error);
517
+ throw error;
518
+ } finally {
519
+ conn.release();
520
+ }
521
+ }
522
+ };
523
+ var mysqlConnector = new MySQLConnector();
524
+ ConnectorRegistry.register(mysqlConnector);
525
+ export {
526
+ MySQLConnector
527
+ };