node-red-nodes-cf-sqldb-dashdb
Advanced tools
Comparing version
@@ -30,12 +30,7 @@ /** | ||
for (var i in appEnv.services) { | ||
// filter for SQLDB services | ||
if (i.match(/^(sqldb)/i)) { | ||
SQLDBservices = SQLDBservices.concat(appEnv.services[i].map(extractProperties)); | ||
} | ||
// filter for dashDB services | ||
else if (i.match(/^(Analytics)/i) || i.match(/^(dashDB)/i)) { | ||
// filter for dashDB and SQLDB services | ||
if (i.match(/^(Analytics)/i) || i.match(/^(dashDB)/i) || i.match(/^(sqldb)/i)) { | ||
dashDBservices = dashDBservices.concat(appEnv.services[i].map(extractProperties)); | ||
} | ||
else if (i.match(/^(user-provided)/i)) { | ||
SQLDBservices = SQLDBservices.concat(appEnv.services[i].map(extractProperties)); | ||
dashDBservices = dashDBservices.concat(appEnv.services[i].map(extractProperties)); | ||
@@ -45,9 +40,2 @@ } | ||
// | ||
// HTTP endpoints that will be accessed from the HTML file | ||
// | ||
RED.httpAdmin.get('/sqldb/vcap', function(req,res) { | ||
res.send(JSON.stringify(SQLDBservices)); | ||
}); | ||
RED.httpAdmin.get('/dashDB/vcap', function(req,res) { | ||
@@ -57,79 +45,23 @@ res.send(JSON.stringify(dashDBservices)); | ||
function SQLDBOutNode(n) { | ||
// | ||
// Create and register nodes | ||
// | ||
function dashDBNode(n) { | ||
RED.nodes.createNode(this, n); | ||
this.name = n.name; | ||
this.hostname = n.hostname; | ||
this.db = n.db; | ||
this.port = n.port; | ||
RED.nodes.createNode(this,n); | ||
var db = require('ibm_db')(); | ||
this.table = n.table; | ||
var SQLDBconfig = appEnv.getService(n.service); | ||
if (!this.table) { | ||
this.error("SQLDB node configuration error: table not defined"); | ||
return; | ||
} | ||
if (!SQLDBconfig) { | ||
this.error("SQLDB node configuration error: service not defined"); | ||
return; | ||
} | ||
var node = this; | ||
var db2 = {}; | ||
if (!debugLocal) { | ||
db2.db = SQLDBconfig.credentials.db; | ||
db2.username = SQLDBconfig.credentials.username; | ||
db2.hostname = SQLDBconfig.credentials.hostname; | ||
db2.password = SQLDBconfig.credentials.password; | ||
db2.port = SQLDBconfig.credentials.port; | ||
var credentials = this.credentials; | ||
if ((credentials) && (credentials.hasOwnProperty("username"))) { this.username = credentials.username; } | ||
if ((credentials) && (credentials.hasOwnProperty("password"))) { this.password = credentials.password; } | ||
} | ||
RED.nodes.registerType("dashDB", dashDBNode, { | ||
credentials: { | ||
password: {type:"password"}, | ||
username: {type:"text"} | ||
} | ||
// This is for debugging locally | ||
else { | ||
db2 = { | ||
db: "TESTDB", | ||
hostname: "localhost", | ||
port: 50000, | ||
username: "db2admin", | ||
password: "yourpassword" | ||
}; | ||
} | ||
}); | ||
var connString = "DRIVER={DB2};DATABASE=" + db2.db + ";UID=" + db2.username + ";PWD=" + db2.password + ";HOSTNAME=" + db2.hostname + ";port=" + db2.port; | ||
try { | ||
console.log("SQLDB output node: Opening db connection..."); | ||
db.openSync(connString); | ||
console.log("SQLDB output node: Connection open"); | ||
} | ||
catch (e) { | ||
node.error(e.message); | ||
} | ||
var columnList = getColumns(node,db,node.table,"SQLDB output node"); | ||
console.log("SQLDB output node: columnList: " + columnList); | ||
node.on("close", function() { | ||
console.log("SQLDB output node: Closing db connection..."); | ||
db.closeSync(); | ||
console.log("SQLDB output node: Connection closed"); | ||
}); | ||
console.log("columnList: " + columnList); | ||
var questionMarks = genQuestionMarks(columnList); | ||
var insertStatement = "insert into \""+node.table+"\" (" + columnList + ") values("+questionMarks+")"; | ||
console.log("SQLDB output node: Preparing insert statement: " + insertStatement); | ||
node.on("input", function(msg) { | ||
db.prepare(insertStatement, function (err, stmt) { | ||
if (err) { | ||
node.error("SQLDB output node: " + err); | ||
} | ||
else { | ||
console.log("SQLDB output node: Prepare successful"); | ||
processInput(node,msg,db,stmt,columnList,"SQLDB"); | ||
} | ||
}); | ||
}); | ||
} | ||
function dashDBOutNode(n) { | ||
@@ -141,3 +73,3 @@ | ||
this.table = n.table; | ||
var dashDBconfig = appEnv.getService(n.service); | ||
var dashDBconfig = _getdashDBconfig(n); | ||
@@ -151,27 +83,20 @@ if (!this.table) { | ||
this.error("dashDB node configuration error: service not defined"); | ||
return; | ||
return; | ||
} | ||
var node = this; | ||
var db2 = {}; | ||
if (!debugLocal) { | ||
db2.db = dashDBconfig.credentials.db; | ||
db2.username = dashDBconfig.credentials.username; | ||
db2.hostname = dashDBconfig.credentials.hostname; | ||
db2.password = dashDBconfig.credentials.password; | ||
db2.port = dashDBconfig.credentials.port; | ||
} | ||
// This is for debugging locally | ||
else { | ||
db2 = { | ||
db: "TESTDB", | ||
hostname: "localhost", | ||
port: 50000, | ||
username: "db2admin", | ||
password: "yourpassword" | ||
}; | ||
} | ||
var connString = "DRIVER={DB2};DATABASE=" + db2.db + ";UID=" + db2.username + ";PWD=" + db2.password + ";HOSTNAME=" + db2.hostname + ";port=" + db2.port; | ||
// This is for debugging locally | ||
if (debugLocal) { | ||
dashDBconfig = { | ||
db: "TESTDB", | ||
hostname: "localhost", | ||
port: 50000, | ||
username: "db2admin", | ||
password: "yourpassword" | ||
}; | ||
} | ||
var connString = "DRIVER={DB2};DATABASE=" + dashDBconfig.db + ";UID=" + dashDBconfig.username + ";PWD=" + dashDBconfig.password + ";HOSTNAME=" + dashDBconfig.hostname + ";port=" + dashDBconfig.port; | ||
try { | ||
@@ -187,3 +112,3 @@ console.log("dashDB output node: Opening db connection..."); | ||
var columnList = getColumns(node,db,node.table,"dashDB output node"); | ||
var columnListWithQuotes = ""; | ||
@@ -193,5 +118,4 @@ for (var i = 0; i < columnList.length; i++) { | ||
columnListWithQuotes += "\"" + columnList[i] + "\""; | ||
} | ||
console.log("dashDB output node: columnList: " + columnListWithQuotes); | ||
@@ -206,6 +130,6 @@ | ||
var questionMarks = genQuestionMarks(columnList); | ||
var insertStatement = "insert into \""+node.table+"\" (" + columnListWithQuotes + ") values("+questionMarks+")"; | ||
console.log("dashDB output node: Preparing insert statement: " + insertStatement); | ||
console.log("dashDB output node: Preparing insert statement: " + insertStatement); | ||
node.on("input", function(msg) { | ||
@@ -224,3 +148,2 @@ db.prepare(insertStatement, function (err, stmt) { | ||
RED.nodes.registerType("sqldb out", SQLDBOutNode); | ||
RED.nodes.registerType("dashDB out", dashDBOutNode); | ||
@@ -235,3 +158,3 @@ | ||
try { | ||
sysibmColumns = db.querySync("select name from sysibm.syscolumns where tbname = '"+table+"' and generated = ''"); | ||
sysibmColumns = db.querySync("select name from sysibm.syscolumns where tbname = '"+table+"' and generated = ''"); | ||
} | ||
@@ -244,3 +167,3 @@ catch (e) { | ||
if (sysibmColumns.length == 0) { | ||
node.error(service+": table "+table+" not found - is it defined? Case matters."); | ||
node.error(service+": table "+table+" not found - is it defined? Case matters."); | ||
return -1; | ||
@@ -265,6 +188,6 @@ } | ||
batchInsert = true; | ||
insertIterations = msg.payload.length; | ||
insertIterations = msg.payload.length; | ||
} | ||
else { | ||
console.log(service+": msg.payload not an array"); | ||
console.log(service+": msg.payload not an array"); | ||
batchInsert = false; | ||
@@ -280,6 +203,7 @@ insertIterations = 1; | ||
else valueToInsert = msg.payload[columnList[j]]; | ||
if (valueToInsert !== undefined) { | ||
if (valueToInsert == 'TIMESTAMP') { | ||
valueList.push(genDB2Timestamp()); | ||
} | ||
} | ||
else { | ||
@@ -295,5 +219,5 @@ valueList.push(valueToInsert); | ||
if (err) { | ||
node.error(service+": Insert failed: "+err); | ||
node.error(service+": Insert failed: "+err); | ||
} else { | ||
console.log(service+": Insert successful!"); | ||
console.log(service+": Insert successful!"); | ||
result.closeSync(); | ||
@@ -369,95 +293,2 @@ } | ||
function SQLDBQueryNode(n) { | ||
RED.nodes.createNode(this,n); | ||
var db = require('ibm_db')(); | ||
var query = n.query; | ||
var params = n.params; | ||
var SQLDBconfig = appEnv.getService(n.service); | ||
if (!SQLDBconfig) { | ||
this.error("SQLDB node configuration error: service not defined"); | ||
return; | ||
} | ||
var node = this; | ||
var db2 = {}; | ||
if (!debugLocal) { | ||
db2.db = SQLDBconfig.credentials.db; | ||
db2.username = SQLDBconfig.credentials.username; | ||
db2.hostname = SQLDBconfig.credentials.hostname; | ||
db2.password = SQLDBconfig.credentials.password; | ||
db2.port = SQLDBconfig.credentials.port; | ||
} | ||
// This is for debugging locally | ||
else { | ||
db2 = { | ||
db: "TESTDB", | ||
hostname: "localhost", | ||
port: 50000, | ||
username: "db2admin", | ||
password: "yourpassword" | ||
}; | ||
} | ||
var connString = "DRIVER={DB2};DATABASE=" + db2.db + ";UID=" + db2.username + ";PWD=" + db2.password + ";HOSTNAME=" + db2.hostname + ";port=" + db2.port; | ||
try { | ||
console.log("SQLDB query node: Opening db connection..."); | ||
db.openSync(connString); | ||
console.log("SQLDB query node: connection open"); | ||
} | ||
catch (e) { | ||
node.error(e.message); | ||
} | ||
node.on("close", function() { | ||
console.log("SQLDB query node: Closing db connection..."); | ||
db.closeSync(); | ||
console.log("SQLDB query node: Connection closed"); | ||
}); | ||
this.on('input', function(msg) { | ||
if (query == "" || query == null) { | ||
if (msg.payload == "" || msg.payload == null) { | ||
node.error("SQLDB query node: msg.payload is empty!"); | ||
return; | ||
} | ||
queryToUse = msg.payload; | ||
} | ||
else { | ||
queryToUse = query; | ||
} | ||
var parameterValues=[]; | ||
if (params != "" && params != null) { | ||
var path = pathToArray(params.toString()); | ||
console.log("Input node: pathToArray: " + path); | ||
parameterValues = extractValues(msg, path); | ||
console.log("Input node: parameterValues: " + parameterValues); | ||
} | ||
db.query(queryToUse,parameterValues,function (err, rows, moreResultSets) { | ||
queryresult = null; | ||
if (err) { | ||
node.error("SQLDB query node: " + err); | ||
msg.error = err; | ||
} else { | ||
console.log("Fetching rows: " + rows); | ||
console.log("value 1: " + JSON.stringify(rows[0])); | ||
if (rows.length == 1) {queryresult = rows[0];} | ||
else { | ||
queryresult = []; | ||
for (var i = 0; i < rows.length; i++) { | ||
queryresult.push(rows[i]); | ||
} | ||
} | ||
} | ||
msg.payload = queryresult; | ||
node.send(msg); | ||
}); | ||
}); | ||
} | ||
RED.nodes.registerType("sqldb in",SQLDBQueryNode); | ||
function dashDBQueryNode(n) { | ||
@@ -469,3 +300,3 @@ | ||
var params = n.params; | ||
var dashDBconfig = appEnv.getService(n.service); | ||
var dashDBconfig = _getdashDBconfig(n); | ||
@@ -478,22 +309,15 @@ if (!dashDBconfig) { | ||
var node = this; | ||
var db2 = {}; | ||
if (!debugLocal) { | ||
db2.db = dashDBconfig.credentials.db; | ||
db2.username = dashDBconfig.credentials.username; | ||
db2.hostname = dashDBconfig.credentials.hostname; | ||
db2.password = dashDBconfig.credentials.password; | ||
db2.port = dashDBconfig.credentials.port; | ||
} | ||
// This is for debugging locally | ||
else { | ||
db2 = { | ||
db: "TESTDB", | ||
hostname: "localhost", | ||
port: 50000, | ||
username: "db2admin", | ||
password: "yourpassword" | ||
if (debugLocal) { | ||
dashDBconfig = { | ||
db: "TESTDB", | ||
hostname: "localhost", | ||
port: 50000, | ||
username: "db2admin", | ||
password: "yourpassword" | ||
}; | ||
} | ||
var connString = "DRIVER={DB2};DATABASE=" + db2.db + ";UID=" + db2.username + ";PWD=" + db2.password + ";HOSTNAME=" + db2.hostname + ";port=" + db2.port; | ||
var connString = "DRIVER={DB2};DATABASE=" + dashDBconfig.db + ";UID=" + dashDBconfig.username + ";PWD=" + dashDBconfig.password + ";HOSTNAME=" + dashDBconfig.hostname + ";port=" + dashDBconfig.port; | ||
@@ -521,4 +345,4 @@ try { | ||
return; | ||
} | ||
queryToUse = msg.payload; | ||
} | ||
queryToUse = msg.payload; | ||
} | ||
@@ -545,3 +369,3 @@ else { | ||
else { | ||
queryresult = []; | ||
queryresult = []; | ||
for (var i = 0; i < rows.length; i++) { | ||
@@ -559,2 +383,20 @@ queryresult.push(rows[i]); | ||
function _getdashDBconfig(n) { | ||
if (n.service === "_ext_") { | ||
return RED.nodes.getNode(n.dashDB); | ||
} else if (n.service !== "") { | ||
var service = appEnv.getService(n.service); | ||
var dashDBconfig = { }; | ||
dashDBconfig.hostname = service.credentials.hostname; | ||
dashDBconfig.username = service.credentials.username; | ||
dashDBconfig.password = service.credentials.password; | ||
dashDBconfig.db = service.credentials.db; | ||
dashDBconfig.port = service.credentials.port; | ||
return dashDBconfig; | ||
} | ||
} | ||
}; |
{ | ||
"name" : "node-red-nodes-cf-sqldb-dashdb", | ||
"version" : "0.2.21", | ||
"version" : "0.2.22", | ||
"description" : "Node-RED nodes to access SQLDB and dashDB services on Bluemix", | ||
@@ -5,0 +5,0 @@ "dependencies" : { |
Sorry, the diff of this file is not supported yet
28904
-24.55%336
-29.71%