Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #49 #53

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ node_modules
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
wrangler.toml

cfai/
.wrangler/
8 changes: 6 additions & 2 deletions src/lib/plugin/AddRecord.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
import { t } from "svelte-i18n";
import type { PluginData } from "./type";
import { z } from "zod";
import { quote_name, quote_record } from "$lib/utils";

export let database: string;
export let table: string;
export let data: PluginData;

const quoted_table = quote_name(table);

const cols = data.db
.find(({ name }) => name === table)
?.columns.sort(({ cid: a }, { cid: b }) => a - b);
Expand Down Expand Up @@ -76,9 +79,10 @@
);

try {
const res = await fetch(`/api/db/${database}/${table}/data`, {
const quoted_record = quote_record(data);
const res = await fetch(`/api/db/${database}/${quoted_table}/data`, {
method: "POST",
body: JSON.stringify(data),
body: JSON.stringify(quoted_record),
});

const json = await res.json<typeof result | typeof error>();
Expand Down
8 changes: 6 additions & 2 deletions src/lib/plugin/CSV.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import type { PluginData } from "./type";
import type { Type } from "../sqlite";
import { affinity, cast } from "../sqlite";
import { quote_name } from "$lib/utils";

export let database: string;
export let table: string;
export let data: PluginData;

const quoted_table = quote_name(table);

const cols = data.db
.find(({ name }) => name === table)
?.columns.sort(({ cid: a }, { cid: b }) => a - b);
Expand Down Expand Up @@ -108,8 +111,9 @@
return bodies;
}

const quoted_keys = keys?.map(quote_name);
const queries = bodies.map(
(body) => `INSERT INTO ${table} (${keys?.join(", ")}) VALUES ${body}`,
(body) => `INSERT INTO ${quoted_table} (${quoted_keys?.join(", ")}) VALUES ${body}`,
);

console.log(queries);
Expand Down Expand Up @@ -155,7 +159,7 @@

try {
const module = import("csv-stringify/browser/esm/sync");
const res = await fetch(`/api/db/${database}/${table}/data`);
const res = await fetch(`/api/db/${database}/${quoted_table}/data`);
const json = await res.json<any>();

const { stringify } = await module;
Expand Down
15 changes: 11 additions & 4 deletions src/lib/plugin/TableBrowser.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import { t } from "svelte-i18n";
import type { PluginData } from "./type";
import Icon from "@iconify/svelte";
import { quote_name, quote_record } from "$lib/utils";

export let database: string;
export let table: string;
export let data: PluginData;

const quoted_table = quote_name(table);

const cols =
data.db
.find(({ name }) => name === table)
Expand Down Expand Up @@ -51,7 +54,9 @@
params.set("order", order);
params.set("dir", dir);
}
const res = await fetch(`/api/db/${database}/${table}/data?${params.toString()}`);
const res = await fetch(
`/api/db/${database}/${quoted_table}/data?${params.toString()}`,
);

const json = await res.json<typeof result | typeof error>();
if (json) {
Expand Down Expand Up @@ -103,7 +108,7 @@
throw new Error($t("plugin.table-browser.invalid-rowid"));
}

const res = await fetch(`/api/db/${database}/${table}/data/?rowid=${rowid}`, {
const res = await fetch(`/api/db/${database}/${quoted_table}/data/?rowid=${rowid}`, {
method: "DELETE",
});

Expand Down Expand Up @@ -153,13 +158,15 @@
if (!record) {
throw new Error($t("plugin.table-browser.no-record"));
}
const res = await fetch(`/api/db/${database}/${table}/data/?rowid=${rowid}`, {
const quoted_record = quote_record(record);

const res = await fetch(`/api/db/${database}/${quoted_table}/data/?rowid=${rowid}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...record,
...quoted_record,
_: undefined,
}),
});
Expand Down
35 changes: 35 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// This file contains utility functions that are used in the project

// This list contains all special characters that should be quoted in a name string
const SPECIAL_CHARS = ["-"];

/**
* This function checks if a string contains any special characters and therefore should be quoted
* @param str The string to check
* @returns True if the string contains any special characters, otherwise false
*/
function should_quote(str: string) {
return SPECIAL_CHARS.some((char) => str.includes(char));
}

/**
* This function enclose a name string in single quotes if it contains special characters predefined by the SPECIAL_CHARS list
* @param name The name to escape
* @returns A name enclosed in single quotes if it contains special characters, otherwise the name itself
*/
export function quote_name(name: string): string {
return should_quote(name) ? `'${name}'` : name;
}

/**
* This function encloses all keys in a record in single quotes if they contain special characters
* @param record The record to quote
* @returns A new record with all keys enclosed in single quotes if they contain special characters
*/
export function quote_record(record: Record<string, any>): Record<string, any> {
return Object.fromEntries(
Object.entries(record).map(([key, value]) => {
return [quote_name(key), value];
}),
);
}
Loading