From 4c5f3aba653f9b7418ae7579979b3e22c5098dd3 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Sat, 11 Oct 2014 14:09:29 -0400 Subject: [PATCH] Add support for native rowMode: array This completes the port from the old native bindings to the new node-pg-native bindings! Time to build in support for older versions of postgres & start the pull request process. --- lib/native/index.js | 8 +++++++- lib/native/query.js | 5 ++++- lib/query.js | 4 +--- package.json | 2 +- test/integration/client/custom-types-tests.js | 18 ++++++++++++++++++ .../client/results-as-array-tests.js | 5 ----- 6 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 test/integration/client/custom-types-tests.js diff --git a/lib/native/index.js b/lib/native/index.js index 5b97c0a8..1b79488e 100644 --- a/lib/native/index.js +++ b/lib/native/index.js @@ -7,7 +7,12 @@ var NativeQuery = require('./query'); var Client = module.exports = function(config) { EventEmitter.call(this); - this.native = new Native(); + config = config || {}; + + this.native = new Native({ + types: config.types || require('pg-types') + }); + this._queryQueue = []; this._connected = false; @@ -85,6 +90,7 @@ Client.prototype.query = function(config, values, callback) { query.values = config.values; query.name = config.name; query.callback = config.callback; + query._arrayMode = config.rowMode == 'array'; } //support query({...}, function() {}) style calls diff --git a/lib/native/query.js b/lib/native/query.js index 457ce566..4ca9cc24 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -10,6 +10,7 @@ var NativeQuery = module.exports = function(native) { this.name = null; this.callback = null; this.state = 'new'; + this._arrayMode = false; //if the 'row' event is listened for //then emit them as they come in @@ -75,8 +76,10 @@ NativeQuery.prototype.handleError = function(err) { NativeQuery.prototype.submit = function(client) { this.state = 'running'; var self = this; + client.native.arrayMode = this._arrayMode; var after = function(err, rows) { + client.native.arrayMode = false; setImmediate(function() { self.emit('_done'); }); @@ -121,7 +124,7 @@ NativeQuery.prototype.submit = function(client) { } //plan the named query the first time, then execute it return this.native.prepare(this.name, this.text, values.length, function(err) { - if(err) return self.handleError(err); + if(err) return after(err); client.namedQueries[self.name] = true; return self.native.execute(self.name, values, after); }); diff --git a/lib/query.js b/lib/query.js index 0c6bbc70..dc98580a 100644 --- a/lib/query.js +++ b/lib/query.js @@ -23,9 +23,7 @@ var Query = function(config, values, callback) { if(process.domain && config.callback) { this.callback = process.domain.bind(config.callback); } - this._fieldNames = []; - this._fieldConverters = []; - this._result = new Result(config.rowMode); + this._result = new Result(config.rowMode, config.types); this.isPreparedStatement = false; this._canceledDueToError = false; EventEmitter.call(this); diff --git a/package.json b/package.json index 3f17d4ab..2ae84237 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "nan": "~1.3.0", "packet-reader": "0.2.0", "pg-connection-string": "0.1.1", - "pg-native": "1.2.0", + "pg-native": "1.4.0", "pg-types": "1.4.0", "pgpass": "0.0.3" }, diff --git a/test/integration/client/custom-types-tests.js b/test/integration/client/custom-types-tests.js new file mode 100644 index 00000000..38479229 --- /dev/null +++ b/test/integration/client/custom-types-tests.js @@ -0,0 +1,18 @@ +var helper = require(__dirname + '/test-helper'); +return console.log('TODO: get this working for non-native client'); + +helper.config.types = { + getTypeParser: function() { + return function() { + return 'okay!' + } + } +}; + +helper.pg.connect(helper.config, assert.success(function(client, done) { + client.query('SELECT NOW() as val', assert.success(function(res) { + assert.equal(res.rows[0].val, 'okay!'); + done(); + helper.pg.end(); + })); +})); diff --git a/test/integration/client/results-as-array-tests.js b/test/integration/client/results-as-array-tests.js index 8fce7531..ef11a891 100644 --- a/test/integration/client/results-as-array-tests.js +++ b/test/integration/client/results-as-array-tests.js @@ -1,8 +1,3 @@ -console.log('results-as-array: GET TO PASS') -console.log('results-as-array: GET TO PASS') -console.log('results-as-array: GET TO PASS') -console.log('results-as-array: GET TO PASS') -return console.log('results-as-array: GET TO PASS') var util = require('util'); var helper = require('./test-helper');