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

View File

@@ -1,6 +1,7 @@
import _ from 'lodash';
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';
export default function newQuery({
@@ -9,6 +10,7 @@ export default function newQuery({
title = undefined,
initialData = undefined,
multiTabIndex = undefined,
fixCurrentConnection = false,
...props
} = {}) {
const currentDb = getCurrentDatabase();
@@ -17,6 +19,16 @@ export default function newQuery({
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(
{
title: title || 'Query #',

View File

@@ -68,6 +68,13 @@
testEnabled: () => getCurrentEditor() != null,
onClick: () => getCurrentEditor().toggleAutoExecute(),
});
registerCommand({
id: 'query.toggleFixedConnection',
category: 'Query',
name: 'Toggle fixed connection',
testEnabled: () => getCurrentEditor() != null,
onClick: () => getCurrentEditor().toggleFixedConnection(),
});
registerCommand({
id: 'query.beginTransaction',
category: 'Query',
@@ -121,7 +128,7 @@
import VerticalSplitter from '../elements/VerticalSplitter.svelte';
import SqlEditor from '../query/SqlEditor.svelte';
import useEditorData from '../query/useEditorData';
import { currentEditorWrapEnabled, extensions } from '../stores';
import { currentEditorWrapEnabled, extensions, getCurrentDatabase } from '../stores';
import applyScriptTemplate from '../utility/applyScriptTemplate';
import { changeTab, markTabUnsaved, sleep } from '../utility/common';
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) {
if (isProApp()) {
if (event.code == 'Space' && event.shiftKey && event.ctrlKey && !isAiAssistantVisible) {
@@ -636,6 +661,7 @@
{ command: 'query.executeCurrent' },
{ command: 'query.kill' },
{ command: 'query.toggleAutoExecute' },
{ command: 'query.toggleFixedConnection' },
{ divider: true },
{ command: 'query.toggleComment' },
{ command: 'query.formatCode' },

View File

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