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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
use crate::protocol::parts::{OutputParameters, ResultSet};
use crate::{HdbError, HdbResult};
use dist_tx::tm::XaTransactionId;
use std::fmt;

/// An enum that describes a single database return value.
#[derive(Debug)]
pub enum HdbReturnValue {
    /// A resultset of a query.
    ResultSet(ResultSet),
    /// A list of numbers of affected rows.
    AffectedRows(Vec<usize>),
    /// Values of output parameters of a procedure call.
    OutputParameters(OutputParameters),
    /// Indication that a db call was successful.
    Success,
    /// A list of `XaTransactionId`s.
    XaTransactionIds(Vec<XaTransactionId>),
}
impl HdbReturnValue {
    /// Turns itself into a single resultset.
    ///
    /// # Errors
    ///
    /// `HdbError::Evaluation` for other variants than `HdbReturnValue::ResultSet`.
    pub fn into_resultset(self) -> HdbResult<ResultSet> {
        match self {
            Self::ResultSet(rs) => Ok(rs),
            _ => Err(HdbError::Evaluation("Not a HdbReturnValue::ResultSet")),
        }
    }

    /// Turns itself into a Vector of numbers (each number representing a
    /// number of affected rows).
    ///
    /// # Errors
    ///
    /// `HdbError::Evaluation` for other variants than `HdbReturnValue::AffectedRows`.
    pub fn into_affected_rows(self) -> HdbResult<Vec<usize>> {
        match self {
            Self::AffectedRows(array) => Ok(array),
            _ => Err(HdbError::Evaluation("Not a HdbReturnValue::AffectedRows")),
        }
    }

    /// Turns itself into a Vector of numbers (each number representing a
    /// number of affected rows).
    ///
    /// # Errors
    ///
    /// `HdbError::Evaluation` for other variants than `HdbReturnValue::OutputParameters`.
    pub fn into_output_parameters(self) -> HdbResult<OutputParameters> {
        match self {
            Self::OutputParameters(op) => Ok(op),
            _ => Err(HdbError::Evaluation(
                "Not a HdbReturnValue::OutputParameters",
            )),
        }
    }

    /// Turns itself into (), if the statement had returned successfully.
    ///
    /// # Errors
    ///
    /// `HdbError::Evaluation` for other variants of `HdbReturnValue`.
    pub fn into_success(self) -> HdbResult<()> {
        match self {
            Self::Success => Ok(()),
            Self::AffectedRows(_) => {
                if self.is_success() {
                    Ok(())
                } else {
                    Err(HdbError::Evaluation(
                        "HdbReturnValue::AffectedRows contained value > 0",
                    ))
                }
            }
            Self::OutputParameters(_) | Self::ResultSet(_) | Self::XaTransactionIds(_) => Err(
                HdbError::Evaluation("Not a HdbReturnValue::AffectedRows or ::Success"),
            ),
        }
    }

    /// Returns true if the statement had returned successfully.
    pub fn is_success(&self) -> bool {
        match *self {
            Self::Success => true,
            Self::AffectedRows(ref vec) => vec.len() == 1 && vec.get(0) == Some(&0),
            _ => false,
        }
    }
}

impl fmt::Display for HdbReturnValue {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Self::AffectedRows(ref vec) => writeln!(fmt, "AffectedRows {:?},", vec),
            Self::OutputParameters(ref op) => writeln!(fmt, "OutputParameters [{}],", op),
            Self::ResultSet(ref rs) => writeln!(fmt, "ResultSet [{}],", rs),
            Self::Success => writeln!(fmt, "Success,"),
            Self::XaTransactionIds(_) => writeln!(fmt, "XaTransactionIds,<"),
        }
    }
}