Skip to content

Commit

Permalink
Merge pull request #54 from moalamri/fix49
Browse files Browse the repository at this point in the history
Fix #49
  • Loading branch information
JacobLinCool authored Apr 15, 2024
2 parents 1e980bd + f87d4c6 commit a128efe
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
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/
2 changes: 1 addition & 1 deletion src/lib/plugin/RunQuery.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
export let database: string;
export let table: string;
$: query = `SELECT * FROM ${table} LIMIT 100`;
$: query = `SELECT * FROM \`${table}\` LIMIT 100`;
$: danger = is_dangerous(query);
let running = false;
Expand Down
4 changes: 2 additions & 2 deletions src/routes/api/db/[database]/[table]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const GET: RequestHandler = async ({ params, locals, url, fetch }) => {
throw error(404, "Database not found");
}

const { results } = await db.prepare(`SELECT COUNT(*) AS count FROM ${params.table}`).all<{
const { results } = await db.prepare(`SELECT COUNT(*) AS count FROM \`${params.table}\``).all<{
count: number;
}>();

Expand All @@ -35,6 +35,6 @@ export const DELETE: RequestHandler = async ({ params, locals }) => {
throw error(404, "Database not found");
}

const result = await db.prepare(`DROP TABLE ${params.table}`).run();
const result = await db.prepare(`DROP TABLE \`${params.table}\``).run();
return json(result);
};
14 changes: 7 additions & 7 deletions src/routes/api/db/[database]/[table]/data/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const GET: RequestHandler = async ({ url, params, locals, fetch }) => {

const { results } = await db
.prepare(
`SELECT ${select} FROM ${params.table}${where ? ` WHERE ${where}` : ""}${
`SELECT ${select} FROM \`${params.table}\`${where ? ` WHERE ${where}` : ""}${
order ? ` ORDER BY ${order} ${dir}` : ""
} LIMIT ${limit} OFFSET ${offset}`,
)
Expand Down Expand Up @@ -58,9 +58,9 @@ export const POST: RequestHandler = async ({ request, params, locals }) => {

const statement = db
.prepare(
`INSERT INTO ${params.table} (${Object.keys(data).join(", ")}) VALUES (${Object.keys(
data,
)
`INSERT INTO \`${params.table}\` (${Object.keys(data)
.map((key) => `\`${key}\``)
.join(", ")}) VALUES (${Object.keys(data)
.map(() => "?")
.join(", ")})`,
)
Expand Down Expand Up @@ -95,8 +95,8 @@ export const PUT: RequestHandler = async ({ url, request, params, locals }) => {

const statement = db
.prepare(
`UPDATE ${params.table} SET ${Object.keys(data)
.map((key) => `${key} = ?`)
`UPDATE \`${params.table}\` SET ${Object.keys(data)
.map((key) => `\`${key}\` = ?`)
.join(", ")} WHERE ${where_sql(where)}`,
)
.bind(...Object.values(data), ...Object.values(where));
Expand All @@ -119,7 +119,7 @@ export const DELETE: RequestHandler = async ({ url, params, locals }) => {
const where = Object.fromEntries(url.searchParams.entries());

const statement = db
.prepare(`DELETE FROM ${params.table} WHERE ${where_sql(where)}`)
.prepare(`DELETE FROM \`${params.table}\` WHERE ${where_sql(where)}`)
.bind(...Object.values(where));
const result = await statement.run();
return json(result);
Expand Down

0 comments on commit a128efe

Please sign in to comment.