db = new RestoreDb( $db ); $this->charset = $charset; } /** * Applies the session settings a faithful restore needs. Repeated after a reconnect. * * NO_AUTO_VALUE_ON_ZERO keeps explicit 0 ids as 0, and an otherwise empty sql_mode * switches off strict mode and zero-date checks that old dumps would trip over. */ public function prepareSession(): void { $this->preparing = true; try { $this->exec( "SET SESSION sql_mode = 'NO_AUTO_VALUE_ON_ZERO'" ); $this->exec( 'SET SESSION foreign_key_checks = 0' ); $this->exec( 'SET SESSION unique_checks = 1' ); if ( 1 === preg_match( '/^[a-z0-9_]{2,32}$/i', $this->charset ) && ! $this->exec( 'SET NAMES ' . $this->charset ) ) { // The destination server may not know the source's charset (utf8mb4 on very old MySQL). $this->exec( 'SET NAMES ' . ( $this->db->charset ?: 'utf8' ) ); } } finally { $this->preparing = false; } } public function exec( string $sql ): bool { for ( $attempt = 0; $attempt < 2; $attempt++ ) { if ( false !== $this->db->run( $sql ) ) { $this->errno = 0; $this->error = ''; return true; } $this->remember(); if ( 0 === $attempt && ! $this->preparing && $this->recover() ) { continue; } return false; } return false; } /** * Rows of a SELECT as associative arrays, or null on error (see error()). * * @param bool $numeric Rows indexed by position instead of by column name. * @return array>|null */ public function select( string $sql, bool $numeric = false ): ?array { for ( $attempt = 0; $attempt < 2; $attempt++ ) { $rows = $this->db->rows( $sql, $numeric ); if ( null !== $rows ) { $this->errno = 0; $this->error = ''; return $rows; } $this->remember(); if ( 0 === $attempt && $this->recover() ) { continue; } return null; } return null; } /** * Frees a mysqli result at most once, and forgets it. * * Freeing a result that is already closed throws "mysqli_result object is already closed" on PHP 8.1+. * Callers keep the result in a variable, call this from the loop and again from a finally block, and the * second call finds null and does nothing. A free that fails for any reason is not worth stopping for: * the result is dropped either way. * * @param \mysqli_result|null $result Set to null on return. */ public static function freeResult( ?\mysqli_result &$result ): void { if ( $result instanceof \mysqli_result ) { try { $result->free(); } catch ( \Throwable $e ) { unset( $e ); // already closed (or the connection is gone): nothing is left to free. } } $result = null; } public function errno(): int { return $this->errno; } public function error(): string { return $this->error; } private function remember(): void { $this->errno = $this->db->errno(); $this->error = $this->db->message(); } /** * After a lost connection (2006 "server has gone away", 2013 "lost connection"): reconnects and restores * the session, and says whether the statement should be tried again. */ private function recover(): bool { if ( ! $this->db->connectionLost() || ! $this->db->reconnect() ) { return false; } $this->prepareSession(); return true; } }