Comparing version 0.1.0 to 0.2.0
v0.2.0 / 2015-12-02 | ||
=================== | ||
* Merge pull request #10 from kessler/master | ||
* Add support for a very useful pg feature: http://www.postgresql.org/docs/8.3/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING | ||
v0.1.0 / 2015-04-13 | ||
@@ -3,0 +9,0 @@ =================== |
39
index.js
@@ -37,3 +37,3 @@ | ||
var args = arguments; | ||
return fmt.replace(/%([%sIL])/g, function(_, type){ | ||
return fmt.replace(/%([%sILQ])/g, function(_, type){ | ||
if ('%' == type) return '%'; | ||
@@ -46,2 +46,3 @@ | ||
case 'L': return exports.literal(arg); | ||
case 'Q': return exports.dollarQuotedString(arg); | ||
} | ||
@@ -64,2 +65,38 @@ }); | ||
/** | ||
* Dollar-Quoted String Constants | ||
*/ | ||
var randomTags = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'j', 'k', | ||
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']; | ||
/** | ||
* produces a random number from a given range | ||
* | ||
* @param {Number} start start of random numbers range | ||
* @param {Number} end end of random numbers range (inclusive) | ||
* @return {Number} | ||
* @api private | ||
*/ | ||
function random(start, end) { | ||
var range = end - start; | ||
return Math.floor((Math.random() * range) + start); | ||
} | ||
/** | ||
* Format as dollar quoted string. | ||
* see: http://www.postgresql.org/docs/8.3/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING | ||
* | ||
* @param {Mixed} val | ||
* @return {String} | ||
* @api public | ||
*/ | ||
exports.dollarQuotedString = function(val) { | ||
if (val === undefined || val === null || val === '') return ''; | ||
var randomTag = '$'+ randomTags[ random(0, randomTags.length) ] +'$'; | ||
return randomTag + val + randomTag; | ||
} | ||
/** | ||
* Format as identifier. | ||
@@ -66,0 +103,0 @@ * |
{ | ||
"name": "pg-escape", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"repository": "segmentio/pg-escape", | ||
@@ -5,0 +5,0 @@ "description": "escape postgres queries which do not support stored procedures", |
@@ -35,2 +35,6 @@ | ||
### escape.dollarQuotedString(val) | ||
Format as a [dollar quoted string](http://www.postgresql.org/docs/8.3/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING) | ||
### escape.ident(val) | ||
@@ -47,2 +51,3 @@ | ||
- `%s` formats the argument value as a simple string. A null value is treated as an empty string. | ||
- `%Q` formats the argument value as a [dollar quoted string](http://www.postgresql.org/docs/8.3/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING). A null value is treated as an empty string. | ||
- `%I` treats the argument value as an SQL identifier, double-quoting it if necessary. It is an error for the value to be null. | ||
@@ -49,0 +54,0 @@ - `%L` quotes the argument value as an SQL literal. A null value is displayed as the string NULL, without quotes. |
@@ -41,2 +41,9 @@ | ||
}) | ||
describe('%Q', function(){ | ||
it('should format as a dollar quoted string', function(){ | ||
escape('%Q', "Tobi's") | ||
.should.match(/\$[a-z]{1}\$Tobi's\$[a-z]\$/); | ||
}) | ||
}) | ||
}) | ||
@@ -53,2 +60,11 @@ | ||
describe('escape.dollarQuotedString(val)', function() { | ||
it('should coerce to a dollar quoted string', function(){ | ||
escape.dollarQuotedString().should.equal(''); | ||
escape.dollarQuotedString(0).should.match(/\$[a-z]{1}\$0\$[a-z]\$/); | ||
escape.dollarQuotedString(15).should.match(/\$[a-z]{1}\$15\$[a-z]\$/); | ||
escape.dollarQuotedString('something').should.match(/\$[a-z]{1}\$something\$[a-z]\$/); | ||
}) | ||
}) | ||
describe('escape.ident(val)', function(){ | ||
@@ -55,0 +71,0 @@ it('should quote when necessary', function(){ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9910
227
57