forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
229 lines (210 loc) · 6.46 KB
/
Copy pathindex.js
File metadata and controls
229 lines (210 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//require the c++ bindings & export to javascript
var EventEmitter = require('events').EventEmitter;
var ConnectionParameters = require(__dirname + '/../connection-parameters');
var utils = require(__dirname + "/../utils");
var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream;
var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
var binding;
try {
//v0.5.x
binding = require(__dirname + '/../../build/Release/binding.node');
} catch(e) {
//v0.4.x
binding = require(__dirname + '/../../build/default/binding');
}
var Connection = binding.Connection;
var types = require(__dirname + "/../types");
var NativeQuery = require(__dirname + '/query');
var EventEmitter = require('events').EventEmitter;
var p = Connection.prototype;
for(var k in EventEmitter.prototype) {
p[k] = EventEmitter.prototype[k];
}
var nativeConnect = p.connect;
p.connect = function(cb) {
var self = this;
this.connectionParameters.getLibpqConnectionString(function(err, conString) {
if(err) {
return cb ? cb(err) : self.emit('error', err);
}
if(cb) {
var errCallback;
var connectCallback = function() {
//remove single-fire connection error callback
self.removeListener('error', errCallback);
cb(null);
};
errCallback = function(err) {
//remove singel-fire connection success callback
self.removeListener('connect', connectCallback);
cb(err);
};
self.once('connect', connectCallback);
self.once('error', errCallback);
}
nativeConnect.call(self, conString);
});
};
p._copy = function (text, stream) {
var q = new NativeQuery(text, function (error) {
if (error) {
q.stream.error(error);
} else {
q.stream.close();
}
});
q.stream = stream;
this._queryQueue.push(q);
this._pulseQueryQueue();
return q.stream;
};
p.copyFrom = function (text) {
return this._copy(text, new CopyFromStream());
};
p.copyTo = function (text) {
return this._copy(text, new CopyToStream());
};
p.sendCopyFromChunk = function (chunk) {
this._sendCopyFromChunk(chunk);
};
p.endCopyFrom = function (msg) {
this._endCopyFrom(msg);
};
p.query = function(config, values, callback) {
var query = (config instanceof NativeQuery) ? config :
new NativeQuery(config, values, callback);
this._queryQueue.push(query);
this._pulseQueryQueue();
return query;
};
var nativeCancel = p.cancel;
p.cancel = function(client, query) {
if (client._activeQuery == query)
this.connect(nativeCancel.bind(client));
else if (client._queryQueue.indexOf(query) != -1)
client._queryQueue.splice(client._queryQueue.indexOf(query), 1);
};
p._pulseQueryQueue = function(initialConnection) {
if(!this._connected) {
return;
}
if(this._activeQuery) {
return;
}
var query = this._queryQueue.shift();
if(!query) {
if(!initialConnection) {
if(this._drainPaused) {
this._drainPaused++;
} else {
this.emit('drain');
}
}
return;
}
this._activeQuery = query;
if(query.name) {
if(this._namedQueries[query.name]) {
this._sendQueryPrepared(query.name, query.values||[]);
} else {
this._namedQuery = true;
this._namedQueries[query.name] = true;
this._sendPrepare(query.name, query.text, (query.values||[]).length);
}
}
else if(query.values) {
//call native function
this._sendQueryWithParams(query.text, query.values);
} else {
//call native function
this._sendQuery(query.text);
}
};
p.pauseDrain = function() {
this._drainPaused = 1;
};
p.resumeDrain = function() {
if(this._drainPaused > 1) {
this.emit('drain');
}
this._drainPaused = 0;
};
p.sendCopyFail = function(msg) {
this.endCopyFrom(msg);
};
var clientBuilder = function(config) {
config = config || {};
var connection = new Connection();
connection._queryQueue = [];
connection._namedQueries = {};
connection._activeQuery = null;
connection.connectionParameters = new ConnectionParameters(config);
//attach properties to normalize interface with pure js client
connection.user = connection.connectionParameters.user;
connection.password = connection.connectionParameters.password;
connection.database = connection.connectionParameters.database;
connection.host = connection.connectionParameters.host;
connection.port = connection.connectionParameters.port;
connection.on('connect', function() {
connection._connected = true;
connection._pulseQueryQueue(true);
});
//proxy some events to active query
connection.on('_row', function(row) {
connection._activeQuery.handleRow(row);
});
connection.on('_cmdStatus', function(status) {
//set this here so we can pass it to the query
//when the query completes
connection._lastMeta = status;
});
//TODO: emit more native error properties (make it match js error)
connection.on('_error', function(err) {
//create Error object from object literal
var error = new Error(err.message || "Unknown native driver error");
for(var key in err) {
error[key] = err[key];
}
//give up on trying to wait for named query prepare
this._namedQuery = false;
if(connection._activeQuery) {
connection._activeQuery.handleError(error);
} else {
connection.emit('error', error);
}
});
connection.on('_readyForQuery', function() {
var q = this._activeQuery;
//a named query finished being prepared
if(this._namedQuery) {
this._namedQuery = false;
this._sendQueryPrepared(q.name, q.values||[]);
} else {
connection._activeQuery.handleReadyForQuery(connection._lastMeta);
connection._activeQuery = null;
connection._pulseQueryQueue();
}
});
connection.on('copyInResponse', function () {
//connection is ready to accept chunks
//start to send data from stream
connection._activeQuery.streamData(connection);
});
connection.on('copyOutResponse', function(msg) {
if (connection._activeQuery.stream === undefined) {
connection._activeQuery._canceledDueToError =
new Error('No destination stream defined');
(new clientBuilder({port: connection.port, host: connection.host}))
.cancel(connection, connection._activeQuery);
}
});
connection.on('copyData', function (chunk) {
//recieve chunk from connection
//move it to stream
connection._activeQuery.handleCopyFromChunk(chunk);
});
return connection;
};
// expose a Query constructor
clientBuilder.Query = NativeQuery;
module.exports = clientBuilder;