Skip to content

Commit 16424e2

Browse files
committed
feat(convert): add sql export
1 parent 78135ac commit 16424e2

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pages/_document.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export default function Document() {
44
return (
55
<Html lang="en">
66
<Head>
7-
{/* i dont care that the meta tags are in the wrong place, this is one page and as long as it works why should i care */}
87
<meta charSet="UTF-8" />
98
<meta
109
content="width=device-width, initial-scale=1.0"

pages/convert.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ export default function Home() {
106106
blobs.push({ name: `${file.name}.json`, blob: jsonBlob });
107107
}
108108

109+
if (selectedKeys.has("SQL")) {
110+
const sqlStatements = tableNames
111+
.map((table) => {
112+
const createTableResult = database.exec(`SELECT sql FROM sqlite_master WHERE type='table' AND name='${table}'`);
113+
const createTableStatement = createTableResult[0]?.values[0][0] as string;
114+
115+
const result = database.exec(`SELECT * FROM ${table}`);
116+
if (result.length === 0) return createTableStatement;
117+
118+
const columns = result[0].columns;
119+
const rows = result[0].values.map((row) =>
120+
row.map((value) => `'${value}'`).join(","),
121+
);
122+
123+
return `${createTableStatement};\nINSERT INTO ${table} (${columns.join(",")}) VALUES ${rows.map((row) => `(${row})`).join(",")};`;
124+
})
125+
.filter((statement) => statement !== "")
126+
.join("\n");
127+
128+
const sqlBlob = new Blob([sqlStatements], {
129+
type: "application/sql;charset=utf-8;",
130+
});
131+
132+
blobs.push({ name: `${file.name}.sql`, blob: sqlBlob });
133+
}
134+
109135
if (blobs.length === 0) {
110136
alert(
111137
'No data to export. Make sure you have selected "CSV" or "JSON" format.',
@@ -205,11 +231,13 @@ export default function Home() {
205231
{[
206232
{ key: "CSV", icon: <LuFileSpreadsheet /> },
207233
{ key: "JSON", icon: <VscJson /> },
234+
{ key: "SQL", icon: <FaDatabase /> },
208235
].map(({ key, icon }) => {
209236
const descriptions: { [key: string]: string } =
210237
{
211238
CSV: "Comma-Separated Values",
212239
JSON: "JavaScript Object Notation",
240+
SQL: "Structured Query Language",
213241
};
214242

215243
return (

0 commit comments

Comments
 (0)