Repository: diesel-rs/diesel
File: diesel/src/pg/connection/stmt/mod.rs (Rust)

Code before:
    pub(super) fn execute(
        &self,
        raw_connection: &mut RawConnection,
        param_data: &[Option<Vec<u8>>],
        row_by_row: bool,
    ) -> QueryResult<PgResult> {
        let params_pointer = param_data
            .iter()
            .map(|data| {
                data.as_ref()
                    .map(|d| d.as_ptr() as *const libc::c_char)
                    .unwrap_or(ptr::null())
            })
            .collect::<Vec<_>>();
        let param_lengths = param_data
            .iter()
            .map(|data| data.as_ref().map(|d| d.len().try_into()).unwrap_or(Ok(0)))
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| crate::result::Error::SerializationError(Box::new(e)))?;
        let param_count: libc::c_int = params_pointer
            .len()
            .try_into()
            .map_err(|e| crate::result::Error::SerializationError(Box::new(e)))?;

        if let Some(ref sql) = self.sql {
            // For regular unnamed statements, use send_query_params
            unsafe {
                raw_connection.send_query_params(
                    sql.as_ptr(),
                    param_count,
                    param_types_to_ptr(self.param_types.as_ref()),
                    params_pointer.as_ptr(),
                    param_lengths.as_ptr(),
                    self.param_formats.as_ptr(),
                    1,
                )
            }?;
        } else {
            // For named statements, use the existing approach
            unsafe {
                raw_connection.send_query_prepared(
                    self.name.as_ptr(),
                    param_count,
                    params_pointer.as_ptr(),
                    param_lengths.as_ptr(),
                    self.param_formats.as_ptr(),
                    1,
                )
            }?;
        }


Review comment (suggestion):
It would be great to include the reasons why we should use `send_query_params` here

Code after:
}

impl Statement {
    pub(super) fn execute(
        &self,
        raw_connection: &mut RawConnection,
        param_data: &[Option<Vec<u8>>],
        row_by_row: bool,
    ) -> QueryResult<PgResult> {
        let params_pointer = param_data
            .iter()
            .map(|data| {
                data.as_ref()
                    .map(|d| d.as_ptr() as *const libc::c_char)
                    .unwrap_or(ptr::null())
            })
            .collect::<Vec<_>>();
        let param_lengths = param_data
            .iter()
            .map(|data| data.as_ref().map(|d| d.len().try_into()).unwrap_or(Ok(0)))
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| crate::result::Error::SerializationError(Box::new(e)))?;
        let param_count: libc::c_int = params_pointer
            .len()
            .try_into()
            .map_err(|e| crate::result::Error::SerializationError(Box::new(e)))?;

        match &self.kind {
            StatementKind::Named { name } => {
                unsafe {
                    // execute the previously prepared statement
                    // in autocommit mode, this will be a new transaction
                    raw_connection.send_query_prepared(
                        name.as_ptr(),
                        param_count,
                        params_pointer.as_ptr(),
                        param_lengths.as_ptr(),
                        self.param_formats.as_ptr(),
                        1,
                    )
                }?
            }
            StatementKind::Unnamed { sql, param_types } => unsafe {
                // execute the unnamed prepared statement using send_query_params
                // which internally calls PQsendQueryParams, making sure the
                // prepare and execute happens in a single transaction. This
                // makes sure these are handled by PgBouncer.
                // See https://github.com/diesel-rs/diesel/pull/4539
                raw_connection.send_query_params(
                    sql.as_ptr(),
                    param_count,