This commit is contained in:
welefen 2013-12-12 14:03:41 +08:00
parent 9339fd4a79
commit 280e6411bb
3 changed files with 42 additions and 19 deletions

View File

@ -37,12 +37,9 @@ var db = module.exports = Db(function(){
query: function(str){
this.initConnect(false);
if (!this._linkID) {
return false;
return get_promise(false);
};
this.queryStr = str;
if (this.queryID) {
this.free();
};
N('db_query', 1);
var self = this;
return this._linkID.query(str).then(function(ret){
@ -64,9 +61,6 @@ var db = module.exports = Db(function(){
return false;
};
this.queryStr = str;
if (this.queryID) {
this.free();
};
N('db_execute', 1);
var self = this;
return this._linkID.query(str).then(function(ret){

View File

@ -8,15 +8,33 @@ var mysql = require("mysql");
var socket = module.exports = Class(function(){
return {
connect: null,
error : "",
init: function(config){
this.connect = null;
this.error = "";
var self = this;
//创建连接
var connection = mysql.createConnection({
host : config.host || "localhost",
user : config.user || "root",
password : config.password || "",
database : config.database || ""
});
connection.connect();
//连接
connection.connect(function(err){
if (err) {
console.log('error when connecting to db:', err);
};
});
//错误时重新连接
connection.on("error", function(err){
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
self.init(config);
} else {
self.error = err;
console.log("db error:" , err);
}
})
this.connect = connection;
},
/**
@ -29,20 +47,30 @@ var socket = module.exports = Class(function(){
console.log(sql);
};
var deferred = when.defer();
this.connect.query(sql, function(err, rows, fields){
if (err) {
return deferred.resolve({
var self = this;
if (this.error) {
process.nextTick(function(){
deferred.resolve({
errno: 1,
errmsg: err,
errmsg: "db error",
});
})
}else{
this.connect.query(sql, function(err, rows, fields){
if (err) {
return deferred.resolve({
errno: 1,
errmsg: err,
fields: fields
});
};
deferred.resolve({
errno: 0,
data: rows || [],
fields: fields
});
};
deferred.resolve({
errno: 0,
data: rows || [],
fields: fields
});
})
}
return deferred.promise;
},
/**
@ -51,6 +79,7 @@ var socket = module.exports = Class(function(){
*/
close: function(){
this.connect && this.connect.end();
this.connect = null;
}
}
})

View File

@ -1,7 +1,7 @@
{
"name": "thinkjs",
"description": "thinkphp web framework for nodejs",
"version": "0.1.19",
"version": "0.1.21",
"author": {
"name": "welefen",
"email": "welefen@gmail.com"