Skip to content

Update from upstream #2

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

Merged
merged 25 commits into from
Feb 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fde38d7
Update README.md
sibedge Jan 20, 2016
fa16966
add failing test that shows the issue
mnahkies Jan 27, 2016
3a9f5c3
wrap entry point to prepareValue to only accept 1 arg
mnahkies Jan 27, 2016
8807936
Merge pull request #921 from sibedge/patch-2
brianc Jan 27, 2016
e6649f2
Merge pull request #925 from mnahkies/fix-prepare-value-map-to-postgres
brianc Jan 27, 2016
cde7b3d
Adding MassiveJS to Extras section of README
bradymholt Jan 27, 2016
a49181a
Merge pull request #926 from bradyholt/patch-1
brianc Jan 27, 2016
10a0e48
Bump version
brianc Jan 27, 2016
efdcf75
pg-rxjs mention added
jadbox Jan 27, 2016
99a0dce
Merge pull request #927 from jadbox/master
brianc Jan 27, 2016
02c47f5
Support querying tables with column names with multiple apostrophes (…
Feb 10, 2016
ac438e8
Add integration test for Result.addRow
coagmano Feb 12, 2016
b78a508
Add function stub to native result to solve broken test
coagmano Feb 12, 2016
1ace25a
Bump buffer-writer minor version for better large insert performance
brianc Feb 15, 2016
ef64f5d
Bump version
brianc Feb 15, 2016
f26cb73
Merge pull request #937 from coagmano/master
brianc Feb 15, 2016
93d1e88
Merge pull request #935 from jkgeyti/master
brianc Feb 15, 2016
dbf3a04
Bump version
brianc Feb 15, 2016
8cfe39b
Add pg-client to the list of extras in README.md
koistya Feb 23, 2016
909c0f1
add option to parse input Dates as UTC
spollack Feb 24, 2016
f558ce4
Merge pull request #942 from koistya/patch-1
brianc Feb 25, 2016
1c6da45
Merge pull request #943 from RivalIQ/utc-date-input
brianc Feb 25, 2016
dbe0588
Update news
brianc Feb 25, 2016
2a04d92
Bump version
brianc Feb 25, 2016
93aa52d
Bump version
brianc Feb 25, 2016
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ For richer information consult the commit log on github with referenced pull req

We do not include break-fix version release in this file.

### v4.5.0
- Add option to parse JS date objects in query parameters as [UTC](https://github.com/brianc/node-postgres/pull/943)

### v4.4.0
- Warn to `stderr` if a named query exceeds 63 characters which is the max lenght supported by postgres.

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ node-postgres is by design pretty light on abstractions. These are some handy m
- [iceddev/pg-transact](https://github.com/iceddev/pg-transact) - A nicer API on node-postgres transactions
- [sehrope/node-pg-db](https://github.com/sehrope/node-pg-db) - Simpler interface, named parameter support, transaction management and event hooks.
- [vitaly-t/pg-promise](https://github.com/vitaly-t/pg-promise) - Use node-postgres via [Promises/A+](https://promisesaplus.com/).
- [kriasoft/node-pg-client](https://github.com/kriasoft/node-pg-client) - Promise-based wrapper for `node-postgres` designed for easy use with ES7 async/await.
- [pg-then](https://github.com/coderhaoxin/pg-then) A tiny wrapper of `pg` for promise api.
- [pg-rxjs](https://github.com/jadbox/pg-rxjs) Another tiny wrapper like `pg-then` but for [RxJS](https://github.com/Reactive-Extensions/RxJS)
- [acarl/pg-restify](https://github.com/acarl/pg-restify) - Creates a generic REST API for a postgres database using restify.
- [XeCycle/pg-template-tag](https://github.com/XeCycle/pg-template-tag) - Write queries with ES6 tagged template literals, a "poor man's query builder".
- [recursivefunk/pg-gen](https://github.com/recursivefunk/pg-gen) - Use ES6 Generators to paginate through large Postgres result sets
- [vitaly-t/pg-minify](https://github.com/vitaly-t/pg-minify) - Minifies PostgreSQL scripts.
- [MassiveJS](https://github.com/robconery/massive-js) - A simple relational data access tool that has full JSONB document support for Postgres.

## License

Expand Down
6 changes: 4 additions & 2 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ var defaults = module.exports = {

ssl: false,

application_name : undefined,
fallback_application_name: undefined
application_name: undefined,
fallback_application_name: undefined,

parseInputDatesAsUTC: false
};

//parse int8 so you can get your count values as actual numbers
Expand Down
7 changes: 7 additions & 0 deletions lib/native/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ NativeResult.prototype.addCommandComplete = function(pq) {
});
}
};

NativeResult.prototype.addRow = function(row) {
// This is empty to ensure pg code doesn't break when switching to pg-native
// pg-native loads all rows into the final result object by default.
// This is because libpg loads all rows into memory before passing the result
// to pg-native.
};
5 changes: 4 additions & 1 deletion lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ var inlineParser = function(fieldName, i) {
//fields containing single quotes will break
//the evaluated javascript unless they are escaped
//see https://github.com/brianc/node-postgres/issues/507
fieldName.replace("'", "\\'") +
//Addendum: However, we need to make sure to replace all
//occurences of apostrophes, not just the first one.
//See https://github.com/brianc/node-postgres/issues/934
fieldName.replace(/'/g, "\\'") +
"'] = " +
"rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
};
Expand Down
39 changes: 31 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var defaults = require('./defaults');

// convert a JS array to a postgres array literal
// uses comma separator so won't work for types like box that use
Expand Down Expand Up @@ -32,7 +33,11 @@ var prepareValue = function(val, seen) {
return val;
}
if(val instanceof Date) {
return dateToString(val);
if(defaults.parseInputDatesAsUTC) {
return dateToStringUTC(val);
} else {
return dateToString(val);
}
}
if(Array.isArray(val)) {
return arrayString(val);
Expand All @@ -59,13 +64,14 @@ function prepareObject(val, seen) {
return JSON.stringify(val);
}

function pad(number, digits) {
number = "" +number;
while(number.length < digits)
number = "0" + number;
return number;
}

function dateToString(date) {
function pad(number, digits) {
number = ""+number;
while(number.length < digits)
number = "0"+number;
return number;
}

var offset = -date.getTimezoneOffset();
var ret = pad(date.getFullYear(), 4) + '-' +
Expand All @@ -86,6 +92,19 @@ function dateToString(date) {
return ret + pad(Math.floor(offset/60), 2) + ":" + pad(offset%60, 2);
}

function dateToStringUTC(date) {

var ret = pad(date.getUTCFullYear(), 4) + '-' +
pad(date.getUTCMonth() + 1, 2) + '-' +
pad(date.getUTCDate(), 2) + 'T' +
pad(date.getUTCHours(), 2) + ':' +
pad(date.getUTCMinutes(), 2) + ':' +
pad(date.getUTCSeconds(), 2) + '.' +
pad(date.getUTCMilliseconds(), 3);

return ret + "+00:00";
}

function normalizeQueryConfig (config, values, callback) {
//can take in strings or config objects
config = (typeof(config) == 'string') ? { text: config } : config;
Expand All @@ -103,6 +122,10 @@ function normalizeQueryConfig (config, values, callback) {
}

module.exports = {
prepareValue: prepareValue,
prepareValue: function prepareValueWrapper (value) {
//this ensures that extra arguments do not get passed into prepareValue
//by accident, eg: from calling values.map(utils.prepareValue)
return prepareValue(value);
},
normalizeQueryConfig: normalizeQueryConfig
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg",
"version": "4.4.3",
"version": "4.5.1",
"description": "PostgreSQL client - pure javascript & libpq with the same API",
"keywords": [
"postgres",
Expand All @@ -18,7 +18,7 @@
"author": "Brian Carlson <brian.m.carlson@gmail.com>",
"main": "./lib",
"dependencies": {
"buffer-writer": "1.0.0",
"buffer-writer": "1.0.1",
"generic-pool": "2.1.1",
"packet-reader": "0.2.0",
"pg-connection-string": "0.1.3",
Expand Down
13 changes: 13 additions & 0 deletions test/integration/client/query-column-names-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var helper = require(__dirname + '/../test-helper');
var pg = helper.pg;

test('support for complex column names', function() {
pg.connect(helper.config, assert.success(function(client, done) {
client.query("CREATE TEMP TABLE t ( \"complex''column\" TEXT )");
client.query('SELECT * FROM t', assert.success(function(res) {
done();
assert.strictEqual(res.fields[0].name, "complex''column");
pg.end();
}));
}));
});
21 changes: 21 additions & 0 deletions test/integration/client/simple-query-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ test("simple query interface", function() {
});
});

test("simple query interface using addRow", function() {

var client = helper.client();

var query = client.query("select name from person order by name");

client.on('drain', client.end.bind(client));

query.on('row', function(row, result) {
assert.ok(result);
result.addRow(row);
});

query.on('end', function(result) {
assert.lengthIs(result.rows, 26, "result returned wrong number of rows");
assert.lengthIs(result.rows, result.rowCount);
assert.equal(result.rows[0].name, "Aaron");
assert.equal(result.rows[25].name, "Zanzabar");
});
});

test("multiple simple queries", function() {
var client = helper.client();
client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');"})
Expand Down
22 changes: 22 additions & 0 deletions test/unit/utils-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ test('prepareValues: date prepared properly', function() {
helper.resetTimezoneOffset();
});

test('prepareValues: date prepared properly as UTC', function() {
defaults.parseInputDatesAsUTC = true;

// make a date in the local timezone that represents a specific UTC point in time
var date = new Date(Date.UTC(2014, 1, 1, 11, 11, 1, 7));
var out = utils.prepareValue(date);
assert.strictEqual(out, "2014-02-01T11:11:01.007+00:00");

defaults.parseInputDatesAsUTC = false;
});

test('prepareValues: undefined prepared properly', function() {
var out = utils.prepareValue(void 0);
assert.strictEqual(out, null);
Expand Down Expand Up @@ -171,3 +182,14 @@ test('prepareValue: objects with circular toPostgres rejected', function() {
}
throw new Error("Expected prepareValue to throw exception");
});

test('prepareValue: can safely be used to map an array of values including those with toPostgres functions', function() {
var customType = {
toPostgres: function() {
return "zomgcustom!";
}
};
var values = [1, "test", customType]
var out = values.map(utils.prepareValue)
assert.deepEqual(out, [1, "test", "zomgcustom!"])
})