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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use {HdbError, HdbResult};
use protocol::lowlevel::parts::output_parameters::OutputParameters;
use hdb_return_value::HdbReturnValue;
use protocol::lowlevel::parts::resultset::ResultSet;
use std::fmt;

/// Represents all possible non-error responses to a database command.
///
#[derive(Debug)]
pub struct HdbResponse(Vec<HdbReturnValue>);

impl HdbResponse {
    /// 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> {
        self.into_single_retval()?.into_resultset()
    }

    /// 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>> {
        self.into_single_retval()?.into_affected_rows()
    }

    /// 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> {
        self.into_single_retval()?.into_output_parameters()
    }

    /// 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<()> {
        self.into_single_retval()?.into_success()
    }

    /// Turns itself into a single return value, if there is one any only one.
    pub fn into_single_retval(mut self) -> HdbResult<HdbReturnValue> {
        if self.0.len() > 1 {
            Err(HdbError::Evaluation(
                "Not a single HdbReturnValue".to_string(),
            ))
        } else {
            self.0.pop().ok_or_else(|| {
                HdbError::Evaluation("expected a single HdbReturnValue, found none".to_string())
            })
        }
    }

    /// Returns () if a successful execution was signaled by the database explicitly,
    /// or an error otherwise.
    pub fn get_success(&mut self) -> HdbResult<()> {
        if let Some(i) = self.find_success() {
            return self.0.remove(i).into_success();
        }
        Err(self.get_err("success"))
    }
    fn find_success(&self) -> Option<usize> {
        for (i, rt) in self.0.iter().enumerate().rev() {
            if rt.is_success() {
                return Some(i);
            }
        }
        None
    }

    /// Returns the next `ResultSet`, or an error if there is none.
    pub fn get_resultset(&mut self) -> HdbResult<ResultSet> {
        if let Some(i) = self.find_resultset() {
            return self.0.remove(i).into_resultset();
        }
        Err(self.get_err("resultset"))
    }
    fn find_resultset(&self) -> Option<usize> {
        for (i, rt) in self.0.iter().enumerate().rev() {
            if let HdbReturnValue::ResultSet(_) = *rt {
                return Some(i);
            }
        }
        None
    }

    /// Returns the next set of affected rows counters, or an error if there is
    /// none.
    pub fn get_affected_rows(&mut self) -> HdbResult<Vec<usize>> {
        if let Some(i) = self.find_affected_rows() {
            return self.0.remove(i).into_affected_rows();
        }
        Err(self.get_err("affected_rows"))
    }
    fn find_affected_rows(&self) -> Option<usize> {
        for (i, rt) in self.0.iter().enumerate().rev() {
            if let HdbReturnValue::AffectedRows(_) = *rt {
                return Some(i);
            }
        }
        None
    }

    /// Returns the next `OutputParameters`, or an error if there is none.
    pub fn get_output_parameters(&mut self) -> HdbResult<OutputParameters> {
        if let Some(i) = self.find_output_parameters() {
            return self.0.remove(i).into_output_parameters();
        }
        Err(self.get_err("output_parameters"))
    }
    fn find_output_parameters(&self) -> Option<usize> {
        for (i, rt) in self.0.iter().enumerate().rev() {
            if let HdbReturnValue::OutputParameters(_) = *rt {
                return Some(i);
            }
        }
        None
    }

    fn get_err(&self, type_s: &str) -> HdbError {
        let mut errmsg = String::new();
        errmsg.push_str("No ");
        errmsg.push_str(type_s);
        errmsg.push_str(" found in this HdbResponse [");
        for rt in &self.0 {
            errmsg.push_str(match *rt {
                HdbReturnValue::ResultSet(_) => "ResultSet, ",
                HdbReturnValue::AffectedRows(_) => "AffectedRows, ",
                HdbReturnValue::OutputParameters(_) => "OutputParameters, ",
                HdbReturnValue::Success => "Success, ",
                HdbReturnValue::XaTransactionIds(_) => "XaTransactionIds, ",
            });
        }
        errmsg.push_str("]");
        HdbError::Evaluation(errmsg)
    }
}

impl fmt::Display for HdbResponse {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt("HdbResponse [", fmt)?;
        for dbretval in &self.0 {
            fmt::Display::fmt(dbretval, fmt)?;
        }
        fmt::Display::fmt("]", fmt)?;
        Ok(())
    }
}

pub mod factory {
    use super::{HdbResponse, HdbReturnValue};
    use {HdbError, HdbResult};
    use protocol::lowlevel::parts::resultset::ResultSet;
    use protocol::lowlevel::parts::rows_affected::RowsAffected;
    use protocol::lowlevel::parts::output_parameters::OutputParameters;

    #[derive(Debug)]
    pub enum InternalReturnValue {
        ResultSet(ResultSet),
        AffectedRows(Vec<RowsAffected>),
        OutputParameters(OutputParameters),
    }

    pub fn resultset(mut int_return_values: Vec<InternalReturnValue>) -> HdbResult<HdbResponse> {
        if int_return_values.len() > 1 {
            return Err(HdbError::Impl(
                "Only a single ResultSet was expected".to_owned(),
            ));
        }
        match int_return_values.pop() {
            Some(InternalReturnValue::ResultSet(rs)) => {
                Ok(HdbResponse(vec![HdbReturnValue::ResultSet(rs)]))
            }
            None => Err(HdbError::Impl(
                "Nothing found, but a single Resultset was expected".to_owned(),
            )),
            _ => Err(HdbError::Impl(
                "Wrong HdbReturnValue, a single Resultset was expected".to_owned(),
            )),
        }
    }

    pub fn rows_affected(
        mut int_return_values: Vec<InternalReturnValue>,
    ) -> HdbResult<HdbResponse> {
        if int_return_values.len() > 1 {
            return Err(HdbError::Impl(
                "Only a single AffectedRows was expected".to_owned(),
            ));
        }
        match int_return_values.pop() {
            Some(InternalReturnValue::AffectedRows(vec_ra)) => {
                let mut vec_i = Vec::<usize>::new();
                for ra in vec_ra {
                    match ra {
                        RowsAffected::Count(i) => vec_i.push(i),
                        RowsAffected::SuccessNoInfo => vec_i.push(0),
                        RowsAffected::ExecutionFailed => {
                            return Err(HdbError::Impl(
                                "Found unexpected returnvalue ExecutionFailed".to_owned(),
                            ));
                        }
                    }
                }
                Ok(HdbResponse(vec![HdbReturnValue::AffectedRows(vec_i)]))
            }
            Some(InternalReturnValue::OutputParameters(_)) => Err(HdbError::Impl(
                "Found OutputParameters, but a single AffectedRows was expected".to_owned(),
            )),
            Some(InternalReturnValue::ResultSet(_)) => Err(HdbError::Impl(
                "Found ResultSet, but a single AffectedRows was expected".to_owned(),
            )),
            None => Err(HdbError::Impl(
                "Nothing found, but a single AffectedRows was expected".to_owned(),
            )),
        }
    }

    pub fn success(mut int_return_values: Vec<InternalReturnValue>) -> HdbResult<HdbResponse> {
        if int_return_values.is_empty() {
            return Ok(HdbResponse(vec![HdbReturnValue::Success]));
        } else if int_return_values.len() > 1 {
            return Err(HdbError::Impl(
                "found multiple InternalReturnValues, but only a single Success was expected"
                    .to_owned(),
            ));
        }
        match int_return_values.pop() {
            Some(InternalReturnValue::AffectedRows(mut vec_ra)) => {
                if vec_ra.len() != 1 {
                    return Err(HdbError::Impl(
                        "found no or multiple affected-row-counts, but only a single Success was \
                         expected"
                            .to_owned(),
                    ));
                }
                match vec_ra.pop().unwrap() {
                    RowsAffected::Count(i) => if i > 0 {
                        Err(HdbError::Impl(
                            "found an affected-row-count > 0, but only a single Success was \
                             expected"
                                .to_owned(),
                        ))
                    } else {
                        Ok(HdbResponse(vec![HdbReturnValue::Success]))
                    },
                    RowsAffected::SuccessNoInfo => Ok(HdbResponse(vec![HdbReturnValue::Success])),
                    RowsAffected::ExecutionFailed => Err(HdbError::Impl(
                        "Found unexpected returnvalue ExecutionFailed".to_owned(),
                    )),
                }
            }
            Some(InternalReturnValue::OutputParameters(_)) => Err(HdbError::Impl(
                "Found OutputParameters, but a single Success was expected".to_owned(),
            )),
            Some(InternalReturnValue::ResultSet(_)) => Err(HdbError::Impl(
                "Found ResultSet, but a single Success was expected".to_owned(),
            )),
            None => Err(HdbError::Impl(
                "Nothing found, but a single Success was expected".to_owned(),
            )),
        }
    }

    pub fn multiple_return_values(
        mut int_return_values: Vec<InternalReturnValue>,
    ) -> HdbResult<HdbResponse> {
        let mut vec_dbrv = Vec::<HdbReturnValue>::new();
        int_return_values.reverse();
        for irv in int_return_values {
            match irv {
                InternalReturnValue::AffectedRows(vec_ra) => {
                    let mut vec_i = Vec::<usize>::new();
                    for ra in vec_ra {
                        match ra {
                            RowsAffected::Count(i) => vec_i.push(i),
                            RowsAffected::SuccessNoInfo => vec_i.push(0),
                            RowsAffected::ExecutionFailed => {
                                return Err(HdbError::Impl(
                                    "Found unexpected returnvalue 'ExecutionFailed'".to_owned(),
                                ));
                            }
                        }
                    }
                    vec_dbrv.push(HdbReturnValue::AffectedRows(vec_i));
                }
                InternalReturnValue::OutputParameters(op) => {
                    vec_dbrv.push(HdbReturnValue::OutputParameters(op));
                }
                InternalReturnValue::ResultSet(rs) => {
                    vec_dbrv.push(HdbReturnValue::ResultSet(rs));
                }
            }
        }
        Ok(HdbResponse(vec_dbrv))
    }
}