Automattic\WooCommerce\Database\Migrations\CustomOrderTable

PostsToOrdersMigrationController::db_query()privateWC 1.0

Execute a database query and log any errors.

Method of the class: PostsToOrdersMigrationController{}

No Hooks.

Return

true|false. True if the query succeeded, false if there were errors.

Usage

// private - for code of main (parent) class only
$result = $this->db_query( $query, $supress_errors ): bool;
$query(string) (required)
The SQL query to execute.
$supress_errors(true|false)
Whether to suppress errors.
Default: false

PostsToOrdersMigrationController::db_query() code WC 8.9.0

private function db_query( string $query, bool $supress_errors = false ): bool {
	$wpdb = WC()->get_global( 'wpdb' );

	try {
		if ( $supress_errors ) {
			$suppress = $wpdb->suppress_errors( true );
		}
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( $query );
		if ( $supress_errors ) {
			$wpdb->suppress_errors( $suppress );
		}
	} catch ( \Exception $exception ) {
		$exception_class = get_class( $exception );
		$this->error_logger->error(
			"PostsToOrdersMigrationController: when executing $query: ($exception_class) {$exception->getMessage()}, {$exception->getTraceAsString()}",
			array(
				'source'    => self::LOGS_SOURCE_NAME,
				'exception' => $exception,
			)
		);
		return false;
	}

	$error = $wpdb->last_error;
	if ( '' !== $error ) {
		$this->error_logger->error(
			"PostsToOrdersMigrationController: when executing $query: $error",
			array(
				'source' => self::LOGS_SOURCE_NAME,
				'error'  => $error,
			)
		);
		return false;
	}

	return true;
}