-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathapsw-query.html
398 lines (337 loc) · 8.88 KB
/
apsw-query.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>APSW SQLite query explainer</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 20px;
background: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
h1 {
margin-top: 0;
color: #333;
}
.query-area {
margin-bottom: 20px;
}
textarea {
width: 100%;
min-height: 150px;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
font-family: monospace;
resize: vertical;
}
button {
background: #0066cc;
color: white;
border: none;
padding: 12px 24px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background: #0052a3;
}
button:disabled {
background: #cccccc;
cursor: not-allowed;
}
.results {
margin-top: 20px;
border: 1px solid #eee;
border-radius: 4px;
padding: 20px;
}
.error {
color: #cc0000;
padding: 12px;
background: #fff0f0;
border-radius: 4px;
margin-top: 20px;
}
.loading {
display: none;
margin-top: 20px;
color: #666;
}
pre {
white-space: pre-wrap;
word-wrap: break-word;
background: #f8f8f8;
padding: 12px;
border-radius: 4px;
overflow-x: auto;
}
details {
margin-bottom: 20px;
padding: 10px;
background: #f8f8f8;
border-radius: 4px;
}
summary {
cursor: pointer;
padding: 8px 0;
color: #444;
}
#initialSql {
margin-top: 10px;
}
.param-container {
margin-top: 10px;
}
.param-row {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
}
.param-input {
flex: 1;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.icon-button {
background: none;
border: none;
padding: 8px;
cursor: pointer;
color: #666;
font-size: 20px;
line-height: 1;
display: flex;
align-items: center;
justify-content: center;
}
.icon-button:hover {
background: none;
color: #000;
}
.param-label {
min-width: 30px;
font-size: 14px;
color: #666;
}
</style>
</head>
<body>
<div class="container">
<h1>APSW SQLite query explainer</h1>
<details>
<summary>Initial SQL</summary>
<textarea id="initialSql" placeholder="Enter setup SQL to run before main query (e.g. CREATE TABLE statements)..."></textarea>
</details>
<div class="query-area">
<textarea id="sqlQuery" placeholder="Enter your SQL query here...">SELECT * FROM sqlite_master;</textarea>
<div id="params" class="param-container">
<div class="param-row" data-param="1">
<span class="param-label">p1</span>
<input type="text" class="param-input" placeholder="Parameter value">
<button class="icon-button add-param" title="Add parameter">+</button>
</div>
</div>
<button id="executeBtn">Execute query</button>
</div>
<div id="loading" class="loading">
Executing query...
</div>
<div id="results" class="results">
<pre id="output">Results will appear here</pre>
</div>
</div>
<script type="module">
const sqlQuery = document.getElementById('sqlQuery')
const initialSql = document.getElementById('initialSql')
const executeBtn = document.getElementById('executeBtn')
const loading = document.getElementById('loading')
const output = document.getElementById('output')
const paramsContainer = document.getElementById('params')
function addParam() {
const rows = paramsContainer.querySelectorAll('.param-row')
const newNum = rows.length + 1
const newRow = createParamRow(newNum)
paramsContainer.appendChild(newRow)
updateParamNumbers()
}
// Add click event listeners to all add parameter buttons
document.querySelectorAll('.add-param').forEach(button => {
button.addEventListener('click', addParam)
})
function createParamRow(num) {
const row = document.createElement('div')
row.className = 'param-row'
row.dataset.param = num
const label = document.createElement('span')
label.className = 'param-label'
label.textContent = `p${num}`
const input = document.createElement('input')
input.type = 'text'
input.className = 'param-input'
input.placeholder = 'Parameter value'
const addBtn = document.createElement('button')
addBtn.className = 'icon-button add-param'
addBtn.title = 'Add parameter'
addBtn.textContent = '+'
addBtn.addEventListener('click', addParam)
if (num > 1) {
const removeBtn = document.createElement('button')
removeBtn.className = 'icon-button remove-param'
removeBtn.title = 'Remove parameter'
removeBtn.textContent = '−'
removeBtn.onclick = () => {
row.remove()
updateParamNumbers()
}
row.appendChild(label)
row.appendChild(input)
row.appendChild(removeBtn)
row.appendChild(addBtn)
} else {
row.appendChild(label)
row.appendChild(input)
row.appendChild(addBtn)
}
return row
}
function updateParamNumbers() {
const rows = paramsContainer.querySelectorAll('.param-row')
rows.forEach((row, index) => {
const num = index + 1
row.dataset.param = num
row.querySelector('.param-label').textContent = `p${num}`
})
}
async function initPyodide() {
loading.style.display = 'block'
executeBtn.disabled = true
try {
let pyodide = await loadPyodide()
await pyodide.loadPackage('micropip')
const micropip = pyodide.pyimport('micropip')
await micropip.install('apsw')
return pyodide
} catch (err) {
showError('Failed to initialize Python environment: ' + err.message)
throw err
}
}
function showError(message) {
output.innerHTML = `<div class="error">${message}</div>`
}
function getParameters() {
const params = []
const rows = paramsContainer.querySelectorAll('.param-row')
rows.forEach(row => {
const value = row.querySelector('.param-input').value.trim()
if (value) {
params.push(value)
}
})
return params
}
async function executeQuery(pyodide, sql, setupSql, params) {
try {
const result = await pyodide.runPythonAsync(`
import apsw
import apsw.ext
import json
db = apsw.Connection(":memory:")
# Execute setup SQL if provided
if """${setupSql}""".strip():
db.cursor().execute("""${setupSql}""")
# Convert parameters to a list
params = json.loads("""${JSON.stringify(params)}""")
from dataclasses import is_dataclass
import inspect
def to_serializable(obj):
"""
Recursively convert an object to a JSON-serializable format.
Handles nested dictionaries, lists, and dataclasses.
"""
# Handle None
if obj is None:
return None
# Handle basic types that are already JSON serializable
if isinstance(obj, (str, int, float, bool)):
return obj
# Handle dataclass objects
if is_dataclass(obj):
return {
field.name: to_serializable(getattr(obj, field.name))
for field in getattr(obj, '__dataclass_fields__', {}).values()
}
# Handle dictionaries
if isinstance(obj, dict):
return {
str(key): to_serializable(value)
for key, value in obj.items()
}
# Handle lists and tuples
if isinstance(obj, (list, tuple)):
return [to_serializable(item) for item in obj]
# Handle objects with __dict__ attribute
if hasattr(obj, '__dict__'):
return to_serializable(vars(obj))
# For any other type, convert to string
try:
json.dumps(obj)
return obj
except (TypeError, OverflowError):
return str(obj)
details = apsw.ext.query_info(db, """${sql}""", params, actions=True, expanded_sql=True, explain=True, explain_query_plan=True)
# Convert to serializable format and dump to JSON
result = to_serializable(details)
json.dumps(result)
`)
output.textContent = JSON.stringify(JSON.parse(result), null, 2)
} catch (err) {
showError('Query execution failed: ' + err.message)
throw err
} finally {
loading.style.display = 'none'
executeBtn.disabled = false
}
}
let pyodideInstance = null
executeBtn.addEventListener('click', async () => {
const sql = sqlQuery.value.trim()
const setupSql = initialSql.value.trim()
const params = getParameters()
if (!sql) {
showError('Please enter a SQL query')
return
}
try {
if (!pyodideInstance) {
pyodideInstance = await initPyodide()
}
await executeQuery(pyodideInstance, sql, setupSql, params)
} catch (err) {
console.error('Execution failed:', err)
}
})
</script>
<script src="https://cdn.jsdelivr.net/pyodide/v0.27.2/full/pyodide.js"></script>
</body>
</html>