Download renderer/js/query-editor.js from chahuadev/chahua-database-manager: direct link, hf CLI and curl.
- Browser
- Download file 11.4 kB
-
https://huggingface.co/datasets/chahuadev/chahua-database-manager/resolve/main/renderer/js/query-editor.js
- Command line
-
hf download hf://datasets/chahuadev/chahua-database-manager/renderer/js/query-editor.js
-
curl -L -o query-editor.js https://huggingface.co/datasets/chahuadev/chahua-database-manager/resolve/main/renderer/js/query-editor.js
11.4 kB
| /** | |
| * @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 = ` | |
| <span>Rows: ${data.rowCount}</span> | |
| <span>Execution time: ${executionTime}ms</span> | |
| `; | |
| this.resultsContainer.innerHTML = ''; | |
| this.resultsContainer.appendChild(resultsInfo); | |
| this.resultsContainer.appendChild(table); | |
| } else { | |
| // Show message for non-SELECT queries | |
| 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; | |
| // Get current line(s) | |
| const beforeCursor = text.substring(0, start); | |
| const afterCursor = text.substring(end); | |
| const selectedText = text.substring(start, end); | |
| // Find line boundaries | |
| 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); | |
| // Toggle comment | |
| let newLine; | |
| if (currentLine.trim().startsWith('--')) { | |
| // Remove comment | |
| newLine = currentLine.replace(/^\s*--\s?/, ''); | |
| } else { | |
| // Add comment | |
| const indent = currentLine.match(/^\s*/)[0]; | |
| newLine = indent + '-- ' + currentLine.substring(indent.length); | |
| } | |
| // Replace the line | |
| this.textarea.value = text.substring(0, lineStart) + newLine + text.substring(actualLineEnd); | |
| // Restore cursor position | |
| this.textarea.selectionStart = start; | |
| this.textarea.selectionEnd = end; | |
| } | |
| decreaseIndentation() { | |
| const start = this.textarea.selectionStart; | |
| const end = this.textarea.selectionEnd; | |
| const text = this.textarea.value; | |
| // Get selected lines | |
| 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'); | |
| // Remove one tab or 4 spaces from each line | |
| const newLines = lines.map(line => { | |
| if (line.startsWith('\t')) { | |
| return line.substring(1); | |
| } else if (line.startsWith(' ')) { | |
| return line.substring(4); | |
| } | |
| return line; | |
| }); | |
| // Replace the lines | |
| const newSelectedText = newLines.join('\n'); | |
| this.textarea.value = text.substring(0, lineStart) + newSelectedText + text.substring(actualLineEnd); | |
| // Restore selection | |
| this.textarea.selectionStart = start; | |
| this.textarea.selectionEnd = start + newSelectedText.length; | |
| } | |
| // Basic SQL syntax highlighting | |
| applySyntaxHighlighting() { | |
| // This would be a more complex implementation | |
| // For now, just return the text as-is | |
| return this.textarea.value; | |
| } | |
| // Format SQL query | |
| formatQuery() { | |
| const query = this.getQuery(); | |
| if (!query) return; | |
| // Basic SQL formatting | |
| 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); | |
| } | |
| } | |
| // Make it available globally | |
| if (typeof window !== 'undefined') { | |
| window.QueryEditor = QueryEditor; | |
| } | |