-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_console.js
More file actions
429 lines (368 loc) · 15.5 KB
/
Copy pathsql_console.js
File metadata and controls
429 lines (368 loc) · 15.5 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
/**
* SQL Console Module
* Handles SQL console UI rendering and query execution
*/
export function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/**
* Check if a query is a write operation
*/
function isWriteQuery(sql) {
const trimmed = sql.trim().toUpperCase();
const writeKeywords = [
'INSERT', 'UPDATE', 'DELETE', 'DROP', 'CREATE', 'ALTER', 'REPLACE',
'TRUNCATE', 'ATTACH', 'DETACH', 'VACUUM', 'REINDEX'
];
return writeKeywords.some(keyword => trimmed.startsWith(keyword));
}
/**
* Get all tables and their schema information
*/
function getSchemaInfo(db) {
const tableResult = db.exec("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
if (tableResult.length === 0) return [];
return tableResult[0].values.map(row => {
const tableName = row[0];
const columnResult = db.exec(`PRAGMA table_info("${tableName}")`);
const columns = columnResult[0].values.map(row => ({
name: row[1],
type: row[2],
pk: row[5]
}));
const countResult = db.exec(`SELECT COUNT(*) FROM "${tableName}"`);
return { name: tableName, columns, rowCount: countResult[0].values[0][0] };
});
}
/**
* Execute a SQL query
*/
export function executeQuery(db, sql, consoleState) {
const startTime = performance.now();
const isWrite = isWriteQuery(sql);
// Check if write mode is required
if (isWrite && !consoleState.writeMode) {
return {
success: false,
error: 'Write operations require Write Mode to be enabled',
executionTime: 0
};
}
try {
let result;
if (isWrite) {
// For write operations, use run() and return affected rows
db.run(sql);
const changes = db.getRowsModified();
result = {
success: true,
isWrite: true,
rowsAffected: changes,
executionTime: performance.now() - startTime
};
} else {
// For read operations, use exec() and return results
const queryResult = db.exec(sql);
result = {
success: true,
isWrite: false,
data: queryResult,
executionTime: performance.now() - startTime
};
}
// Add to query history
if (!consoleState.queryHistory.includes(sql)) {
consoleState.queryHistory = [sql, ...consoleState.queryHistory.slice(0, consoleState.maxHistorySize - 1)];
}
return result;
} catch (error) {
return {
success: false,
error: error.message,
executionTime: performance.now() - startTime
};
}
}
/**
* Render the query results as an HTML table
*/
function renderResultsTable(data, rowLimit, offset, append = false) {
if (!data || data.length === 0) {
return '<div style="padding: 20px; text-align: center; color: #666;">No rows returned</div>';
}
const result = data[0];
const columns = result.columns;
const allRows = result.values;
const totalRows = allRows.length;
const rows = allRows.slice(offset, offset + rowLimit);
let html = '';
if (!append) {
// First render - include table header
html += '<div id="resultsTableContainer" style="overflow-x: auto;">';
html += '<table id="resultsTable" style="width: 100%; border-collapse: collapse; margin-top: 10px;">';
// Header
html += '<thead><tr style="background: #f5f5f5; border-bottom: 2px solid #ddd;">';
columns.forEach(col => {
html += `<th style="padding: 10px; text-align: left; font-weight: 600; border-right: 1px solid #ddd;">${escapeHtml(col)}</th>`;
});
html += '</tr></thead>';
html += '<tbody id="resultsTableBody">';
}
// Body rows
rows.forEach((row, index) => {
const actualIndex = offset + index;
const bgColor = actualIndex % 2 === 0 ? '#fff' : '#f9f9f9';
html += `<tr style="background: ${bgColor}; border-bottom: 1px solid #eee;">`;
row.forEach(cell => {
const cellValue = cell === null ? '<em style="color: #999;">NULL</em>' : escapeHtml(String(cell));
html += `<td style="padding: 8px 10px; border-right: 1px solid #eee;">${cellValue}</td>`;
});
html += '</tr>';
});
if (!append) {
html += '</tbody>';
html += '</table>';
html += '</div>';
// Row count and load more button
if (totalRows > 0) {
const showing = Math.min(offset + rowLimit, totalRows);
html += `<div id="rowCountInfo" style="margin-top: 10px; color: #666; font-size: 14px;">`;
html += `Showing ${offset + 1}-${showing} of ${totalRows} rows`;
if (showing < totalRows) {
html += ` <button id="loadMoreBtn" style="margin-left: 10px;">Load more</button>`;
}
html += '</div>';
}
}
return html;
}
/**
* Render a single table in the schema browser
*/
function renderSchemaTable(table) {
const columns = table.columns.map(col => {
const pk = col.pk ? ' <span style="background: #ffc107; color: #000; padding: 1px 4px; border-radius: 2px; font-size: 10px;">PK</span>' : '';
const baseType = col.type.split(' ')[0] || col.type;
return `<div style="margin: 2px 0;"><span class="schema-column" data-table="${escapeHtml(table.name)}" data-column="${escapeHtml(col.name)}" style="cursor: pointer; color: #007bff; text-decoration: underline;">${escapeHtml(col.name)}</span> <span style="color: #666;">${escapeHtml(baseType)}</span>${pk}</div>`;
}).join('');
return `<div style="margin-bottom: 15px;">
<div style="font-weight: 600; color: #333; margin-bottom: 5px;">
<span class="schema-table" data-name="${escapeHtml(table.name)}" style="cursor: pointer; color: #007bff; text-decoration: underline;">${escapeHtml(table.name)}</span>
<span style="color: #666; font-weight: normal; font-size: 12px;">(${table.rowCount} rows)</span>
</div>
<div style="margin-left: 20px; font-size: 13px;">${columns}</div>
</div>`;
}
/**
* Render the schema browser panel
*/
function renderSchemaBrowser(db) {
const schema = getSchemaInfo(db);
const tables = schema.map(table => renderSchemaTable(table)).join('');
return `
<div style="margin-bottom: 20px; border: 1px solid #ddd; border-radius: 4px; background: #f9f9f9;">
<div style="padding: 10px; background: #e9ecef; border-bottom: 1px solid #ddd; font-weight: 600; cursor: pointer;" id="schemaBrowserToggle">
▼ Schema Browser <span style="font-weight: normal; color: #666;">(click table/column to insert)</span>
</div>
<div id="schemaBrowserContent" style="padding: 10px; max-height: 300px; overflow-y: auto;">
${tables}
</div>
</div>
`;
}
/**
* Render query history dropdown
*/
function renderQueryHistory(consoleState) {
if (consoleState.queryHistory.length === 0) return '';
const options = consoleState.queryHistory.map((query, index) => {
const preview = query.length > 80 ? query.substring(0, 80) + '...' : query;
return `<option value="${index}">${escapeHtml(preview)}</option>`;
}).join('');
return `
<div style="margin-bottom: 10px;">
<label for="queryHistory">Query History:</label>
<select id="queryHistory" style="width: calc(100% - 220px);">
<option value="">-- Select from history --</option>
${options}
</select>
<button id="clearHistoryBtn" class="button-secondary">Clear History</button>
</div>
`;
}
/**
* Render query input section
*/
function renderQueryInput(consoleState) {
return `
<div style="display: flex; gap: 20px; margin-bottom: 10px;">
<div style="display: flex; flex-direction: column; gap: 10px; width: 200px;">
<label for="sqlQuery">Enter SQL Query:</label>
<label style="font-weight: normal;">
<input type="checkbox" id="writeModeToggle" ${consoleState.writeMode ? 'checked' : ''}>
Enable Write Mode
</label>
<button id="executeQueryBtn">Execute Query</button>
</div>
<textarea id="sqlQuery" rows="6" style="flex: 1; font-family: monospace; padding: 8px; border: 1px solid #ddd; border-radius: 4px; resize: vertical;">${escapeHtml(consoleState.currentQuery)}</textarea>
</div>
`;
}
/**
* Render write mode warning
*/
function renderWriteModeWarning(consoleState) {
if (!consoleState.writeMode) return '';
return `
<div style="background: #fff3cd; border: 1px solid #ffc107; color: #856404; padding: 10px; margin-bottom: 20px; border-radius: 4px;">
⚠️ <strong>Write Mode Enabled:</strong> Changes will be saved to the in-memory database. Use the download buttons to export your changes.
</div>
`;
}
/**
* Handle history selection - execute read queries automatically
*/
function handleHistorySelection(db, consoleState, query) {
const sqlQuery = document.getElementById('sqlQuery');
sqlQuery.value = query;
consoleState.currentQuery = query;
// Auto-execute read queries, but not write queries
if (!isWriteQuery(query)) {
consoleState.currentOffset = 0;
const result = executeQuery(db, query, consoleState);
updateQueryHistory(db, consoleState);
displayQueryResult(result, db, consoleState);
}
}
/**
* Update just the query history dropdown
*/
function updateQueryHistory(db, consoleState) {
document.getElementById('queryHistoryContainer').innerHTML = renderQueryHistory(consoleState);
attachHistoryListeners(db, consoleState);
}
/**
* Attach history dropdown event listeners
*/
function attachHistoryListeners(db, consoleState) {
const historySelect = document.getElementById('queryHistory');
historySelect?.addEventListener('change', (e) => {
if (e.target.value) {
handleHistorySelection(db, consoleState, consoleState.queryHistory[e.target.value]);
}
});
document.getElementById('clearHistoryBtn')?.addEventListener('click', () => {
consoleState.queryHistory = [];
updateQueryHistory(db, consoleState);
});
}
/**
* Render the SQL console UI
*/
export function renderConsole(db, consoleState) {
const formSection = document.getElementById('form-section');
const html = `
<div id="sqlConsoleContainer">
${renderSchemaBrowser(db)}
<div id="queryHistoryContainer">${renderQueryHistory(consoleState)}</div>
${renderQueryInput(consoleState)}
<div id="writeModeWarningContainer">${renderWriteModeWarning(consoleState)}</div>
<div id="queryResults"></div>
</div>
`;
formSection.innerHTML = html;
formSection.classList.add('active');
// Attach event listeners
attachConsoleEventListeners(db, consoleState);
}
/**
* Attach event listeners to console elements
*/
function attachConsoleEventListeners(db, consoleState) {
const sqlQuery = document.getElementById('sqlQuery');
// Execute query button
document.getElementById('executeQueryBtn').addEventListener('click', () => {
const query = sqlQuery.value.trim();
if (!query) return;
consoleState.currentQuery = query;
consoleState.currentOffset = 0;
const result = executeQuery(db, query, consoleState);
updateQueryHistory(db, consoleState);
displayQueryResult(result, db, consoleState);
});
// Write mode toggle
document.getElementById('writeModeToggle').addEventListener('change', (e) => {
consoleState.writeMode = e.target.checked;
document.getElementById('writeModeWarningContainer').innerHTML = renderWriteModeWarning(consoleState);
});
// Attach history listeners on initial render
attachHistoryListeners(db, consoleState);
// Schema browser toggle
document.getElementById('schemaBrowserToggle').addEventListener('click', (e) => {
const content = document.getElementById('schemaBrowserContent');
const isCollapsed = content.style.display === 'none';
content.style.display = isCollapsed ? 'block' : 'none';
e.target.textContent = e.target.textContent.replace(isCollapsed ? '▶' : '▼', isCollapsed ? '▼' : '▶');
});
// Schema item clicks (insert into query)
document.querySelectorAll('.schema-table').forEach(el => {
el.addEventListener('click', () => insertAtCursor(sqlQuery, el.dataset.name));
});
document.querySelectorAll('.schema-column').forEach(el => {
el.addEventListener('click', () => insertAtCursor(sqlQuery, el.dataset.column));
});
// Save current query on change
sqlQuery.addEventListener('input', (e) => {
consoleState.currentQuery = e.target.value;
});
}
/**
* Insert text at cursor position in textarea
*/
function insertAtCursor(textarea, text) {
const { selectionStart: start, selectionEnd: end, value } = textarea;
textarea.value = value.substring(0, start) + text + value.substring(end);
textarea.selectionStart = textarea.selectionEnd = start + text.length;
textarea.focus();
}
/**
* Display query result in the results area
*/
function displayQueryResult(result, db, consoleState) {
const resultsDiv = document.getElementById('queryResults');
if (!result.success) {
resultsDiv.innerHTML = `<div style="background: #f8d7da; border: 1px solid #f5c6cb; color: #721c24; padding: 15px; border-radius: 4px; margin-top: 10px;"><strong>Error:</strong> ${escapeHtml(result.error)}</div>`;
return;
}
const executionTimeHtml = `<div style="color: #666; font-size: 13px; margin: 10px 0 5px;">Execution time: ${result.executionTime.toFixed(2)}ms</div>`;
if (result.isWrite) {
resultsDiv.innerHTML = `${executionTimeHtml}<div style="background: #d4edda; border: 1px solid #c3e6cb; color: #155724; padding: 15px; border-radius: 4px;"><strong>Success:</strong> Query executed. Rows affected: ${result.rowsAffected}</div>`;
} else {
consoleState.cachedResult = result.data;
resultsDiv.innerHTML = executionTimeHtml + renderResultsTable(result.data, consoleState.rowLimit, consoleState.currentOffset);
attachLoadMoreListener(consoleState);
}
}
/**
* Attach load more button event listener
*/
function attachLoadMoreListener(consoleState) {
const loadMoreBtn = document.getElementById('loadMoreBtn');
if (!loadMoreBtn) return;
loadMoreBtn.addEventListener('click', function handleLoadMore() {
consoleState.currentOffset += consoleState.rowLimit;
const { cachedResult, rowLimit, currentOffset } = consoleState;
const totalRows = cachedResult[0].values.length;
const showing = Math.min(currentOffset + rowLimit, totalRows);
document.getElementById('resultsTableBody').insertAdjacentHTML('beforeend',
renderResultsTable(cachedResult, rowLimit, currentOffset, true));
const infoDiv = document.getElementById('rowCountInfo');
infoDiv.innerHTML = `Showing 1-${showing} of ${totalRows} rows`;
if (showing < totalRows) {
infoDiv.insertAdjacentHTML('beforeend', ' <button id="loadMoreBtn" style="margin-left: 10px;">Load more</button>');
document.getElementById('loadMoreBtn').addEventListener('click', handleLoadMore);
}
});
}