SQL Translator (SQL to Natural Language and Natural Language to SQL)
2 |
3 | Welcome to the SQL and Natural Language Translator! This tool is designed to make it easy for anyone to translate SQL (Structured Query Language) commands into natural language and vice versa. SQL is a programming language used to manage and manipulate data in relational databases, and while it's a powerful tool, it can also be quite complex and difficult to understand. On the other hand, natural language is the language that we speak and write in everyday life, and it's often the preferred way to communicate for people who are not familiar with technical jargon.
4 |
5 | With the SQL and Natural Language Translator, you don't need to be a SQL expert to understand what's going on in your database, or to write SQL queries. You can simply type in your query in natural language and get the corresponding SQL code, or type in your SQL code and get a human-readable translation.This project is 100% free and open source.
6 |
7 |
8 |
9 |
10 |
11 |
12 | ## 🌟 Features
13 |
14 | - Dark mode
15 | - Lowercase/uppercase toggle
16 | - Copy to clipboard
17 | - SQL syntax highlighting
18 | - Schema awareness (beta)
19 | - Query history
20 |
21 |
22 | ## 📖 How to use:
23 |
24 | Using the SQL to Natural Language Translator is easy. Simply navigate to the tool's website and choose whether you want to translate from natural language to SQL or from SQL to natural language. Then, type in your query and hit the "translate" button. The tool will generate the corresponding code or text and display it on the screen.
25 | You can press the 'reverse' button to give input as Natural Language and get SQL queries in response
26 |
27 |
28 | ## 🎯 Roadmap
29 |
30 | - [ ] Functions (WIP)
31 | - [ ] Procedures
32 |
33 |
34 | ## 🛠️ Installation
35 |
36 | ### Local Development Environment
37 |
38 | 1. Clone the repository:
39 |
40 | ```bash
41 | git clone https://github.com/whoiskatrin/sql-translator.git
42 | ```
43 |
44 | 2. Install the required packages:
45 |
46 | ```bash
47 | cd sql-translator
48 | npm install
49 | ```
50 |
51 | 3. Build the application:
52 |
53 | ```bash
54 | npm run build
55 | ```
56 |
57 | 4. Input your OPENAI API key in the .env file, you can get your API key [here](https://beta.openai.com/account/api-keys):
58 |
59 | ```bash
60 | OPENAI_API_KEY=$YOUR_API_KEY
61 | ```
62 |
63 | 5. Start the development server:
64 |
65 | ```bash
66 | npm start
67 | ```
68 |
69 | ### Docker Compose
70 |
71 | 1. Clone the repository:
72 |
73 | ```bash
74 | git clone https://github.com/whoiskatrin/sql-translator.git
75 | ```
76 |
77 | 2. Input your OPENAI API key in the .env.production file, you can get your API key [here](https://beta.openai.com/account/api-keys):
78 |
79 | ```bash
80 | OPENAI_API_KEY=$YOUR_API_KEY
81 | ```
82 |
83 | 3. Start the development server:
84 |
85 | ```bash
86 | docker-compose up
87 | ```
88 |
89 | ## 🖥️ Usage
90 |
91 | Once the development server is running, you can access the application by navigating to `http://localhost:3000` in your web browser.
92 |
93 | Enter a natural language query in the input box and click "Translate" to generate the corresponding SQL code. The generated SQL code will be displayed in the output box.
94 |
95 | ## 👥 Contributing
96 |
97 | Contributions to SQL Translator are welcome and encouraged! To contribute, please follow these steps:
98 |
99 | 1. Fork the repository
100 | 2. Create a new branch
101 | 3. Make your changes
102 | 4. Push your changes to your fork
103 | 5. Submit a pull request
104 |
105 | ## 📜 License
106 |
107 | SQL Translator is released under the MIT [License](LICENSE).
108 |
--------------------------------------------------------------------------------
/.history/README_20230615141736.md:
--------------------------------------------------------------------------------
1 |
SQL Translator (SQL to Natural Language and Natural Language to SQL)
2 |
3 | Welcome to the SQL and Natural Language Translator! This tool is designed to make it easy for anyone to translate SQL (Structured Query Language) commands into natural language and vice versa. SQL is a programming language used to manage and manipulate data in relational databases, and while it's a powerful tool, it can also be quite complex and difficult to understand. On the other hand, natural language is the language that we speak and write in everyday life, and it's often the preferred way to communicate for people who are not familiar with technical jargon.
4 |
5 | With the SQL and Natural Language Translator, you don't need to be a SQL expert to understand what's going on in your database, or to write SQL queries. You can simply type in your query in natural language and get the corresponding SQL code, or type in your SQL code and get a human-readable translation.This project is 100% free and open source.
6 |
7 |
8 |
9 |
10 |
11 |
12 | ## 🌟 Features
13 |
14 | - Dark mode
15 | - Lowercase/uppercase toggle
16 | - Copy to clipboard
17 | - SQL syntax highlighting
18 | - Schema awareness (beta)
19 | - Query history
20 |
21 |
22 | ## 📖 How to use:
23 |
24 | Using the SQL to Natural Language Translator is easy. Simply navigate to the tool's website and choose whether you want to translate from natural language to SQL or from SQL to natural language. Then, type in your query and hit the "translate" button. The tool will generate the corresponding code or text and display it on the screen.
25 | You can press the 'reverse' button to give input as Natural Language and get SQL queries in response
26 |
27 |
28 | ## 🎯 Roadmap
29 |
30 | - [ ] Functions
31 | - [ ] Procedures
32 |
33 |
34 | ## 🛠️ Installation
35 |
36 | ### Local Development Environment
37 |
38 | 1. Clone the repository:
39 |
40 | ```bash
41 | git clone https://github.com/whoiskatrin/sql-translator.git
42 | ```
43 |
44 | 2. Install the required packages:
45 |
46 | ```bash
47 | cd sql-translator
48 | npm install
49 | ```
50 |
51 | 3. Build the application:
52 |
53 | ```bash
54 | npm run build
55 | ```
56 |
57 | 4. Input your OPENAI API key in the .env file, you can get your API key [here](https://beta.openai.com/account/api-keys):
58 |
59 | ```bash
60 | OPENAI_API_KEY=$YOUR_API_KEY
61 | ```
62 |
63 | 5. Start the development server:
64 |
65 | ```bash
66 | npm start
67 | ```
68 |
69 | ### Docker Compose
70 |
71 | 1. Clone the repository:
72 |
73 | ```bash
74 | git clone https://github.com/whoiskatrin/sql-translator.git
75 | ```
76 |
77 | 2. Input your OPENAI API key in the .env.production file, you can get your API key [here](https://beta.openai.com/account/api-keys):
78 |
79 | ```bash
80 | OPENAI_API_KEY=$YOUR_API_KEY
81 | ```
82 |
83 | 3. Start the development server:
84 |
85 | ```bash
86 | docker-compose up
87 | ```
88 |
89 | ## 🖥️ Usage
90 |
91 | Once the development server is running, you can access the application by navigating to `http://localhost:3000` in your web browser.
92 |
93 | Enter a natural language query in the input box and click "Translate" to generate the corresponding SQL code. The generated SQL code will be displayed in the output box.
94 |
95 | ## 👥 Contributing
96 |
97 | Contributions to SQL Translator are welcome and encouraged! To contribute, please follow these steps:
98 |
99 | 1. Fork the repository
100 | 2. Create a new branch
101 | 3. Make your changes
102 | 4. Push your changes to your fork
103 | 5. Submit a pull request
104 |
105 | ## 📜 License
106 |
107 | SQL Translator is released under the MIT [License](LICENSE).
108 |
--------------------------------------------------------------------------------
/.history/README_20230615141805.md:
--------------------------------------------------------------------------------
1 |
SQL Translator (SQL to Natural Language and Natural Language to SQL)
2 |
3 | Welcome to the SQL and Natural Language Translator! This tool is designed to make it easy for anyone to translate SQL (Structured Query Language) commands into natural language and vice versa. SQL is a programming language used to manage and manipulate data in relational databases, and while it's a powerful tool, it can also be quite complex and difficult to understand. On the other hand, natural language is the language that we speak and write in everyday life, and it's often the preferred way to communicate for people who are not familiar with technical jargon.
4 |
5 | With the SQL and Natural Language Translator, you don't need to be a SQL expert to understand what's going on in your database, or to write SQL queries. You can simply type in your query in natural language and get the corresponding SQL code, or type in your SQL code and get a human-readable translation.This project is 100% free and open source.
6 |
7 |
8 |
9 |
10 |
11 |
12 | ## 🌟 Features
13 |
14 | - Dark mode
15 | - Lowercase/uppercase toggle
16 | - Copy to clipboard
17 | - SQL syntax highlighting
18 | - Schema awareness (beta)
19 | - Query history
20 |
21 |
22 | ## 📖 How to use:
23 |
24 | Using the SQL to Natural Language Translator is easy. Simply navigate to the tool's website and choose whether you want to translate from natural language to SQL or from SQL to natural language. Then, type in your query and hit the "translate" button. The tool will generate the corresponding code or text and display it on the screen.
25 | You can press the 'reverse' button to give input as Natural Language and get SQL queries in response
26 |
27 |
28 | ## 🎯 Roadmap
29 |
30 | - [ ] Functions (WIP)
31 | - [ ] Procedures
32 |
33 |
34 | ## 🛠️ Installation
35 |
36 | ### Local Development Environment
37 |
38 | 1. Clone the repository:
39 |
40 | ```bash
41 | git clone https://github.com/whoiskatrin/sql-translator.git
42 | ```
43 |
44 | 2. Install the required packages:
45 |
46 | ```bash
47 | cd sql-translator
48 | npm install
49 | ```
50 |
51 | 3. Build the application:
52 |
53 | ```bash
54 | npm run build
55 | ```
56 |
57 | 4. Input your OPENAI API key in the .env file, you can get your API key [here](https://beta.openai.com/account/api-keys):
58 |
59 | ```bash
60 | OPENAI_API_KEY=$YOUR_API_KEY
61 | ```
62 |
63 | 5. Start the development server:
64 |
65 | ```bash
66 | npm start
67 | ```
68 |
69 | ### Docker Compose
70 |
71 | 1. Clone the repository:
72 |
73 | ```bash
74 | git clone https://github.com/whoiskatrin/sql-translator.git
75 | ```
76 |
77 | 2. Input your OPENAI API key in the .env.production file, you can get your API key [here](https://beta.openai.com/account/api-keys):
78 |
79 | ```bash
80 | OPENAI_API_KEY=$YOUR_API_KEY
81 | ```
82 |
83 | 3. Start the development server:
84 |
85 | ```bash
86 | docker-compose up
87 | ```
88 |
89 | ## 🖥️ Usage
90 |
91 | Once the development server is running, you can access the application by navigating to `http://localhost:3000` in your web browser.
92 |
93 | Enter a natural language query in the input box and click "Translate" to generate the corresponding SQL code. The generated SQL code will be displayed in the output box.
94 |
95 | ## 👥 Contributing
96 |
97 | Contributions to SQL Translator are welcome and encouraged! To contribute, please follow these steps:
98 |
99 | 1. Fork the repository
100 | 2. Create a new branch
101 | 3. Make your changes
102 | 4. Push your changes to your fork
103 | 5. Submit a pull request
104 |
105 | ## 📜 License
106 |
107 | SQL Translator is released under the MIT [License](LICENSE).
108 |
--------------------------------------------------------------------------------
/.history/README_20230615141801.md:
--------------------------------------------------------------------------------
1 |
SQL Translator (SQL to Natural Language and Natural Language to SQL)
2 |
3 | Welcome to the SQL and Natural Language Translator! This tool is designed to make it easy for anyone to translate SQL (Structured Query Language) commands into natural language and vice versa. SQL is a programming language used to manage and manipulate data in relational databases, and while it's a powerful tool, it can also be quite complex and difficult to understand. On the other hand, natural language is the language that we speak and write in everyday life, and it's often the preferred way to communicate for people who are not familiar with technical jargon.
4 |
5 | With the SQL and Natural Language Translator, you don't need to be a SQL expert to understand what's going on in your database, or to write SQL queries. You can simply type in your query in natural language and get the corresponding SQL code, or type in your SQL code and get a human-readable translation.This project is 100% free and open source.
6 |
7 |
8 |
9 |
10 |
11 |
12 | ## 🌟 Features
13 |
14 | - Dark mode
15 | - Lowercase/uppercase toggle
16 | - Copy to clipboard
17 | - SQL syntax highlighting
18 | - Schema awareness (beta)
19 | - Query history
20 |
21 |
22 | ## 📖 How to use:
23 |
24 | Using the SQL to Natural Language Translator is easy. Simply navigate to the tool's website and choose whether you want to translate from natural language to SQL or from SQL to natural language. Then, type in your query and hit the "translate" button. The tool will generate the corresponding code or text and display it on the screen.
25 | You can press the 'reverse' button to give input as Natural Language and get SQL queries in response
26 |
27 |
28 | ## 🎯 Roadmap
29 |
30 | - [ ] Functions (in progress)
31 | - [ ] Procedures
32 |
33 |
34 | ## 🛠️ Installation
35 |
36 | ### Local Development Environment
37 |
38 | 1. Clone the repository:
39 |
40 | ```bash
41 | git clone https://github.com/whoiskatrin/sql-translator.git
42 | ```
43 |
44 | 2. Install the required packages:
45 |
46 | ```bash
47 | cd sql-translator
48 | npm install
49 | ```
50 |
51 | 3. Build the application:
52 |
53 | ```bash
54 | npm run build
55 | ```
56 |
57 | 4. Input your OPENAI API key in the .env file, you can get your API key [here](https://beta.openai.com/account/api-keys):
58 |
59 | ```bash
60 | OPENAI_API_KEY=$YOUR_API_KEY
61 | ```
62 |
63 | 5. Start the development server:
64 |
65 | ```bash
66 | npm start
67 | ```
68 |
69 | ### Docker Compose
70 |
71 | 1. Clone the repository:
72 |
73 | ```bash
74 | git clone https://github.com/whoiskatrin/sql-translator.git
75 | ```
76 |
77 | 2. Input your OPENAI API key in the .env.production file, you can get your API key [here](https://beta.openai.com/account/api-keys):
78 |
79 | ```bash
80 | OPENAI_API_KEY=$YOUR_API_KEY
81 | ```
82 |
83 | 3. Start the development server:
84 |
85 | ```bash
86 | docker-compose up
87 | ```
88 |
89 | ## 🖥️ Usage
90 |
91 | Once the development server is running, you can access the application by navigating to `http://localhost:3000` in your web browser.
92 |
93 | Enter a natural language query in the input box and click "Translate" to generate the corresponding SQL code. The generated SQL code will be displayed in the output box.
94 |
95 | ## 👥 Contributing
96 |
97 | Contributions to SQL Translator are welcome and encouraged! To contribute, please follow these steps:
98 |
99 | 1. Fork the repository
100 | 2. Create a new branch
101 | 3. Make your changes
102 | 4. Push your changes to your fork
103 | 5. Submit a pull request
104 |
105 | ## 📜 License
106 |
107 | SQL Translator is released under the MIT [License](LICENSE).
108 |
--------------------------------------------------------------------------------
/public/stars.svg:
--------------------------------------------------------------------------------
1 |
4 |
--------------------------------------------------------------------------------
/pages/index.tsx:
--------------------------------------------------------------------------------
1 | import React, { useEffect, useState } from "react";
2 | import dynamic from "next/dynamic";
3 | import { Button } from "@tremor/react";
4 | const SyntaxHighlighter = dynamic(() => import("react-syntax-highlighter"));
5 | import { vs, dracula } from "react-syntax-highlighter/dist/cjs/styles/hljs";
6 | import Head from "next/head";
7 | import { Analytics } from "@vercel/analytics/react";
8 | import ThemeButton from "../components/ThemeButton";
9 | import { useTranslate } from "../hooks/useTranslate";
10 | import { toast } from "react-hot-toast";
11 | import LoadingDots from "../components/LoadingDots";
12 | import { useTheme } from "next-themes";
13 | import Toggle from "../components/Toggle";
14 | import { Header } from "../components/Header/Header";
15 | import { GreenOrb, OrangeOrb, WhiteOrb } from "../components/atoms/Orbs";
16 | interface IHistory {
17 | inputText: string;
18 | outputText: string;
19 | tableSchema?: string;
20 | isHumanToSql?: boolean;
21 | }
22 |
23 | interface IHistoryEntry {
24 | inputText: string;
25 | outputText: string;
26 | tableSchema?: string;
27 | isHumanToSql?: boolean;
28 | }
29 |
30 | interface ITextCopied {
31 | isCopied: boolean;
32 | isHistory: boolean;
33 | text: string;
34 | }
35 | interface prev {
36 | previnput: string;
37 | prevoutput: string;
38 | }
39 |
40 | export default function Home() {
41 | const { resolvedTheme } = useTheme();
42 | const isThemeDark = resolvedTheme === "dark";
43 | const [mounted, setMounted] = useState(false);
44 | const {
45 | translate,
46 | translating,
47 | outputText,
48 | setOutputText,
49 | translationError,
50 | } = useTranslate();
51 | const [inputText, setInputText] = useState("");
52 | const [prevquery, setPrevquery] = useState([]);
53 | const [isHumanToSql, setIsHumanToSql] = useState(true);
54 | const [isOutputTextUpperCase, setIsOutputTextUpperCase] = useState(false);
55 | const [tableSchema, setTableSchema] = useState("");
56 | const [showTableSchema, setShowTableSchema] = useState(false);
57 | const [history, setHistory] = useState([]);
58 | const [showHistory, setShowHistory] = useState(false);
59 | const [hasTranslated, setHasTranslated] = useState(false);
60 | const [copied, setCopied] = useState();
61 |
62 | useEffect(() => {
63 | if (inputText && hasTranslated) {
64 | setHistory((prevHistory) => [
65 | ...prevHistory,
66 | {
67 | inputText: JSON.stringify(inputText),
68 | outputText: JSON.stringify(outputText),
69 | tableSchema,
70 | isHumanToSql,
71 | },
72 | ]);
73 |
74 | addHistoryEntry({
75 | inputText: JSON.stringify(inputText),
76 | tableSchema,
77 | isHumanToSql,
78 | outputText: JSON.stringify(outputText),
79 | });
80 |
81 | setHasTranslated(false);
82 | }
83 | }, [outputText]);
84 |
85 | useEffect(() => {
86 | setMounted(true);
87 | }, []);
88 |
89 | useEffect(() => {
90 | if (translationError) toast.error(translationError);
91 | }, [translationError]);
92 |
93 | if (!mounted) {
94 | return null;
95 | }
96 |
97 | const isValidTableSchema = (text: string) => {
98 | const pattern = /^CREATE\s+TABLE\s+\w+\s*\((\s*.+\s*,?\s*)+\);?$/i;
99 | const regex = new RegExp(pattern);
100 | return regex.test(text.trim());
101 | };
102 |
103 | const addHistoryEntry = (entry: IHistory) => {
104 | if (
105 | !history.some(
106 | ({ inputText, outputText }) =>
107 | inputText === entry.inputText && outputText === entry.outputText
108 | ) &&
109 | !prevquery.some(
110 | ({ previnput, prevoutput }) =>
111 | previnput === entry.inputText && prevoutput === entry.outputText
112 | )
113 | ) {
114 | setHistory([...history, entry]);
115 | }
116 | const newhistory: prev = {
117 | previnput: entry.inputText,
118 | prevoutput: entry.outputText,
119 | };
120 | setPrevquery([...prevquery, newhistory]);
121 | };
122 |
123 | function safeJSONParse(str: string) {
124 | try {
125 | return JSON.parse(str);
126 | } catch (e) {
127 | console.error("JSON parse error:", e);
128 | return null;
129 | }
130 | }
131 |
132 | const handleInputChange = (event: React.ChangeEvent) => {
133 | setInputText(event.target.value);
134 | if (!showTableSchema) {
135 | setTableSchema("");
136 | }
137 | };
138 |
139 | const handleCopy = (text: string, isHistory: boolean) => {
140 | navigator.clipboard.writeText(text);
141 | setCopied({
142 | isCopied: true,
143 | isHistory: isHistory,
144 | text: text,
145 | });
146 | setTimeout(() => {
147 | setCopied({
148 | isCopied: false,
149 | isHistory: isHistory,
150 | text: text,
151 | });
152 | }, 3000);
153 | };
154 |
155 | const buttonStyles = {
156 | light: "light-button-w-gradient-border text-black",
157 | dark: "dark-button-w-gradient-border text-[#D8D8D8]",
158 | };
159 |
160 | const handleSubmit = async (event: React.FormEvent) => {
161 | event.preventDefault();
162 |
163 | try {
164 | // Validate input syntax
165 | if (!isHumanToSql) {
166 | const pattern =
167 | /^\s*(select|insert|update|delete|create|alter|drop|truncate|grant|revoke|use|begin|commit|rollback)\s/i;
168 | const regex = new RegExp(pattern);
169 | if (!regex.test(inputText)) {
170 | toast.error("Invalid SQL syntax.");
171 | return;
172 | }
173 | }
174 | if (showTableSchema && !isValidTableSchema(tableSchema)) {
175 | toast.error("Invalid table schema.");
176 | return;
177 | }
178 |
179 | translate({ inputText, tableSchema, isHumanToSql });
180 | setHasTranslated(true);
181 | } catch (error) {
182 | console.log(error);
183 | toast.error(`Error translating ${isHumanToSql ? "to SQL" : "to human"}.`);
184 | }
185 | };
186 |
187 | return (
188 |