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
105
106
107
108
109
110
111
use dist_tx::tm::XaTransactionId;
use protocol::parts::output_parameters::OutputParameters;
use protocol::parts::resultset::ResultSet;
use std::fmt;
use {HdbError, HdbResult};

/// 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.
    ///
    /// If this cannot be done without loss of information, an error is
    /// returned.
    pub fn into_resultset(self) -> HdbResult<ResultSet> {
        match self {
            HdbReturnValue::ResultSet(rs) => Ok(rs),
            _ => Err(HdbError::Evaluation(
                "HdbReturnValue::into_resultset(): not  a ResultSet".to_string(),
            )),
        }
    }

    /// Turns itself into a Vector of numbers (each number representing a
    /// number of affected rows).
    ///
    /// If this cannot be done without loss of information, an error is
    /// returned.
    pub fn into_affected_rows(self) -> HdbResult<Vec<usize>> {
        match self {
            HdbReturnValue::AffectedRows(array) => Ok(array),
            _ => Err(HdbError::Evaluation(
                "Wrong call to HdbReturnValue::into_affected_rows(): not AffectedRows".to_string(),
            )),
        }
    }

    /// Turns itself into a Vector of numbers (each number representing a
    /// number of affected rows).
    ///
    /// If this cannot be done without loss of information, an error is
    /// returned.
    pub fn into_output_parameters(self) -> HdbResult<OutputParameters> {
        match self {
            HdbReturnValue::OutputParameters(op) => Ok(op),
            _ => Err(HdbError::Evaluation(
                "Wrong call to HdbReturnValue::into_output_parameters(): not OutputParameters"
                    .to_string(),
            )),
        }
    }

    /// Turns itself into (), if the statement had returned successfully.
    ///
    /// If this cannot be done without loss of information, an error is
    /// returned.
    pub fn into_success(self) -> HdbResult<()> {
        match self {
            HdbReturnValue::Success => Ok(()),
            HdbReturnValue::AffectedRows(_) => if self.is_success() {
                Ok(())
            } else {
                Err(HdbError::Evaluation(
                    "Wrong call to HdbReturnValue::into_success(): non-zero AffectRows".to_string(),
                ))
            },
            HdbReturnValue::OutputParameters(_) => Err(HdbError::Evaluation(
                "Wrong call to HdbReturnValue::into_success(): is OutputParameters".to_string(),
            )),
            HdbReturnValue::ResultSet(_) => Err(HdbError::Evaluation(
                "Wrong call to HdbReturnValue::into_success(): is a ResultSet".to_string(),
            )),
            HdbReturnValue::XaTransactionIds(_) => Err(HdbError::Evaluation(
                "Wrong call to HdbReturnValue::into_success(): is a list of XaTransactionIds"
                    .to_string(),
            )),
        }
    }

    /// Returns true if the statement had returned successfully.
    pub fn is_success(&self) -> bool {
        match *self {
            HdbReturnValue::Success => true,
            HdbReturnValue::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 {
            HdbReturnValue::AffectedRows(ref vec) => writeln!(fmt, "AffectedRows {:?},", vec),
            HdbReturnValue::OutputParameters(ref op) => writeln!(fmt, "OutputParameters [{}],", op),
            HdbReturnValue::ResultSet(ref rs) => writeln!(fmt, "ResultSet [{}],", rs),
            HdbReturnValue::Success => writeln!(fmt, "Success,"),
            HdbReturnValue::XaTransactionIds(_) => writeln!(fmt, "XaTransactionIds,<"),
        }
    }
}