我正在尝试从库(研究项目)对Node脚本进行逆向工程,以便按顺序执行以下任务:
1)打开并读取文件(例如,'input.txt')。为简单起见,假设内容是格式正确的SQL查询)
2)创建与MySQL数据库的连接
3)执行查询(从(1)构造 - 假设查询在文件中正确定义)
4)终止与数据库的连接
我希望按顺序执行这些任务(即1--4)。我没有太多使用Promises(Bluebird)的经验。这是我到目前为止的代码的摘录:
//Read the input file
function readFilePromise(){
return new Promise(function(resolve, reject){
var filePath = path.join(__dirname, filename);
//asynchronous read
fs.readFile(filePath, 'utf8', function (err, text){
if (err)
reject(err);
else
resolve(text.split('\n'));
});
})
}
//create connection
function createConnectionPromise(){
return new Promise(function (resolve, reject){
var connection = mysql.createConnection(connectionOptions);//global
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
reject(err);
}
else{
console.log('Connection established');
resolve(connection);
}
});
})
}
//do transactions
function doTransactionsPromise (data){
return new Promise(function (resolve, reject){
var connection = data[0];
var lines = data[1];
var topPromises = [];
lines.forEach(function(sSQL){
var p = new Promise(function(resolve,reject){
console.log('Add: ' + sSQL);
makeTransaction(connection, sSQL);
return connection;
});
topPromises.push(p);
});
resolve(topPromises);
});
}
//make transaction
function makeTransaction(connection, sSQL){
connection.beginTransaction(function(err){
function treatErro(err, connection) {
console.log('Failed to insert data in the database . Undoing!');
connection.rollback();
}
function final() {
connection.commit(function(err) {
if(err) {
treatErro(err, connection);
}
else {
console.log('Added: ' + sSQL);
return connection;
}
});
}
if(err) {
treatErro(err, connection);
}
else {
connection.query(sSQL, function (err, result) {
if(err) {
console.log(sSQL);
treatErro(err, connection);
}
else {
id = result.insertId;
}
});
final();
}
});
}
Promise.all([createConnectionPromise(), readFilePromise()])
.then(doTransactionsPromise)
.then(function(promises){
Promise.all(promises)
.then(function(data){
var connection = data[0];
connection.end();
});
})
.catch(function(error) {
console.log('Error occurred!', error);
});
查询执行正常,但与DB的连接不会终止。任何帮助表示赞赏。
PS:我确信代码可以大规模改进。
答案 0 :(得分:1)
我在代码中看到的可能问题是函数doTransaction
function doTransactionsPromise (data){
return new Promise(function (resolve, reject){
var connection = data[0];
var lines = data[1];
var topPromises = [];
lines.forEach(function(sSQL){
var p = new Promise(function(resolve,reject){
console.log('Add: ' + sSQL);
makeTransaction(connection, sSQL);
return connection;
});
// P is never fullfilled.
//Either transfer the responsibility to full-fill the promise to makeTransaction
// or makeTransaction function should return the promise which is full-filled by itself.
topPromises.push(p);
});
resolve(topPromises);
});
}
答案 1 :(得分:0)
我没有测试过代码,但是代码应该执行
//Read the input file
function readFilePromise(){
return new Promise(function(resolve, reject){
var filePath = path.join(__dirname, filename);
//asynchronous read
fs.readFile(filePath, 'utf8', function (err, text){
if (err)
reject(err);
else
resolve(text.split('\n'));
});
})
}
//create connection
function createConnectionPromise(){
return new Promise(function (resolve, reject){
var connection = mysql.createConnection(connectionOptions);//global
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
reject(err);
}
else{
console.log('Connection established');
resolve(connection);
}
});
})
}
//do transactions
function doTransactionsPromise (data){
var connection = data[0];
var lines = data[1];
var topPromises = [];
topPromise = lines.map(function(sSQL){
return makeTransaction(connection, sSQL);
});
return Promise.all(topPromises).then( function(){
return connection;
},function(){
return connection;
});
}
//make transaction
function makeTransaction(connection, sSQL){
return new Promise(resolve, reject, function(){
connection.beginTransaction(function(err){
function treatErro(err, connection) {
console.log('Failed to insert data in the database . Undoing!');
connection.rollback();
reject(connection);
}
function final() {
connection.commit(function(err) {
if(err) {
treatErro(err, connection);
}
else {
console.log('Added: ' + sSQL);
resolve(connection);
return connection;
}
});
}
if(err) {
treatErro(err, connection);
}
else {
connection.query(sSQL, function (err, result) {
if(err) {
console.log(sSQL);
treatErro(err, connection);
}
else {
id = result.insertId;
}
});
final();
}
});
})
}
Promise.all([createConnectionPromise(), readFilePromise()])
.then(doTransactionsPromise)
.then(function(connection){
return connection.end();
})
.catch(function(error) {
console.log('Error occurred!', error);
});