Skip to content

Commit

Permalink
refactor: put getSingleTableName in try
Browse files Browse the repository at this point in the history
  • Loading branch information
keppere committed Dec 13, 2024
1 parent 5d0213c commit 7d4118a
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions src/components/gui/tabs/query-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,31 +361,35 @@ export default function QueryWindow({
}

function getSingleTableName(query: string): string | null {
// Normalize query by removing extra spaces and converting to lowercase
const normalizedQuery = query.replace(/\s+/g, " ").trim().toLowerCase();
try {
// Normalize query by removing extra spaces and converting to lowercase
const normalizedQuery = query.replace(/\s+/g, " ").trim().toLowerCase();

// Match the table names after "from" keyword
const fromMatch = normalizedQuery.match(/from\s+([^\s,;]+)/i);
const joinMatches = normalizedQuery.match(/join\s+([^\s,;]+)/gi);
// Match the table names after "from" keyword
const fromMatch = normalizedQuery.match(/from\s+([^\s,;]+)/i);
const joinMatches = normalizedQuery.match(/join\s+([^\s,;]+)/gi);

// If there are JOINs, more than one table is referenced
if (joinMatches && joinMatches.length > 0) {
return null;
}
// If there are JOINs, more than one table is referenced
if (joinMatches && joinMatches.length > 0) {
return null;
}

// Check if a single table is present
if (fromMatch) {
const tableName = fromMatch[1];
// Check if a single table is present
if (fromMatch) {
const tableName = fromMatch[1];

// Ensure no additional tables are mentioned
const additionalTablesMatch = normalizedQuery.match(/,\s*[^\s,;]+/);
if (additionalTablesMatch) {
return null;
// Ensure no additional tables are mentioned
const additionalTablesMatch = normalizedQuery.match(/,\s*[^\s,;]+/);
if (additionalTablesMatch) {
return null;
}

return tableName;
}

return tableName;
// No table found
return null;
} catch (e) {
return null;
}

// No table found
return null;
}

0 comments on commit 7d4118a

Please sign in to comment.