function nextId($seq_name) { $seqname = db_get_sequence_name($seq_name); $repeat = false; do { // $this->pushErrorHandling(PEAR_ERROR_RETURN); $result = db_fetch("SELECT NEXTVAL('{$seqname}')"); // $this->popErrorHandling(); if ($ondemand && DB::isError($result) && $result->getCode() == DB_ERROR_NOSUCHTABLE) { $repeat = true; $result = $this->createSequence($seq_name); if (DB::isError($result)) { return $this->raiseError($result); } } else { $repeat = false; } } while ($repeat); if (DB::isError($result)) { return $this->raiseError($result); } $arr = $result->fetchRow(DB_FETCHMODE_ORDERED); $result->free(); return $arr[0]; }
function nextId($seq_name) { $seqname = db_get_sequence_name($seq_name); do { $repeat = 0; // $this->pushErrorHandling(PEAR_ERROR_RETURN); $result = db_execute("UPDATE {$seqname} " . 'SET id=LAST_INSERT_ID(id+1)'); // $this->popErrorHandling(); if ($result) { /** COMMON CASE **/ $id = mysql_insert_id($this->connection); if ($id != 0) { return $id; } /** EMPTY SEQ TABLE **/ // Sequence table must be empty for some reason, so fill it and return 1 // Obtain a user-level lock $result = db_single("SELECT GET_LOCK('{$seqname}_lock',10)"); if (!$result) { return false; } // if (DB::isError($result)) { // return $this->raiseError($result); // } if ($result == 0) { // Failed to get the lock, bail with a DB_ERROR_NOT_LOCKED error // return $this->mysqlRaiseError(DB_ERROR_NOT_LOCKED); $this->error = 'Failed to obtain a db lock'; return false; } // add the default value $result = db_execute("REPLACE INTO {$seqname} VALUES (0)"); if (!$result) { return false; } // if (DB::isError($result)) { // return $this->raiseError($result); // } // Release the lock $result = db_single("SELECT RELEASE_LOCK('{$seqname}_lock')"); if (!$result) { return false; } // if (DB::isError($result)) { // return $this->raiseError($result); // } // We know what the result will be, so no need to try again return 1; /** ONDEMAND TABLE CREATION **/ } elseif ($ondemand && DB::isError($result) && $result->getCode() == DB_ERROR_NOSUCHTABLE) { $result = $this->createSequence($seq_name); // Since createSequence initializes the ID to be 1, // we do not need to retrieve the ID again (or we will get 2) if (!$result) { return false; // if (DB::isError($result)) { // return $this->raiseError($result); } else { // First ID of a newly created sequence is 1 return 1; } /** BACKWARDS COMPAT **/ /* } elseif (DB::isError($result) && $result->getCode() == DB_ERROR_ALREADY_EXISTS) { // see _BCsequence() comment $result = $this->_BCsequence($seqname); if (DB::isError($result)) { return $this->raiseError($result); } $repeat = 1; */ } } while ($repeat); // return $this->raiseError($result); return false; }