/
query.js
237 lines (204 loc) · 7.38 KB
/
query.js
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
230
231
232
233
234
235
236
237
// Generated by CoffeeScript 1.6.2
var EventEmitter, FrontendMessage, Query, Resultset, decoders,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
EventEmitter = require('events').EventEmitter;
FrontendMessage = require('./frontend_message');
decoders = require('./types').decoders;
Resultset = require('./resultset');
Query = (function(_super) {
__extends(Query, _super);
function Query(connection, sql, callback) {
this.connection = connection;
this.sql = sql;
this.callback = callback;
this._handlingCopyIn = false;
}
Query.prototype.run = function() {
this.emit('start');
this.connection._writeMessage(new FrontendMessage.Query(this.sql));
this.connection.once('EmptyQueryResponse', this.onEmptyQueryListener = this.onEmptyQuery.bind(this));
this.connection.on('RowDescription', this.onRowDescriptionListener = this.onRowDescription.bind(this));
this.connection.on('DataRow', this.onDataRowListener = this.onDataRow.bind(this));
this.connection.on('CommandComplete', this.onCommandCompleteListener = this.onCommandComplete.bind(this));
this.connection.once('ErrorResponse', this.onErrorResponseListener = this.onErrorResponse.bind(this));
this.connection.once('ReadyForQuery', this.onReadyForQueryListener = this.onReadyForQuery.bind(this));
return this.connection.once('CopyInResponse', this.onCopyInResponseListener = this.onCopyInResponse.bind(this));
};
Query.prototype.onEmptyQuery = function() {
if (this.callback) {
return this.error = "The query was empty!";
} else {
return this.emit('error', "The query was empty!");
}
};
Query.prototype.onRowDescription = function(msg) {
var column, field, _i, _len, _ref;
if ((this.callback != null) && (this.status != null)) {
throw "Cannot handle multi-queries with a callback!";
}
this.fields = [];
_ref = msg.columns;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
column = _ref[_i];
field = new Query.Field(column);
this.emit('field', field);
this.fields.push(field);
}
if (this.callback) {
this.rows = [];
}
return this.emit('fields', this.fields);
};
Query.prototype.onDataRow = function(msg) {
var index, row, value, _i, _len, _ref;
row = [];
_ref = msg.values;
for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
value = _ref[index];
row.push(value != null ? this.fields[index].decoder(value) : null);
}
if (this.callback) {
this.rows.push(row);
}
return this.emit('row', row);
};
Query.prototype.onReadyForQuery = function(msg) {
var _this = this;
this._removeAllListeners();
if (this.callback) {
return process.nextTick(function() {
if (_this.error) {
return _this.callback(_this.error);
} else {
return _this.callback(null, new Resultset({
fields: _this.fields,
rows: _this.rows,
status: _this.status
}));
}
});
}
};
Query.prototype.onCommandComplete = function(msg) {
if (this.callback) {
this.status = msg.status;
}
return this.emit('end', msg.status);
};
Query.prototype.onErrorResponse = function(msg) {
if (this.callback) {
return this.error = msg;
} else {
return this.emit('error', msg.message);
}
};
Query.prototype.onConnectionError = function(msg) {
var _this = this;
this._removeAllListeners();
if (this.callback) {
return process.nextTick(function() {
return _this.callback(msg);
});
} else {
return this.emit('error', msg);
}
};
Query.prototype.onCopyInResponse = function(msg) {
var copyInHandler, dataHandler, failureHandler, successHandler,
_this = this;
this._handlingCopyIn = true;
dataHandler = function(data) {
return _this.copyData(data);
};
successHandler = function() {
return _this.copyDone();
};
failureHandler = function(err) {
return _this.copyFail(err);
};
copyInHandler = this._getCopyInHandler();
return copyInHandler(dataHandler, successHandler, failureHandler);
};
Query.prototype._getCopyInHandler = function() {
var existsSync, fs, stream;
if (typeof this.copyInSource === 'function') {
return this.copyInSource;
} else if (typeof this.copyInSource === 'string') {
fs = require('fs');
existsSync = fs.existsSync || require('path').existsSync;
if (existsSync(this.copyInSource)) {
stream = fs.createReadStream(this.copyInSource);
return this._getStreamCopyInHandler(stream);
} else {
return this.copyFail("Could not find local file " + this.dataSource + ".");
}
} else if (this.copyInSource === process.stdin) {
process.stdin.resume();
return this._getStreamCopyInHandler(process.stdin);
} else {
throw "No copy in handler defined to handle the COPY statement.";
}
};
Query.prototype._getStreamCopyInHandler = function(stream) {
return function(transfer, success, fail) {
stream.on('data', function(data) {
return transfer(data);
});
stream.on('end', function() {
return success();
});
return stream.on('error', function(err) {
return fail(err);
});
};
};
Query.prototype.copyData = function(data) {
if (this._handlingCopyIn) {
return this.connection._writeMessage(new FrontendMessage.CopyData(data));
} else {
throw "Copy in mode not active!";
}
};
Query.prototype.copyDone = function() {
if (this._handlingCopyIn) {
this.connection._writeMessage(new FrontendMessage.CopyDone());
return this._handlingCopyIn = false;
} else {
throw "Copy in mode not active!";
}
};
Query.prototype.copyFail = function(error) {
if (this._handlingCopyIn) {
this.connection._writeMessage(new FrontendMessage.CopyFail(error.toString()));
return this._handlingCopyIn = false;
} else {
throw "Copy in mode not active!";
}
};
Query.prototype._removeAllListeners = function() {
this.connection.removeListener('EmptyQueryResponse', this.onEmptyQueryListener);
this.connection.removeListener('RowDescription', this.onRowDescriptionListener);
this.connection.removeListener('DataRow', this.onDataRowListener);
this.connection.removeListener('CommandComplete', this.onCommandCompleteListener);
this.connection.removeListener('ErrorResponse', this.onErrorResponseListener);
this.connection.removeListener('ReadyForQuery', this.onReadyForQueryListener);
return this.connection.removeListener('CopyInResponse', this.onCopyInResponseListener);
};
return Query;
})(EventEmitter);
Query.Field = (function() {
function Field(msg) {
this.name = msg.name;
this.tableOID = msg.tableOID;
this.tableFieldIndex = msg.tableFieldIndex;
this.typeOID = msg.typeOID;
this.type = msg.type;
this.size = msg.size;
this.modifier = msg.modifier;
this.formatCode = msg.formatCode;
this.decoder = decoders[this.formatCode][this.type] || decoders[this.formatCode]["default"];
}
return Field;
})();
module.exports = Query;