/** * Replicate session data to specified target * * @param string $target Target to replicate to * @param string $id Id of record to replicate, * if not specified current session id will be used * * @return boolean */ public function replicate($target, $id = null) { if (is_null($id)) { $id = HTTP_Session2::id(); } // Check if table row already exists $query = sprintf("SELECT COUNT(id) FROM %s WHERE id = %s", $target, $this->_db->quoteSmart(md5($id))); $result = $this->_db->getOne($query); if (DB::isError($result)) { new DB_Error($result->code, PEAR_ERROR_DIE); return false; } // Insert new row into target table if (0 == intval($result)) { $query = "INSERT INTO {$target} SELECT * FROM"; $query .= " " . $this->options['table']; $query .= " WHERE id = " . $this->_db->quoteSmart(md5($id)); } else { // Update existing row $query = "UPDATE {$target} dst,"; $query .= " " . $this->options['table']; $query .= " src SET dst.expiry = src.expiry,"; $query .= " dst.data = src.data"; $query .= " WHERE dst.id = src.id"; $query .= " AND src.id = " . $this->_db->quoteSmart(md5($id)); } $result = $this->_db->query($query); if (DB::isError($result)) { new DB_Error($result->code, PEAR_ERROR_DIE); return false; } return true; }
/** * Replicate session data to specified target * * @param string $target The target (table) to replicate to. * @param string $id Id of record to replicate, * if not specified current session id will be used * * @return boolean * @throws HTTP_Session2_Exception To carry any MDB2 related error out. */ public function replicate($target, $id = null) { if ($id === null) { $id = HTTP_Session2::id(); } // Check if table row already exists $query = "SELECT COUNT(id) FROM {$target}"; $query .= " WHERE id = " . $this->db->quote(md5($id), 'text'); $result = $this->db->queryOne($query); if (MDB2::isError($result)) { throw new HTTP_Session2_Exception($result->getDebugInfo(), $result->getCode()); } // Insert new row into dest table if (0 == intval($result)) { $query = sprintf("INSERT INTO %s SELECT * FROM %s WHERE id = %s", $target, $this->options['table'], $this->db->quote(md5($id), 'text')); } else { // Update existing row $query = "UPDATE {$target} dst, " . $this->options['table']; $query .= " src SET dst.expiry = src.expiry,"; $query .= " dst.data = src.data"; $query .= " WHERE dst.id = src.id"; $query .= " AND src.id = " . $this->db->quote(md5($id), 'text'); } $result = $this->db->query($query); if (MDB2::isError($result)) { throw new HTTP_Session2_Exception($result->getDebugInfo(), $result->getCode()); } return true; }