-
-
Notifications
You must be signed in to change notification settings - Fork 19
Add support for bind params #42
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* A param that's expected to be bound later of included in `values`. | ||
*/ | ||
export const BIND_PARAM = Symbol("BIND_PARAM"); | ||
|
||
/** | ||
* Values supported by SQL engine. | ||
*/ | ||
|
@@ -12,6 +17,7 @@ export type RawValue = Value | Sql; | |
* A SQL instance can be nested within each other to build SQL strings. | ||
*/ | ||
export class Sql { | ||
readonly bindParams = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only question to myself is whether |
||
readonly values: Value[]; | ||
readonly strings: string[]; | ||
|
||
|
@@ -53,15 +59,20 @@ export class Sql { | |
|
||
let childIndex = 0; | ||
while (childIndex < child.values.length) { | ||
this.values[pos++] = child.values[childIndex++]; | ||
this.strings[pos] = child.strings[childIndex]; | ||
const value = child.values[childIndex++]; | ||
const str = child.strings[childIndex]; | ||
|
||
this.values[pos++] = value; | ||
this.strings[pos] = str; | ||
if (value === BIND_PARAM) this.bindParams++; | ||
} | ||
|
||
// Append raw string to current string. | ||
this.strings[pos] += rawString; | ||
} else { | ||
this.values[pos++] = child; | ||
this.strings[pos] = rawString; | ||
if (child === BIND_PARAM) this.bindParams++; | ||
} | ||
} | ||
} | ||
|
@@ -90,6 +101,23 @@ export class Sql { | |
return value; | ||
} | ||
|
||
bind(...params: Value[]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't technically need this method if you know what you're doing, but the array length safety and merging with any non-params seems like a good pattern to enforce. |
||
if (params.length !== this.bindParams) { | ||
throw new TypeError( | ||
`Expected ${this.bindParams} parameters to be bound, but got ${params.length}`, | ||
); | ||
} | ||
|
||
const values = new Array(this.values.length); | ||
|
||
for (let i = 0, j = 0; i < this.values.length; i++) { | ||
const value = this.values[i]; | ||
values[i] = value === BIND_PARAM ? params[j++] : value; | ||
} | ||
|
||
return values; | ||
} | ||
|
||
inspect() { | ||
return { | ||
sql: this.sql, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name options:
BIND_PARAMETER
BIND_VALUE
BIND
BIND_KEY
PARAM