aboutsummaryrefslogtreecommitdiff
path: root/src/tools/sql-prettify/sql-prettify.vue
blob: ee4abdc60754c644c46d5816ad93021b1b580a70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<script setup lang="ts">
import { type FormatFnOptions, format as formatSQL } from 'sql-formatter';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
import { useStyleStore } from '@/stores/style.store';

const inputElement = ref<HTMLElement>();
const styleStore = useStyleStore();
const config = reactive<Partial<FormatFnOptions>>({
  keywordCase: 'upper',
  useTabs: false,
  language: 'sql',
  indentStyle: 'standard',
  tabulateAlias: true,
});

const rawSQL = ref('select field1,field2,field3 from my_table where my_condition;');
const prettySQL = computed(() => formatSQL(rawSQL.value, config));
</script>

<template>
  <div style="flex: 0 0 100%">
    <div mx-auto style="max-width: 600px" flex gap-2 :class="{ 'flex-col': styleStore.isSmallScreen }">
      <n-form-item label="Dialect" label-width="500" flex-1>
        <n-select
          v-model:value="config.language"
          :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' },
          ]"
        />
      </n-form-item>
      <n-form-item label="Keyword case" flex-1>
        <n-select
          v-model:value="config.keywordCase"
          :options="[
            { label: 'UPPERCASE', value: 'upper' },
            { label: 'lowercase', value: 'lower' },
            { label: 'Preserve', value: 'preserve' },
          ]"
        />
      </n-form-item>
      <n-form-item label="Indent style" flex-1>
        <n-select
          v-model:value="config.indentStyle"
          :options="[
            { label: 'Standard', value: 'standard' },
            { label: 'Tabular left', value: 'tabularLeft' },
            { label: 'Tabular right', value: 'tabularRight' },
          ]"
        />
      </n-form-item>
    </div>
  </div>

  <n-form-item label="Your SQL query">
    <n-input
      ref="inputElement"
      v-model:value="rawSQL"
      placeholder="Put your SQL query here..."
      type="textarea"
      rows="20"
      autocomplete="off"
      autocorrect="off"
      autocapitalize="off"
      spellcheck="false"
    />
  </n-form-item>
  <n-form-item label="Prettify version of your query">
    <TextareaCopyable :value="prettySQL" language="sql" :follow-height-of="inputElement" />
  </n-form-item>
</template>

<style lang="less" scoped>
.result-card {
  position: relative;
  .copy-button {
    position: absolute;
    top: 10px;
    right: 10px;
  }
}
</style>