-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.php
More file actions
128 lines (107 loc) · 4.17 KB
/
Copy pathsql.php
File metadata and controls
128 lines (107 loc) · 4.17 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
<?php include('header.php'); ?>
<h1><i class="fas fa-code"></i> SQL Formatter</h1>
<style>
.form-row {
display: flex;
gap: 1rem;
align-items: center;
flex-wrap: wrap;
margin-bottom: 1.5rem;
}
</style>
<div class="tool-container">
<div class="tool-section">
<p>Форматирование SQL запросов с поддержкой различных диалектов</p>
<div class="form-group">
<label for="src">Вставьте «сырое» SQL-выражение:</label>
<textarea id="src" class="form-control" placeholder="SELECT * FROM b_crm_deal WHERE STAGE_ID='WON';"></textarea>
</div>
<div class="form-row">
<button id="run" class="btn">Форматировать</button>
<label>Диалект:
<select id="lang" class="form-select">
<option value="postgresql">PostgreSQL</option>
<option value="mysql">MySQL</option>
<option value="sqlite">SQLite</option>
<option value="sql">Standard SQL</option>
</select>
</label>
<button id="copy" class="btn btn-sm">Скопировать ⧉</button>
</div>
<div class="output-section">
<h4>Результат:</h4>
<textarea id="dst" class="form-control" readonly placeholder="Отформатированный SQL появится здесь..."></textarea>
</div>
</div>
</div>
<!-- Библиотека sql-formatter из CDN -->
<script src="https://cdn.jsdelivr.net/npm/sql-formatter@12.2.2/dist/sql-formatter.min.js"></script>
<script>
const src = document.getElementById('src');
const dst = document.getElementById('dst');
const lang = document.getElementById('lang');
const run = document.getElementById('run');
const copy = document.getElementById('copy');
function formatSQL() {
const sqlText = src.value.trim();
if (!sqlText) {
dst.value = '';
return;
}
try {
const formatted = sqlFormatter.format(sqlText, {
language: lang.value,
tabWidth: 2,
keywordCase: 'upper',
linesBetweenQueries: 2
});
dst.value = formatted;
localStorage.setItem('sqlFormatter:last', sqlText);
// Show success feedback
run.textContent = '✓ Отформатировано';
setTimeout(() => run.textContent = 'Форматировать', 1500);
} catch (error) {
dst.value = `-- Ошибка форматирования SQL --
-- ${error.message}
-- Проверьте синтаксис SQL запроса
${sqlText}`;
// Show error feedback
run.textContent = '⚠ Ошибка';
setTimeout(() => run.textContent = 'Форматировать', 2000);
}
}
run.addEventListener('click', formatSQL);
// Auto-format on language change
lang.addEventListener('change', formatSQL);
// Auto-format with debounce on input
let formatTimeout;
src.addEventListener('input', () => {
clearTimeout(formatTimeout);
formatTimeout = setTimeout(formatSQL, 500);
});
copy.addEventListener('click', async () => {
if (!dst.value.trim()) {
copy.textContent = '⚠ Нет данных';
setTimeout(() => copy.textContent = 'Скопировать ⧉', 1500);
return;
}
try {
await navigator.clipboard.writeText(dst.value);
copy.textContent = '✓ Скопировано';
setTimeout(() => copy.textContent = 'Скопировать ⧉', 1500);
} catch (error) {
copy.textContent = '⚠ Ошибка';
setTimeout(() => copy.textContent = 'Скопировать ⧉', 1500);
}
});
// Load last query from localStorage
if (localStorage.getItem('sqlFormatter:last')) {
src.value = localStorage.getItem('sqlFormatter:last');
formatSQL();
} else {
// Set example SQL
src.value = `SELECT u.id, u.name, u.email, p.title as profile_title FROM users u LEFT JOIN profiles p ON u.id = p.user_id WHERE u.active = 1 AND u.created_at > '2023-01-01' ORDER BY u.created_at DESC LIMIT 100;`;
formatSQL();
}
</script>
<?php include('footer.php'); ?>