-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathsqlite.ts
More file actions
167 lines (145 loc) · 4.9 KB
/
Copy pathsqlite.ts
File metadata and controls
167 lines (145 loc) · 4.9 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import CommonFormats, { Category } from "src/CommonFormats.ts";
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
import {parse} from "papaparse";
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: true,
internal: "sqlite3",
category: Category.DATABASE,
lossless: false
},
{
name: "iTunes Database",
format: "itdb",
extension: "itdb",
mime: "application/vnd.sqlite3",
from: true,
to: false,
internal: "sqlite3",
category: Category.DATABASE,
lossless: false
},
// Lossy because extracts only tables
CommonFormats.CSV.builder("csv").allowTo().allowFrom()
];
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}.csv`,
bytes: new Uint8Array(encoder.encode(csvStr))
})
}
}
}
if (inputFormat.internal === "csv" && outputFormat.internal === "sqlite3") {
const db = new sqlite3.oo1.DB();
if (!db.pointer) {
throw new Error("Database pointer is undefined")
}
for (const file of inputFiles) {
const decoder = new TextDecoder('utf-8'); // decode as UTF-8
parse(decoder.decode(file.bytes), {
header: false,
skipEmptyLines: true,
complete: function(result) {
const tableName = file.name.replace(".csv", "")
const header = result.data[0] as string[];
const firstRow = result.data[1] as string[];
const schema = inferSchema(header, firstRow);
console.log(schema);
db.exec(`CREATE TABLE ${tableName} (${schema.map(v => v.join(" ")).join(", ")})`)
for (const row of result.data.slice(1) as string[][]) {
db.exec(`INSERT INTO ${tableName} (${header}) VALUES (${row.map((v, i) => formatValue(v, schema[i][1]))})`)
}
}
});
}
let bfr = sqlite3.capi.sqlite3_js_db_export(db);
outputFiles.push({
name: "database.db",
bytes: bfr
});
}
return outputFiles;
}
}
export default sqlite3Handler;
function formatValue(value: string, type: string) {
value = value.substring(1); // Strip of space from left
if (value == "") return "NULL";
if (type === "TEXT") return `'${value.replace(/'/g, "''")}'`;
return value;
}
function inferType(value: string): string {
if (!isNaN(Number(value))) {
if (Number.isInteger(Number(value))) return "INTEGER"
return "REAL"
}
return "TEXT";
}
function inferSchema(header: string[], row: string[]): string[][] {
return header.map((h, i) => {
const type = inferType(row[i]);
return [h, type];
})
}