use crate::io::Encode;
use crate::mysql::protocol::text::ColumnFlags;
use crate::mysql::protocol::Capabilities;
use crate::mysql::MySqlArguments;

// https://dev.mysql.com/doc/dev/mysql-server/8.0.12/page_protocol_com_stmt_execute.html

#[derive(Debug)]
pub struct Execute<'q> {
    pub statement: u32,
    pub arguments: &'q MySqlArguments,
}

impl<'q> Encode<'_, Capabilities> for Execute<'q> {
    fn encode_with(&self, buf: &mut Vec<u8>, _: Capabilities) {
        buf.push(0x17); // COM_STMT_EXECUTE
        buf.extend(&self.statement.to_le_bytes());
        buf.push(0); // NO_CURSOR
        buf.extend(&0_u32.to_le_bytes()); // iterations (always 1): int<4>

        if !self.arguments.values.is_empty() {
            buf.extend(&*self.arguments.null_bitmap);
            buf.push(1); // send type to server

            for ty in &self.arguments.types {
                buf.push(ty.r#type as u8);

                buf.push(if ty.flags.contains(ColumnFlags::UNSIGNED) {
                    0x80
                } else {
                    0
                });
            }

            buf.extend(&*self.arguments.values);
        }
    }
}
