-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathsql-pretty-printer.html
216 lines (187 loc) · 4.87 KB
/
sql-pretty-printer.html
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SQL Pretty Printer</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
margin-bottom: 24px;
}
.container {
display: flex;
flex-direction: column;
gap: 20px;
}
.field {
display: flex;
flex-direction: column;
gap: 8px;
}
label {
font-weight: bold;
color: #555;
}
textarea {
width: 100%;
min-height: 200px;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
font-family: monospace;
resize: vertical;
}
textarea:focus {
outline: none;
border-color: #666;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 16px;
margin-bottom: 20px;
}
.control-group {
display: flex;
align-items: center;
gap: 8px;
}
select {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.copy-button {
display: none;
margin-top: 8px;
padding: 8px 16px;
background: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
.copy-button:hover {
background: #0056b3;
}
input[type="number"] {
width: 60px;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
input[type="checkbox"] {
width: 16px;
height: 16px;
}
</style>
</head>
<body>
<h1>SQL Pretty Printer</h1>
<div class="container">
<div class="controls">
<div class="control-group">
<label for="dialect">Dialect:</label>
<select id="dialect">
<option value="sqlite">SQLite</option>
<option value="sql">Standard SQL</option>
<option value="postgresql">PostgreSQL</option>
<option value="mysql">MySQL</option>
</select>
</div>
<div class="control-group">
<label for="tabWidth">Tab width:</label>
<input type="number" id="tabWidth" value="2" min="1" max="8">
</div>
<div class="control-group">
<label for="keywordCase">Keyword case:</label>
<select id="keywordCase">
<option value="preserve">preserve</option>
<option value="upper">UPPER</option>
<option value="lower">lower</option>
</select>
</div>
<div class="control-group">
<label for="indentStyle">Indent style:</label>
<select id="indentStyle">
<option value="standard">Standard</option>
<option value="tabularLeft">Tabular (left)</option>
<option value="tabularRight">Tabular (right)</option>
</select>
</div>
</div>
<div class="field">
<label for="input">Input SQL:</label>
<textarea id="input" spellcheck="false"></textarea>
</div>
<div class="field">
<label for="output">Formatted SQL:</label>
<textarea id="output" readonly spellcheck="false"></textarea>
</div>
<button class="copy-button">Copy to clipboard</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql-formatter/12.2.3/sql-formatter.min.js"></script>
<script type="module">
const inputArea = document.getElementById('input')
const outputArea = document.getElementById('output')
const copyButton = document.querySelector('.copy-button')
const dialectSelect = document.getElementById('dialect')
const tabWidthInput = document.getElementById('tabWidth')
const uppercaseCheckbox = document.getElementById('uppercase')
const keywordCaseSelect = document.getElementById('keywordCase')
const indentStyleSelect = document.getElementById('indentStyle')
function formatSQL(sql) {
if (!sql.trim()) return ''
try {
const options = {
language: dialectSelect.value,
tabWidth: parseInt(tabWidthInput.value, 10),
keywordCase: keywordCaseSelect.value,
indentStyle: indentStyleSelect.value
}
return sqlFormatter.format(sql, options)
} catch (err) {
return `Error formatting SQL: ${err.message}`
}
}
function updateFormatting() {
const formatted = formatSQL(inputArea.value)
outputArea.value = formatted
copyButton.style.display = formatted ? 'block' : 'none'
}
// Add event listeners to all controls
const controls = [
inputArea, dialectSelect, tabWidthInput,
keywordCaseSelect, indentStyleSelect
]
controls.forEach(control => {
control.addEventListener('input', updateFormatting)
control.addEventListener('change', updateFormatting)
})
copyButton.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(outputArea.value)
copyButton.textContent = 'Copied!'
setTimeout(() => {
copyButton.textContent = 'Copy to clipboard'
}, 1500)
} catch (err) {
console.error('Failed to copy:', err)
}
})
</script>
</body>
</html>