-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-12-SQL-Formatter-Analyzer.html
More file actions
602 lines (513 loc) · 22.2 KB
/
tool-12-SQL-Formatter-Analyzer.html
File metadata and controls
602 lines (513 loc) · 22.2 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tool 12 - SQL Formatter Analyzer - Developer Toolbox</title>
<link rel="stylesheet" href="assets/core.css">
<style>
.sql-grid {
display: grid;
grid-template-columns: 1fr;
gap: 2rem;
margin-top: 2rem;
min-height: 500px;
}
@media (min-width: 900px) {
.sql-grid {
grid-template-columns: 2fr 1fr;
/* Output gets more space, or equal */
}
}
.panel-container {
display: flex;
flex-direction: column;
gap: 1rem;
height: 100%;
}
.panel-header {
display: flex;
justify-content: space-between;
align-items: center;
}
textarea {
flex: 1;
width: 100%;
height: 350px;
resize: vertical;
padding: 1rem;
background: rgba(0, 0, 0, 0.2);
border: 2px solid var(--border-color);
border-radius: var(--radius-md);
color: var(--text-primary);
font-family: var(--font-mono);
font-size: 0.9rem;
outline: none;
transition: all 0.3s;
line-height: 1.5;
white-space: pre;
}
textarea:focus {
border-color: var(--accent-primary);
box-shadow: 0 0 0 2px rgba(187, 134, 252, 0.2);
}
.btn-controls {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
.options-row {
display: flex;
gap: 1.5rem;
align-items: center;
justify-content: center;
margin: 1rem 0;
padding: 1rem;
background: rgba(0, 0, 0, 0.2);
border-radius: var(--radius-md);
border: 1px solid var(--border-color);
}
.options-row select {
padding: 0.5rem;
background: var(--bg-main);
border: 1px solid var(--border-color);
color: var(--text-primary);
border-radius: var(--radius-sm);
outline: none;
font-family: var(--font-mono);
}
/* Analyzer Styles */
.analyzer-box {
background: rgba(0, 0, 0, 0.3);
border: 1px solid var(--border-color);
border-radius: var(--radius-md);
padding: 1rem;
margin-top: 1rem;
}
.score-display {
display: flex;
align-items: center;
gap: 1rem;
margin-bottom: 1rem;
}
.score-circle {
width: 60px;
height: 60px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-family: var(--font-mono);
font-size: 1.5rem;
font-weight: bold;
border: 4px solid var(--text-secondary);
color: var(--text-primary);
}
.score-low {
border-color: #33d9b2;
color: #33d9b2;
}
.score-med {
border-color: #ffb142;
color: #ffb142;
}
.score-high {
border-color: #ff5252;
color: #ff5252;
}
.extracted-list {
list-style: none;
padding: 0;
margin: 0;
max-height: 200px;
overflow-y: auto;
}
.extracted-list li {
padding: 0.3rem 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
font-family: var(--font-mono);
font-size: 0.85rem;
word-break: break-all;
}
.extracted-list li:last-child {
border-bottom: none;
}
.badge {
display: inline-block;
padding: 0.1rem 0.4rem;
border-radius: var(--radius-sm);
font-size: 0.7rem;
margin-right: 0.5rem;
font-weight: bold;
color: #000;
}
.badge-table {
background: #34ace0;
}
.badge-join {
background: #ffb142;
}
/* 3D background container */
#canvas-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
opacity: 0.4;
pointer-events: none;
}
</style>
</head>
<body class="theme-dark">
<div id="canvas-container"></div>
<main class="main-content">
<div class="container">
<div class="tool-header">
<div>
<a href="index.html" class="back-link">← Back to Toolbox</a>
<h1>12. SQL Formatter Analyzer</h1>
<p style="color: var(--text-secondary); margin-top: 0.5rem;">Beautify messy SQL queries and extract
basic table/complexity metrics.</p>
</div>
<div>
<select id="theme-select" aria-label="Select Theme">
<option value="dark">Dark Theme</option>
<option value="light">Light Theme</option>
<option value="solarized">Solarized</option>
<option value="neon">Neon Glow</option>
<option value="highcontrast">High Contrast</option>
</select>
</div>
</div>
<div class="options-row">
<span style="color: var(--text-secondary);">Dialect:</span>
<select id="dialect-select">
<option value="sql" selected>Standard SQL</option>
<option value="postgresql">PostgreSQL</option>
<option value="mysql">MySQL</option>
<option value="mariadb">MariaDB</option>
<option value="sqlite">SQLite</option>
<option value="tsql">SQL Server (T-SQL)</option>
</select>
<span style="color: var(--text-secondary); margin-left: 1rem;">Keywords:</span>
<select id="case-select">
<option value="upper" selected>UPPERCASE</option>
<option value="lower">lowercase</option>
</select>
</div>
<div class="sql-grid">
<!-- Editor Column -->
<div style="display: flex; flex-direction: column; gap: 2rem;">
<!-- Input Panel -->
<div class="glass-panel panel-container" style="flex:1;">
<div class="panel-header">
<h3>Raw SQL</h3>
<div class="btn-controls">
<button id="btn-clear" class="btn">Clear</button>
<button id="btn-paste" class="btn">Paste</button>
<button id="btn-format" class="btn btn-primary"
style="background:#03DAC6; color:#000;">Format Query</button>
</div>
</div>
<textarea id="sql-input"
placeholder="Paste your raw SQL here...
select id, name, email from users as u inner join orders as o on u.id = o.user_id where o.amount > 100 group by u.id order by amount desc;"></textarea>
</div>
<!-- Output Panel -->
<div class="glass-panel panel-container" style="flex:1;">
<div class="panel-header">
<h3>Formatted SQL</h3>
<div class="btn-controls">
<button id="btn-copy" class="btn btn-primary">Copy</button>
</div>
</div>
<textarea id="sql-output" readonly placeholder="Formatted SQL will appear here..."></textarea>
</div>
</div>
<!-- Analyzer Column -->
<div class="glass-panel panel-container">
<h3>Query Analyzer</h3>
<p class="text-secondary" style="font-size: 0.85rem;">Basic static analysis of the query structure.
</p>
<div class="analyzer-box">
<div class="score-display">
<div class="score-circle" id="complexity-score">0</div>
<div>
<h4 style="margin:0;">Complexity Score</h4>
<span id="complexity-label" class="text-secondary"
style="font-size: 0.85rem;">Simple</span>
</div>
</div>
<p class="text-secondary" style="font-size: 0.8rem; margin: 0;">Score increases based on joins,
subqueries, and conditions.</p>
</div>
<div class="analyzer-box" style="flex:1; display:flex; flex-direction:column;">
<h4 style="margin-top:0;">Detected Tables</h4>
<ul class="extracted-list" id="table-list">
<li class="text-secondary" style="border:none;">No tables detected yet.</li>
</ul>
</div>
</div>
</div>
</div>
</main>
<footer class="global-footer">
<div class="container">
Made with ❤️ by <a href="https://github.com/Aliriyaj007" target="_blank">Aliriyaj007</a>
</div>
</footer>
<!-- Three.js and GSAP for 3D Background -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script>
<!-- SQL Formatter CDN -->
<script src="https://unpkg.com/sql-formatter@15.3.0/dist/sql-formatter.min.js"></script>
<script src="assets/core.js"></script>
<script>
// --- Core Converter Logic ---
const sqlInput = document.getElementById('sql-input');
const sqlOutput = document.getElementById('sql-output');
const dialectSelect = document.getElementById('dialect-select');
const caseSelect = document.getElementById('case-select');
const btnFormat = document.getElementById('btn-format');
const complexityScore = document.getElementById('complexity-score');
const complexityLabel = document.getElementById('complexity-label');
const tableList = document.getElementById('table-list');
function analyzeSQL(sql) {
// Very simplistic regex-based static analysis for demonstration
if (!sql.trim()) {
complexityScore.textContent = '0';
complexityScore.className = 'score-circle';
complexityLabel.textContent = 'Empty';
tableList.innerHTML = '<li class="text-secondary" style="border:none;">No tables detected yet.</li>';
return;
}
const upperSQL = sql.toUpperCase();
// 1. Calculate approximate complexity
let score = 1; // base
// Count JOINs
const joins = (upperSQL.match(/ JOIN /g) || []).length;
score += (joins * 2);
// Count Subqueries (simplistic: SELECT inside parens)
const subqs = (upperSQL.match(/\(\s*SELECT /g) || []).length;
score += (subqs * 3);
// Count conditions
const conditions = (upperSQL.match(/ AND | OR | WHERE | HAVING /g) || []).length;
score += Math.floor(conditions * 0.5);
complexityScore.textContent = score;
let grade = 'score-low';
let label = 'Simple';
if (score > 5 && score <= 10) { grade = 'score-med'; label = 'Moderate'; }
else if (score > 10) { grade = 'score-high'; label = 'Complex'; }
complexityScore.className = `score-circle ${grade}`;
complexityLabel.textContent = label;
// 2. Extract simplistic FROM / JOIN tables
const tables = new Set();
// match `FROM table_name` or `JOIN table_name`
// Simplistic regex, doesn't handle aliasing perfectly or complex schemas, but sufficient for utility visualization
const tableRegex = /(?:FROM|JOIN)\s+([a-zA-Z0-9_\.]+)/gi;
let match;
while ((match = tableRegex.exec(sql)) !== null) {
const parts = match[0].toUpperCase().split(/\s+/);
const type = parts[0]; // FROM or JOIN
const tName = match[1];
tables.add(JSON.stringify({ type, name: tName }));
}
tableList.innerHTML = '';
if (tables.size === 0) {
tableList.innerHTML = '<li class="text-secondary" style="border:none;">No explicit tables found.</li>';
} else {
Array.from(tables).map(t => JSON.parse(t)).forEach(t => {
const li = document.createElement('li');
const badgeClass = t.type === 'JOIN' ? 'badge-join' : 'badge-table';
li.innerHTML = `<span class="badge ${badgeClass}">${t.type}</span> ${t.name}`;
tableList.appendChild(li);
});
}
}
function formatQuery() {
const raw = sqlInput.value;
if (!raw) return;
try {
// Formatting
const formatted = sqlFormatter.format(raw, {
language: dialectSelect.value,
keywordCase: caseSelect.value
});
sqlOutput.value = formatted;
// Analysis
analyzeSQL(raw);
// Trigger 3D Animation (Align DB tracks)
if (window.triggerAnim) window.triggerAnim('format');
} catch (e) {
sqlOutput.value = "Error parsing SQL: \n" + e.message;
}
}
btnFormat.addEventListener('click', formatQuery);
// Wobble DB when typing
sqlInput.addEventListener('input', () => {
if (window.triggerAnim) window.triggerAnim('type');
});
// Setup Buttons
const setupBtn = (id, targetInput, isInput) => {
const btn = document.getElementById(id);
if (!btn) return;
btn.addEventListener('click', async () => {
const originalText = btn.textContent;
if (id.includes('clear')) {
targetInput.value = '';
if (isInput) {
sqlOutput.value = '';
analyzeSQL('');
}
} else if (id.includes('paste')) {
try {
const text = await navigator.clipboard.readText();
targetInput.value = text;
} catch (err) {
console.error('Failed to read clipboard', err);
}
} else if (id.includes('copy')) {
if (!targetInput.value) return;
navigator.clipboard.writeText(targetInput.value);
btn.textContent = 'Copied!';
setTimeout(() => btn.textContent = originalText, 1500);
}
});
};
setupBtn('btn-clear', sqlInput, true);
setupBtn('btn-paste', sqlInput, true);
setupBtn('btn-copy', sqlOutput, false);
// --- 3D Background Setup (Database Cylinders) ---
let scene, camera, renderer, dbGroup;
let cylinders = [];
const init3D = () => {
const container = document.getElementById('canvas-container');
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.z = 20;
camera.position.x = 8; // Offset to right
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
container.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0x03DAC6, 0.8);
dirLight.position.set(5, 10, 5);
scene.add(dirLight);
const dirLight2 = new THREE.DirectionalLight(0xBB86FC, 0.8);
dirLight2.position.set(-5, -5, 5);
scene.add(dirLight2);
dbGroup = new THREE.Group();
// Create Stack of Discs representing a DB
const numDiscs = 6;
const geo = new THREE.CylinderGeometry(2, 2, 0.8, 32);
const mat = new THREE.MeshStandardMaterial({
color: 0x2c3e50,
metalness: 0.6,
roughness: 0.2
});
for (let i = 0; i < numDiscs; i++) {
const mesh = new THREE.Mesh(geo, mat);
// Stack vertically
const yPos = (i * 1.2) - ((numDiscs * 1.2) / 2);
mesh.position.y = yPos;
// Add glowing ring
const ringGeo = new THREE.TorusGeometry(2, 0.05, 16, 100);
const ringMat = new THREE.MeshBasicMaterial({ color: 0x33d9b2, transparent: true, opacity: 0.5 });
const ring = new THREE.Mesh(ringGeo, ringMat);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.4; // top edge
mesh.add(ring);
mesh.userData = { origY: yPos, targetX: 0, targetZ: 0 };
cylinders.push(mesh);
dbGroup.add(mesh);
}
// Tilt the whole DB
dbGroup.rotation.x = 0.2;
dbGroup.rotation.z = -0.1;
scene.add(dbGroup);
let typingTimeout;
window.triggerAnim = (state) => {
if (state === 'type') {
// Wobble cylinders slightly horizontally to indicate messiness
cylinders.forEach((cyl, i) => {
gsap.to(cyl.position, {
x: (Math.random() - 0.5) * 1.5,
z: (Math.random() - 0.5) * 1.5,
duration: 0.5,
ease: "power1.inOut"
});
// Dim rings
cyl.children[0].material.opacity = 0.2;
});
clearTimeout(typingTimeout);
typingTimeout = setTimeout(() => {
window.triggerAnim('idle');
}, 1000);
}
else if (state === 'format') {
// Align perfectly
cylinders.forEach((cyl, i) => {
gsap.to(cyl.position, {
x: 0,
z: 0,
duration: 1,
ease: "back.out(1.5)",
delay: i * 0.1 // Stagger alignment from bottom up
});
// Glow rings
gsap.to(cyl.children[0].material, {
opacity: 0.8,
duration: 0.5,
delay: i * 0.1
});
// Flash color
const prevColor = cyl.material.color.getHex();
cyl.material.color.setHex(0x03DAC6);
setTimeout(() => { cyl.material.color.setHex(prevColor); }, 300);
});
}
else if (state === 'idle') {
// Slowly drift back to center if just wobble faded out
cylinders.forEach((cyl, i) => {
gsap.to(cyl.position, {
x: 0,
z: 0,
duration: 2,
ease: "power2.out"
});
cyl.children[0].material.opacity = 0.5;
});
}
};
const animate = () => {
requestAnimationFrame(animate);
// Idle Spin
dbGroup.rotation.y += 0.005;
dbGroup.position.y = Math.sin(Date.now() * 0.002) * 0.3;
renderer.render(scene, camera);
};
animate();
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
if (window.innerWidth < 900) {
camera.position.x = 0; // Center on mobile
camera.position.z = 25;
} else {
camera.position.x = 8;
camera.position.z = 20;
}
});
window.dispatchEvent(new Event('resize'));
};
if (document.getElementById('canvas-container')) {
init3D();
}
</script>
</body>
</html>