(query: string)
| 485 | } |
| 486 | |
| 487 | export function getSingleTableName(query: string): string | null { |
| 488 | try { |
| 489 | // Normalize query by removing extra spaces and converting to lowercase |
| 490 | const normalizedQuery = query.replace(/\s+/g, " ").trim().toLowerCase(); |
| 491 | |
| 492 | // Match the table names after "from" keyword |
| 493 | const fromMatch = normalizedQuery.match( |
| 494 | /from\s+([^\s,;]+(?:\s*,\s*[^\s,;]+)*)/i |
| 495 | ); |
| 496 | const joinMatches = normalizedQuery.match(/join\s+([^\s,;]+)/gi); |
| 497 | |
| 498 | // If there are JOINs, more than one table is referenced |
| 499 | if (joinMatches && joinMatches.length > 0) { |
| 500 | return null; |
| 501 | } |
| 502 | |
| 503 | // Check if a single table is present |
| 504 | if (fromMatch) { |
| 505 | const tableName = fromMatch[1]; |
| 506 | |
| 507 | // Ensure no additional tables are mentioned |
| 508 | const additionalTablesMatch = tableName.match(/,\s*[^\s,;]+/); |
| 509 | if (additionalTablesMatch) { |
| 510 | return null; |
| 511 | } |
| 512 | |
| 513 | return tableName; |
| 514 | } |
| 515 | |
| 516 | // No table found |
| 517 | return null; |
| 518 | } catch (e) { |
| 519 | return null; |
| 520 | } |
| 521 | } |
no test coverage detected