Skip to content

Commit 20b47e7

Browse files
committed
Merge pull request brianc#298 from brianc/v0.14.0-pre
V0.14.0 pre
2 parents db03c53 + 2135186 commit 20b47e7

File tree

10 files changed

+143
-33
lines changed

10 files changed

+143
-33
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
language: node_js
22
node_js:
33
- 0.8
4+
- 0.9
45
before_script:
56
- node script/create-test-tables.js pg://[email protected]:5432/postgres

lib/client.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var Connection = require(__dirname + '/connection');
1010
var CopyFromStream = require(__dirname + '/copystream').CopyFromStream;
1111
var CopyToStream = require(__dirname + '/copystream').CopyToStream;
1212

13+
var deprecate = require('deprecate');
14+
1315
var Client = function(config) {
1416
EventEmitter.call(this);
1517

@@ -256,11 +258,23 @@ Client.prototype.query = function(config, values, callback) {
256258
//prevents client from otherwise emitting 'drain' event until 'resumeDrain' is
257259
//called
258260
Client.prototype.pauseDrain = function() {
261+
deprecate('Client.prototype.pauseDrain is deprecated and will be removed it v1.0.0 (very soon)',
262+
'please see the following for more details:',
263+
'https://github.com/brianc/node-postgres/wiki/pg',
264+
'https://github.com/brianc/node-postgres/issues/227',
265+
'https://github.com/brianc/node-postgres/pull/274',
266+
'feel free to get in touch via github if you have questions');
259267
this._drainPaused = 1;
260268
};
261269

262270
//resume raising 'drain' event
263271
Client.prototype.resumeDrain = function() {
272+
deprecate('Client.prototype.resumeDrain is deprecated and will be removed it v1.0.0 (very soon)',
273+
'please see the following for more details:',
274+
'https://github.com/brianc/node-postgres/wiki/pg',
275+
'https://github.com/brianc/node-postgres/issues/227',
276+
'https://github.com/brianc/node-postgres/pull/274',
277+
'feel free to get in touch via github if you have questions');
264278
if(this._drainPaused > 1) {
265279
this.emit('drain');
266280
}

lib/defaults.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ module.exports = {
3333
//pool log function / boolean
3434
poolLog: false
3535
};
36+
37+
var deprecate = require('deprecate');
38+
//getter/setter to disable deprecation warnings
39+
module.exports.__defineGetter__("hideDeprecationWarnings", function() {
40+
return deprecate.silent;
41+
});
42+
module.exports.__defineSetter__("hideDeprecationWarnings", function(val) {
43+
deprecate.silence = val;
44+
});

lib/deprecate.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var os = require('os');
2+
var defaults = require(__dirname + '/defaults');
3+
4+
var hits = {
5+
};
6+
var deprecate = module.exports = function(methodName, message) {
7+
if(defaults.hideDeprecationWarnings) return;
8+
if(hits[deprecate.caller]) return;
9+
hits[deprecate.caller] = true;
10+
process.stderr.write(os.EOL);
11+
process.stderr.write('\x1b[31;1m');
12+
process.stderr.write('WARNING!!');
13+
process.stderr.write(os.EOL);
14+
process.stderr.write(methodName);
15+
process.stderr.write(os.EOL);
16+
for(var i = 1; i < arguments.length; i++) {
17+
process.stderr.write(arguments[i]);
18+
process.stderr.write(os.EOL);
19+
}
20+
process.stderr.write('\x1b[0m');
21+
process.stderr.write(os.EOL);
22+
process.stderr.write("You can silence these warnings with `require('pg').defaults.hideDeprecationWarnings = true`");
23+
process.stderr.write(os.EOL);
24+
process.stderr.write(os.EOL);
25+
};

lib/pool.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var EventEmitter = require('events').EventEmitter;
33
var defaults = require(__dirname + '/defaults');
44
var genericPool = require('generic-pool');
55

6+
var deprecate = require('deprecate');
7+
68
var pools = {
79
//dictionary of all key:pool pairs
810
all: {},
@@ -77,6 +79,14 @@ var errorMessage = [
7779
].join(require('os').EOL);
7880

7981
var oldConnect = function(pool, client, cb) {
82+
deprecate('pg.connect(function(err, client) { ...}) is deprecated and will be removed it v1.0.0 (very soon)',
83+
'instead, use pg.connect(function(err, client, done) { ... })',
84+
'automatic releasing of clients back to the pool was a mistake and will be removed',
85+
'please see the following for more details:',
86+
'https://github.com/brianc/node-postgres/wiki/pg',
87+
'https://github.com/brianc/node-postgres/issues/227',
88+
'https://github.com/brianc/node-postgres/pull/274',
89+
'feel free to get in touch via github if you have questions');
8090
var tid = setTimeout(function() {
8191
console.error(errorMessage);
8292
}, alarmDuration);

lib/types/binaryParsers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var deprecate = require('deprecate');
2+
13
var parseBits = function(data, bits, offset, invert, callback) {
24
offset = offset || 0;
35
invert = invert || false;
@@ -45,6 +47,12 @@ var parseBits = function(data, bits, offset, invert, callback) {
4547
};
4648

4749
var parseFloatFromBits = function(data, precisionBits, exponentBits) {
50+
deprecate('parsing and returning floats from PostgreSQL server is deprecated',
51+
'JavaScript has a hard time with floats and there is precision loss which can cause',
52+
'unexpected, hard to trace, potentially bad bugs in your program',
53+
'for more information see the following:',
54+
'https://github.com/brianc/node-postgres/pull/271',
55+
'in node-postgres v1.0.0 all floats & decimals will be returned as strings');
4856
var bias = Math.pow(2, exponentBits - 1) - 1;
4957
var sign = parseBits(data, 1);
5058
var exponent = parseBits(data, exponentBits, 1);

lib/types/textParsers.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var deprecate = require('deprecate');
2+
13
var arrayParser = require(__dirname + "/arrayParser.js");
24

35
//parses PostgreSQL server formatted date strings into javascript date objects
@@ -76,6 +78,13 @@ var parseIntegerArray = function(val) {
7678
};
7779

7880
var parseFloatArray = function(val) {
81+
deprecate('parsing and returning floats from PostgreSQL server is deprecated',
82+
'JavaScript has a hard time with floats and there is precision loss which can cause',
83+
'unexpected, hard to trace, potentially bad bugs in your program',
84+
'for more information see the following:',
85+
'https://github.com/brianc/node-postgres/pull/271',
86+
'in node-postgres v1.0.0 all floats & decimals will be returned as strings',
87+
'feel free to get in touch via a github issue if you have any questions');
7988
if(!val) { return null; }
8089
var p = arrayParser.create(val, function(entry){
8190
if(entry !== null) {
@@ -162,24 +171,27 @@ var parseInteger = function(val) {
162171
return parseInt(val, 10);
163172
};
164173

174+
var parseFloatAndWarn = function(val) {
175+
deprecate('parsing and returning floats from PostgreSQL server is deprecated',
176+
'JavaScript has a hard time with floats and there is precision loss which can cause',
177+
'unexpected, hard to trace, potentially bad bugs in your program',
178+
'for more information see the following:',
179+
'https://github.com/brianc/node-postgres/pull/271',
180+
'in node-postgres v1.0.0 all floats & decimals will be returned as strings');
181+
return parseFloat(val);
182+
};
183+
165184
var init = function(register) {
166185
register(20, parseInteger);
167186
register(21, parseInteger);
168187
register(23, parseInteger);
169188
register(26, parseInteger);
170189
//TODO remove for v1.0
171-
register(1700, function(val){
172-
if(val.length > maxLen) {
173-
console.warn(
174-
'WARNING: value %s is longer than max supported numeric value in ' +
175-
'javascript. Possible data loss', val);
176-
}
177-
return parseFloat(val);
178-
});
190+
register(1700, parseFloatAndWarn);
179191
//TODO remove for v1.0
180-
register(700, parseFloat);
192+
register(700, parseFloatAndWarn);
181193
//TODO remove for v1.0
182-
register(701, parseFloat);
194+
register(701, parseFloatAndWarn);
183195
register(16, parseBool);
184196
register(1082, parseDate); // date
185197
register(1114, parseDate); // timestamp without timezone

package.json

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
{ "name": "pg",
1+
{
2+
"name": "pg",
23
"version": "0.13.3",
34
"description": "PostgreSQL client - pure javascript & libpq with the same API",
4-
"keywords" : ["postgres", "pg", "libpq", "postgre", "database", "rdbms"],
5+
"keywords": [
6+
"postgres",
7+
"pg",
8+
"libpq",
9+
"postgre",
10+
"database",
11+
"rdbms"
12+
],
513
"homepage": "http://github.com/brianc/node-postgres",
6-
"repository" : {
7-
"type" : "git",
8-
"url" : "git://github.com/brianc/node-postgres.git"
14+
"repository": {
15+
"type": "git",
16+
"url": "git://github.com/brianc/node-postgres.git"
917
},
10-
"author" : "Brian Carlson <[email protected]>",
11-
"main" : "./lib",
12-
"dependencies" : {
13-
"generic-pool" : "2.0.2"
18+
"author": "Brian Carlson <[email protected]>",
19+
"main": "./lib",
20+
"dependencies": {
21+
"generic-pool": "2.0.2",
22+
"deprecate": "~0.1.0"
1423
},
15-
"devDependencies" : {
16-
"jshint" : "git://github.com/jshint/jshint.git"
24+
"devDependencies": {
25+
"jshint": "git://github.com/jshint/jshint.git"
1726
},
18-
"scripts" : {
19-
"test" : "make test-all connectionString=pg://postgres@localhost:5432/postgres",
27+
"scripts": {
28+
"test": "make test-all connectionString=pg://postgres@localhost:5432/postgres",
2029
"prepublish": "rm -r build || (exit 0)",
21-
"install" : "node-gyp rebuild || (exit 0)"
30+
"install": "node-gyp rebuild || (exit 0)"
2231
},
23-
"engines" : { "node": ">= 0.8.0" }
32+
"engines": {
33+
"node": ">= 0.8.0"
34+
}
2435
}

src/binding.cc

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,17 @@ class Connection : public ObjectWrap {
249249
bool ioInitialized_;
250250
bool copyOutMode_;
251251
bool copyInMode_;
252+
bool reading_;
253+
bool writing_;
252254
Connection () : ObjectWrap ()
253255
{
254256
connection_ = NULL;
255257
connecting_ = false;
256258
ioInitialized_ = false;
257259
copyOutMode_ = false;
258260
copyInMode_ = false;
261+
reading_ = false;
262+
writing_ = false;
259263
TRACE("Initializing ev watchers");
260264
read_watcher_.data = this;
261265
write_watcher_.data = this;
@@ -304,20 +308,23 @@ class Connection : public ObjectWrap {
304308

305309
int Send(const char *queryText)
306310
{
311+
TRACE("js::Send")
307312
int rv = PQsendQuery(connection_, queryText);
308313
StartWrite();
309314
return rv;
310315
}
311316

312317
int SendQueryParams(const char *command, const int nParams, const char * const *paramValues)
313318
{
319+
TRACE("js::SendQueryParams")
314320
int rv = PQsendQueryParams(connection_, command, nParams, NULL, paramValues, NULL, NULL, 0);
315321
StartWrite();
316322
return rv;
317323
}
318324

319325
int SendPrepare(const char *name, const char *command, const int nParams)
320326
{
327+
TRACE("js::SendPrepare")
321328
int rv = PQsendPrepare(connection_, name, command, nParams, NULL);
322329
StartWrite();
323330
return rv;
@@ -430,7 +437,7 @@ class Connection : public ObjectWrap {
430437
if(PQconsumeInput(connection_) == 0) {
431438
End();
432439
EmitLastError();
433-
LOG("Something happened, consume input is 0");
440+
//LOG("Something happened, consume input is 0");
434441
return;
435442
}
436443

@@ -476,7 +483,8 @@ class Connection : public ObjectWrap {
476483
if(revents & UV_WRITABLE) {
477484
TRACE("revents & UV_WRITABLE");
478485
if (PQflush(connection_) == 0) {
479-
StopWrite();
486+
//nothing left to write, poll the socket for more to read
487+
StartRead();
480488
}
481489
}
482490
}
@@ -669,12 +677,10 @@ class Connection : public ObjectWrap {
669677
switch(status) {
670678
case PGRES_POLLING_READING:
671679
TRACE("Polled: PGRES_POLLING_READING");
672-
StopWrite();
673680
StartRead();
674681
break;
675682
case PGRES_POLLING_WRITING:
676683
TRACE("Polled: PGRES_POLLING_WRITING");
677-
StopRead();
678684
StartWrite();
679685
break;
680686
case PGRES_POLLING_FAILED:
@@ -712,30 +718,42 @@ class Connection : public ObjectWrap {
712718

713719
void StopWrite()
714720
{
715-
TRACE("Stoping write watcher");
721+
TRACE("write STOP");
716722
if(ioInitialized_) {
717723
uv_poll_stop(&write_watcher_);
724+
writing_ = false;
718725
}
719726
}
720727

721728
void StartWrite()
722729
{
723-
TRACE("Starting write watcher");
730+
TRACE("write START");
731+
if(reading_) {
732+
TRACE("stop READ to start WRITE");
733+
StopRead();
734+
}
724735
uv_poll_start(&write_watcher_, UV_WRITABLE, io_event);
736+
writing_ = true;
725737
}
726738

727739
void StopRead()
728740
{
729-
TRACE("Stoping read watcher");
741+
TRACE("read STOP");
730742
if(ioInitialized_) {
731743
uv_poll_stop(&read_watcher_);
744+
reading_ = false;
732745
}
733746
}
734747

735748
void StartRead()
736749
{
737-
TRACE("Starting read watcher");
750+
TRACE("read START");
751+
if(writing_) {
752+
TRACE("stop WRITE to start READ");
753+
StopWrite();
754+
}
738755
uv_poll_start(&read_watcher_, UV_READABLE, io_event);
756+
reading_ = true;
739757
}
740758
//Converts a v8 array to an array of cstrings
741759
//the result char** array must be free() when it is no longer needed

test/test-helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var BufferList = require(__dirname+'/buffer-list')
77

88
var Connection = require(__dirname + '/../lib/connection');
99

10+
require(__dirname + '/../lib').defaults.hideDeprecationWarnings = true;
11+
1012
Client = require(__dirname + '/../lib').Client;
1113

1214
process.on('uncaughtException', function(d) {

0 commit comments

Comments
 (0)