()
| 43 | const QUERY_INTERVAL_LIMIT = 1000; |
| 44 | |
| 45 | export const useExecuteSQL = () => { |
| 46 | const { t } = useTranslation(); |
| 47 | const lastQueryTimeRef = useRef<number | undefined>(undefined); |
| 48 | // Eagerly fetch the subscription so the batch-query / database-group |
| 49 | // gates below see the licensed plan when the user clicks Run. The hook |
| 50 | // is mounted by `EditorMain` on every tab open, so this fires once per |
| 51 | // tab; `loadSubscription` itself dedupes via an in-flight request. |
| 52 | const loadSubscription = useAppStore((s) => s.loadSubscription); |
| 53 | useEffect(() => { |
| 54 | void loadSubscription(); |
| 55 | }, [loadSubscription]); |
| 56 | const notify = ( |
| 57 | type: BBNotificationStyle, |
| 58 | title: string, |
| 59 | description?: string |
| 60 | ) => { |
| 61 | useAppStore.getState().notify({ |
| 62 | module: "bytebase", |
| 63 | style: type, |
| 64 | title, |
| 65 | description, |
| 66 | }); |
| 67 | }; |
| 68 | |
| 69 | const preflight = useCallback( |
| 70 | async (params: SQLEditorQueryParams) => { |
| 71 | lastQueryTimeRef.current = Date.now(); |
| 72 | |
| 73 | const tabsState = getSQLEditorTabsState(); |
| 74 | const tab = tabsState.tabsById.get(tabsState.currentTabId); |
| 75 | if (!tab) { |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // Mirrors `useIsDisconnected` selector logic without a hook |
| 80 | // (preflight runs inside a callback, not during render). |
| 81 | if (!isConnectedSQLEditorTab(tab)) { |
| 82 | notify("CRITICAL", t("sql-editor.select-connection")); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if (isEmpty(params.statement)) { |
| 87 | notify("CRITICAL", t("sql-editor.notify-empty-statement")); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | if (!tab.databaseQueryContexts) { |
| 92 | tabsState.updateTab(tab.id, { databaseQueryContexts: new Map() }); |
| 93 | } |
| 94 | return true; |
| 95 | }, |
| 96 | [t] |
| 97 | ); |
| 98 | |
| 99 | // Propagates the status change to the Zustand store via |
| 100 | // `updateDatabaseQueryContext` (the path that drives React re-renders). |
| 101 | // For ad-hoc contexts that aren't tracked under `(database, ctx.id)` — |
| 102 | // e.g. the ephemeral context in `getExplainTokenForMSSQL` — the store |
no test coverage detected