SYNC: SQL fixed database WIP

This commit is contained in:
SPRINX0\prochazka
2025-06-23 13:10:18 +02:00
committed by Diflow
parent d004e6e86c
commit a648f1ee67
4 changed files with 47 additions and 4 deletions

View File

@@ -644,6 +644,7 @@ export function parseNumberSafe(value) {
const frontMatterRe = /^--\ >>>[ \t\r]*\n(.*)\n-- <<<[ \t\r]*\n/s; const frontMatterRe = /^--\ >>>[ \t\r]*\n(.*)\n-- <<<[ \t\r]*\n/s;
export function getSqlFrontMatter(text: string, yamlModule) { export function getSqlFrontMatter(text: string, yamlModule) {
if (!text || !_isString(text)) return null;
const match = text.match(frontMatterRe); const match = text.match(frontMatterRe);
if (!match) return null; if (!match) return null;
const yamlContentMapped = match[1].replace(/^--[ ]?/gm, ''); const yamlContentMapped = match[1].replace(/^--[ ]?/gm, '');
@@ -651,6 +652,7 @@ export function getSqlFrontMatter(text: string, yamlModule) {
} }
export function removeSqlFrontMatter(text: string) { export function removeSqlFrontMatter(text: string) {
if (!text || !_isString(text)) return null;
return text.replace(frontMatterRe, ''); return text.replace(frontMatterRe, '');
} }
@@ -673,5 +675,5 @@ export function setSqlFrontMatter(text: string, data: { [key: string]: any }, ya
.map(line => '-- ' + line) .map(line => '-- ' + line)
.join('\n'); .join('\n');
const frontMatterContent = `-- >>>\n${yamlContentMapped}\n-- <<<\n`; const frontMatterContent = `-- >>>\n${yamlContentMapped}\n-- <<<\n`;
return frontMatterContent + textClean; return frontMatterContent + (textClean || '');
} }

View File

@@ -1,6 +1,7 @@
import _ from 'lodash'; import _ from 'lodash';
import { getCurrentDatabase } from '../stores'; import { getCurrentDatabase } from '../stores';
import { getConnectionLabel } from 'dbgate-tools'; import { getConnectionLabel, getSqlFrontMatter, setSqlFrontMatter } from 'dbgate-tools';
import yaml from 'js-yaml';
import openNewTab from '../utility/openNewTab'; import openNewTab from '../utility/openNewTab';
export default function newQuery({ export default function newQuery({
@@ -9,6 +10,7 @@ export default function newQuery({
title = undefined, title = undefined,
initialData = undefined, initialData = undefined,
multiTabIndex = undefined, multiTabIndex = undefined,
fixCurrentConnection = false,
...props ...props
} = {}) { } = {}) {
const currentDb = getCurrentDatabase(); const currentDb = getCurrentDatabase();
@@ -17,6 +19,16 @@ export default function newQuery({
const tooltip = `${getConnectionLabel(connection)}\n${database}`; const tooltip = `${getConnectionLabel(connection)}\n${database}`;
if (fixCurrentConnection && !_.isEmpty(connection)) {
const frontMatter = getSqlFrontMatter(initialData, yaml);
const newFrontMatter = {
...frontMatter,
connectionId: connection._id,
databaseName: database,
};
initialData = setSqlFrontMatter(initialData, newFrontMatter, yaml);
}
openNewTab( openNewTab(
{ {
title: title || 'Query #', title: title || 'Query #',

View File

@@ -68,6 +68,13 @@
testEnabled: () => getCurrentEditor() != null, testEnabled: () => getCurrentEditor() != null,
onClick: () => getCurrentEditor().toggleAutoExecute(), onClick: () => getCurrentEditor().toggleAutoExecute(),
}); });
registerCommand({
id: 'query.toggleFixedConnection',
category: 'Query',
name: 'Toggle fixed connection',
testEnabled: () => getCurrentEditor() != null,
onClick: () => getCurrentEditor().toggleFixedConnection(),
});
registerCommand({ registerCommand({
id: 'query.beginTransaction', id: 'query.beginTransaction',
category: 'Query', category: 'Query',
@@ -121,7 +128,7 @@
import VerticalSplitter from '../elements/VerticalSplitter.svelte'; import VerticalSplitter from '../elements/VerticalSplitter.svelte';
import SqlEditor from '../query/SqlEditor.svelte'; import SqlEditor from '../query/SqlEditor.svelte';
import useEditorData from '../query/useEditorData'; import useEditorData from '../query/useEditorData';
import { currentEditorWrapEnabled, extensions } from '../stores'; import { currentEditorWrapEnabled, extensions, getCurrentDatabase } from '../stores';
import applyScriptTemplate from '../utility/applyScriptTemplate'; import applyScriptTemplate from '../utility/applyScriptTemplate';
import { changeTab, markTabUnsaved, sleep } from '../utility/common'; import { changeTab, markTabUnsaved, sleep } from '../utility/common';
import { getDatabaseInfo, useConnectionInfo } from '../utility/metadataLoaders'; import { getDatabaseInfo, useConnectionInfo } from '../utility/metadataLoaders';
@@ -607,6 +614,24 @@
); );
} }
export function toggleFixedConnection() {
const frontMatter = getSqlFrontMatter($editorValue, yaml);
const currentDatabase = getCurrentDatabase();
setEditorData(
setSqlFrontMatter(
$editorValue,
frontMatter?.connectionId &&
frontMatter?.connectionId == currentDatabase?.connection?._id &&
frontMatter?.databaseName == currentDatabase?.name
? { ...frontMatter, connectionId: undefined, databaseName: undefined }
: currentDatabase?.connection?._id
? { ...frontMatter, connectionId: currentDatabase.connection._id, databaseName: currentDatabase.name }
: { ...frontMatter, connectionId: undefined, databaseName: undefined },
yaml
)
);
}
async function handleKeyDown(event) { async function handleKeyDown(event) {
if (isProApp()) { if (isProApp()) {
if (event.code == 'Space' && event.shiftKey && event.ctrlKey && !isAiAssistantVisible) { if (event.code == 'Space' && event.shiftKey && event.ctrlKey && !isAiAssistantVisible) {
@@ -636,6 +661,7 @@
{ command: 'query.executeCurrent' }, { command: 'query.executeCurrent' },
{ command: 'query.kill' }, { command: 'query.kill' },
{ command: 'query.toggleAutoExecute' }, { command: 'query.toggleAutoExecute' },
{ command: 'query.toggleFixedConnection' },
{ divider: true }, { divider: true },
{ command: 'query.toggleComment' }, { command: 'query.toggleComment' },
{ command: 'query.formatCode' }, { command: 'query.formatCode' },

View File

@@ -40,7 +40,7 @@
import runCommand from '../commands/runCommand'; import runCommand from '../commands/runCommand';
import SaveFileModal from '../modals/SaveFileModal.svelte'; import SaveFileModal from '../modals/SaveFileModal.svelte';
import newQuery from '../query/newQuery'; import newQuery from '../query/newQuery';
let filter = ''; let filter = '';
let domSqlObjectList = null; let domSqlObjectList = null;
@@ -130,6 +130,9 @@
savedFile, savedFile,
savedCloudFolderId, savedCloudFolderId,
savedCloudContentId, savedCloudContentId,
fixCurrentConnection: true,
title: savedFile,
}); });
}, },
}); });