feat: SQL Formatter enhancements

This commit is contained in:
ShareVB committed 2025-03-23 22:42:03 +01:00
1 parent 21d35801ac
commit 3e5ad755f5
5 files changed
+583 -372

No files matched your search

+6
View File
@@ -254,16 +254,22 @@ declare module '@vue/runtime-core' {
NanoMemo: typeof import('./src/tools/nano-memo/nano-memo.vue')['default']
'NanoMemo.content': typeof import('./src/tools/nano-memo/nano-memo.content.md')['default']
NavbarButtons: typeof import('./src/components/NavbarButtons.vue')['default']
NCheckbox: typeof import('naive-ui')['NCheckbox']
NCode: typeof import('naive-ui')['NCode']
NCollapseTransition: typeof import('naive-ui')['NCollapseTransition']
NConfigProvider: typeof import('naive-ui')['NConfigProvider']
NEllipsis: typeof import('naive-ui')['NEllipsis']
NFormItem: typeof import('naive-ui')['NFormItem']
NginxFormatter: typeof import('./src/tools/nginx-formatter/nginx-formatter.vue')['default']
NH1: typeof import('naive-ui')['NH1']
NH3: typeof import('naive-ui')['NH3']
NIcon: typeof import('naive-ui')['NIcon']
NInputNumber: typeof import('naive-ui')['NInputNumber']
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSpace: typeof import('naive-ui')['NSpace']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OcrImage: typeof import('./src/tools/ocr-image/ocr-image.vue')['default']
Option43Generator: typeof import('./src/tools/option43-generator/option43-generator.vue')['default']
+1 -1
View File
@@ -231,7 +231,7 @@
"silly-password-generator": "^1.0.28",
"slashes": "^3.0.12",
"spdx-satisfies": "^5.0.1",
"sql-formatter": "^15.4.10",
"sql-formatter": "^15.5.1",
"sshpk": "^1.18.0",
"stringify-object": "^5.0.0",
"svg2png-wasm": "^1.4.1",
+487 -333
View File
File diff suppressed because it is too large. Load diff
+89 -37
View File
@@ -1,55 +1,93 @@
<script setup lang="ts">
import { type FormatOptionsWithLanguage, format as formatSQL } from 'sql-formatter';
import { type IndentStyle, type KeywordCase, format as formatSQL } from 'sql-formatter';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
import { useStyleStore } from '@/stores/style.store';
import { useQueryParamOrStorage } from '@/composable/queryParams';
const keywordCase = useQueryParamOrStorage({ name: 'keywordCase', storageName: 'sql-prt:kw', defaultValue: 'upper' });
const dataTypeCase = useQueryParamOrStorage({ name: 'typeCase', storageName: 'sql-prt:dt', defaultValue: 'upper' });
const functionCase = useQueryParamOrStorage({ name: 'functionCase', storageName: 'sql-prt:ft', defaultValue: 'upper' });
const useTabs = useQueryParamOrStorage({ name: 'tabs', storageName: 'sql-prt:tab', defaultValue: false });
const language = useQueryParamOrStorage({ name: 'lang', storageName: 'sql-prt:l', defaultValue: 'sql' });
const indentStyle = useQueryParamOrStorage({ name: 'indent', storageName: 'sql-prt:ind', defaultValue: 'standard' });
const expressionWidth = useQueryParamOrStorage({ name: 'exprWidth', storageName: 'sql-prt:w', defaultValue: 50 });
const inputElement = ref<HTMLElement>();
const styleStore = useStyleStore();
const config = reactive<FormatOptionsWithLanguage>({
keywordCase: 'upper',
useTabs: false,
language: 'sql',
indentStyle: 'standard',
});
const caseOptions = [
{ label: 'UPPERCASE', value: 'upper' },
{ label: 'lowercase', value: 'lower' },
{ label: 'Preserve', value: 'preserve' },
];
const rawSQL = ref('select field1,field2,field3 from my_table where my_condition;');
const prettySQL = computed(() => formatSQL(rawSQL.value, config));
const prettySQL = computed(() => {
try {
return ({
prettyQuery: formatSQL(rawSQL.value, {
keywordCase: keywordCase.value as KeywordCase,
dataTypeCase: dataTypeCase.value as KeywordCase,
functionCase: functionCase.value as KeywordCase,
useTabs: useTabs.value,
language: language.value as never,
indentStyle: indentStyle.value as IndentStyle,
expressionWidth: expressionWidth.value,
}),
error: '',
});
}
catch (e: any) {
return { prettyQuery: rawSQL.value, error: e.toString() };
}
});
const sqlDialects = [
{ value: 'sql', label: 'Standard SQL' },
{ value: 'bigquery', label: 'GCP BigQuery' },
{ value: 'db2', label: 'IBM DB2' },
{ value: 'db2i', label: 'IBM DB2i (experimental)' },
{ value: 'hive', label: 'Apache Hive' },
{ value: 'mariadb', label: 'MariaDB' },
{ value: 'mysql', label: 'MySQL' },
{ value: 'tidb', label: 'TiDB' },
{ value: 'n1ql', label: 'Couchbase N1QL' },
{ value: 'plsql', label: 'Oracle PL/SQL' },
{ value: 'postgresql', label: 'PostgreSQL' },
{ value: 'redshift', label: 'Amazon Redshift' },
{ value: 'singlestoredb', label: 'SingleStoreDB' },
{ value: 'snowflake', label: 'Snowflake' },
{ value: 'spark', label: 'Spark' },
{ value: 'sqlite', label: 'SQLite' },
{ value: 'transactsql', label: 'SQL Server Transact-SQL' },
{ value: 'trino', label: 'Trino/Presto' },
];
</script>
<template>
<div style="flex: 0 0 100%">
<div style="max-width: 600px" :class="{ 'flex-col': styleStore.isSmallScreen }" mx-auto mb-5 flex gap-2>
<n-space justify="center" mb-3>
<c-select
v-model:value="config.language"
v-model:value="language"
flex-1
label="Dialect"
:options="[
{ label: 'GCP BigQuery', value: 'bigquery' },
{ label: 'IBM DB2', value: 'db2' },
{ label: 'Apache Hive', value: 'hive' },
{ label: 'MariaDB', value: 'mariadb' },
{ label: 'MySQL', value: 'mysql' },
{ label: 'Couchbase N1QL', value: 'n1ql' },
{ label: 'Oracle PL/SQL', value: 'plsql' },
{ label: 'PostgreSQL', value: 'postgresql' },
{ label: 'Amazon Redshift', value: 'redshift' },
{ label: 'Spark', value: 'spark' },
{ label: 'Standard SQL', value: 'sql' },
{ label: 'sqlite', value: 'sqlite' },
{ label: 'SQL Server Transact-SQL', value: 'tsql' },
]"
:options="sqlDialects"
/>
<c-select
v-model:value="config.keywordCase" label="Keyword case"
v-model:value="keywordCase" label="Keyword case"
flex-1
:options="[
{ label: 'UPPERCASE', value: 'upper' },
{ label: 'lowercase', value: 'lower' },
{ label: 'Preserve', value: 'preserve' },
]"
:options="caseOptions"
/>
<c-select
v-model:value="config.indentStyle" label="Indent style"
v-model:value="dataTypeCase" label="Datatype case"
flex-1
:options="caseOptions"
/>
<c-select
v-model:value="functionCase" label="Function case"
flex-1
:options="caseOptions"
/>
<c-select
v-model:value="indentStyle" label="Indent style"
flex-1
:options="[
{ label: 'Standard', value: 'standard' },
@@ -57,7 +95,16 @@ const prettySQL = computed(() => formatSQL(rawSQL.value, config));
{ label: 'Tabular right', value: 'tabularRight' },
]"
/>
</div>
</n-space>
<n-space justify="center">
<n-form-item label="Use tabs" label-placement="left">
<n-checkbox v-model:value="useTabs" mr-2 />
</n-form-item>
<n-form-item label="Expressions width" label-placement="left">
<n-input-number v-model:value="expressionWidth" :min="0" size="small" />
</n-form-item>
</n-space>
</div>
<n-form-item label="Your SQL query">
@@ -74,9 +121,14 @@ const prettySQL = computed(() => formatSQL(rawSQL.value, config));
monospace
/>
</n-form-item>
<n-form-item label="Prettify version of your query">
<TextareaCopyable :value="prettySQL" language="sql" :follow-height-of="inputElement" />
<n-form-item v-if="!prettySQL.error" label="Prettify version of your query">
<TextareaCopyable :value="prettySQL.prettyQuery" language="sql" :follow-height-of="inputElement" />
</n-form-item>
<c-alert v-if="prettySQL.error" title="Parsing error">
{{ prettySQL.error }}
</c-alert>
</template>
<style lang="less" scoped>
@@ -13,7 +13,6 @@ test.describe('Tool - Text to Unicode', () => {
await page.getByTestId('text-to-unicode-input').fill('"it-tools" 文字');
const unicode = await page.getByTestId('text-to-unicode-output').inputValue();
// eslint-disable-next-line unicorn/escape-case
expect(unicode).toEqual(String.raw`"it-tools" &#25991;&#23383;`);
});