Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
177 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
const rule = /"/g; | ||
|
||
/** | ||
* @see https://github.com/brianc/node-postgres/blob/6c840aabb09f8a2d640800953f6b884b6841384c/lib/client.js#L306-L322 | ||
*/ | ||
export const escapeIdentifier = (identifier: string): string => { | ||
return '"' + identifier.replace(/"/g, '""') + '"'; | ||
return '"' + identifier.replace(rule, '""') + '"'; | ||
}; |
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,26 @@ | ||
/** | ||
* @see https://github.com/brianc/node-postgres/blob/6c840aabb09f8a2d640800953f6b884b6841384c/lib/client.js#L325-L348 | ||
*/ | ||
export const escapeLiteralValue = (subject: string): string => { | ||
let hasBackslash = false; | ||
let escaped = '\''; | ||
|
||
for (const character of subject) { | ||
if (character === '\'') { | ||
escaped += character + character; | ||
} else if (character === '\\') { | ||
escaped += character + character; | ||
hasBackslash = true; | ||
} else { | ||
escaped += character; | ||
} | ||
} | ||
|
||
escaped += '\''; | ||
|
||
if (hasBackslash === true) { | ||
escaped = 'E' + escaped; | ||
} | ||
|
||
return escaped; | ||
}; |
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,19 @@ | ||
import test from 'ava'; | ||
import { | ||
createSqlTag, | ||
} from '../../../../src/factories/createSqlTag'; | ||
import { | ||
SqlToken, | ||
} from '../../../../src/tokens'; | ||
|
||
const sql = createSqlTag(); | ||
|
||
test('creates an object describing a query with an inlined literal value', (t) => { | ||
const query = sql`CREATE USER foo WITH PASSWORD ${sql.literalValue('bar')}`; | ||
|
||
t.deepEqual(query, { | ||
sql: 'CREATE USER foo WITH PASSWORD \'bar\'', | ||
type: SqlToken, | ||
values: [], | ||
}); | ||
}); |
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,11 @@ | ||
import test from 'ava'; | ||
import { | ||
escapeLiteralValue, | ||
} from '../../../src/utilities'; | ||
|
||
test('escapes SQL literal value', (t) => { | ||
t.is(escapeLiteralValue('foo'), '\'foo\''); | ||
t.is(escapeLiteralValue('foo bar'), '\'foo bar\''); | ||
t.is(escapeLiteralValue('"foo"'), '\'"foo"\''); | ||
t.is(escapeLiteralValue('foo\\bar'), 'E\'foo\\\\bar\''); | ||
}); |