8 lines
9.6 KiB
Plaintext
8 lines
9.6 KiB
Plaintext
|
{
|
||
|
"version": 3,
|
||
|
"sources": ["../../src/utils/sql.ts"],
|
||
|
"sourcesContent": ["import isPlainObject from 'lodash/isPlainObject';\nimport type { AbstractDialect } from '../dialects/abstract/index.js';\nimport { escape as escapeSqlValue } from '../sql-string';\n\ntype BindOrReplacements = { [key: string]: unknown } | unknown[];\n\n/**\n * Inlines replacements in places where they would be valid SQL values.\n *\n * @param sqlString The SQL that contains the replacements\n * @param dialect The dialect of the SQL\n * @param replacements if provided, this method will replace ':named' replacements & positional replacements (?)\n *\n * @returns The SQL with replacements rewritten in their dialect-specific syntax.\n */\nexport function injectReplacements(\n sqlString: string,\n dialect: AbstractDialect,\n replacements: BindOrReplacements\n): string {\n if (replacements == null) {\n return sqlString;\n }\n\n if (!Array.isArray(replacements) && !isPlainObject(replacements)) {\n throw new TypeError(`\"replacements\" must be an array or a plain object, but received ${JSON.stringify(replacements)} instead.`);\n }\n\n const isNamedReplacements = isPlainObject(replacements);\n const isPositionalReplacements = Array.isArray(replacements);\n let lastConsumedPositionalReplacementIndex = -1;\n\n let output = '';\n\n let currentDollarStringTagName = null;\n let isString = false;\n let isColumn = false;\n let previousSliceEnd = 0;\n let isSingleLineComment = false;\n let isCommentBlock = false;\n\n for (let i = 0; i < sqlString.length; i++) {\n const char = sqlString[i];\n\n if (isColumn) {\n if (char === dialect.TICK_CHAR_RIGHT) {\n isColumn = false;\n }\n\n continue;\n }\n\n if (isString) {\n if (char === '\\'' && !isBackslashEscaped(sqlString, i - 1)) {\n isString = false;\n }\n\n continue;\n }\n\n if (currentDollarStringTagName !== null) {\n if (char !== '$') {\n continue;\n }\n\n const remainingString = sqlString.slice(i, sqlString.length);\n\n const dollarStringStartMatch = remainingString.match(/^\\$(?<name>[a-z_][0-9a-z_])?(\\$)/i);\n const tagName = dollarStringStartMatch?.groups?.name;\n if (currentDollarStringTagName === tagName) {\n currentDollarStringTagName = null;\n }\n\n continue;\n }\n\n if (isSingleLineComment) {\n if (char === '\\n') {\n isSingleLineComment = false;\n }\n\n continue;\n }\n\n if (isCommentBlock) {\n if (char === '*' && sqlString[i + 1] === '/') {\n isCommentBlock = false;\n }\n\n continue;\n }\n\n if (char === dialect.TICK_CHAR_LEFT) {\n isColumn = true;\n continue;\n }\n\n if (char === '\\'') {\n isString = true;\n continue;\n }\n\n if (char === '-' && sqlString.slice(i, i + 3) === '-- ') {\n isSingleLineComment = true;\n continue;\n }\n\n if (char === '/' && sqlString.slice(i, i + 2) === '/*') {\n isCommentBlock = true;\n continue;\n }\n\n // either the start of a $bind parameter, or the start of a $tag$string$tag$\n if (char === '$') {\n const previousChar = sqlString[i - 1];\n\n // we are part of an identifier\n if (/[0-9a-z_]/i.test(previousChar)) {\n continue;\n }\n\n const remainingString = sqlString.slice(i, sqlString.length);\n\n const dollarStringStartMatch = remainingString.match(/^\\$(?<name>[a-z_][0-9a-z_]*)?\\$/i);\n if (dollarStringStartMatch) {\n currentDollarStringTagName = dollarStringStartMatch.groups?.name ?? '';\n\n continue;\n }\n\n continue;\n }\n\n if (isNamedReplacements && char === ':') {\n const previousChar = sqlString[i - 1];\n // we want to be conservative with what we consider to be a replacement to avoid risk of conflict with potential operators\n // users need to add a space before the bind parameter (except after '(', ',', and '=', '[' (for arrays))\n if (previousChar !== undefined && !/[\\s(,=[]/.test(previousChar)) {\n continue;\n }\n\n const remainingStrin
|
||
|
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA,2BAA0B;AAE1B,wBAAyC;AAalC,4BACL,WACA,SACA,cACQ;AAnBV;AAoBE,MAAI,gBAAgB,MAAM;AACxB,WAAO;AAAA;AAGT,MAAI,CAAC,MAAM,QAAQ,iBAAiB,CAAC,kCAAc,eAAe;AAChE,UAAM,IAAI,UAAU,mEAAmE,KAAK,UAAU;AAAA;AAGxG,QAAM,sBAAsB,kCAAc;AAC1C,QAAM,2BAA2B,MAAM,QAAQ;AAC/C,MAAI,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