var userMaxMessageIdDao = Object.create(baseDao); userMaxMessageIdDao.db = "dianwutong"; userMaxMessageIdDao.table = "user_max_message_id"; userMaxMessageIdDao.selectAll = function(){ var sql = "select * from `" +this.table+ "`"; this.query(sql, function(ret,err){ console.log("userMaxMessageId的所有数据:") console.log(JSON.stringify(ret)); console.log(JSON.stringify(err)); }) } userMaxMessageIdDao.deleteTable = function(){ var sql = "drop table `" +this.table+ "`"; this.exec(sql, function(ret,err){ console.log('删除表:userMaxMessage'); }) } //创建message表 userMaxMessageIdDao.initTable = function(callback) { var sql = " CREATE TABLE IF NOT EXISTS `" + this.table + "` ( " + " `msg_id` INT(11) NOT NULL, " + " `user_id` INT(11) NOT NULL" + " )"; this.exec(sql, function(ret, err){ if(callback){ callback(ret, err); } }); } userMaxMessageIdDao.insert = function( userId, msgId, callback){ var sql = "insert into `" + this.table + "`(`user_id`, `msg_id`) values(" +userId+ "," +msgId+ ")"; this.exec(sql, function(ret, err) { if(callback){ callback(); } }) } userMaxMessageIdDao.update = function( userId, msgId, callback ){ var sql = "update `" +this.table+ "` set `msg_id` = " + msgId +" where user_id = "+userId; this.exec(sql, function(ret, err) { if(callback){ callback(); } }) } userMaxMessageIdDao.updateUserMessageId = function(userId, msgId, callback){ //判断用户是否存在最大id 如果存在 则执行修改操作 否则执行插入操作 var _this = this; _this.getMaxId(userId, function(maxId){ if(maxId){ _this.update(userId, msgId, function(){ if(callback){ callback() } }) }else{ _this.insert(userId, msgId, function(){ if(callback){ callback() } }) } }) } userMaxMessageIdDao.getMaxId = function(userId, callback) { var sql = "select max(msg_id) as max from `" + this.table +"` where user_id = "+userId; this.query(sql, function(ret,err){ var max = 0; if(ret && ret.data &&ret.data[0] && ret.data[0].max){ max = ret.data[0].max; } if(callback){ callback(max); } }) }