Skip to content

Commit

Permalink
Throwing error if parameter is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
acrodrig committed Sep 28, 2023
1 parent d589e52 commit ca34943
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"include": ["src/", "test/"]
},
"imports": {
"mysql/": "https://deno.land/x/[email protected].0/",
"mysql/": "https://deno.land/x/[email protected].1/",
"postgres/": "https://deno.land/x/[email protected]/",
"sqlite/": "https://deno.land/x/[email protected]/",
"std/": "https://deno.land/std@0.201.0/"
"std/": "https://deno.land/std@0.203.0/"
},
"tasks": {
"check": "deno check **/*.ts && deno fmt --check",
Expand Down
5 changes: 3 additions & 2 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@ export class DB {
}

// Transforms parameters (and SQL) into array-like and references via `?`
static _transformParameters(sql: string, objectParameters: { [key: string]: unknown }, arrayParameters: unknown[]): string {
static _transformParameters(sql: string, objectParameters: { [key: string]: unknown }, arrayParameters: unknown[], safe?: boolean): string {
arrayParameters.splice(0, arrayParameters.length);
return sql.replace(/[:][$A-Z_][0-9A-Z_$]*/ig, function (name) {
const value = objectParameters[name.substring(1)];
if (value === undefined && !safe) throw new Error("Undefined parameter '" + name + "'");
const isArray = Array.isArray(value);
// If it is an array we need to repeat N times the '?' and append all the values
if (isArray) arrayParameters.push(...value);
else arrayParameters.push(value ?? undefined);
else arrayParameters.push(value);
return isArray ? value.map((_) => "?").join(",") : "?";
});
}
Expand Down

0 comments on commit ca34943

Please sign in to comment.