Changed around line 1
+ document.addEventListener('DOMContentLoaded', function() {
+ const sqlInput = document.getElementById('sql-input');
+ const parseResult = document.getElementById('parse-result');
+ const visualizeResult = document.getElementById('visualize-result');
+ const parseButton = document.getElementById('parse-button');
+
+ // Sample SQL parsing function (simplified for demo)
+ function parseSQL(sql) {
+ // This is a simplified parser for demonstration
+ // In a real app, you'd use a proper SQL parser library
+
+ try {
+ // Basic parsing
+ const tables = [];
+ const columns = [];
+ const joins = [];
+ const conditions = [];
+
+ // Extract tables (simplified)
+ const fromMatch = sql.match(/FROM\s+([^\s,(]+)/i);
+ if (fromMatch) tables.push(fromMatch[1]);
+
+ // Extract joins (simplified)
+ const joinMatches = sql.match(/JOIN\s+([^\s]+)/gi) || [];
+ joinMatches.forEach(match => {
+ const table = match.replace(/JOIN\s+/i, '');
+ tables.push(table);
+ joins.push(table);
+ });
+
+ // Extract columns (simplified)
+ const selectMatch = sql.match(/SELECT\s+(.+?)\s+FROM/i) || sql.match(/SELECT\s+(.+)/i);
+ if (selectMatch) {
+ const cols = selectMatch[1].split(',').map(col => col.trim());
+ columns.push(...cols);
+ }
+
+ // Extract conditions (simplified)
+ const whereMatch = sql.match(/WHERE\s+(.+)/i);
+ if (whereMatch) conditions.push(whereMatch[1]);
+
+ return {
+ tables: [...new Set(tables)],
+ columns: [...new Set(columns)],
+ joins,
+ conditions,
+ isValid: true
+ };
+ } catch (e) {
+ return {
+ error: "Could not parse SQL",
+ isValid: false
+ };
+ }
+ }
+
+ function visualizeSQL(parsed) {
+ if (!parsed.isValid) {
+ return `
${parsed.error}
`;
+ }
+
+ let html = `
+
+
Tables
+
${parsed.tables.map(t => `- ${t}
`).join('')}
+
+
Columns
+
${parsed.columns.map(c => `- ${c}
`).join('')}
+
+ ${parsed.joins.length ? `
+
Joins
+
${parsed.joins.map(j => `- ${j}
`).join('')}
+ ` : ''}
+
+ ${parsed.conditions.length ? `
+
Conditions
+
${parsed.conditions.map(c => `- ${c}
`).join('')}
+ ` : ''}
+
+ `;
+
+ return html;
+ }
+
+ function renderDiagram(parsed) {
+ if (!parsed.isValid) return '';
+
+ // This would be replaced with actual diagram rendering in a real app
+ return `
+
+
Visual diagram would appear here showing:
+
Tables: ${parsed.tables.join(', ')}+
Relationships between tables+
+
+ `;
+ }
+
+ parseButton.addEventListener('click', function() {
+ const sql = sqlInput.value.trim();
+ if (!sql) return;
+
+ const parsed = parseSQL(sql);
+ parseResult.innerHTML = visualizeSQL(parsed);
+ visualizeResult.innerHTML = renderDiagram(parsed);
+ });
+
+ // Initialize with sample SQL
+ sqlInput.value = `SELECT users.name, orders.total
+ FROM users
+ JOIN orders ON users.id = orders.user_id
+ WHERE orders.date > '2023-01-01'
+ ORDER BY orders.total DESC`;
+
+ // Trigger initial parse
+ parseButton.click();
+ });