diff --git a/index.js b/index.js index abdbf7a1..e9d15c79 100644 --- a/index.js +++ b/index.js @@ -202,8 +202,8 @@ function $wrapValue(val) { // Formats array of javascript-type parameters into a csv list // to be passed into a function, compatible with PostgreSQL. // It can understand both a simple value and an array of simple values. -function $formatParams(values) { - var s = ''; +function $formatCSV(values) { + var s = ""; if (Array.isArray(values)) { for (var i = 0; i < values.length; i++) { if (i > 0) { @@ -222,10 +222,13 @@ function $formatParams(values) { s = $wrapValue(values); if (s === null) { throw new Error("Cannot convert a value of type '" + typeof(values) + "'"); + }else{ + if(typeof(s) !== 'string'){ + s = s.toString(); + } } } } - return s; } @@ -256,33 +259,27 @@ var $wrap = { } }, // Creates a comma-separated list of values formatted for use with PostgreSQL; - csv: function (arr) { - if ($isNull(arr)) { - return 'null'; - } - if (Array.isArray(arr)) { - return $formatParams(arr); - } else { - throw new Error($wrapText(arr) + " doesn't represent a valid Array object or value."); - } + // 'values' can be either an array of simple values, or just one simple value. + csv: function (values) { + return $formatCSV(values); }, // Formats query - parameter using the values passed (simple value or array of simple values); // The main reason for exposing this to the client is to include the parser into the test. // The query can contain variables $1, $2, etc, and 'values' is either one simple value or // an array of simple values, such as: text, boolean, date, numeric or null. format: function (query, values) { - return $formatValues(query, values); + return $formatQuery(query, values); } }; // Formats a proper function call from the parameters. function $createFuncQuery(funcName, values) { - return 'select * from ' + funcName + '(' + $formatParams(values) + ');'; + return 'select * from ' + funcName + '(' + $formatCSV(values) + ');'; } // Parses query for $1, $2,... variables and replaces them with the values passed. // 'values' can be an array of simple values, or just one simple value. -function $formatValues(query, values) { +function $formatQuery(query, values) { var q = query; var result = { success: true @@ -354,7 +351,7 @@ function $query(client, query, values, qrm, options) { query: query }; } else { - req = $formatValues(query, values); + req = $formatQuery(query, values); if (!req.success) { errMsg = req.error; } diff --git a/package.json b/package.json index af581123..09530c9c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-promise", - "version": "0.6.0", + "version": "0.6.1", "description": "PG + Promises/A+, with transactions support.", "main": "index.js", "scripts": { diff --git a/test/indexSpec.js b/test/indexSpec.js index 13f5fb7c..a38ac993 100644 --- a/test/indexSpec.js +++ b/test/indexSpec.js @@ -121,27 +121,37 @@ describe("Type conversion in pgp.as", function () { }); it("must correctly convert any Array of values into CSV", function () { - expect(function () { - pgp.as.csv(1); // test an integer; - }).toThrow("'1' doesn't represent a valid Array object or value."); - expect(function () { - pgp.as.csv("'' doesn't represent a valid Array object or value."); // test an empty string; - }).toThrow(); - expect(function () { - pgp.as.csv("'text' doesn't represent a valid Array object or value."); // test a text string; - }).toThrow(); - expect(pgp.as.csv()).toBe("null"); // test undefined; - expect(pgp.as.csv(null)).toBe("null"); // test null; + expect(pgp.as.csv()).toBe(""); // test undefined; expect(pgp.as.csv([])).toBe(""); // test empty array; - expect(pgp.as.csv([1])).toBe("1"); // test an integer; - expect(pgp.as.csv([-123.456])).toBe("-123.456"); // test a float; - expect(pgp.as.csv(["Hello World!"])).toBe("'Hello World!'"); // test a text; - expect(pgp.as.csv([true])).toBe("TRUE"); // test boolean True; - expect(pgp.as.csv([false])).toBe("FALSE"); // test boolean False; - expect(pgp.as.csv([new Date(2015, 2, 8, 16, 24, 8)])).toBe("'Sun, 08 Mar 2015 16:24:08 GMT'"); // test date; - // test a combination of values; - expect(pgp.as.csv([1, true, "don't break", new Date(2015, 2, 8, 16, 24, 8)])).toBe("1,TRUE,'don''t break','Sun, 08 Mar 2015 16:24:08 GMT'"); + expect(pgp.as.csv(null)).toBe("null"); // test null; + expect(pgp.as.csv([null])).toBe("null"); // test null in array; + expect(pgp.as.csv([undefined])).toBe("null"); // test undefined in array; + expect(pgp.as.csv([null, undefined])).toBe("null,null"); // test combination of null + undefined in array; + + expect(pgp.as.csv(0)).toBe("0"); // test zero; + expect(pgp.as.csv([0])).toBe("0"); // test zero in array; + expect(pgp.as.csv(-123.456)).toBe("-123.456"); // test a float; + expect(pgp.as.csv([-123.456])).toBe("-123.456"); // test a float in array; + + expect(pgp.as.csv(true)).toBe("TRUE"); // test boolean True; + expect(pgp.as.csv([true])).toBe("TRUE"); // test boolean True in array; + + expect(pgp.as.csv(false)).toBe("FALSE"); // test boolean False; + expect(pgp.as.csv([false])).toBe("FALSE"); // test boolean False in array; + + expect(pgp.as.csv("")).toBe("''"); // empty text; + expect(pgp.as.csv([""])).toBe("''"); // empty text in array; + expect(pgp.as.csv("simple text")).toBe("'simple text'"); // simple text; + expect(pgp.as.csv("don't break")).toBe("'don''t break'"); // text with one single-quote symbol; + expect(pgp.as.csv("test ''")).toBe("'test '''''"); // text with two single-quote symbols; + + expect(pgp.as.csv(new Date(2015, 2, 8, 16, 24, 8))).toBe("'Sun, 08 Mar 2015 16:24:08 GMT'"); // test date; + expect(pgp.as.csv([new Date(2015, 2, 8, 16, 24, 8)])).toBe("'Sun, 08 Mar 2015 16:24:08 GMT'"); // test date in array; + + // test a combination of all values types; + expect(pgp.as.csv([12.34, true, "don't break", undefined, new Date(2015, 2, 8, 16, 24, 8)])) + .toBe("12.34,TRUE,'don''t break',null,'Sun, 08 Mar 2015 16:24:08 GMT'"); }); it("must format correctly any query with variables", function () {