"use client"; import React, { useState } from "react"; import Editor from "@monaco-editor/react"; import { format as formatSql } from "sql-formatter"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Database, Copy, RotateCcw, ArrowRight } from "lucide-react"; import { toast } from "sonner"; import { useTheme } from "next-themes"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Label } from "@/components/ui/label"; const SQL_DIALECTS = [ { value: "sql", label: "Standard SQL" }, { value: "mysql", label: "MySQL" }, { value: "postgresql", label: "PostgreSQL" }, { value: "sqlite", label: "SQLite" }, { value: "mariadb", label: "MariaDB" }, { value: "plsql", label: "PL/SQL (Oracle)" }, { value: "tsql", label: "T-SQL (SQL Server)" }, ]; export default function SqlFormatterPage() { const { theme } = useTheme(); const [input, setInput] = useState(""); const [output, setOutput] = useState(""); const [dialect, setDialect] = useState("sql"); const handleFormat = () => { if (!input.trim()) return; try { const formatted = formatSql(input, { language: dialect as any, keywordCase: "upper", }); setOutput(formatted); toast.success("SQL 格式化成功"); } catch (e: any) { toast.error("SQL 格式化失败: " + e.message); } }; const copyToClipboard = async (text: string) => { try { await navigator.clipboard.writeText(text); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; return (

SQL 格式化

支持多种数据库方言的 SQL 美化工具

{/* Options */}
{/* Input */} 输入 SQL
setInput(value || "")} options={{ minimap: { enabled: false }, fontSize: 14, wordWrap: "on" }} />
{/* Output */} 结果
); }