/* Copyright 2026 Marimo. All rights reserved. */ import type { Select as SelectPrimitive } from "radix-ui"; type SelectTriggerProps = SelectPrimitive.SelectTriggerProps; import { useAtomValue } from "jotai"; import { AlertCircle, CircleHelpIcon, DatabaseZap, SearchCheck, } from "lucide-react"; import { getCellForDomProps } from "@/components/data-table/cell-utils"; import { transformDisplayName } from "@/components/databases/display"; import { DatabaseLogo } from "@/components/databases/icon"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectSeparator, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Tooltip } from "@/components/ui/tooltip"; import type { CellId } from "@/core/cells/ids"; import { dataConnectionsMapAtom, setLatestEngineSelected, } from "@/core/datasets/data-source-connections"; import { type ConnectionName, INTERNAL_SQL_ENGINES, } from "@/core/datasets/engines"; import type { DataSourceConnection } from "@/core/kernel/messages"; import { useNonce } from "@/hooks/useNonce"; import { clearAllSqlValidationErrors } from "../languages/sql/banner-validation-errors"; import { type SQLMode, useSQLMode } from "../languages/sql/sql-mode"; interface SelectProps { selectedEngine: ConnectionName; onChange: (engine: ConnectionName) => void; cellId: CellId; } export const SQLEngineSelect: React.FC = ({ selectedEngine, onChange, cellId, }) => { const connectionsMap = useAtomValue(dataConnectionsMapAtom); const internalEngineConnections: DataSourceConnection[] = []; const userDefinedConnections: DataSourceConnection[] = []; for (const [connName, connection] of connectionsMap.entries()) { INTERNAL_SQL_ENGINES.has(connName) ? internalEngineConnections.push(connection) : userDefinedConnections.push(connection); } // Use nonce to force re-render as languageAdapter.engine may not trigger change // If it's disconnected, we display the engine variable. const rerender = useNonce(); const engineIsDisconnected = selectedEngine && !connectionsMap.has(selectedEngine); const handleSelectEngine = (value: string) => { if (value === HELP_KEY) { window.open(HELP_URL, "_blank"); return; } const nextEngine = connectionsMap.get(value as ConnectionName); if (nextEngine) { rerender(); onChange(nextEngine.name); // Update the latest engine selected setLatestEngineSelected(nextEngine.name); } }; const renderConnections = (connections: DataSourceConnection[]) => { // HACK: Ignore iceberg connections // Ideally source_type should be on the DataSourceConnection object connections = connections.filter( (connection) => connection.source !== "iceberg", ); return connections.map((connection) => (
{transformDisplayName(connection.display_name)}
)); }; return (
); }; const HELP_KEY = "__help__"; const HELP_URL = "http://docs.marimo.io/guides/working_with_data/sql/#connecting-to-a-custom-database"; export const SQLModeSelect: React.FC = () => { const { sqlMode, setSQLMode } = useSQLMode(); const handleToggleMode = () => { const nextMode = sqlMode === "validate" ? "default" : "validate"; if (nextMode === "default") { clearAllSqlValidationErrors(); } setSQLMode(nextMode); }; const getModeIcon = (mode: SQLMode) => { return mode === "validate" ? ( ) : ( ); }; const getTooltipContent = (mode: SQLMode) => { return mode === "validate" ? (
Validate Mode

Queries are validated as you write them

) : (
Default Mode

Standard editing

); }; return (
); }; const SQLSelectTrigger: React.FC = ({ children, ...props }) => { return ( {children} ); };