(options?: UseRawSQLOptions)
| 16 | } |
| 17 | |
| 18 | export function useRawSQL(options?: UseRawSQLOptions) { |
| 19 | const { showToast } = useToast(); |
| 20 | const queryClient = useQueryClient(); |
| 21 | |
| 22 | const mutation = useMutation({ |
| 23 | mutationFn: async ({ query, params = [] }: RawSQLParams) => { |
| 24 | return advanceService.runRawSQL(query, params); |
| 25 | }, |
| 26 | onSuccess: (data) => { |
| 27 | // Invalidate database schema queries to ensure UI reflects any schema changes |
| 28 | void queryClient.invalidateQueries({ queryKey: ['database', 'tables'] }); |
| 29 | void queryClient.invalidateQueries({ queryKey: ['database', 'table-schemas'] }); |
| 30 | |
| 31 | if (options?.showSuccessToast !== false) { |
| 32 | const message = 'SQL query executed successfully'; |
| 33 | showToast(message, 'success'); |
| 34 | } |
| 35 | options?.onSuccess?.(data); |
| 36 | }, |
| 37 | onError: (error: Error) => { |
| 38 | if (options?.showErrorToast !== false) { |
| 39 | const errorMessage = error instanceof Error ? error.message : 'Failed to execute SQL query'; |
| 40 | showToast(errorMessage, 'error'); |
| 41 | } |
| 42 | options?.onError?.(error); |
| 43 | }, |
| 44 | }); |
| 45 | |
| 46 | return { |
| 47 | executeSQL: mutation.mutate, |
| 48 | executeSQLAsync: mutation.mutateAsync, |
| 49 | reset: mutation.reset, |
| 50 | isPending: mutation.isPending, |
| 51 | isSuccess: mutation.isSuccess, |
| 52 | isError: mutation.isError, |
| 53 | error: mutation.error, |
| 54 | data: mutation.data, |
| 55 | }; |
| 56 | } |
no test coverage detected