Skip to content

Commit 42712e8

Browse files
authored
Merge pull request #47 from JacobLinCool/split-statement
fix: csv import result
2 parents ed5bf09 + b052044 commit 42712e8

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

src/lib/plugin/CSV.svelte

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,39 @@
108108
return bodies;
109109
}
110110
111-
const query = bodies
112-
.map(
113-
(body) =>
114-
`INSERT INTO ${table} (${keys?.join(", ")}) VALUES ${body.replace(/\n/g, "\\n")}`,
115-
)
116-
.join("\n");
117-
118-
console.log(query);
119-
const res = await fetch(`/api/db/${database}/exec`, {
120-
method: "POST",
121-
body: JSON.stringify({ query }),
122-
});
111+
const queries = bodies.map(
112+
(body) => `INSERT INTO ${table} (${keys?.join(", ")}) VALUES ${body}`,
113+
);
114+
115+
console.log(queries);
116+
let r: typeof result = undefined;
117+
for (const query of queries) {
118+
const res = await fetch(`/api/db/${database}/all`, {
119+
method: "POST",
120+
body: JSON.stringify({ query }),
121+
});
123122
124-
const json = await res.json<any>();
125-
if (json) {
126-
if ("error" in json) {
127-
error = json?.error?.cause || json?.error?.message;
128-
result = undefined;
123+
const json = await res.json<any>();
124+
if (json) {
125+
if ("error" in json) {
126+
error = json?.error?.cause || json?.error?.message;
127+
r = undefined;
128+
} else {
129+
if (r) {
130+
r.meta.duration += json.meta.duration;
131+
r.meta.changes += json.meta.changes;
132+
} else {
133+
r = json;
134+
}
135+
error = undefined;
136+
files = undefined;
137+
keys = undefined;
138+
casted = undefined;
139+
}
140+
result = r;
129141
} else {
130-
result = json;
131-
error = undefined;
132-
files = undefined;
133-
keys = undefined;
134-
casted = undefined;
142+
throw new Error($t("plugin.csv.no-result"));
135143
}
136-
} else {
137-
throw new Error($t("plugin.csv.no-result"));
138144
}
139145
} finally {
140146
running = false;

0 commit comments

Comments
 (0)