|
| 1 | +var Native = require('pg-native'); |
| 2 | +var EventEmitter = require('events').EventEmitter; |
| 3 | +var util = require('util'); |
| 4 | + |
| 5 | +var NativeQuery = require('./query'); |
| 6 | + |
| 7 | +var Client = module.exports = function() { |
| 8 | + EventEmitter.call(this); |
| 9 | + this.native = new Native(); |
| 10 | + this._queryQueue = []; |
| 11 | +}; |
| 12 | + |
| 13 | +util.inherits(Client, EventEmitter); |
| 14 | + |
| 15 | +//connect to the backend |
| 16 | +//pass an optional callback to be called once connected |
| 17 | +//or with an error if there was a connection error |
| 18 | +//if no callback is passed and there is a connection error |
| 19 | +//the client will emit an error event. |
| 20 | +Client.prototype.connect = function(cb) { |
| 21 | + var self = this; |
| 22 | + this.native.connect(function(err) { |
| 23 | + //error handling |
| 24 | + if(err) { |
| 25 | + if(cb) return cb(err); |
| 26 | + return self.emit('error') |
| 27 | + } |
| 28 | + |
| 29 | + //set internal states to connected |
| 30 | + self._connected = true; |
| 31 | + self.emit('connect'); |
| 32 | + self._pulseQueryQueue(true); |
| 33 | + |
| 34 | + //possibly call the optional callback |
| 35 | + if(cb) cb(); |
| 36 | + }); |
| 37 | +}; |
| 38 | + |
| 39 | +Client.prototype.query = function(config, values, callback) { |
| 40 | + var query = new NativeQuery(this.native); |
| 41 | + |
| 42 | + //support query('text', ...) style calls |
| 43 | + if(typeof config == 'string') { |
| 44 | + query.text = config; |
| 45 | + } |
| 46 | + |
| 47 | + //support passing everything in via a config object |
| 48 | + if(typeof config == 'object') { |
| 49 | + query.text = config.text; |
| 50 | + query.values = config.values; |
| 51 | + query.name = config.name; |
| 52 | + query.callback = config.callback; |
| 53 | + } |
| 54 | + |
| 55 | + //support query({...}, function() {}) style calls |
| 56 | + //& support query(..., ['values'], ...) style calls |
| 57 | + if(typeof values == 'function') { |
| 58 | + query.callback = values; |
| 59 | + } |
| 60 | + else if(util.isArray(values)) { |
| 61 | + query.values = values; |
| 62 | + } |
| 63 | + if(typeof callback == 'function') { |
| 64 | + query.callback = callback; |
| 65 | + } |
| 66 | + |
| 67 | + this._queryQueue.push(query); |
| 68 | + this._pulseQueryQueue(); |
| 69 | + return query; |
| 70 | +}; |
| 71 | + |
| 72 | +Client.prototype.end = function(cb) { |
| 73 | + this.native.end(cb); |
| 74 | +}; |
| 75 | + |
| 76 | +Client.prototype._pulseQueryQueue = function(initialConnection) { |
| 77 | + if(!this._connected) { |
| 78 | + return; |
| 79 | + } |
| 80 | + if(this._activeQuery) { |
| 81 | + if(this._activeQuery.state != 'error' && this._activeQuery.state != 'done') { |
| 82 | + return; |
| 83 | + } |
| 84 | + } |
| 85 | + var query = this._queryQueue.shift(); |
| 86 | + if(!query) { |
| 87 | + if(!initialConnection) { |
| 88 | + this.emit('drain'); |
| 89 | + } |
| 90 | + return; |
| 91 | + } |
| 92 | + this._activeQuery = query; |
| 93 | + query.submit(); |
| 94 | +}; |
| 95 | + |
| 96 | + |
| 97 | +return; |
1 | 98 | //require the c++ bindings & export to javascript
|
2 | 99 | var EventEmitter = require('events').EventEmitter;
|
3 | 100 |
|
@@ -108,38 +205,6 @@ Connection.prototype.cancel = function(client, query) {
|
108 | 205 | }
|
109 | 206 | };
|
110 | 207 |
|
111 |
| -Connection.prototype._pulseQueryQueue = function(initialConnection) { |
112 |
| - if(!this._connected) { |
113 |
| - return; |
114 |
| - } |
115 |
| - if(this._activeQuery) { |
116 |
| - return; |
117 |
| - } |
118 |
| - var query = this._queryQueue.shift(); |
119 |
| - if(!query) { |
120 |
| - if(!initialConnection) { |
121 |
| - this.emit('drain'); |
122 |
| - } |
123 |
| - return; |
124 |
| - } |
125 |
| - this._activeQuery = query; |
126 |
| - if(query.name) { |
127 |
| - if(this._namedQueries[query.name]) { |
128 |
| - this._sendQueryPrepared(query.name, query.values||[], query.singleRowMode); |
129 |
| - } else { |
130 |
| - this._namedQuery = true; |
131 |
| - this._namedQueries[query.name] = true; |
132 |
| - this._sendPrepare(query.name, query.text, (query.values||[]).length, query.singleRowMode); |
133 |
| - } |
134 |
| - } else if(query.values) { |
135 |
| - //call native function |
136 |
| - this._sendQueryWithParams(query.text, query.values, query.singleRowMode); |
137 |
| - } else { |
138 |
| - //call native function |
139 |
| - this._sendQuery(query.text, query.singleRowMode); |
140 |
| - } |
141 |
| -}; |
142 |
| - |
143 | 208 | Connection.prototype.sendCopyFail = function(msg) {
|
144 | 209 | this.endCopyFrom(msg);
|
145 | 210 | };
|
|
0 commit comments