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