thinkjs/lib/Lib/Driver/Socket/MysqlSocket.class.js
2013-11-28 18:02:20 +08:00

56 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* mysql socket
* @return {[type]} [description]
*/
//暂时使用mysql库
var mysql = require("mysql");
var socket = module.exports = Class(function(){
return {
connect: null,
init: function(config){
this.connect = null;
var connection = mysql.createConnection({
host : config.host || "localhost",
user : config.user || "root",
password : config.password || "",
database : config.database || ""
});
connection.connect();
this.connect = connection;
},
/**
* 查询sql语句返回一个promise
* @param {[type]} sql [description]
* @return {[type]} [description]
*/
query: function(sql){
if (APP_DEBUG) {
console.log(sql);
};
var deferred = when.defer();
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
});
})
return deferred.promise;
},
/**
* 关闭连接
* @return {[type]} [description]
*/
close: function(){
this.connect && this.connect.end();
}
}
})