MCPcopy Index your code
hub / github.com/bytebase/dbhub / applyLimitToQuery

Method applyLimitToQuery

src/utils/sql-row-limiter.ts:71–87  ·  view source on GitHub ↗

* Add or modify LIMIT clause in a SQL statement

(sql: string, maxRows: number)

Source from the content-addressed store, hash-verified

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)

Callers 1

applyMaxRowsMethod · 0.95

Calls 1

extractLimitValueMethod · 0.95

Tested by

no test coverage detected