Skip to content

Commit 9a68682

Browse files
committed
First tests passing for new native bindings
1 parent 4c81c6d commit 9a68682

File tree

2 files changed

+139
-32
lines changed

2 files changed

+139
-32
lines changed

lib/native/index.js

+97-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,100 @@
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;
198
//require the c++ bindings & export to javascript
299
var EventEmitter = require('events').EventEmitter;
3100

@@ -108,38 +205,6 @@ Connection.prototype.cancel = function(client, query) {
108205
}
109206
};
110207

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-
143208
Connection.prototype.sendCopyFail = function(msg) {
144209
this.endCopyFrom(msg);
145210
};

lib/native/query.js

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
var EventEmitter = require('events').EventEmitter;
22
var util = require('util');
33

4+
var NativeQuery = module.exports = function(native) {
5+
EventEmitter.call(this);
6+
this.native = native;
7+
this.text = null;
8+
this.values = null;
9+
this.name = null;
10+
this.callback = null;
11+
this.state = 'new';
12+
};
13+
14+
util.inherits(NativeQuery, EventEmitter);
15+
16+
NativeQuery.prototype.submit = function() {
17+
this.state = 'running';
18+
var self = this;
19+
20+
var after = function(err, rows) {
21+
22+
//handle possible query error
23+
if(err) {
24+
self.state = 'error';
25+
if(self.callback) return self.callback(err);
26+
return self.emit('error', err);
27+
}
28+
29+
//handle successful result
30+
self.state = 'done';
31+
self.emit('done');
32+
if(self.callback) {
33+
self.callback(null, {rows: rows})
34+
}
35+
}
36+
37+
if(this.values) {
38+
this.native.query(this.text, this.values, after);
39+
} else {
40+
this.native.query(this.text, after);
41+
}
42+
};
43+
44+
return;
45+
446
var utils = require(__dirname + '/../utils');
547
var Result = require(__dirname + '/../result');
648

0 commit comments

Comments
 (0)