mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55:06 +03:00
### Context As per PR title - figured this might be a nice convenience. Submitting a SQL query in the chat form on the explorer home page will open the query in a query tab and run it <img width="854" height="632" alt="image" src="https://github.com/user-attachments/assets/57dc7538-74b5-4801-a7ed-83c1cfaf123f" /> <img width="872" height="519" alt="image" src="https://github.com/user-attachments/assets/597ce0eb-76d4-4f12-83db-20b628c03904" /> ### To test - [ ] Run a couple of SQL statements in the home tab - should open it in query tab and run it - [ ] Run a couple of non-SQL statements in the home tab, should default to opening in a chat tab <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **New Features** - SQL statements submitted from the Explorer home screen now open in a query tab and run automatically. - Natural-language prompts continue to open in the chat experience. - Queries restored from drafts can automatically run once the editor is ready. - **Bug Fixes** - Improved recognition of SQL with leading whitespace, comments, and common statement formats while avoiding misclassification of conversational prompts. - Draft-based auto-run behavior now waits until the query editor is ready. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
34 lines
1.8 KiB
TypeScript
34 lines
1.8 KiB
TypeScript
import { removeCommentsFromSql } from '@/lib/helpers'
|
|
|
|
// [Joshen] `explain` is deliberately excluded even though it's a real SQL keyword - "Explain
|
|
// how RLS works" is a common natural-language prompt, and colliding with it would misroute
|
|
// a normal chat question into a query tab.
|
|
const UNAMBIGUOUS_SQL_STATEMENT_REGEX =
|
|
/^\s*(select|insert|update|delete|alter|drop|truncate|grant|revoke|begin|vacuum|analyze|merge|call|copy|lock|reindex|refresh|execute|prepare|deallocate|comment)\b/i
|
|
|
|
// `create`, `show`, `set`, and `with` are also common natural-language sentence openers
|
|
// ("Create a table for me", "Show me my indexes", "Set up RLS", "With my current schema...").
|
|
// Matching the bare keyword alone would misroute those into a query tab, so each requires
|
|
// syntax that only shows up in the real SQL statement.
|
|
const CREATE_STATEMENT_REGEX =
|
|
/^\s*create\s+(or\s+replace\s+)?(table|unique\s+index|index|view|materialized\s+view|function|procedure|trigger|schema|extension|role|user|policy|type|sequence|database|domain|rule|publication|foreign\s+table)\b/i
|
|
const SHOW_STATEMENT_REGEX = /^\s*show\s+(all|[a-zA-Z_][a-zA-Z0-9_.]*)\s*;?\s*$/i
|
|
const SET_STATEMENT_REGEX =
|
|
/^\s*set\s+(session\s+|local\s+)?(time\s+zone\b|[a-zA-Z_][a-zA-Z0-9_.]*\s*(=|to)\s*\S)/i
|
|
const WITH_STATEMENT_REGEX = /^\s*with\s+(recursive\s+)?[a-zA-Z_][a-zA-Z0-9_]*\s+as\s*\(/i
|
|
|
|
/**
|
|
* Whether `message` looks like a SQL statement rather than a natural-language chat prompt,
|
|
* so the Explorer home tab can route it to a query tab instead of creating an AI chat.
|
|
*/
|
|
export function isSqlStatement(message: string): boolean {
|
|
const sql = removeCommentsFromSql(message)
|
|
return (
|
|
UNAMBIGUOUS_SQL_STATEMENT_REGEX.test(sql) ||
|
|
CREATE_STATEMENT_REGEX.test(sql) ||
|
|
SHOW_STATEMENT_REGEX.test(sql) ||
|
|
SET_STATEMENT_REGEX.test(sql) ||
|
|
WITH_STATEMENT_REGEX.test(sql)
|
|
)
|
|
}
|