Compare commits

...

5 Commits

3 changed files with 118 additions and 5 deletions

View File

@@ -24,6 +24,14 @@ const sameEditorPosition = (left: any, right: any): boolean => (
&& 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 text = String(value || '').trim();
if (!text) return '';
@@ -194,6 +202,72 @@ 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) => {
if (transparentThemesRegistered) {
return;
@@ -291,6 +365,7 @@ const MonacoEditor: React.FC<MonacoEditorProps> = ({
const handleMount: OnMount = useCallback((editor, monaco) => {
installOceanBaseOracleNavigationFallback(editor);
patchQueryEditorAiInlineRightArrowFallback(editor, monaco);
installPrintableInputFallback(editor, monaco);
onMount?.(editor, monaco);
}, [onMount]);

View File

@@ -26,6 +26,25 @@ describe('queryResultPagination', () => {
});
});
it('treats query-editor injected LIMIT as a capped page rather than an uncounted total', () => {
const page = createInitialQueryResultPagination({
executedSql: 'SELECT id, name FROM users LIMIT 500',
exportSql: 'SELECT id, name FROM users',
dbType: 'mysql',
returnedRowCount: 500,
fallbackPageSize: 500,
});
expect(page).toMatchObject({
current: 1,
pageSize: 500,
total: 500,
totalKnown: true,
baseSql: 'SELECT id, name FROM users',
exportAllSql: 'SELECT id, name FROM users',
});
});
it('builds the next page SQL with one lookahead row', () => {
expect(buildQueryResultPageSql({
baseSql: 'SELECT id FROM users',

View File

@@ -22,6 +22,14 @@ const normalizePositiveInteger = (value: unknown): number => {
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
};
const normalizeSqlForComparison = (sql: string): string => (
String(sql || '')
.replace(/\s+/g, ' ')
.replace(/;+\s*$/g, '')
.trim()
.toLowerCase()
);
const parseTopLevelLimit = (sql: string): LimitInfo | null => {
const { main } = splitSqlTail(sql);
const limitPos = findTopLevelKeyword(main, 'limit');
@@ -60,6 +68,14 @@ const stripExplicitLimitForExport = (sql: string): string => {
return splitSqlTail(sql).main.trim();
};
const wasLimitAppliedByQueryEditorCap = (executedSql: string, exportSql: string): 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 resolveWrappedBaseSql = (dbType: string, baseSql: string): string => {
const normalizedType = String(dbType || '').trim().toLowerCase();
const base = baseSql.trim();
@@ -146,11 +162,14 @@ export const createInitialQueryResultPagination = (params: {
const exportAllSql = exportSql && getLeadingKeyword(exportSql) === 'select'
? stripExplicitLimitForExport(exportSql)
: stripExplicitLimitForExport(executedSql);
const totalState = resolveQueryResultPaginationTotal({
current,
pageSize,
rowCount: returnedRowCount,
});
const autoLimitCap = current === 1 && wasLimitAppliedByQueryEditorCap(executedSql, exportSql);
const totalState = autoLimitCap
? { total: returnedRowCount, totalKnown: true }
: resolveQueryResultPaginationTotal({
current,
pageSize,
rowCount: returnedRowCount,
});
return {
current,