$value ) { if ( property_exists( self::class, $name ) ) { $this->$name = $value; } } // The result handle of the source's last SELECT is the source's own: wpdb frees it when the source runs // its next statement. If this copy kept it, this copy's first flush() would free it, and the source's // next flush() would free it again, which PHP 8.1+ reports as "mysqli_result object is already closed". $this->result = null; $this->last_result = array(); $this->suppress_errors = true; $this->show_errors = false; } /** * wpdb::flush(), except that a result which has already been closed is dropped instead of freed again. * That cannot happen for the result this copy made itself; it is a guard, so that an export or a restore * can never stop on "mysqli_result object is already closed". */ public function flush() { // After a statement that returns no rows, wpdb holds `true` here, not a result. if ( $this->result instanceof \mysqli_result ) { $result = $this->result; Connection::freeResult( $result ); } $this->result = null; parent::flush(); } /** * Runs a statement. The result is what wpdb::query() returns: false on failure, see errno(). * * @return int|bool */ public function run( string $sql ) { $this->lost = false; $this->errno = 0; $this->message = ''; $result = $this->query( $sql ); $this->trim(); if ( false !== $result ) { return $result; } $this->message = (string) $this->last_error; $this->errno = $this->lost ? 2006 : $this->lookUpErrno( $this->message ); return false; } /** * Rows of a SELECT, or null on failure (see errno()). * * Each row is converted and released in turn, so a row set of large values is never held twice at once. * * @return array>|null */ public function rows( string $sql, bool $numeric ): ?array { if ( false === $this->run( $sql ) ) { return null; } $rows = array(); if ( is_array( $this->last_result ) ) { foreach ( array_keys( $this->last_result ) as $n ) { $row = get_object_vars( $this->last_result[ $n ] ); $rows[] = $numeric ? array_values( $row ) : $row; $this->last_result[ $n ] = null; } } $this->flush(); return $rows; } public function errno(): int { return $this->errno; } public function message(): string { return $this->message; } /** * Whether the last statement failed because the connection was gone (2006 "server has gone away" * or 2013 "lost connection"): it was not run, or it is not known whether it was. */ public function connectionLost(): bool { return in_array( $this->errno, array( 2006, 2013 ), true ); } /** * Opens the connection again. The session settings are gone with the old one: the caller restores them. */ public function reconnect(): bool { $this->may_reconnect = true; try { return (bool) $this->check_connection( false ); } finally { $this->may_reconnect = false; } } /** * Runs the statement with the "query" filter switched off: it is a hook for statements WordPress composed. * * @param string $query Database query. * @return int|bool */ public function query( $query ) { global $wp_filter; $hooks = isset( $wp_filter['query'] ) ? $wp_filter['query'] : null; unset( $wp_filter['query'] ); try { return parent::query( $query ); } finally { if ( null !== $hooks ) { $wp_filter['query'] = $hooks; } } } /** * wpdb::query() calls this when it meets error 2006 and then repeats the statement on the new * connection. Declined, so that run() can report the loss and the caller can restore the session first. * * @param bool $allow_bail Whether to give up with a fatal error. * @return bool */ public function check_connection( $allow_bail = true ) { if ( ! $this->may_reconnect ) { $this->lost = true; return false; } return parent::check_connection( $allow_bail ); } /** * Every statement is passed as it is: whatever its text, it is the dump's own. * * @param string $input_string The query. * @return bool */ protected function check_ascii( $input_string ) { return true; } /** * The error number of the statement that just failed. * * wpdb keeps the message only. SHOW ERRORS reports the last statement's error with its number, and * does not clear it. */ private function lookUpErrno( string $message ): int { if ( 1 === preg_match( '/server has gone away/i', $message ) ) { return 2006; } if ( 1 === preg_match( '/lost connection to/i', $message ) ) { return 2013; } $rows = $this->get_results( 'SHOW ERRORS', ARRAY_A ); $this->trim(); $this->last_error = $message; return ( is_array( $rows ) && isset( $rows[0]['Code'] ) ) ? (int) $rows[0]['Code'] : 0; } /** * Drops what a long restore would otherwise pile up: the statement log, when SAVEQUERIES is on. */ private function trim(): void { if ( ! empty( $this->queries ) ) { $this->queries = array(); } } }