forked from p2r3/convert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.ts
More file actions
105 lines (89 loc) · 2.76 KB
/
Copy pathsqlite.ts
File metadata and controls
105 lines (89 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
class sqlite3Handler implements FormatHandler {
public name: string = "sqlite3";
public supportedFormats?: FileFormat[];
public ready: boolean = false;
async init () {
this.supportedFormats = [
{
name: "SQLite3",
format: "sqlite3",
extension: "db",
mime: "application/vnd.sqlite3",
from: true,
to: false,
internal: "sqlite3"
},
{
name: "Comma Seperated Values",
format: "csv",
extension: "csv",
mime: "text/csv",
from: false,
to: true,
internal: "csv"
},
];
this.ready = true;
}
getTables(db: any) {
const stmt = db.prepare("SELECT name FROM sqlite_master WHERE type='table';");
let row: any[] = [];
try {
while (stmt.step()) {
row.push(stmt.get(0));
}
} finally {
stmt.finalize();
}
return row;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
console.log(inputFormat, outputFormat);
const sqlite3 = await sqlite3InitModule();
if (inputFormat.internal == "sqlite3" && outputFormat.internal == "csv") {
for (const file of inputFiles) {
const p = sqlite3.wasm.allocFromTypedArray(file.bytes);
const db = new sqlite3.oo1.DB();
if (!db.pointer) {
throw new Error("Database pointer is undefined")
}
const flags = sqlite3.capi.SQLITE_DESERIALIZE_FREEONCLOSE;
const rc = sqlite3.capi.sqlite3_deserialize(
db.pointer,
"main",
p,
file.bytes.byteLength,
file.bytes.byteLength,
flags
);
db.checkRc(rc);
for (const table of this.getTables(db)) {
const stmt = db.prepare(`SELECT * FROM ${table}`);
let csvStr = stmt.getColumnNames().join(",") + "\n";
try {
while (stmt.step()) {
const row = Array.from({length: stmt.columnCount }, (_, j) => stmt.get(j))
csvStr += row.join(", ") + "\n"
}
} finally {
stmt.finalize();
}
const encoder = new TextEncoder()
outputFiles.push({
name: table,
bytes: new Uint8Array(encoder.encode(csvStr))
})
}
}
}
return outputFiles;
}
}
export default sqlite3Handler;