-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(driver-adapters): fix bigint handling on creation and filter (#4648
) * feat(driver-adapters): fix bigint handling on creation and filter * chore(driver-adapters): comment out native_other_types, like it was before * chore(driver-adapters): excluded PlanetScale from test * tmp/fix: avoid adding quotes to bigint numbers in PlanetScale, waiting for upstream PR to be merged * chore: update comments, remove PlanetScale exclusion for "using_bigint_field" test
- Loading branch information
Showing
6 changed files
with
105 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
query-engine/driver-adapters/executor/src/planetscale/sanitize.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// This is a temporary workaround for `bigint` serialization issues in the Planetscale driver, | ||
// which will be fixed upstream once https://github.com/planetscale/database-js/pull/159 is published. | ||
// This only impacts Rust tests concerning `driver-adapters`. | ||
|
||
type Stringable = { toString: () => string } | ||
type Value = null | undefined | number | boolean | string | Array<Value> | Date | Stringable | ||
|
||
export function format(query: string, values: Value[] | Record<string, Value>): string { | ||
return Array.isArray(values) ? replacePosition(query, values) : replaceNamed(query, values) | ||
} | ||
|
||
function replacePosition(query: string, values: Value[]): string { | ||
let index = 0 | ||
return query.replace(/\?/g, (match) => { | ||
return index < values.length ? sanitize(values[index++]) : match | ||
}) | ||
} | ||
|
||
function replaceNamed(query: string, values: Record<string, Value>): string { | ||
return query.replace(/:(\w+)/g, (match, name) => { | ||
return hasOwn(values, name) ? sanitize(values[name]) : match | ||
}) | ||
} | ||
|
||
function hasOwn(obj: unknown, name: string): boolean { | ||
return Object.prototype.hasOwnProperty.call(obj, name) | ||
} | ||
|
||
function sanitize(value: Value): string { | ||
if (value == null) { | ||
return 'null' | ||
} | ||
|
||
if (['number', 'bigint'].includes(typeof value)) { | ||
return String(value) | ||
} | ||
|
||
if (typeof value === 'boolean') { | ||
return value ? 'true' : 'false' | ||
} | ||
|
||
if (typeof value === 'string') { | ||
return quote(value) | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return value.map(sanitize).join(', ') | ||
} | ||
|
||
if (value instanceof Date) { | ||
return quote(value.toISOString().slice(0, -1)) | ||
} | ||
|
||
return quote(value.toString()) | ||
} | ||
|
||
function quote(text: string): string { | ||
return `'${escape(text)}'` | ||
} | ||
|
||
const re = /[\0\b\n\r\t\x1a\\"']/g | ||
|
||
function escape(text: string): string { | ||
return text.replace(re, replacement) | ||
} | ||
|
||
function replacement(text: string): string { | ||
switch (text) { | ||
case '"': | ||
return '\\"' | ||
case "'": | ||
return "\\'" | ||
case '\n': | ||
return '\\n' | ||
case '\r': | ||
return '\\r' | ||
case '\t': | ||
return '\\t' | ||
case '\\': | ||
return '\\\\' | ||
case '\0': | ||
return '\\0' | ||
case '\b': | ||
return '\\b' | ||
case '\x1a': | ||
return '\\Z' | ||
default: | ||
return '' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters