Compare commits

..

2 Commits

Author SHA1 Message Date
Syngnat
5712b39cf9 test(oceanbase): expect Oracle auto limit FETCH FIRST 2026-07-08 11:52:06 +08:00
Syngnat
07dc1436af perf(oceanbase): use FETCH FIRST for Oracle auto limit 2026-07-08 11:51:05 +08:00
3 changed files with 13 additions and 83 deletions

View File

@@ -24,14 +24,6 @@ const sameEditorPosition = (left: any, right: any): boolean => (
&& Number(left?.column) === Number(right?.column) && Number(left?.column) === Number(right?.column)
); );
const isSelectionEmpty = (selection: any): boolean => (
!selection
|| (
Number(selection.startLineNumber) === Number(selection.endLineNumber)
&& Number(selection.startColumn) === Number(selection.endColumn)
)
);
const stripSqlIdentifierQuotes = (value: string): string => { const stripSqlIdentifierQuotes = (value: string): string => {
const text = String(value || '').trim(); const text = String(value || '').trim();
if (!text) return ''; if (!text) return '';
@@ -202,72 +194,6 @@ const patchQueryEditorAiInlineRightArrowFallback = (editor: any, monaco: any) =>
}; };
}; };
const installPrintableInputFallback = (editor: any, monaco: any) => {
const editorDomNode = editor?.getDomNode?.();
if (!editorDomNode || editor.__gonaviPrintableInputFallbackInstalled) {
return;
}
const input = editorDomNode.querySelector?.('textarea.inputarea, .inputarea textarea, textarea') as HTMLTextAreaElement | null;
if (!(input instanceof HTMLTextAreaElement)) {
return;
}
Object.defineProperty(editor, '__gonaviPrintableInputFallbackInstalled', {
value: true,
configurable: true,
});
const isReadOnly = (): boolean => {
try {
const optionId = monaco?.editor?.EditorOption?.readOnly;
return optionId !== undefined ? editor.getOption?.(optionId) === true : false;
} catch {
return false;
}
};
const handleBeforeInput = (event: InputEvent) => {
const text = String(event.data || '');
if (
event.defaultPrevented
|| event.isComposing
|| event.inputType !== 'insertText'
|| !text
|| text.length > 8
|| isReadOnly()
) {
return;
}
const selectionBefore = editor.getSelection?.();
if (!isSelectionEmpty(selectionBefore)) {
return;
}
const beforeValue = String(editor.getValue?.() ?? '');
const beforePosition = editor.getPosition?.();
window.setTimeout(() => {
const domNode = editor.getDomNode?.();
if (!(domNode instanceof HTMLElement) || !domNode.isConnected || isReadOnly()) {
return;
}
if (document.activeElement && !domNode.contains(document.activeElement)) {
return;
}
const afterValue = String(editor.getValue?.() ?? '');
const afterPosition = editor.getPosition?.();
if (afterValue !== beforeValue || !sameEditorPosition(beforePosition, afterPosition)) {
return;
}
editor.trigger?.('gonavi-printable-input-fallback', 'type', { text });
}, 16);
};
input.addEventListener('beforeinput', handleBeforeInput);
editor.onDidDispose?.(() => {
input.removeEventListener('beforeinput', handleBeforeInput);
});
};
export const registerGonaviMonacoThemes: BeforeMount = (monaco) => { export const registerGonaviMonacoThemes: BeforeMount = (monaco) => {
if (transparentThemesRegistered) { if (transparentThemesRegistered) {
return; return;
@@ -365,7 +291,6 @@ const MonacoEditor: React.FC<MonacoEditorProps> = ({
const handleMount: OnMount = useCallback((editor, monaco) => { const handleMount: OnMount = useCallback((editor, monaco) => {
installOceanBaseOracleNavigationFallback(editor); installOceanBaseOracleNavigationFallback(editor);
patchQueryEditorAiInlineRightArrowFallback(editor, monaco); patchQueryEditorAiInlineRightArrowFallback(editor, monaco);
installPrintableInputFallback(editor, monaco);
onMount?.(editor, monaco); onMount?.(editor, monaco);
}, [onMount]); }, [onMount]);

View File

@@ -40,9 +40,9 @@ describe('applyQueryAutoLimit', () => {
['dameng'], ['dameng'],
['dm'], ['dm'],
['dm8'], ['dm8'],
])('adds ROWNUM limit for %s connections', (dbType) => { ])('adds FETCH FIRST limit for %s connections', (dbType) => {
expect(applyQueryAutoLimit('SELECT * FROM MYCIMLED.EDC_LOG', dbType, 500).sql) expect(applyQueryAutoLimit('SELECT * FROM MYCIMLED.EDC_LOG', dbType, 500).sql)
.toBe('SELECT * FROM (SELECT * FROM MYCIMLED.EDC_LOG) WHERE ROWNUM <= 500'); .toBe('SELECT * FROM MYCIMLED.EDC_LOG FETCH FIRST 500 ROWS ONLY');
}); });
it.each([ it.each([
@@ -61,8 +61,8 @@ describe('applyQueryAutoLimit', () => {
}); });
it.each([ it.each([
['oracle', 'SELECT * FROM (SELECT * FROM users) WHERE ROWNUM <= 500'], ['oracle', 'SELECT * FROM users FETCH FIRST 500 ROWS ONLY'],
['dm8', 'SELECT * FROM (SELECT * FROM users) WHERE ROWNUM <= 500'], ['dm8', 'SELECT * FROM users FETCH FIRST 500 ROWS ONLY'],
['mssql', 'SELECT TOP 500 * FROM users'], ['mssql', 'SELECT TOP 500 * FROM users'],
['postgresql', 'SELECT * FROM users LIMIT 500'], ['postgresql', 'SELECT * FROM users LIMIT 500'],
['gauss-db', 'SELECT * FROM users LIMIT 500'], ['gauss-db', 'SELECT * FROM users LIMIT 500'],
@@ -76,12 +76,17 @@ describe('applyQueryAutoLimit', () => {
it('keeps trailing semicolon and comments after injected Oracle limit', () => { it('keeps trailing semicolon and comments after injected Oracle limit', () => {
expect(applyQueryAutoLimit('SELECT * FROM MYCIMLED.EDC_LOG; -- preview', 'oracle', 500).sql) expect(applyQueryAutoLimit('SELECT * FROM MYCIMLED.EDC_LOG; -- preview', 'oracle', 500).sql)
.toBe('SELECT * FROM (SELECT * FROM MYCIMLED.EDC_LOG) WHERE ROWNUM <= 500; -- preview'); .toBe('SELECT * FROM MYCIMLED.EDC_LOG FETCH FIRST 500 ROWS ONLY; -- preview');
}); });
it('uses Oracle 11g compatible ROWNUM limit for simple table queries', () => { it('uses Oracle FETCH FIRST limit for simple table queries', () => {
expect(applyQueryAutoLimit('select 1 from xxx', 'oracle', 500).sql) expect(applyQueryAutoLimit('select 1 from xxx', 'oracle', 500).sql)
.toBe('SELECT * FROM (select 1 from xxx) WHERE ROWNUM <= 500'); .toBe('select 1 from xxx FETCH FIRST 500 ROWS ONLY');
});
it('keeps ORDER BY semantics with Oracle FETCH FIRST', () => {
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');
}); });
it('does not add another generic limit when SQL already limits rows', () => { it('does not add another generic limit when SQL already limits rows', () => {

View File

@@ -322,7 +322,7 @@ export const applyQueryAutoLimit = (
if (offsetPos >= 0 && (fromPos < 0 || offsetPos > fromPos)) return { sql, applied: false, maxRows }; if (offsetPos >= 0 && (fromPos < 0 || offsetPos > fromPos)) return { sql, applied: false, maxRows };
const forPos = findTopLevelKeyword(main, 'for'); const forPos = findTopLevelKeyword(main, 'for');
if (forPos >= 0 && (fromPos < 0 || forPos > fromPos)) return { sql, applied: false, maxRows }; if (forPos >= 0 && (fromPos < 0 || forPos > fromPos)) return { sql, applied: false, maxRows };
return { sql: `SELECT * FROM (${main.trimEnd()}) WHERE ROWNUM <= ${maxRows}${tail}`, applied: true, maxRows }; return { sql: `${main.trimEnd()} FETCH FIRST ${maxRows} ROWS ONLY${tail}`, applied: true, maxRows };
} }
const offsetPos = findTopLevelKeyword(main, 'offset'); const offsetPos = findTopLevelKeyword(main, 'offset');