Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dollar quoted string constants #127

Merged
merged 2 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,6 @@ This is required for some SQL operations that cannot be run within a transaction
- `returns` _[string]_ - returns clause
- `language` _[string]_ - language name of function definition
- `replace` _[boolean]_ - create or replace function
- `delimiter` _[string]_ - delimiter for definition
- `window` _[boolean]_ - window function
- `behavior` _[string]_ - `IMMUTABLE`, `STABLE`, or `VOLATILE`
- `onNull` _[boolean]_ - `RETURNS NULL ON NULL INPUT`
Expand Down
5 changes: 1 addition & 4 deletions lib/operations/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const create = (type_shorthands) => {
const {
replace,
returns = 'void',
delimiter = '$$',
language,
window,
behavior = 'VOLATILE',
Expand Down Expand Up @@ -62,9 +61,7 @@ export const create = (type_shorthands) => {

return template`CREATE${replace ? ' OR REPLACE' : ''} FUNCTION "${function_name}"${formatParams(function_params, type_shorthands)}
RETURNS ${returns}
AS ${delimiter}
${definition}
${delimiter}
AS ${escapeValue(definition)}
${options.join('\n ')};`;
};

Expand Down
8 changes: 7 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ export const escapeValue = (val) => {
return val.toString();
}
if (typeof val === 'string') {
return `'${escape(val)}'`;
let dollars;
let index = 0;
do {
index += 1;
dollars = `$pg${index}$`;
} while (val.indexOf(dollars) >= 0);
return `${dollars}${val}${dollars}`;
}
if (typeof val === 'number') {
return val;
Expand Down