Skip to content

Commit

Permalink
-changing as.csv to be consistent with function parameter formatting,…
Browse files Browse the repository at this point in the history
… supporting simple values.

- code refactoring.
  • Loading branch information
vitaly-t committed Mar 26, 2015
1 parent 62bc3f2 commit bc32708
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
29 changes: 13 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
48 changes: 29 additions & 19 deletions test/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit bc32708

Please sign in to comment.