(tableName: string, schema?: string)
| 352 | } |
| 353 | |
| 354 | async getTableSchema(tableName: string, schema?: string): Promise<TableColumn[]> { |
| 355 | if (!this.pool) { |
| 356 | throw new Error("Not connected to database"); |
| 357 | } |
| 358 | |
| 359 | try { |
| 360 | // In MySQL, schema is synonymous with database |
| 361 | // If no schema is provided, use the current database context via DATABASE() function |
| 362 | // This means tables will be retrieved from whatever database the connection is currently using |
| 363 | const schemaClause = schema ? "WHERE TABLE_SCHEMA = ?" : "WHERE TABLE_SCHEMA = DATABASE()"; |
| 364 | |
| 365 | const queryParams = schema ? [schema, tableName] : [tableName]; |
| 366 | |
| 367 | // Get table columns with comments |
| 368 | const [rows] = (await this.pool.query( |
| 369 | ` |
| 370 | SELECT |
| 371 | COLUMN_NAME as column_name, |
| 372 | DATA_TYPE as data_type, |
| 373 | IS_NULLABLE as is_nullable, |
| 374 | COLUMN_DEFAULT as column_default, |
| 375 | COLUMN_COMMENT as description |
| 376 | FROM INFORMATION_SCHEMA.COLUMNS |
| 377 | ${schemaClause} |
| 378 | AND TABLE_NAME = ? |
| 379 | ORDER BY ORDINAL_POSITION |
| 380 | `, |
| 381 | queryParams |
| 382 | )) as [any[], any]; |
| 383 | |
| 384 | // Normalize empty string comments to null for token-efficient output |
| 385 | return rows.map((row: any) => ({ |
| 386 | ...row, |
| 387 | description: row.description || null, |
| 388 | })); |
| 389 | } catch (error) { |
| 390 | console.error("Error getting table schema:", error); |
| 391 | throw error; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | async getTableComment(tableName: string, schema?: string): Promise<string | null> { |
| 396 | if (!this.pool) { |
nothing calls this directly
no outgoing calls
no test coverage detected