🐛 fix(query-editor): 修复 Oracle 查询自动限行语法

- 将 Oracle/Dameng 查询编辑器自动限行从 FETCH FIRST 改为 ROWNUM 兼容写法

- 识别 ROWNUM 自动封顶 SQL,避免结果分页继续基于截断 SQL 查询

- 补充自动限行与结果分页回归测试,覆盖 Oracle/Dameng 分支
This commit is contained in:
Syngnat
2026-07-09 17:37:09 +08:00
parent 92aedde121
commit 52592ffd6c
4 changed files with 62 additions and 17 deletions

View File

@@ -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', () => {

View File

@@ -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');

View File

@@ -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',

View File

@@ -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({