()
| 14 | import ExecutionWarningBanner from "./ExecutionView/ExecutionWarningBanner"; |
| 15 | |
| 16 | const QueryDrawer = () => { |
| 17 | const { t } = useTranslation(); |
| 18 | const queryStore = useQueryStore(); |
| 19 | const messageStore = useMessageStore(); |
| 20 | const [executionResult, setExecutionResult] = useState<ExecutionResult | undefined>(undefined); |
| 21 | const [originalStatement, setOriginalStatement] = useState<string>(""); |
| 22 | const [statement, setStatement] = useState<string>(""); |
| 23 | const [isLoading, setIsLoading] = useState(false); |
| 24 | const context = queryStore.context; |
| 25 | const executionMessage = executionResult ? getMessageFromExecutionResult(executionResult) : ""; |
| 26 | const showExecutionWarningBanner = statement.trim() && !checkStatementIsSelect(statement); |
| 27 | |
| 28 | useEffect(() => { |
| 29 | if (!queryStore.showDrawer) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | const statement = context?.statement || ""; |
| 34 | setStatement(statement); |
| 35 | // Save original statement when open QueryDrawer. |
| 36 | if (!originalStatement) { |
| 37 | setOriginalStatement(statement); |
| 38 | } |
| 39 | |
| 40 | if (statement !== "" && checkStatementIsSelect(statement)) { |
| 41 | executeStatement(statement); |
| 42 | } |
| 43 | setExecutionResult(undefined); |
| 44 | }, [context, queryStore.showDrawer]); |
| 45 | |
| 46 | // Reset original statement to "" when close QueryDrawer. |
| 47 | useEffect(() => { |
| 48 | if (!queryStore.showDrawer) { |
| 49 | setOriginalStatement(""); |
| 50 | } |
| 51 | }, [queryStore.showDrawer]); |
| 52 | |
| 53 | const executeStatement = async (statement: string) => { |
| 54 | if (!statement) { |
| 55 | toast.error("Please enter a statement."); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (!context) { |
| 60 | toast.error("No execution context found."); |
| 61 | setIsLoading(false); |
| 62 | setExecutionResult(undefined); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | setIsLoading(true); |
| 67 | setExecutionResult(undefined); |
| 68 | const { connection, database } = context; |
| 69 | try { |
| 70 | const response = await fetch("/api/connection/execute", { |
| 71 | method: "POST", |
| 72 | headers: { |
| 73 | "Content-Type": "application/json", |
nothing calls this directly
no test coverage detected