/** * 初始化一个序列,如果已经初始化,则什么也不做 * * @param string $id_field 用于初始化的table中的id字段 * @return int */ public function initSeq($id_field) { $key = $this->getMemKey(); $seq = $this->cache->get($key); if ($seq === false) { $sql = sprintf("select next_value from id_sequence where id_key='%s'", $this->table_name); $value = $this->dbhelper->resultFirst($sql); if (empty($value)) { if ($this->data_exists) { // 如果数据存在,则从数据库中取得最大值 $sql = sprintf("select ifnull(max(%s),0) max_value from %s", $id_field, $this->table_name); $res = $this->dbhelper->getOne($sql); $value = $res['max_value']; } else { // 如果没有初始化id sequence表,则设置开始值为0 $value = 2; } } $this->cache->setMulti(array($key => $value, $key . '_next_value' => $value + $this->step), 0); $sql = "insert into id_sequence (id_key,current_value,next_value) values('%s',%d,%d)\n\t\t\t\t on duplicate key update next_value=next_value + %d"; $params = array($this->table_name, $value, $value + $this->step, $this->step); $this->dbhelper->execute($sql, $params); return $value; } return $seq; }
/** * * @return int */ private function initSeq() { $key = $this->getMemKey(); $seq = $this->cache->get($key); if ($seq === FALSE) { $sql = sprintf("select LAST_INSERT_ID() from %s", $this->table_name); $value = $this->dbhelper->resultFirst($sql); if (!empty($value)) { $seq = $value['gameuid']; } } if ($seq != FALSE && $seq >= 1) { $user_mgr = new UserAccountManager(); $account = $user_mgr->getUserAccount($seq); $nexAccount = $user_mgr->getUserAccount($seq); } if (empty($account) || !empty($nexAccount)) { $sql = sprintf("select ifnull(max(%s),0) max_value from %s", "gameuid", $this->table_name); $res = $this->dbhelper->getOne($sql); $seq = $res['max_value']; } return $seq; }