| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| class QueryEditor {
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| constructor() {
|
| this.textarea = document.getElementById('queryTextarea');
|
| this.executeBtn = document.getElementById('executeQueryBtn');
|
| this.resultsContainer = document.getElementById('resultsContent');
|
| this.executionTimeDisplay = document.getElementById('executionTime');
|
|
|
| this.initializeEditor();
|
| }
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| initializeEditor() {
|
| if (!this.textarea) return;
|
|
|
|
|
| this.textarea.addEventListener('input', () => {
|
| this.updateLineNumbers();
|
| });
|
|
|
|
|
| this.textarea.addEventListener('keydown', (e) => {
|
| if (e.key === 'Tab') {
|
| e.preventDefault();
|
| const start = this.textarea.selectionStart;
|
| const end = this.textarea.selectionEnd;
|
|
|
|
|
| this.textarea.value = this.textarea.value.substring(0, start) +
|
| '\t' + this.textarea.value.substring(end);
|
|
|
|
|
| this.textarea.selectionStart = this.textarea.selectionEnd = start + 1;
|
| }
|
| });
|
|
|
|
|
| this.textarea.addEventListener('keydown', (e) => {
|
|
|
| if (e.ctrlKey && e.key === 'Enter') {
|
| e.preventDefault();
|
| this.executeQuery();
|
| }
|
|
|
| if (e.ctrlKey && e.key === 'a') {
|
|
|
| return;
|
| }
|
|
|
| if (e.ctrlKey && e.key === 'z') {
|
|
|
| return;
|
| }
|
|
|
| if (e.ctrlKey && e.key === 'y') {
|
|
|
| return;
|
| }
|
|
|
| if (e.ctrlKey && e.key === 'f') {
|
|
|
| return;
|
| }
|
|
|
| if (e.ctrlKey && e.key === '/') {
|
| e.preventDefault();
|
| this.toggleComment();
|
| }
|
|
|
| if (e.shiftKey && e.key === 'Tab') {
|
| e.preventDefault();
|
| this.decreaseIndentation();
|
| }
|
| });
|
| }
|
|
|
| updateLineNumbers() {
|
|
|
| const lines = this.textarea.value.split('\n').length;
|
|
|
| console.log(`Lines: ${lines}`);
|
| }
|
|
|
| async executeQuery() {
|
|
|
| if (window.dbManager && typeof window.dbManager.executeQuery === 'function') {
|
| console.log('Executing query via dbManager...');
|
| window.dbManager.executeQuery();
|
| } else {
|
| console.error('dbManager not found or executeQuery method not available');
|
| }
|
| }
|
|
|
| renderResults(data, executionTime) {
|
| if (!this.resultsContainer) return;
|
|
|
|
|
| if (this.executionTimeDisplay) {
|
| this.executionTimeDisplay.textContent = `Executed in ${executionTime}ms`;
|
| }
|
|
|
| if (data.type === 'select' && data.rows && data.rows.length > 0) {
|
|
|
| const table = UIHelpers.createTable(data.columns, data.rows);
|
|
|
|
|
| const resultsInfo = document.createElement('div');
|
| resultsInfo.className = 'results-info';
|
| resultsInfo.innerHTML = `
|
| <span>Rows: ${data.rowCount}</span>
|
| <span>Execution time: ${executionTime}ms</span>
|
| `;
|
|
|
| this.resultsContainer.innerHTML = '';
|
| this.resultsContainer.appendChild(resultsInfo);
|
| this.resultsContainer.appendChild(table);
|
| } else {
|
|
|
| this.resultsContainer.innerHTML = `
|
| <div class="results-message">
|
| <p>${data.message || `Query executed successfully. ${data.rowCount || 0} rows affected.`}</p>
|
| <p class="text-muted">Execution time: ${executionTime}ms</p>
|
| </div>
|
| `;
|
| }
|
| }
|
|
|
| renderError(errorMessage) {
|
| if (!this.resultsContainer) return;
|
|
|
| this.resultsContainer.innerHTML = `
|
| <div class="results-error">
|
| <h4>Query Error</h4>
|
| <pre>${errorMessage}</pre>
|
| </div>
|
| `;
|
| }
|
|
|
| clearQuery() {
|
| if (this.textarea) {
|
| this.textarea.value = '';
|
| }
|
| }
|
|
|
| setQuery(query) {
|
| if (this.textarea) {
|
| this.textarea.value = query;
|
| }
|
| }
|
|
|
| getQuery() {
|
| return this.textarea ? this.textarea.value : '';
|
| }
|
|
|
| toggleComment() {
|
| const start = this.textarea.selectionStart;
|
| const end = this.textarea.selectionEnd;
|
| const text = this.textarea.value;
|
|
|
|
|
| const beforeCursor = text.substring(0, start);
|
| const afterCursor = text.substring(end);
|
| const selectedText = text.substring(start, end);
|
|
|
|
|
| const lineStart = beforeCursor.lastIndexOf('\n') + 1;
|
| const lineEnd = text.indexOf('\n', end);
|
| const actualLineEnd = lineEnd === -1 ? text.length : lineEnd;
|
|
|
| const currentLine = text.substring(lineStart, actualLineEnd);
|
|
|
|
|
| let newLine;
|
| if (currentLine.trim().startsWith('--')) {
|
|
|
| newLine = currentLine.replace(/^\s*--\s?/, '');
|
| } else {
|
|
|
| const indent = currentLine.match(/^\s*/)[0];
|
| newLine = indent + '-- ' + currentLine.substring(indent.length);
|
| }
|
|
|
|
|
| this.textarea.value = text.substring(0, lineStart) + newLine + text.substring(actualLineEnd);
|
|
|
|
|
| this.textarea.selectionStart = start;
|
| this.textarea.selectionEnd = end;
|
| }
|
|
|
| decreaseIndentation() {
|
| const start = this.textarea.selectionStart;
|
| const end = this.textarea.selectionEnd;
|
| const text = this.textarea.value;
|
|
|
|
|
| const beforeCursor = text.substring(0, start);
|
| const lineStart = beforeCursor.lastIndexOf('\n') + 1;
|
| const lineEnd = text.indexOf('\n', end);
|
| const actualLineEnd = lineEnd === -1 ? text.length : lineEnd;
|
|
|
| const selectedLines = text.substring(lineStart, actualLineEnd);
|
| const lines = selectedLines.split('\n');
|
|
|
|
|
| const newLines = lines.map(line => {
|
| if (line.startsWith('\t')) {
|
| return line.substring(1);
|
| } else if (line.startsWith(' ')) {
|
| return line.substring(4);
|
| }
|
| return line;
|
| });
|
|
|
|
|
| const newSelectedText = newLines.join('\n');
|
| this.textarea.value = text.substring(0, lineStart) + newSelectedText + text.substring(actualLineEnd);
|
|
|
|
|
| this.textarea.selectionStart = start;
|
| this.textarea.selectionEnd = start + newSelectedText.length;
|
| }
|
|
|
|
|
| applySyntaxHighlighting() {
|
|
|
|
|
| return this.textarea.value;
|
| }
|
|
|
|
|
| formatQuery() {
|
| const query = this.getQuery();
|
| if (!query) return;
|
|
|
|
|
| const formatted = query
|
| .replace(/\s+/g, ' ')
|
| .replace(/\s*,\s*/g, ', ')
|
| .replace(/\s*\(\s*/g, ' (')
|
| .replace(/\s*\)\s*/g, ') ')
|
| .replace(/\bSELECT\b/gi, 'SELECT')
|
| .replace(/\bFROM\b/gi, '\nFROM')
|
| .replace(/\bWHERE\b/gi, '\nWHERE')
|
| .replace(/\bORDER BY\b/gi, '\nORDER BY')
|
| .replace(/\bGROUP BY\b/gi, '\nGROUP BY')
|
| .replace(/\bHAVING\b/gi, '\nHAVING')
|
| .replace(/\bLIMIT\b/gi, '\nLIMIT');
|
|
|
| this.setQuery(formatted);
|
| }
|
| }
|
|
|
|
|
| if (typeof window !== 'undefined') {
|
| window.QueryEditor = QueryEditor;
|
| }
|
|
|