diff --git a/frontend/src/utils/queryAutoLimit.test.ts b/frontend/src/utils/queryAutoLimit.test.ts index 957c0092..89731f5b 100644 --- a/frontend/src/utils/queryAutoLimit.test.ts +++ b/frontend/src/utils/queryAutoLimit.test.ts @@ -40,9 +40,9 @@ describe('applyQueryAutoLimit', () => { ['dameng'], ['dm'], ['dm8'], - ])('adds FETCH FIRST limit for %s connections', (dbType) => { + ])('adds ROWNUM limit for %s connections', (dbType) => { expect(applyQueryAutoLimit('SELECT * FROM MYCIMLED.EDC_LOG', dbType, 500).sql) - .toBe('SELECT * FROM MYCIMLED.EDC_LOG FETCH FIRST 500 ROWS ONLY'); + .toBe('SELECT * FROM (SELECT * FROM MYCIMLED.EDC_LOG) WHERE ROWNUM <= 500'); }); it.each([ @@ -61,8 +61,8 @@ describe('applyQueryAutoLimit', () => { }); it.each([ - ['oracle', 'SELECT * FROM users FETCH FIRST 500 ROWS ONLY'], - ['dm8', 'SELECT * FROM users FETCH FIRST 500 ROWS ONLY'], + ['oracle', 'SELECT * FROM (SELECT * FROM users) WHERE ROWNUM <= 500'], + ['dm8', 'SELECT * FROM (SELECT * FROM users) WHERE ROWNUM <= 500'], ['mssql', 'SELECT TOP 500 * FROM users'], ['postgresql', 'SELECT * FROM users LIMIT 500'], ['gauss-db', 'SELECT * FROM users LIMIT 500'], @@ -74,19 +74,19 @@ describe('applyQueryAutoLimit', () => { .toBe(expected); }); - it('keeps trailing semicolon and comments after injected Oracle limit', () => { + it('keeps trailing semicolon and comments after injected Oracle ROWNUM limit', () => { expect(applyQueryAutoLimit('SELECT * FROM MYCIMLED.EDC_LOG; -- preview', 'oracle', 500).sql) - .toBe('SELECT * FROM MYCIMLED.EDC_LOG FETCH FIRST 500 ROWS ONLY; -- preview'); + .toBe('SELECT * FROM (SELECT * FROM MYCIMLED.EDC_LOG) WHERE ROWNUM <= 500; -- preview'); }); - it('uses Oracle FETCH FIRST limit for simple table queries', () => { + it('uses Oracle ROWNUM limit for simple table queries', () => { expect(applyQueryAutoLimit('select 1 from xxx', 'oracle', 500).sql) - .toBe('select 1 from xxx FETCH FIRST 500 ROWS ONLY'); + .toBe('SELECT * FROM (select 1 from xxx) WHERE ROWNUM <= 500'); }); - it('keeps ORDER BY semantics with Oracle FETCH FIRST', () => { + it('keeps ORDER BY semantics with Oracle ROWNUM wrapping', () => { expect(applyQueryAutoLimit('SELECT * FROM users ORDER BY created_at DESC', 'oracle', 100).sql) - .toBe('SELECT * FROM users ORDER BY created_at DESC FETCH FIRST 100 ROWS ONLY'); + .toBe('SELECT * FROM (SELECT * FROM users ORDER BY created_at DESC) WHERE ROWNUM <= 100'); }); it('does not add another generic limit when SQL already limits rows', () => { diff --git a/frontend/src/utils/queryAutoLimit.ts b/frontend/src/utils/queryAutoLimit.ts index 053bea63..ff5bf468 100644 --- a/frontend/src/utils/queryAutoLimit.ts +++ b/frontend/src/utils/queryAutoLimit.ts @@ -1,4 +1,5 @@ import { resolveSqlDialect } from './sqlDialect'; +import { buildPaginatedSelectSQL } from './sql'; const isWS = (ch: string) => ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r'; const isWord = (ch: string) => /[A-Za-z0-9_]/.test(ch); @@ -322,7 +323,7 @@ export const applyQueryAutoLimit = ( if (offsetPos >= 0 && (fromPos < 0 || offsetPos > fromPos)) return { sql, applied: false, maxRows }; const forPos = findTopLevelKeyword(main, 'for'); if (forPos >= 0 && (fromPos < 0 || forPos > fromPos)) return { sql, applied: false, maxRows }; - return { sql: `${main.trimEnd()} FETCH FIRST ${maxRows} ROWS ONLY${tail}`, applied: true, maxRows }; + return { sql: `${buildPaginatedSelectSQL(normalizedType, main, '', maxRows, 0)}${tail}`, applied: true, maxRows }; } const offsetPos = findTopLevelKeyword(main, 'offset'); diff --git a/frontend/src/utils/queryResultPagination.test.ts b/frontend/src/utils/queryResultPagination.test.ts index cff1e8f2..551015c6 100644 --- a/frontend/src/utils/queryResultPagination.test.ts +++ b/frontend/src/utils/queryResultPagination.test.ts @@ -45,6 +45,25 @@ describe('queryResultPagination', () => { }); }); + it('treats query-editor injected Oracle ROWNUM wrapper as a capped page', () => { + const page = createInitialQueryResultPagination({ + executedSql: 'SELECT * FROM (SELECT id, name FROM users ORDER BY created_at DESC) WHERE ROWNUM <= 500', + exportSql: 'SELECT id, name FROM users ORDER BY created_at DESC', + dbType: 'oracle', + returnedRowCount: 500, + fallbackPageSize: 500, + }); + + expect(page).toMatchObject({ + current: 1, + pageSize: 500, + total: 500, + totalKnown: true, + baseSql: 'SELECT id, name FROM users ORDER BY created_at DESC', + exportAllSql: 'SELECT id, name FROM users ORDER BY created_at DESC', + }); + }); + it('builds the next page SQL with one lookahead row', () => { expect(buildQueryResultPageSql({ baseSql: 'SELECT id FROM users', diff --git a/frontend/src/utils/queryResultPagination.ts b/frontend/src/utils/queryResultPagination.ts index 2a3ead09..0b0be434 100644 --- a/frontend/src/utils/queryResultPagination.ts +++ b/frontend/src/utils/queryResultPagination.ts @@ -68,12 +68,29 @@ const stripExplicitLimitForExport = (sql: string): string => { return splitSqlTail(sql).main.trim(); }; -const wasLimitAppliedByQueryEditorCap = (executedSql: string, exportSql: string): boolean => { +const wasLimitAppliedByQueryEditorCap = ( + executedSql: string, + exportSql: string, + dbType: string, + driver: string, + fallbackPageSize: number, +): boolean => { const executed = String(executedSql || '').trim(); const exportable = String(exportSql || '').trim(); if (!executed || !exportable) return false; if (normalizeSqlForComparison(executed) === normalizeSqlForComparison(exportable)) return false; - return normalizeSqlForComparison(stripExplicitLimitForExport(executed)) === normalizeSqlForComparison(stripExplicitLimitForExport(exportable)); + + const exportBaseSql = stripExplicitLimitForExport(exportable); + if (normalizeSqlForComparison(stripExplicitLimitForExport(executed)) === normalizeSqlForComparison(exportBaseSql)) { + return true; + } + + const pageSize = normalizePositiveInteger(fallbackPageSize); + if (pageSize <= 0 || getLeadingKeyword(exportBaseSql) !== 'select') return false; + + const dialect = resolveSqlDialect(dbType || 'mysql', driver || ''); + const queryEditorCappedSql = buildPaginatedSelectSQL(dialect, exportBaseSql, '', pageSize, 0); + return normalizeSqlForComparison(executed) === normalizeSqlForComparison(queryEditorCappedSql); }; const resolveWrappedBaseSql = (dbType: string, baseSql: string): string => { @@ -155,14 +172,22 @@ export const createInitialQueryResultPagination = (params: { : 1; if (current <= 1 && returnedRowCount < pageSize) return undefined; - const baseSql = explicitLimit?.baseSql || mainSql; - if (!baseSql) return undefined; - const exportSql = String(params.exportSql || '').trim(); const exportAllSql = exportSql && getLeadingKeyword(exportSql) === 'select' ? stripExplicitLimitForExport(exportSql) : stripExplicitLimitForExport(executedSql); - const autoLimitCap = current === 1 && wasLimitAppliedByQueryEditorCap(executedSql, exportSql); + const autoLimitCap = current === 1 && wasLimitAppliedByQueryEditorCap( + executedSql, + exportSql, + params.dbType, + params.driver || '', + fallbackPageSize, + ); + const baseSql = autoLimitCap && exportAllSql + ? exportAllSql + : explicitLimit?.baseSql || mainSql; + if (!baseSql) return undefined; + const totalState = autoLimitCap ? { total: returnedRowCount, totalKnown: true } : resolveQueryResultPaginationTotal({