Skip to content

Commit

Permalink
throwing away support for se parameter in as.format() completely.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Apr 2, 2015
1 parent 4189dff commit 2ae9845
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 212 deletions.
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,11 @@ db.proc(query, values); // calls db.func(query, values, queryResult.one | queryR
```

### Conversion Helpers

The library provides several helper functions to convert basic javascript types into their proper PostgreSQL presentation that can be passed
directly into queries or functions as parameters. All of such helper functions are located within namespace ```pgp.as```:
directly into queries or functions as parameters. All of such helper functions are located within namespace `pgp.as`, and each function
returns a formatted string when successful or throws an error when it fails.

```javascript
pgp.as.bool(value); // returns proper PostgreSQL boolean presentation;

Expand All @@ -393,18 +396,9 @@ pgp.as.date(value); // returns proper PostgreSQL date/time presentation,
pgp.as.csv(array); // returns a CSV string with values formatted according
// to their type, using the above methods;

pgp.as.format(query, values, se);
pgp.as.format(query, values);
// Replaces variables in a query with their `values` as specified.
// `values` is either a simple value or an array of simple values.
// Returns formatted query string, if successful, or throws an error
// when it fails.
// `se` - Suppress Errors. It is an optional parameter, which when set,
// forces return of an object:
// {
// success: true/false,
// query: resulting query text, if success=true, otherwise it is undefined;
// error: error description, if success=false, otherwise it is undefined;
// }
```
As these helpers are not associated with any database, they can be used from anywhere.

Expand Down
123 changes: 41 additions & 82 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,79 +282,43 @@ function $createFuncQuery(funcName, values) {
// 'pg-promise' own query formatting solution;
// It 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;
// When fails, the function throws an error, unless 'se' was set.
// 'se' - suppress errors (optional). When set, the function will never throw an error;
// Instead, it will always return an object:
// {
// success: true/false,
// query: resulting query text, if success=true, otherwise it is undefined;
// error: error description, if success=false, otherwise it is undefined;
// }
function $formatQuery(query, values, se) {
var result = {
success: true
};
// When fails, the function throws an error.
function $formatQuery(query, values) {
if (typeof(query) !== 'string') {
result.success = false;
result.error = "Parameter 'query' must be a text string.";
} else {
if (Array.isArray(values)) {
for (var i = 0; i < values.length; i++) {
// variable name must exist and not be followed by a digit;
var pattern = '\\$' + (i + 1) + '(?!\\d)';
if (query.search(pattern) === -1) {
result.success = false;
result.error = "More values passed in array than variables in the query.";
break;
} else {
// expect a simple value;
var value = $wrapValue(values[i]);
if (value === null) {
// error: not a simple value;
result.success = false;
result.error = "Cannot convert parameter with index " + i;
break;
} else {
var reg = new RegExp(pattern, 'g');
query = query.replace(reg, value);
}
}
throw new Error("Parameter 'query' must be a text string.");
}
if (Array.isArray(values)) {
for (var i = 0; i < values.length; i++) {
// variable name must exist and not be followed by a digit;
var pattern = '\\$' + (i + 1) + '(?!\\d)';
if (query.search(pattern) === -1) {
throw new Error("More values passed in array than variables in the query.");
}
} else {
if (values !== undefined) {
// variable name must exist and not be followed by a digit;
if (query.search(/\$1(?!\d)/) === -1) {
// a single value was passed, but variable $1 doesn't exist;
result.success = false;
result.error = "No variable found in the query to replace with the passed value.";
} else {
// expect a simple value;
var value = $wrapValue(values);
if (value === null) {
// error: not a simple value;
result.success = false;
result.error = "Cannot convert type '" + typeof(values) + "' into a query variable value.";
} else {
query = query.replace(/\$1(?!\d)/g, value);
}
}
// expect a simple value;
var value = $wrapValue(values[i]);
if (value === null) {
// error: not a simple value;
throw new Error("Cannot convert parameter with index " + i);
}
query = query.replace(new RegExp(pattern, 'g'), value);
}
}
if (result.success) {
result.query = query;
}
if (se) {
// suppress errors;
return result;
} else {
// errors are not to be suppressed;
if (result.success) {
return result.query;
} else {
throw new Error(result.error);
if (values !== undefined) {
// variable name must exist and not be followed by a digit;
if (query.search(/\$1(?!\d)/) === -1) {
throw new Error("No variable found in the query to replace with the passed value.");
} else {
// expect a simple value;
var value = $wrapValue(values);
if (value === null) {
// error: not a simple value;
throw new Error("Cannot convert type '" + typeof(values) + "' into a query variable value.");
}
query = query.replace(/\$1(?!\d)/g, value);
}
}
}
return query;
}

// Generic, static query call;
Expand All @@ -363,25 +327,20 @@ function $query(client, query, values, qrm, options) {
if ($isDBNull(qrm)) {
qrm = queryResult.any; // default query result;
}
var errMsg, req, pgFormatting = (options && options.pgFormatting);
var errMsg, pgFormatting = (options && options.pgFormatting);
if (!query) {
errMsg = "Invalid query specified.";
} else {
var badMask = queryResult.one | queryResult.many; // the combination isn't supported;
if ((qrm & badMask) === badMask || qrm < 1 || qrm > 6) {
errMsg = "Invalid Query Result Mask specified.";
} else {
if (pgFormatting) {
// 'node-postgres' will do the parameter formatting for us;
req = {
success: true,
query: query
};
} else {
if (!pgFormatting) {
// use 'pg-promise' implementation of parameter formatting;
req = $formatQuery(query, values, true);
if (!req.success) {
errMsg = req.error;
try {
query = $formatQuery(query, values);
} catch (err) {
errMsg = err.message;
}
}
}
Expand All @@ -396,25 +355,25 @@ function $query(client, query, values, qrm, options) {
throw new Error("Function was expected for 'options.query'");
}
try {
func(client, req.query, params); // notify the client;
func(client, query, params); // notify the client;
} catch (err) {
reject(err);
return;
}
}
try {
client.query(req.query, params, function (err, result) {
client.query(query, params, function (err, result) {
if (err) {
reject(err.message);
} else {
var data = result.rows;
var l = result.rows.length;
if (l) {
if (l > 1 && (qrm & queryResult.one)) {
reject("Single row was expected from query: " + req.query);
reject("Single row was expected from query: " + query);
} else {
if (!(qrm & (queryResult.one | queryResult.many))) {
reject("No return data was expected from query: " + req.query);
reject("No return data was expected from query: " + query);
} else {
if (!(qrm & queryResult.many)) {
data = result.rows[0];
Expand All @@ -425,7 +384,7 @@ function $query(client, query, values, qrm, options) {
if (qrm & queryResult.none) {
data = (qrm & queryResult.many) ? [] : null;
} else {
reject("No rows returned from query: " + req.query);
reject("No rows returned from query: " + query);
}
}
resolve(data);
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.7.0",
"version": "0.7.1",
"description": "PG + Promises/A+, with transactions.",
"main": "index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 2ae9845

Please sign in to comment.