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

Method applyMaxRows

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

* Apply maxRows limit to a SELECT query only * * This method is used by PostgreSQL, MySQL, MariaDB, and SQLite connectors which all support * the LIMIT clause syntax. SQL Server uses applyMaxRowsForSQLServer() instead with TOP syntax. * * For parameterized LIMIT clauses (e.g., LIMIT $

(sql: string, maxRows: number | undefined)

Source from the content-addressed store, hash-verified

124 * to enforce max_rows as a hard cap, since the parameter value is not known until runtime.
125 */
126 static applyMaxRows(sql: string, maxRows: number | undefined): string {
127 if (!maxRows || !this.isSelectQuery(sql)) {
128 return sql;
129 }
130
131 // If query has a parameterized LIMIT, wrap it in a subquery with maxRows
132 // This ensures max_rows is respected even when user provides a large parameter value
133 if (this.hasParameterizedLimit(sql)) {
134 // Wrap the query: SELECT * FROM (original_query) AS subq LIMIT max_rows
135 // Note: Subquery wrapping is safe for PostgreSQL, MySQL, MariaDB, and SQLite
136 const trimmed = sql.trim();
137 const hasSemicolon = trimmed.endsWith(';');
138 const sqlWithoutSemicolon = hasSemicolon ? trimmed.slice(0, -1) : trimmed;
139 return `SELECT * FROM (${sqlWithoutSemicolon}) AS subq LIMIT ${maxRows}${hasSemicolon ? ';' : ''}`;
140 }
141
142 // For literal LIMIT values, apply the minimum logic
143 return this.applyLimitToQuery(sql, maxRows);
144 }
145
146 /**
147 * Apply maxRows limit to a SELECT query using SQL Server TOP syntax

Callers 5

executeSQLMethod · 0.80
executeSQLMethod · 0.80
executeSQLMethod · 0.80
executeSQLMethod · 0.80

Calls 3

isSelectQueryMethod · 0.95
hasParameterizedLimitMethod · 0.95
applyLimitToQueryMethod · 0.95

Tested by

no test coverage detected