* Add or modify LIMIT clause in a SQL statement
(sql: string, maxRows: number)
| 69 | * Add or modify LIMIT clause in a SQL statement |
| 70 | */ |
| 71 | static applyLimitToQuery(sql: string, maxRows: number): string { |
| 72 | const existingLimit = this.extractLimitValue(sql); |
| 73 | |
| 74 | if (existingLimit !== null) { |
| 75 | // Use the minimum of existing limit and maxRows |
| 76 | const effectiveLimit = Math.min(existingLimit, maxRows); |
| 77 | return sql.replace(/\blimit\s+\d+/i, `LIMIT ${effectiveLimit}`); |
| 78 | } else { |
| 79 | // Add LIMIT clause to the end of the query |
| 80 | // Handle semicolon at the end |
| 81 | const trimmed = sql.trim(); |
| 82 | const hasSemicolon = trimmed.endsWith(';'); |
| 83 | const sqlWithoutSemicolon = hasSemicolon ? trimmed.slice(0, -1) : trimmed; |
| 84 | |
| 85 | return `${sqlWithoutSemicolon} LIMIT ${maxRows}${hasSemicolon ? ';' : ''}`; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Add or modify TOP clause in a SQL statement (SQL Server) |
no test coverage detected