sql generate aliases automatically #1122

This commit is contained in:
SPRINX0\prochazka
2025-06-11 17:02:38 +02:00
parent a540b38151
commit 76d9a511b8
3 changed files with 34 additions and 11 deletions

View File

@@ -12,6 +12,7 @@
import useEffect from '../utility/useEffect'; import useEffect from '../utility/useEffect';
import { getContext } from 'svelte'; import { getContext } from 'svelte';
import { mountCodeCompletion } from './codeCompletion'; import { mountCodeCompletion } from './codeCompletion';
import { getCurrentSettings } from '../stores';
export let engine = null; export let engine = null;
export let conid = null; export let conid = null;
export let database = null; export let database = null;
@@ -44,6 +45,7 @@
database, database,
editor, editor,
getText: () => domEditor.getCodeCompletionCommandText(), getText: () => domEditor.getCodeCompletionCommandText(),
getSettings: () => getCurrentSettings(),
}); });
} }
return () => {}; return () => {};

View File

@@ -26,7 +26,13 @@ const COMMON_KEYWORDS = [
'go', 'go',
]; ];
function createTableLikeList(schemaList, dbinfo, schemaCondition) { function createTableShortcut(name) {
return (_.upperFirst(_.camelCase(name)).match(/[A-Z]/g) || []).join('').toLowerCase();
}
function createTableLikeList(schemaList, dbinfo, schemaCondition, getSettings) {
const settings = getSettings();
const useAliases = settings?.['sqlEditor.showTableAliasesInCodeCompletion'];
return [ return [
...(schemaList?.map(x => ({ ...(schemaList?.map(x => ({
name: x.schemaName, name: x.schemaName,
@@ -37,22 +43,22 @@ function createTableLikeList(schemaList, dbinfo, schemaCondition) {
})) || []), })) || []),
...dbinfo.tables.filter(schemaCondition).map(x => ({ ...dbinfo.tables.filter(schemaCondition).map(x => ({
name: x.pureName, name: x.pureName,
value: x.pureName, value: useAliases ? `${x.pureName} ${createTableShortcut(x.pureName)}` : x.pureName,
caption: x.pureName, caption: useAliases ? `${x.pureName} ${createTableShortcut(x.pureName)}` : x.pureName,
meta: 'table', meta: 'table',
score: 1000, score: 1000,
})), })),
...dbinfo.views.filter(schemaCondition).map(x => ({ ...dbinfo.views.filter(schemaCondition).map(x => ({
name: x.pureName, name: x.pureName,
value: x.pureName, value: useAliases ? `${x.pureName} ${createTableShortcut(x.pureName)}` : x.pureName,
caption: x.pureName, caption: useAliases ? `${x.pureName} ${createTableShortcut(x.pureName)}` : x.pureName,
meta: 'view', meta: 'view',
score: 1000, score: 1000,
})), })),
...dbinfo.matviews.filter(schemaCondition).map(x => ({ ...dbinfo.matviews.filter(schemaCondition).map(x => ({
name: x.pureName, name: x.pureName,
value: x.pureName, value: useAliases ? `${x.pureName} ${createTableShortcut(x.pureName)}` : x.pureName,
caption: x.pureName, caption: useAliases ? `${x.pureName} ${createTableShortcut(x.pureName)}` : x.pureName,
meta: 'matview', meta: 'matview',
score: 1000, score: 1000,
})), })),
@@ -73,7 +79,7 @@ function createTableLikeList(schemaList, dbinfo, schemaCondition) {
]; ];
} }
export function mountCodeCompletion({ conid, database, editor, getText }) { export function mountCodeCompletion({ conid, database, editor, getText, getSettings }) {
setCompleters([]); setCompleters([]);
addCompleter({ addCompleter({
getCompletions: async function (editor, session, pos, prefix, callback) { getCompletions: async function (editor, session, pos, prefix, callback) {
@@ -155,7 +161,7 @@ export function mountCodeCompletion({ conid, database, editor, getText }) {
} else { } else {
const schema = (schemaList || []).find(x => x.schemaName == colMatch[1]); const schema = (schemaList || []).find(x => x.schemaName == colMatch[1]);
if (schema) { if (schema) {
list = createTableLikeList(schemaList, dbinfo, x => x.schemaName == schema.schemaName); list = createTableLikeList(schemaList, dbinfo, x => x.schemaName == schema.schemaName, getSettings);
} }
} }
} else { } else {
@@ -173,7 +179,12 @@ export function mountCodeCompletion({ conid, database, editor, getText }) {
} else { } else {
list = [ list = [
...(onlyTables ? [] : list), ...(onlyTables ? [] : list),
...createTableLikeList(schemaList, dbinfo, x => !defaultSchema || defaultSchema == x.schemaName), ...createTableLikeList(
schemaList,
dbinfo,
x => !defaultSchema || defaultSchema == x.schemaName,
getSettings
),
...(onlyTables ...(onlyTables
? [] ? []

View File

@@ -192,7 +192,11 @@ ORDER BY
]} ]}
/> />
<FormCheckboxField name="dataGrid.showAllColumnsWhenSearch" label="Show all columns when searching" defaultValue={false} /> <FormCheckboxField
name="dataGrid.showAllColumnsWhenSearch"
label="Show all columns when searching"
defaultValue={false}
/>
<div class="heading">SQL editor</div> <div class="heading">SQL editor</div>
@@ -235,6 +239,12 @@ ORDER BY
label="Return only N rows from query" label="Return only N rows from query"
placeholder="(No rows limit)" placeholder="(No rows limit)"
/> />
<FormCheckboxField
name="sqlEditor.showTableAliasesInCodeCompletion"
label="Show table aliases in code completion"
defaultValue={false}
/>
</svelte:fragment> </svelte:fragment>
<svelte:fragment slot="2"> <svelte:fragment slot="2">
<div class="heading">Connection</div> <div class="heading">Connection</div>