-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from outerbase/invisal/support-raw-placeholders
support raw based holder
- Loading branch information
Showing
14 changed files
with
369 additions
and
24 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
const RE_PARAM = /(?:\?)|(?::(\d+|(?:[a-zA-Z][a-zA-Z0-9_]*)))/g, | ||
DQUOTE = 34, | ||
SQUOTE = 39, | ||
BSLASH = 92; | ||
|
||
/** | ||
* This code is based on https://github.com/mscdex/node-mariasql/blob/master/lib/Client.js#L296-L420 | ||
* License: https://github.com/mscdex/node-mariasql/blob/master/LICENSE | ||
* | ||
* @param query | ||
* @returns | ||
*/ | ||
function parse(query: string): [string] | [string[], (string | number)[]] { | ||
let ppos = RE_PARAM.exec(query); | ||
let curpos = 0; | ||
let start = 0; | ||
let end; | ||
const parts = []; | ||
let inQuote = false; | ||
let escape = false; | ||
let qchr; | ||
const tokens = []; | ||
let qcnt = 0; | ||
let lastTokenEndPos = 0; | ||
let i; | ||
|
||
if (ppos) { | ||
do { | ||
for (i = curpos, end = ppos.index; i < end; ++i) { | ||
let chr = query.charCodeAt(i); | ||
if (chr === BSLASH) escape = !escape; | ||
else { | ||
if (escape) { | ||
escape = false; | ||
continue; | ||
} | ||
if (inQuote && chr === qchr) { | ||
if (query.charCodeAt(i + 1) === qchr) { | ||
// quote escaped via "" or '' | ||
++i; | ||
continue; | ||
} | ||
inQuote = false; | ||
} else if (!inQuote && (chr === DQUOTE || chr === SQUOTE)) { | ||
inQuote = true; | ||
qchr = chr; | ||
} | ||
} | ||
} | ||
if (!inQuote) { | ||
parts.push(query.substring(start, end)); | ||
tokens.push(ppos[0].length === 1 ? qcnt++ : ppos[1]); | ||
start = end + ppos[0].length; | ||
lastTokenEndPos = start; | ||
} | ||
curpos = end + ppos[0].length; | ||
} while ((ppos = RE_PARAM.exec(query))); | ||
|
||
if (tokens.length) { | ||
if (curpos < query.length) { | ||
parts.push(query.substring(lastTokenEndPos)); | ||
} | ||
return [parts, tokens]; | ||
} | ||
} | ||
return [query]; | ||
} | ||
|
||
export function namedPlaceholder( | ||
query: string, | ||
params: Record<string, unknown>, | ||
numbered = false | ||
): { query: string; bindings: unknown[] } { | ||
const parts = parse(query); | ||
|
||
if (parts.length === 1) { | ||
return { query, bindings: [] }; | ||
} | ||
|
||
const bindings = []; | ||
let newQuery = ''; | ||
|
||
const [sqlFragments, placeholders] = parts; | ||
|
||
// If placeholders contains any number, then it's a mix of named and numbered placeholders | ||
if (placeholders.some((p) => typeof p === 'number')) { | ||
throw new Error( | ||
'Mixing named and positional placeholder should throw error' | ||
); | ||
} | ||
|
||
for (let i = 0; i < sqlFragments.length; i++) { | ||
newQuery += sqlFragments[i]; | ||
|
||
if (i < placeholders.length) { | ||
const key = placeholders[i]; | ||
|
||
if (numbered) { | ||
newQuery += `$${i + 1}`; | ||
} else { | ||
newQuery += `?`; | ||
} | ||
|
||
const placeholderValue = params[key]; | ||
if (placeholderValue === undefined) { | ||
throw new Error(`Missing value for placeholder ${key}`); | ||
} | ||
|
||
bindings.push(params[key]); | ||
} | ||
} | ||
|
||
return { query: newQuery, bindings }; | ||
} | ||
|
||
export function toNumberedPlaceholders( | ||
query: string, | ||
params: unknown[] | ||
): { | ||
query: string; | ||
bindings: unknown[]; | ||
} { | ||
const parts = parse(query); | ||
|
||
if (parts.length === 1) { | ||
return { query, bindings: [] }; | ||
} | ||
|
||
const bindings = []; | ||
let newQuery = ''; | ||
|
||
const [sqlFragments, placeholders] = parts; | ||
|
||
if (placeholders.length !== params.length) { | ||
throw new Error( | ||
'Number of positional placeholder should match with the number of values' | ||
); | ||
} | ||
|
||
// Mixing named and numbered placeholders should throw error | ||
if (placeholders.some((p) => typeof p === 'string')) { | ||
throw new Error( | ||
'Mixing named and positional placeholder should throw error' | ||
); | ||
} | ||
|
||
for (let i = 0; i < sqlFragments.length; i++) { | ||
newQuery += sqlFragments[i]; | ||
|
||
if (i < placeholders.length) { | ||
newQuery += `$${i + 1}`; | ||
bindings.push(params[i]); | ||
} | ||
} | ||
|
||
return { query: newQuery, bindings }; | ||
} |
Oops, something went wrong.