var mysql = require('cofy-mysql');
var pool = mysql.createPool({
host : '127.0.0.1',
user : 'root',
password : '',
database : 'test',
queryFormat: function(query, values) {
if (!values) return query;
return query.replace(/\:(\w+)/g, function(txt, key) {
if (values.hasOwnProperty(key)) {
return this.escape(values[key]);
}
return txt;
}.bind(this));
}
});
co(function*(){
var users = (yield pool.$query("select * from user"))[0];
var apps = yield pool.$q("select * from apps where uid=:uid" , {uid:1});
var count = yield pool.$q1("select count(*) as count from user where name=:name and pwd=:pwd" , {name:'name',pwd:'111'}));
console.log(users);
var con ;
try{
con = yield pool.$getConnection();
yield con.$beginTransaction();
yield con.$query("select some");
yield con.$query("insert some");
yield con.$query("update some");
yield con.$commit();
}catch(e){
yield con.$rollback();
console.error(e);
}finally{
if(con){
con.release();
}
}
})