import React, { useState, useEffect } from 'react'; import { generateSql } from './services/gemini'; import { Button } from './components/Button'; import { TextArea } from './components/TextArea'; import { Select } from './components/Select'; import { SettingsModal } from './components/SettingsModal'; import { LoadingState, DatabaseType } from './types'; // Default placeholders to help the user understand what to input const PLACEHOLDER_STRUCTURE = `例如: CREATE TABLE student ( id INT PRIMARY KEY, name VARCHAR(50) COMMENT '姓名', school_code VARCHAR(20) COMMENT '学校代码', nation_code VARCHAR(10) COMMENT '国籍代码', politics_code VARCHAR(10) COMMENT '政治面貌代码', year VARCHAR(4) COMMENT '年度', address VARCHAR(200) COMMENT '家庭住址' ); CREATE TABLE school ( code VARCHAR(20), name VARCHAR(100) );`; const PLACEHOLDER_DICT = `例如: 字典表 dict_common ( type_code VARCHAR(50), -- 字典类型,如 'NATION', 'POLITICS' item_code VARCHAR(50), -- 实际值,如 '01', 'CN' item_name VARCHAR(100) -- 显示名,如 '汉族', '中国' ) 关联关系: - student.nation_code -> dict_common (type_code='NATION') - student.politics_code -> dict_common (type_code='POLITICS')`; const PLACEHOLDER_REQ = `例如: 我需要查询学校基本信息。 输出:学校名称,学校代码,学生姓名,手机号,家庭住址,年度,政治面貌(需要字典翻译),国籍(需要字典翻译)。`; const DB_OPTIONS = [ { value: 'MySQL', label: 'MySQL / MariaDB' }, { value: 'PostgreSQL', label: 'PostgreSQL' }, { value: 'Oracle', label: 'Oracle Database' }, { value: 'SQL Server', label: 'SQL Server (MSSQL)' }, { value: 'Hive', label: 'Hive / SparkSQL' }, { value: 'Dm', label: '达梦数据库 (Dameng)' }, { value: 'SQLite', label: 'SQLite' }, ]; const App: React.FC = () => { const [databaseType, setDatabaseType] = useState('MySQL'); const [tableStructure, setTableStructure] = useState(''); const [dictionaryData, setDictionaryData] = useState(''); const [requirement, setRequirement] = useState(''); const [generatedSql, setGeneratedSql] = useState(''); const [status, setStatus] = useState(LoadingState.IDLE); const [errorMsg, setErrorMsg] = useState(null); // API Key Management const [apiKey, setApiKey] = useState(''); const [isSettingsOpen, setIsSettingsOpen] = useState(false); useEffect(() => { const storedKey = localStorage.getItem('user_api_key'); if (storedKey) { setApiKey(storedKey); } }, []); const handleSaveApiKey = (key: string) => { setApiKey(key); localStorage.setItem('user_api_key', key); setIsSettingsOpen(false); }; const handleGenerate = async () => { if (!tableStructure.trim() || !requirement.trim()) { setErrorMsg("请至少填写表结构和查询需求。"); return; } setStatus(LoadingState.LOADING); setErrorMsg(null); setGeneratedSql(''); try { const sql = await generateSql({ tableStructure, dictionaryData, requirement, databaseType, apiKey // Pass the custom API key }); setGeneratedSql(sql); setStatus(LoadingState.SUCCESS); } catch (err: any) { setErrorMsg(err.message || "未知错误"); setStatus(LoadingState.ERROR); } }; const copyToClipboard = () => { if (generatedSql) { navigator.clipboard.writeText(generatedSql); } }; return (
setIsSettingsOpen(false)} onSave={handleSaveApiKey} currentApiKey={apiKey} /> {/* Header */}

SQL Translate Pro

智能 SQL 生成 & 字典自动转义工具

{/* Main Content - Two Columns Layout */}
{/* Left Panel: Inputs */}