/** * @fileoverview Chahua Database Manager - Query Editor Module * @description SQL Query Editor พร้อมคุณสมบัติ syntax highlighting และ keyboard shortcuts * คุณสมบัติ: SQL editing, Query execution, Results display, Line numbers * @author ทีมพัฒนา Chahua * @version 1.0.0 * @since 2024-01-01 * @calledBy app.js, renderer/index.html * @dependencies * - DOM elements: queryTextarea, executeQueryBtn, resultsContent * - app.js: Query execution and results handling * @features * - SQL syntax highlighting (basic) * - Line number display * - Keyboard shortcuts (Ctrl+Enter for execution) * - Tab indentation support * - Query execution with timing * - Results display and formatting * @security * - Input sanitization for SQL queries * - XSS protection in results display * @example * // Initialize query editor * const editor = new QueryEditor(); * * // Set query text * editor.setQuery('SELECT * FROM users'); * * // Execute current query * await editor.executeQuery(); */ // Query Editor functionality /** * Query Editor - Class for managing SQL query editing and execution * @class QueryEditor * @description Provides SQL editing capabilities with syntax highlighting, shortcuts, and execution * @features * - Basic SQL syntax highlighting * - Line numbers and editor enhancements * - Keyboard shortcuts for common operations * - Query execution with timing display * - Results formatting and display */ class QueryEditor { /** * Create a QueryEditor instance * @constructor * @description Initializes the query editor with DOM elements and event listeners * @features * - Textarea and button element binding * - Event listener setup * - Editor enhancement initialization */ constructor() { this.textarea = document.getElementById('queryTextarea'); this.executeBtn = document.getElementById('executeQueryBtn'); this.resultsContainer = document.getElementById('resultsContent'); this.executionTimeDisplay = document.getElementById('executionTime'); this.initializeEditor(); } /** * Initialize editor functionality * @function initializeEditor * @description Sets up event listeners and editor enhancements * @features * - Line number updates on input * - Tab key handling for indentation * - Keyboard shortcuts setup * - Ctrl+Enter for query execution */ initializeEditor() { if (!this.textarea) return; // Add basic syntax highlighting (simple approach) this.textarea.addEventListener('input', () => { this.updateLineNumbers(); }); // Handle tab key for indentation this.textarea.addEventListener('keydown', (e) => { if (e.key === 'Tab') { e.preventDefault(); const start = this.textarea.selectionStart; const end = this.textarea.selectionEnd; // Insert tab character this.textarea.value = this.textarea.value.substring(0, start) + '\t' + this.textarea.value.substring(end); // Restore cursor position this.textarea.selectionStart = this.textarea.selectionEnd = start + 1; } }); // Keyboard shortcuts for editor this.textarea.addEventListener('keydown', (e) => { // Ctrl+Enter: Execute Query if (e.ctrlKey && e.key === 'Enter') { e.preventDefault(); this.executeQuery(); } // Ctrl+A: Select All if (e.ctrlKey && e.key === 'a') { // Let default behavior work return; } // Ctrl+Z: Undo (browser default) if (e.ctrlKey && e.key === 'z') { // Let default behavior work return; } // Ctrl+Y: Redo (browser default) if (e.ctrlKey && e.key === 'y') { // Let default behavior work return; } // Ctrl+F: Find if (e.ctrlKey && e.key === 'f') { // Let default browser find work return; } // Ctrl+/: Toggle comment if (e.ctrlKey && e.key === '/') { e.preventDefault(); this.toggleComment(); } // Shift+Tab: Decrease indentation if (e.shiftKey && e.key === 'Tab') { e.preventDefault(); this.decreaseIndentation(); } }); } updateLineNumbers() { // Simple line numbers implementation const lines = this.textarea.value.split('\n').length; // This would be where line numbers are updated console.log(`Lines: ${lines}`); } async executeQuery() { // เรียกใช้ฟังก์ชันหลักที่อยู่ใน dbManager (จากไฟล์ app.js) แทน 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; // Update execution time if (this.executionTimeDisplay) { this.executionTimeDisplay.textContent = `Executed in ${executionTime}ms`; } if (data.type === 'select' && data.rows && data.rows.length > 0) { // Create table for SELECT results const table = UIHelpers.createTable(data.columns, data.rows); // Add results info const resultsInfo = document.createElement('div'); resultsInfo.className = 'results-info'; resultsInfo.innerHTML = ` Rows: ${data.rowCount} Execution time: ${executionTime}ms `; this.resultsContainer.innerHTML = ''; this.resultsContainer.appendChild(resultsInfo); this.resultsContainer.appendChild(table); } else { // Show message for non-SELECT queries this.resultsContainer.innerHTML = `
`; } } renderError(errorMessage) { if (!this.resultsContainer) return; this.resultsContainer.innerHTML = `${errorMessage}