-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
286 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// @flow | ||
|
||
export {default as normalizeValuePlaceholders} from './normalizeValuePlaceholders'; | ||
export {default as normalizeAnonymousValuePlaceholders} from './normalizeAnonymousValuePlaceholders'; | ||
export {default as normalizeNamedValuePlaceholders} from './normalizeNamedValuePlaceholders'; |
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,59 @@ | ||
// @flow | ||
|
||
import type { | ||
NamedValuePlaceholderValuesType, | ||
NormalizedQueryType | ||
} from '../types'; | ||
|
||
/** | ||
* @see https://regex101.com/r/KrEe8i/1 | ||
*/ | ||
const namedPlaceholderRegex = /[\s,(]:([a-zA-Z]+)/g; | ||
|
||
/** | ||
* @see https://github.com/mysqljs/sqlstring/blob/f946198800a8d7f198fcf98d8bb80620595d01ec/lib/SqlString.js#L73 | ||
*/ | ||
export default ( | ||
sql: string, | ||
values: NamedValuePlaceholderValuesType = {} | ||
): NormalizedQueryType => { | ||
let chunkIndex = 0; | ||
let result = ''; | ||
let match; | ||
let placeholderIndex = 0; | ||
|
||
const normalizedValues = []; | ||
|
||
// eslint-disable-next-line no-cond-assign | ||
while (match = namedPlaceholderRegex.exec(sql)) { | ||
const matchIndex = match.index + 1; | ||
const matchName = match[1]; | ||
|
||
if (!values.hasOwnProperty(matchName)) { | ||
throw new Error('Value placeholder is missing a value.'); | ||
} | ||
|
||
const value = values[matchName]; | ||
|
||
normalizedValues.push(value); | ||
|
||
result += sql.slice(chunkIndex, matchIndex); | ||
|
||
chunkIndex = namedPlaceholderRegex.lastIndex; | ||
|
||
++placeholderIndex; | ||
|
||
result += '$' + placeholderIndex; | ||
} | ||
|
||
if (chunkIndex === 0) { | ||
result = sql; | ||
} else if (chunkIndex < sql.length) { | ||
result += sql.slice(chunkIndex); | ||
} | ||
|
||
return { | ||
sql: result, | ||
values: normalizedValues | ||
}; | ||
}; |
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 @@ | ||
// @flow | ||
|
||
import test from 'ava'; | ||
import { | ||
normalizeAnonymousValuePlaceholders | ||
} from '../../src/utilities'; | ||
|
||
test('does not error when placeholders are absent', (t) => { | ||
const { | ||
sql, | ||
values | ||
} = normalizeAnonymousValuePlaceholders('SELECT 1'); | ||
|
||
t.true(sql === 'SELECT 1'); | ||
t.deepEqual(values, []); | ||
}); | ||
|
||
test('interpolates a value placeholder', (t) => { | ||
const { | ||
sql, | ||
values | ||
} = normalizeAnonymousValuePlaceholders('SELECT ?', [ | ||
'foo' | ||
]); | ||
|
||
t.true(sql === 'SELECT $1'); | ||
t.deepEqual(values, [ | ||
'foo' | ||
]); | ||
}); | ||
|
||
test('interpolates multiple value placeholders', (t) => { | ||
const { | ||
sql, | ||
values | ||
} = normalizeAnonymousValuePlaceholders('SELECT ?, ?', [ | ||
'foo', | ||
'bar' | ||
]); | ||
|
||
t.true(sql === 'SELECT $1, $2'); | ||
t.deepEqual(values, [ | ||
'foo', | ||
'bar' | ||
]); | ||
}); | ||
|
||
test('interpolates a value set', (t) => { | ||
const { | ||
sql, | ||
values | ||
} = normalizeAnonymousValuePlaceholders('SELECT ?', [ | ||
[ | ||
'foo', | ||
'bar' | ||
] | ||
]); | ||
|
||
t.true(sql === 'SELECT ($1, $2)'); | ||
t.deepEqual(values, [ | ||
'foo', | ||
'bar' | ||
]); | ||
}); | ||
|
||
test('interpolates a list of value sets', (t) => { | ||
const { | ||
sql, | ||
values | ||
} = normalizeAnonymousValuePlaceholders('SELECT ?', [ | ||
[ | ||
[ | ||
'foo', | ||
'bar' | ||
], | ||
[ | ||
'baz', | ||
'qux' | ||
] | ||
] | ||
]); | ||
|
||
t.true(sql === 'SELECT ($1, $2), ($3, $4)'); | ||
t.deepEqual(values, [ | ||
'foo', | ||
'bar', | ||
'baz', | ||
'qux' | ||
]); | ||
}); |
Oops, something went wrong.