1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::protocol::parts::server_error::ServerError;
use crate::HdbResult;

use byteorder::{LittleEndian, ReadBytesExt};
use std::fmt;

/// Describes the success of a command.
#[derive(Debug)]
pub enum ExecutionResult {
    /// Number of rows that were affected by the successful execution.
    RowsAffected(usize),
    /// Command was successful.
    SuccessNoInfo, // -2
    /// Execution failed with given ServerError.
    Failure(Option<ServerError>), // -3
}
impl ExecutionResult {
    pub(crate) fn parse<T: std::io::BufRead>(
        count: usize,
        rdr: &mut T,
    ) -> HdbResult<Vec<ExecutionResult>> {
        let mut vec = Vec::<ExecutionResult>::with_capacity(count);
        for _ in 0..count {
            match rdr.read_i32::<LittleEndian>()? {
                -2 => vec.push(ExecutionResult::SuccessNoInfo),
                -3 => vec.push(ExecutionResult::Failure(None)),
                i => vec.push(ExecutionResult::RowsAffected(i as usize)),
            }
        }
        Ok(vec)
    }
    /// True if it is an instance of ExecutionResult::Failure.
    pub fn is_failure(&self) -> bool {
        match self {
            ExecutionResult::Failure(_) => true,
            _ => false,
        }
    }
    /// True if it is an instance of ExecutionResult::RowsAffected.
    pub fn is_rows_affected(&self) -> bool {
        match self {
            ExecutionResult::RowsAffected(_) => true,
            _ => false,
        }
    }
}

impl fmt::Display for ExecutionResult {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ExecutionResult::RowsAffected(count) => {
                writeln!(fmt, "Number of affected rows: {}, ", count)?
            }
            ExecutionResult::SuccessNoInfo => writeln!(
                fmt,
                "Command successfully executed but number of affected rows cannot be determined"
            )?,
            ExecutionResult::Failure(Some(ref server_error)) => writeln!(
                fmt,
                "Execution of statement or processing of row has failed with {:?}",
                server_error
            )?,
            ExecutionResult::Failure(None) => writeln!(
                fmt,
                "Execution of statement or processing of row has failed"
            )?,
        }
        Ok(())
    }
}