diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a547bf3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* +lerna-debug.log* + +node_modules +dist +dist-ssr +*.local + +# Editor directories and files +.vscode/* +!.vscode/extensions.json +.idea +.DS_Store +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/App.tsx b/App.tsx new file mode 100644 index 0000000..d468c02 --- /dev/null +++ b/App.tsx @@ -0,0 +1,228 @@ + +import React, { useState } from 'react'; +import { generateSql } from './services/gemini'; +import { Button } from './components/Button'; +import { TextArea } from './components/TextArea'; +import { Select } from './components/Select'; +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); + + 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 + }); + setGeneratedSql(sql); + setStatus(LoadingState.SUCCESS); + } catch (err: any) { + setErrorMsg(err.message || "未知错误"); + setStatus(LoadingState.ERROR); + } + }; + + const copyToClipboard = () => { + if (generatedSql) { + navigator.clipboard.writeText(generatedSql); + // Optional: Show a toast here, but for now we'll just rely on user action + } + }; + + return ( +
+ {/* Header */} +
+
+
+ + + +
+
+

SQL Translate Pro

+

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

+
+
+
+ {/* Placeholder for future user settings or profile */} +
+
+ + {/* Main Content - Two Columns Layout */} +
+ + {/* Left Panel: Inputs */} +
+ +
+