|
6 | 6 | * README.md file in the root directory of this source tree.
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -var Native = require('pg-native'); |
10 |
| -var TypeOverrides = require('../type-overrides'); |
11 |
| -var semver = require('semver'); |
12 |
| -var pkg = require('../../package.json'); |
13 |
| -var assert = require('assert'); |
14 |
| -var EventEmitter = require('events').EventEmitter; |
15 |
| -var util = require('util'); |
16 |
| -var ConnectionParameters = require('../connection-parameters'); |
17 |
| - |
18 |
| -var msg = 'Version >= ' + pkg.minNativeVersion + ' of pg-native required.'; |
19 |
| -assert(semver.gte(Native.version, pkg.minNativeVersion), msg); |
20 |
| - |
21 |
| -var NativeQuery = require('./query'); |
22 |
| - |
23 |
| -var Client = module.exports = function(config) { |
24 |
| - EventEmitter.call(this); |
25 |
| - config = config || {}; |
26 |
| - |
27 |
| - this._types = new TypeOverrides(config.types); |
28 |
| - |
29 |
| - this.native = new Native({ |
30 |
| - types: this._types |
31 |
| - }); |
32 |
| - |
33 |
| - this._queryQueue = []; |
34 |
| - this._connected = false; |
35 |
| - |
36 |
| - //keep these on the object for legacy reasons |
37 |
| - //for the time being. TODO: deprecate all this jazz |
38 |
| - var cp = this.connectionParameters = new ConnectionParameters(config); |
39 |
| - this.user = cp.user; |
40 |
| - this.password = cp.password; |
41 |
| - this.database = cp.database; |
42 |
| - this.host = cp.host; |
43 |
| - this.port = cp.port; |
44 |
| - |
45 |
| - //a hash to hold named queries |
46 |
| - this.namedQueries = {}; |
47 |
| -}; |
48 |
| - |
49 |
| -util.inherits(Client, EventEmitter); |
50 |
| - |
51 |
| -//connect to the backend |
52 |
| -//pass an optional callback to be called once connected |
53 |
| -//or with an error if there was a connection error |
54 |
| -//if no callback is passed and there is a connection error |
55 |
| -//the client will emit an error event. |
56 |
| -Client.prototype.connect = function(cb) { |
57 |
| - var self = this; |
58 |
| - |
59 |
| - var onError = function(err) { |
60 |
| - if(cb) return cb(err); |
61 |
| - return self.emit('error', err); |
62 |
| - }; |
63 |
| - |
64 |
| - this.connectionParameters.getLibpqConnectionString(function(err, conString) { |
65 |
| - if(err) return onError(err); |
66 |
| - self.native.connect(conString, function(err) { |
67 |
| - if(err) return onError(err); |
68 |
| - |
69 |
| - //set internal states to connected |
70 |
| - self._connected = true; |
71 |
| - |
72 |
| - //handle connection errors from the native layer |
73 |
| - self.native.on('error', function(err) { |
74 |
| - //error will be handled by active query |
75 |
| - if(self._activeQuery && self._activeQuery.state != 'end') { |
76 |
| - return; |
77 |
| - } |
78 |
| - self.emit('error', err); |
79 |
| - }); |
80 |
| - |
81 |
| - self.native.on('notification', function(msg) { |
82 |
| - self.emit('notification', { |
83 |
| - channel: msg.relname, |
84 |
| - payload: msg.extra |
85 |
| - }); |
86 |
| - }); |
87 |
| - |
88 |
| - //signal we are connected now |
89 |
| - self.emit('connect'); |
90 |
| - self._pulseQueryQueue(true); |
91 |
| - |
92 |
| - //possibly call the optional callback |
93 |
| - if(cb) cb(); |
94 |
| - }); |
95 |
| - }); |
96 |
| -}; |
97 |
| - |
98 |
| -//send a query to the server |
99 |
| -//this method is highly overloaded to take |
100 |
| -//1) string query, optional array of parameters, optional function callback |
101 |
| -//2) object query with { |
102 |
| -// string query |
103 |
| -// optional array values, |
104 |
| -// optional function callback instead of as a separate parameter |
105 |
| -// optional string name to name & cache the query plan |
106 |
| -// optional string rowMode = 'array' for an array of results |
107 |
| -// } |
108 |
| -Client.prototype.query = function(config, values, callback) { |
109 |
| - var query = new NativeQuery(this.native); |
110 |
| - |
111 |
| - //support query('text', ...) style calls |
112 |
| - if(typeof config == 'string') { |
113 |
| - query.text = config; |
114 |
| - } |
115 |
| - |
116 |
| - //support passing everything in via a config object |
117 |
| - if(typeof config == 'object') { |
118 |
| - query.text = config.text; |
119 |
| - query.values = config.values; |
120 |
| - query.name = config.name; |
121 |
| - query.callback = config.callback; |
122 |
| - query._arrayMode = config.rowMode == 'array'; |
123 |
| - } |
124 |
| - |
125 |
| - //support query({...}, function() {}) style calls |
126 |
| - //& support query(..., ['values'], ...) style calls |
127 |
| - if(typeof values == 'function') { |
128 |
| - query.callback = values; |
129 |
| - } |
130 |
| - else if(util.isArray(values)) { |
131 |
| - query.values = values; |
132 |
| - } |
133 |
| - if(typeof callback == 'function') { |
134 |
| - query.callback = callback; |
135 |
| - } |
136 |
| - |
137 |
| - this._queryQueue.push(query); |
138 |
| - this._pulseQueryQueue(); |
139 |
| - return query; |
140 |
| -}; |
141 |
| - |
142 |
| -//disconnect from the backend server |
143 |
| -Client.prototype.end = function(cb) { |
144 |
| - var self = this; |
145 |
| - if(!this._connected) { |
146 |
| - this.once('connect', this.end.bind(this, cb)); |
147 |
| - } |
148 |
| - this.native.end(function() { |
149 |
| - //send an error to the active query |
150 |
| - if(self._hasActiveQuery()) { |
151 |
| - var msg = 'Connection terminated'; |
152 |
| - self._queryQueue.length = 0; |
153 |
| - self._activeQuery.handleError(new Error(msg)); |
154 |
| - } |
155 |
| - self.emit('end'); |
156 |
| - if(cb) cb(); |
157 |
| - }); |
158 |
| -}; |
159 |
| - |
160 |
| -Client.prototype._hasActiveQuery = function() { |
161 |
| - return this._activeQuery && this._activeQuery.state != 'error' && this._activeQuery.state != 'end'; |
162 |
| -}; |
163 |
| - |
164 |
| -Client.prototype._pulseQueryQueue = function(initialConnection) { |
165 |
| - if(!this._connected) { |
166 |
| - return; |
167 |
| - } |
168 |
| - if(this._hasActiveQuery()) { |
169 |
| - return; |
170 |
| - } |
171 |
| - var query = this._queryQueue.shift(); |
172 |
| - if(!query) { |
173 |
| - if(!initialConnection) { |
174 |
| - this.emit('drain'); |
175 |
| - } |
176 |
| - return; |
177 |
| - } |
178 |
| - this._activeQuery = query; |
179 |
| - query.submit(this); |
180 |
| - var self = this; |
181 |
| - query.once('_done', function() { |
182 |
| - self._pulseQueryQueue(); |
183 |
| - }); |
184 |
| -}; |
185 |
| - |
186 |
| -//attempt to cancel an in-progress query |
187 |
| -Client.prototype.cancel = function(query) { |
188 |
| - if(this._activeQuery == query) { |
189 |
| - this.native.cancel(function() {}); |
190 |
| - } else if (this._queryQueue.indexOf(query) != -1) { |
191 |
| - this._queryQueue.splice(this._queryQueue.indexOf(query), 1); |
192 |
| - } |
193 |
| -}; |
194 |
| - |
195 |
| -Client.prototype.setTypeParser = function(oid, format, parseFn) { |
196 |
| - return this._types.setTypeParser(oid, format, parseFn); |
197 |
| -}; |
198 |
| - |
199 |
| -Client.prototype.getTypeParser = function(oid, format) { |
200 |
| - return this._types.getTypeParser(oid, format); |
201 |
| -}; |
| 9 | +// var Native = require('pg-native'); |
| 10 | +// var TypeOverrides = require('../type-overrides'); |
| 11 | +// var semver = require('semver'); |
| 12 | +// var pkg = require('../../package.json'); |
| 13 | +// var assert = require('assert'); |
| 14 | +// var EventEmitter = require('events').EventEmitter; |
| 15 | +// var util = require('util'); |
| 16 | +// var ConnectionParameters = require('../connection-parameters'); |
| 17 | +// |
| 18 | +// var msg = 'Version >= ' + pkg.minNativeVersion + ' of pg-native required.'; |
| 19 | +// assert(semver.gte(Native.version, pkg.minNativeVersion), msg); |
| 20 | +// |
| 21 | +// var NativeQuery = require('./query'); |
| 22 | +// |
| 23 | +// var Client = module.exports = function(config) { |
| 24 | +// EventEmitter.call(this); |
| 25 | +// config = config || {}; |
| 26 | +// |
| 27 | +// this._types = new TypeOverrides(config.types); |
| 28 | +// |
| 29 | +// this.native = new Native({ |
| 30 | +// types: this._types |
| 31 | +// }); |
| 32 | +// |
| 33 | +// this._queryQueue = []; |
| 34 | +// this._connected = false; |
| 35 | +// |
| 36 | +// //keep these on the object for legacy reasons |
| 37 | +// //for the time being. TODO: deprecate all this jazz |
| 38 | +// var cp = this.connectionParameters = new ConnectionParameters(config); |
| 39 | +// this.user = cp.user; |
| 40 | +// this.password = cp.password; |
| 41 | +// this.database = cp.database; |
| 42 | +// this.host = cp.host; |
| 43 | +// this.port = cp.port; |
| 44 | +// |
| 45 | +// //a hash to hold named queries |
| 46 | +// this.namedQueries = {}; |
| 47 | +// }; |
| 48 | +// |
| 49 | +// util.inherits(Client, EventEmitter); |
| 50 | +// |
| 51 | +// //connect to the backend |
| 52 | +// //pass an optional callback to be called once connected |
| 53 | +// //or with an error if there was a connection error |
| 54 | +// //if no callback is passed and there is a connection error |
| 55 | +// //the client will emit an error event. |
| 56 | +// Client.prototype.connect = function(cb) { |
| 57 | +// var self = this; |
| 58 | +// |
| 59 | +// var onError = function(err) { |
| 60 | +// if(cb) return cb(err); |
| 61 | +// return self.emit('error', err); |
| 62 | +// }; |
| 63 | +// |
| 64 | +// this.connectionParameters.getLibpqConnectionString(function(err, conString) { |
| 65 | +// if(err) return onError(err); |
| 66 | +// self.native.connect(conString, function(err) { |
| 67 | +// if(err) return onError(err); |
| 68 | +// |
| 69 | +// //set internal states to connected |
| 70 | +// self._connected = true; |
| 71 | +// |
| 72 | +// //handle connection errors from the native layer |
| 73 | +// self.native.on('error', function(err) { |
| 74 | +// //error will be handled by active query |
| 75 | +// if(self._activeQuery && self._activeQuery.state != 'end') { |
| 76 | +// return; |
| 77 | +// } |
| 78 | +// self.emit('error', err); |
| 79 | +// }); |
| 80 | +// |
| 81 | +// self.native.on('notification', function(msg) { |
| 82 | +// self.emit('notification', { |
| 83 | +// channel: msg.relname, |
| 84 | +// payload: msg.extra |
| 85 | +// }); |
| 86 | +// }); |
| 87 | +// |
| 88 | +// //signal we are connected now |
| 89 | +// self.emit('connect'); |
| 90 | +// self._pulseQueryQueue(true); |
| 91 | +// |
| 92 | +// //possibly call the optional callback |
| 93 | +// if(cb) cb(); |
| 94 | +// }); |
| 95 | +// }); |
| 96 | +// }; |
| 97 | +// |
| 98 | +// //send a query to the server |
| 99 | +// //this method is highly overloaded to take |
| 100 | +// //1) string query, optional array of parameters, optional function callback |
| 101 | +// //2) object query with { |
| 102 | +// // string query |
| 103 | +// // optional array values, |
| 104 | +// // optional function callback instead of as a separate parameter |
| 105 | +// // optional string name to name & cache the query plan |
| 106 | +// // optional string rowMode = 'array' for an array of results |
| 107 | +// // } |
| 108 | +// Client.prototype.query = function(config, values, callback) { |
| 109 | +// var query = new NativeQuery(this.native); |
| 110 | +// |
| 111 | +// //support query('text', ...) style calls |
| 112 | +// if(typeof config == 'string') { |
| 113 | +// query.text = config; |
| 114 | +// } |
| 115 | +// |
| 116 | +// //support passing everything in via a config object |
| 117 | +// if(typeof config == 'object') { |
| 118 | +// query.text = config.text; |
| 119 | +// query.values = config.values; |
| 120 | +// query.name = config.name; |
| 121 | +// query.callback = config.callback; |
| 122 | +// query._arrayMode = config.rowMode == 'array'; |
| 123 | +// } |
| 124 | +// |
| 125 | +// //support query({...}, function() {}) style calls |
| 126 | +// //& support query(..., ['values'], ...) style calls |
| 127 | +// if(typeof values == 'function') { |
| 128 | +// query.callback = values; |
| 129 | +// } |
| 130 | +// else if(util.isArray(values)) { |
| 131 | +// query.values = values; |
| 132 | +// } |
| 133 | +// if(typeof callback == 'function') { |
| 134 | +// query.callback = callback; |
| 135 | +// } |
| 136 | +// |
| 137 | +// this._queryQueue.push(query); |
| 138 | +// this._pulseQueryQueue(); |
| 139 | +// return query; |
| 140 | +// }; |
| 141 | +// |
| 142 | +// //disconnect from the backend server |
| 143 | +// Client.prototype.end = function(cb) { |
| 144 | +// var self = this; |
| 145 | +// if(!this._connected) { |
| 146 | +// this.once('connect', this.end.bind(this, cb)); |
| 147 | +// } |
| 148 | +// this.native.end(function() { |
| 149 | +// //send an error to the active query |
| 150 | +// if(self._hasActiveQuery()) { |
| 151 | +// var msg = 'Connection terminated'; |
| 152 | +// self._queryQueue.length = 0; |
| 153 | +// self._activeQuery.handleError(new Error(msg)); |
| 154 | +// } |
| 155 | +// self.emit('end'); |
| 156 | +// if(cb) cb(); |
| 157 | +// }); |
| 158 | +// }; |
| 159 | +// |
| 160 | +// Client.prototype._hasActiveQuery = function() { |
| 161 | +// return this._activeQuery && this._activeQuery.state != 'error' && this._activeQuery.state != 'end'; |
| 162 | +// }; |
| 163 | +// |
| 164 | +// Client.prototype._pulseQueryQueue = function(initialConnection) { |
| 165 | +// if(!this._connected) { |
| 166 | +// return; |
| 167 | +// } |
| 168 | +// if(this._hasActiveQuery()) { |
| 169 | +// return; |
| 170 | +// } |
| 171 | +// var query = this._queryQueue.shift(); |
| 172 | +// if(!query) { |
| 173 | +// if(!initialConnection) { |
| 174 | +// this.emit('drain'); |
| 175 | +// } |
| 176 | +// return; |
| 177 | +// } |
| 178 | +// this._activeQuery = query; |
| 179 | +// query.submit(this); |
| 180 | +// var self = this; |
| 181 | +// query.once('_done', function() { |
| 182 | +// self._pulseQueryQueue(); |
| 183 | +// }); |
| 184 | +// }; |
| 185 | +// |
| 186 | +// //attempt to cancel an in-progress query |
| 187 | +// Client.prototype.cancel = function(query) { |
| 188 | +// if(this._activeQuery == query) { |
| 189 | +// this.native.cancel(function() {}); |
| 190 | +// } else if (this._queryQueue.indexOf(query) != -1) { |
| 191 | +// this._queryQueue.splice(this._queryQueue.indexOf(query), 1); |
| 192 | +// } |
| 193 | +// }; |
| 194 | +// |
| 195 | +// Client.prototype.setTypeParser = function(oid, format, parseFn) { |
| 196 | +// return this._types.setTypeParser(oid, format, parseFn); |
| 197 | +// }; |
| 198 | +// |
| 199 | +// Client.prototype.getTypeParser = function(oid, format) { |
| 200 | +// return this._types.getTypeParser(oid, format); |
| 201 | +// }; |
0 commit comments