Skip to content

Commit 113b629

Browse files
committed
use ConnectionParameters with native bindings and remove unused util functions
1 parent 020607c commit 113b629

File tree

5 files changed

+24
-257
lines changed

5 files changed

+24
-257
lines changed

lib/native/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//require the c++ bindings & export to javascript
22
var EventEmitter = require('events').EventEmitter;
3+
4+
var ConnectionParameters = require(__dirname + '/../connection-parameters');
35
var utils = require(__dirname + "/../utils");
46
var CopyFromStream = require(__dirname + '/../copystream').CopyFromStream;
57
var CopyToStream = require(__dirname + '/../copystream').CopyToStream;
@@ -28,7 +30,7 @@ var nativeConnect = p.connect;
2830

2931
p.connect = function(cb) {
3032
var self = this;
31-
utils.buildLibpqConnectionString(this._config, function(err, conString) {
33+
this.connectionParameters.getLibpqConnectionString(function(err, conString) {
3234
if(err) {
3335
return cb ? cb(err) : self.emit('error', err);
3436
}
@@ -143,13 +145,13 @@ var clientBuilder = function(config) {
143145
connection._queryQueue = [];
144146
connection._namedQueries = {};
145147
connection._activeQuery = null;
146-
connection._config = utils.normalizeConnectionInfo(config);
148+
connection.connectionParameters = new ConnectionParameters(config);
147149
//attach properties to normalize interface with pure js client
148-
connection.user = connection._config.user;
149-
connection.password = connection._config.password;
150-
connection.database = connection._config.database;
151-
connection.host = connection._config.host;
152-
connection.port = connection._config.port;
150+
connection.user = connection.connectionParameters.user;
151+
connection.password = connection.connectionParameters.password;
152+
connection.database = connection.connectionParameters.database;
153+
connection.host = connection.connectionParameters.host;
154+
connection.port = connection.connectionParameters.port;
153155
connection.on('connect', function() {
154156
connection._connected = true;
155157
connection._pulseQueryQueue(true);

lib/utils.js

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -13,88 +13,6 @@ if(typeof events.EventEmitter.prototype.once !== 'function') {
1313
};
1414
}
1515

16-
var parseConnectionString = function(str) {
17-
//unix socket
18-
if(str.charAt(0) === '/') {
19-
return { host: str };
20-
}
21-
var result = url.parse(str);
22-
var config = {};
23-
config.host = result.hostname;
24-
config.database = result.pathname ? result.pathname.slice(1) : null
25-
var auth = (result.auth || ':').split(':');
26-
config.user = auth[0];
27-
config.password = auth[1];
28-
config.port = result.port;
29-
return config;
30-
};
31-
32-
//allows passing false as property to remove it from config
33-
var norm = function(config, propName) {
34-
config[propName] = (config[propName] || (config[propName] === false ? undefined : defaults[propName]))
35-
};
36-
37-
//normalizes connection info
38-
//which can be in the form of an object
39-
//or a connection string
40-
var normalizeConnectionInfo = function(config) {
41-
switch(typeof config) {
42-
case 'object':
43-
norm(config, 'user');
44-
norm(config, 'password');
45-
norm(config, 'host');
46-
norm(config, 'port');
47-
norm(config, 'database');
48-
return config;
49-
case 'string':
50-
return normalizeConnectionInfo(parseConnectionString(config));
51-
default:
52-
throw new Error("Unrecognized connection config parameter: " + config);
53-
}
54-
};
55-
56-
57-
var add = function(params, config, paramName) {
58-
var value = config[paramName];
59-
if(value) {
60-
params.push(paramName+"='"+value+"'");
61-
}
62-
}
63-
64-
//builds libpq specific connection string
65-
//from a supplied config object
66-
//the config object conforms to the interface of the config object
67-
//accepted by the pure javascript client
68-
var getLibpgConString = function(config, callback) {
69-
if(typeof config == 'object') {
70-
var params = []
71-
add(params, config, 'user');
72-
add(params, config, 'password');
73-
add(params, config, 'port');
74-
if(config.database) {
75-
params.push("dbname='" + config.database + "'");
76-
}
77-
if(config.host) {
78-
if (!config.host.indexOf("/")) {
79-
params.push("host=" + config.host);
80-
} else {
81-
if(config.host != 'localhost' && config.host != '127.0.0.1') {
82-
//do dns lookup
83-
return require('dns').lookup(config.host, function(err, address) {
84-
if(err) return callback(err, null);
85-
params.push("hostaddr="+address)
86-
callback(null, params.join(" "))
87-
})
88-
}
89-
params.push("hostaddr=127.0.0.1 ");
90-
}
91-
}
92-
callback(null, params.join(" "));
93-
} else {
94-
throw new Error("Unrecognized config type for connection");
95-
}
96-
}
97-
9816
//converts values from javascript types
9917
//to their 'raw' counterparts for use as a postgres parameter
10018
//note: you can override this function to provide your own conversion mechanism
@@ -126,12 +44,6 @@ function normalizeQueryConfig (config, values, callback) {
12644
}
12745

12846
module.exports = {
129-
normalizeConnectionInfo: normalizeConnectionInfo,
130-
//only exported here to make testing of this method possible
131-
//since it contains quite a bit of logic and testing for
132-
//each connection scenario in an integration test is impractical
133-
buildLibpqConnectionString: getLibpgConString,
134-
parseConnectionString: parseConnectionString,
13547
prepareValue: prepareValue,
13648
normalizeQueryConfig: normalizeQueryConfig
13749
}

test/cli.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
var config = {};
2-
if(process.argv[2]) {
3-
config = require(__dirname + '/../lib/utils').parseConnectionString(process.argv[2]);
4-
}
5-
//TODO use these environment variables in lib/ code
6-
//http://www.postgresql.org/docs/8.4/static/libpq-envars.html
7-
config.host = config.host || process.env['PGHOST'] || process.env['PGHOSTADDR'];
8-
config.port = config.port || process.env['PGPORT'];
9-
config.database = config.database || process.env['PGDATABASE'];
10-
config.user = config.user || process.env['PGUSER'];
11-
config.password = config.password || process.env['PGPASSWORD'];
1+
var ConnectionParameters = require(__dirname + '/../lib/connection-parameters');
2+
var config = new ConnectionParameters(process.argv[2]);
123

134
for(var i = 0; i < process.argv.length; i++) {
145
switch(process.argv[i].toLowerCase()) {

test/integration/client/copy-tests.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ if(helper.args.native) {
44
pg = require(__dirname + '/../../../lib').native;
55
}
66
var ROWS_TO_INSERT = 1000;
7-
87
var prepareTable = function (client, callback) {
98
client.query(
109
'CREATE TEMP TABLE copy_test (id SERIAL, name CHARACTER VARYING(10), age INT)',
@@ -14,9 +13,8 @@ var prepareTable = function (client, callback) {
1413
})
1514
);
1615
};
17-
1816
test('COPY FROM', function () {
19-
pg.connect(helper.config, assert.calls(function (error, client) {
17+
pg.connect(helper.config, function (error, client) {
2018
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
2119
prepareTable(client, function () {
2220
var stream = client.copyFrom("COPY copy_test (name, age) FROM stdin WITH CSV");
@@ -27,21 +25,20 @@ test('COPY FROM', function () {
2725
stream.write( String(Date.now() + Math.random()).slice(0,10) + ',' + i + '\n');
2826
}
2927
assert.emits(stream, 'close', function () {
30-
client.query("SELECT count(*), sum(age) from copy_test", assert.calls(function (err, result) {
28+
client.query("SELECT count(*), sum(age) from copy_test", function (err, result) {
3129
assert.equal(err, null, "Query should not fail");
3230
assert.lengthIs(result.rows, 1)
3331
assert.equal(result.rows[0].sum, ROWS_TO_INSERT * (0 + ROWS_TO_INSERT -1)/2);
3432
assert.equal(result.rows[0].count, ROWS_TO_INSERT);
3533
pg.end(helper.config);
36-
}));
34+
});
3735
}, "COPY FROM stream should emit close after query end");
3836
stream.end();
3937
});
40-
}));
38+
});
4139
});
42-
4340
test('COPY TO', function () {
44-
pg.connect(helper.config, assert.calls(function (error, client) {
41+
pg.connect(helper.config, function (error, client) {
4542
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
4643
prepareTable(client, function () {
4744
var stream = client.copyTo("COPY person (id, name, age) TO stdin WITH CSV");
@@ -59,10 +56,11 @@ test('COPY TO', function () {
5956
pg.end(helper.config);
6057
}, "COPY IN stream should emit end event after all rows");
6158
});
62-
}));
59+
});
6360
});
6461

6562
test('COPY TO, queue queries', function () {
63+
if(helper.config.native) return false;
6664
pg.connect(helper.config, assert.calls(function (error, client) {
6765
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
6866
prepareTable(client, function () {
@@ -77,10 +75,10 @@ test('COPY TO, queue queries', function () {
7775
//imitate long query, to make impossible,
7876
//that copy query end callback runs after
7977
//second query callback
80-
client.query("SELECT pg_sleep(1)", assert.calls(function () {
78+
client.query("SELECT pg_sleep(1)", function () {
8179
query2Done = true;
8280
assert.ok(copyQueryDone && query2Done, "second query has to be executed after others");
83-
}));
81+
});
8482
var buf = new Buffer(0);
8583
stream.on('error', function (error) {
8684
assert.ok(false, "COPY TO stream should not emit errors" + helper.sys.inspect(error));
@@ -101,6 +99,7 @@ test('COPY TO, queue queries', function () {
10199
});
102100

103101
test("COPY TO incorrect usage with large data", function () {
102+
if(helper.config.native) return false;
104103
//when many data is loaded from database (and it takes a lot of time)
105104
//there are chance, that query will be canceled before it ends
106105
//but if there are not so much data, cancel message may be
@@ -125,6 +124,7 @@ test("COPY TO incorrect usage with large data", function () {
125124
});
126125

127126
test("COPY TO incorrect usage with small data", function () {
127+
if(helper.config.native) return false;
128128
pg.connect(helper.config, assert.calls(function (error, client) {
129129
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
130130
//intentionally incorrect usage of copy.
@@ -144,7 +144,7 @@ test("COPY TO incorrect usage with small data", function () {
144144
});
145145

146146
test("COPY FROM incorrect usage", function () {
147-
pg.connect(helper.config, assert.calls(function (error, client) {
147+
pg.connect(helper.config, function (error, client) {
148148
assert.equal(error, null, "Failed to connect: " + helper.sys.inspect(error));
149149
prepareTable(client, function () {
150150
//intentionally incorrect usage of copy.
@@ -161,6 +161,6 @@ test("COPY FROM incorrect usage", function () {
161161
})
162162
);
163163
});
164-
}));
164+
});
165165
});
166166

0 commit comments

Comments
 (0)