Skip to content

Commit d6541f7

Browse files
committed
fix bug test plus pg
1 parent 66b7cbb commit d6541f7

File tree

5 files changed

+113
-109
lines changed

5 files changed

+113
-109
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ _Note_ The examples will require the drivers to be installed
134134

135135
# Version
136136

137+
0.1.4 Bug fix for bug fix
138+
137139
0.1.3 Bug fix node-red depreciated features on nodes-started, added initial test connection test, timeout for postgresql rather than wait indefinitely
138140

139141
0.1.1 Bug fix commits DataStax and Cassandra as concept doesn't exist

connectionManager/admin-connections.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = function(RED) {
3838
if(n.type!=="Connection Manager") return;
3939
let cmNode=RED.nodes.getNode(n.id);
4040
if(cmNode) {
41-
node.testConnection(()=>{
41+
cmNode.testConnection(()=>{
4242
const out=Object.assign({},msg,
4343
{payload:{name:n.name,poolsize:cmNode.poolsize}}
4444
);

connectionManager/connection-manager.js

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ function query(msg,connection,statement,params,done,error) {
7474
return;
7575
}
7676
const pool=getPool(connector.pool);
77-
77+
7878
if(logger.active) logger.send({label:"query ",connection:connection,prepare:this.prepareSQL, preparable:pool.preparable});
7979
if(this.prepareSQL && pool.preparable) {
8080
const node=this;
81-
pool.prepare(connector,
81+
pool.prepare(connector,
8282
(prepared)=>pool.exec(prepared,done,error,params),
8383
(err)=>{
8484
if(logger.active) logger.send({label:"query prepare", error:err});
@@ -99,7 +99,7 @@ function query(msg,connection,statement,params,done,error) {
9999
params
100100
);
101101
return;
102-
}
102+
}
103103
if(logger.active) logger.send("query connection all connections");
104104
if(this.prepareSQL) {
105105
const node=this;
@@ -143,7 +143,7 @@ function batch(msg,connection,statement,params,done,error) {
143143
params
144144
);
145145
return;
146-
}
146+
}
147147
if(logger.active) logger.send("query connection all connections");
148148
connectionProcessor.apply(this,[msg,"batch",done,error,statement,params]);
149149
}
@@ -169,7 +169,7 @@ function release(msg,done,error) {
169169
cmProcessor.apply(this,[msg,"release",done,error]);
170170
return;
171171
}
172-
const thisObject=this,
172+
const thisObject=this,
173173
action=this.rollbackTransaction?"rollback":"commit";
174174
cmProcessor.apply(this,[msg,action,
175175
()=>{
@@ -229,7 +229,7 @@ function ConnectionPool(node) {
229229
logger.sendError("ConnectionPool "+ex.message);
230230
}
231231
}
232-
ConnectionPool.prototype.batch=function(c,done,error,sql,params) {
232+
ConnectionPool.prototype.batch=function(c,done,error,sql,params) {
233233
if(logger.active) logger.send({label:"ConnectionPool batch",id:c.id,sql:sql,parms:params,prepareid:prepareid});
234234
this.lastUsed[c.id]=new Date();
235235
const pool=this;
@@ -298,7 +298,7 @@ ConnectionPool.prototype.error=function(err,callback) {
298298
this.lastError=err;
299299
callback(err);
300300
}
301-
ConnectionPool.prototype.exec=function(c,done,error,id,params) {
301+
ConnectionPool.prototype.exec=function(c,done,error,id,params) {
302302
if(logger.active) logger.send("ConnectionPool exec connection id: "+c.id+" prepared id: "+id+" params: "+JSON.stringify(params));
303303
if(!this.preparable) {
304304
this.query(c,done,error,this.prepared[c.id][id],params,id);
@@ -323,7 +323,7 @@ ConnectionPool.prototype.getConnection=function(done,error) {
323323
return;
324324
}
325325
if(logger.active) logger.send("ConnectionPool getConnection create new connection");
326-
if(++this.newConnections>this.size) { // this.pool.length updated too late
326+
if(++this.newConnections>this.size) { // this.pool.length updated too late
327327
connectionPool.error("maximum pool size "+this.size,error);
328328
return;
329329
}
@@ -347,7 +347,7 @@ ConnectionPool.prototype.getConnection=function(done,error) {
347347
ConnectionPool.prototype.getDetails=function() {
348348
return {connected:this.pool.length,active:this.active.length,free:this.free.length,lastError:this.lastError||'',autoCommit:this.autoCommit};
349349
}
350-
ConnectionPool.prototype.prepare=function(c,done,error,sql,id) {
350+
ConnectionPool.prototype.prepare=function(c,done,error,sql,id) {
351351
if(logger.active) logger.send("ConnectionPool prepare connection id: "+c.id+" prepare id: "+id+" sql: "+sql);
352352
if(this.prepared[c.id]) {
353353
if(logger.active) logger.send("ConnectionPool prepare already done");
@@ -377,13 +377,13 @@ ConnectionPool.prototype.prepare=function(c,done,error,sql,id) {
377377
pool.prepared[c.id][id]=prepared;
378378
if(logger.active) logger.send("ConnectionPool prepared calling done");
379379
done(prepared);
380-
},
380+
},
381381
(err)=>{
382382
if(logger.active) logger.send("ConnectionPool prepare "+err);
383383
error(err)
384384
});
385385
};
386-
ConnectionPool.prototype.query=function(c,done,error,sql,params,prepareid) {
386+
ConnectionPool.prototype.query=function(c,done,error,sql,params,prepareid) {
387387
if(logger.active) logger.send({label:"ConnectionPool query connection",id:c.id,sql:sql,parms:params,prepareid:prepareid});
388388
this.lastUsed[c.id]=new Date();
389389
const pool=this;
@@ -424,7 +424,7 @@ ConnectionPool.prototype.rollback=function(c,done,error) {
424424
pool.checkDeadConnection(c,err);
425425
error(err)
426426
});
427-
}
427+
}
428428
ConnectionPool.prototype.releaseStaleConnections=function() {
429429
try{
430430
const thisObject=this,
@@ -462,16 +462,22 @@ ConnectionPool.prototype.releaseFreeConnections=function() {
462462
}
463463
ConnectionPool.prototype.test=function(done,error) {
464464
if(this.connectionTested) done();
465+
const thisObject=this;
465466
this.getConnection(
466467
(conn)=>{
467-
conn.pool.release(conn.c,()=>{
468-
conn.pool.connectionTested=true;
469-
done();
470-
},
471-
error
472-
)
468+
try{
469+
thisObject.release(conn.c,()=>{
470+
conn.pool.connectionTested=true;
471+
done();
472+
},
473+
error
474+
)
475+
} catch(ex) {
476+
logger.sendError("ConnectionPool.test error: "+ex);
477+
error(ex);
478+
}
473479
}
474-
,error);
480+
,error);
475481
}
476482

477483
module.exports = function(RED) {
@@ -486,7 +492,7 @@ module.exports = function(RED) {
486492
node.connectionError=err;
487493
});
488494
node.testConnection=function(done,error) {
489-
if(node.connectionError) error(node.connectionError);
495+
if(node.connectionError) error(node.connectionError);
490496
else done();
491497
}
492498
node.setMsg= function(msg,done,error) {
@@ -518,7 +524,7 @@ module.exports = function(RED) {
518524
);
519525
};
520526
node.on("close", function(removed,done) {
521-
clearInterval(node.releaseStaleConnections);
527+
clearInterval(node.releaseStaleConnections);
522528
node.connectionPool.close(done);
523529
});
524530
node.releaseStaleConnections = setInterval(function(node) {node.connectionPool.releaseStaleConnections.apply(node.connectionPool)}, 1000*60,node);
@@ -535,10 +541,10 @@ function Driver(a) {
535541
if(logger.active) logger.send({label:"New Drive ",argument:a});
536542
if(!a.optionsMapping) {
537543
this.optionsMapping ={
538-
host : "host",
539-
port : "port",
540-
database : "dbname",
541-
user : "user",
544+
host : "host",
545+
port : "port",
546+
database : "dbname",
547+
user : "user",
542548
password : "password"
543549
};
544550
}
@@ -562,7 +568,7 @@ function Driver(a) {
562568
Driver.prototype.batchArrayNotSupported=function(pool,conn,sql,params,done,error,i,results,errors,prepared) {
563569
error("Driver "+this.driverName+" doesn't support batch array");
564570
if(i && i>params.length) {
565-
if(errors)
571+
if(errors)
566572
done(results);
567573
return
568574
} else {
@@ -571,7 +577,7 @@ Driver.prototype.batchArrayNotSupported=function(pool,conn,sql,params,done,error
571577
this.driver.prepare(pool,conn,sql,
572578
(prepared)=>{
573579
this.batchArrayNotSupported(pool,conn,sql,params,done,error,0,results,errors,prepared);
574-
},
580+
},
575581
(err)=>{
576582
if(logger.active) logger.send("batchArrayNotSupported prepare "+err);
577583
error(err);
@@ -700,22 +706,27 @@ Driver.prototype.getOptions=function(node) {
700706
}
701707
return this.optionsCached;
702708
};
709+
710+
Driver.prototype.toStringConnection=function(node) {
711+
return JSON.stringify(Object.assign({},this.getOptions(node),{password:"***masked"}))
712+
};
713+
703714
Driver.prototype.getConnectionC=function(pool,node,done,error) {
704715
try{
705716
const options=this.getOptions(node);
706-
if(logger.active) logger.send("getConnectionC options "+JSON.stringify(Object.assign({},options,{password:"***masked"})));
707-
const thisObject=this;
708-
const c=new (this.Driver())(options);
717+
if(logger.active) logger.send("getConnectionC options "+this.toStringConnection(node));
718+
const thisObject=this, Driver=this.Driver();
719+
const c=new Driver(options);
709720
c.connect((err)=>{
710721
if(err) {
711-
if(logger.active) logger.send("getConnectionC error "+err);
722+
if(logger.active) logger.send("getConnectionC options: "+this.toStringConnection(node)+" error "+err);
712723
error(err);
713724
return;
714725
}
715726
if(logger.active) logger.send("getConnectionC OK ");
716727
if(thisObject.testOnConnect) {
717728
if(!thisObject.onConnectCache) {
718-
const mustache=require("mustache");
729+
const mustache=require("mustache");
719730
thisObject.onConnectCache=mustache.render(thisObject.testOnConnect,node);
720731
}
721732
thisObject.query(pool,c,thisObject.onConnectCache,null,()=>done(c),error);
@@ -724,16 +735,16 @@ Driver.prototype.getConnectionC=function(pool,node,done,error) {
724735
}
725736
});
726737
} catch(e) {
727-
logger.sendError("Driver.getConnectionC error: "+e);
738+
logger.sendError("Driver.getConnectionC options: "+this.toStringConnection(node)+" error: "+e);
728739
error(e);
729740
}
730741
};
731742
Driver.prototype.getConnectionO=function(pool,node,done,error) {
732743
try{
733744
const options=this.getOptions(node);
734-
if(logger.active) logger.send("getConnectionO options "+JSON.stringify(Object.assign({},options,{password:"***masked"})));
745+
if(logger.active) logger.send("getConnectionO options "+this.toStringConnection(node));
735746
if(!this.driverInstance) this.driverInstance= this.Driver();
736-
let thisObject=this,
747+
const thisObject=this,
737748
connectString="DATABASE="+options.database+";HOSTNAME="+options.host+";UID="+options.user+";PWD="+options.password+";PORT="+options.port+";PROTOCOL=TCPIP";
738749
// var ibmdb = require('ibm_db');
739750
// ibmdb.open(connectString,(err,conn)=>{
@@ -1107,10 +1118,10 @@ const DriverType = {
11071118
prepare:Driver.prototype.prepareQ,
11081119
exec:Driver.prototype.execQ,
11091120
optionsMapping: {
1110-
host : "host",
1111-
port : "port",
1112-
dbname : "dbname",
1113-
user : "user",
1121+
host : "host",
1122+
port : "port",
1123+
dbname : "dbname",
1124+
user : "user",
11141125
password : "password"
11151126
},
11161127
prepareIsQuery:false,
@@ -1130,12 +1141,12 @@ const DriverType = {
11301141
testOnConnect:null,
11311142
getConnection: Driver.prototype.getConnectionNeo4j,
11321143
query:Driver.prototype.queryNeo4j
1133-
}),
1144+
}),
11341145
'pg': new Driver({
1135-
// Driver:(()=>{const { Client } = require('pg'); return Client}),
1146+
Driver:(()=>{const { Client } = require('pg'); return Client}),
11361147
requireName:'pg',
11371148
autoCommit:true,
1138-
connectOptions:{"connectionTimeoutMillis": 2000},
1149+
connectOptions:{"connectionTimeoutMillis": 4000},
11391150
getResultArray:((r)=>r.rows),
11401151
prepareIsQuery:true,
11411152
query:Driver.prototype.queryCPG,
@@ -1146,4 +1157,4 @@ const DriverType = {
11461157
};
11471158
function ArrayObjects2ArrayArray(v){
11481159
return v.map((row)=>Object.values(row));
1149-
}
1160+
}

0 commit comments

Comments
 (0)