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
//! Result types to enabling handling of ODBC Errors
use super::{DiagnosticRecord, GetDiagRec};
use safe;
use std;

/// Result type returned by most functions in this crate
pub type Result<T> = std::result::Result<T, DiagnosticRecord>;

#[must_use]
pub enum Return<T> {
    Success(T),
    SuccessWithInfo(T),
    Error,
}

impl<T> Return<T> {
    pub fn into_result<O: GetDiagRec>(self, odbc_object: &O) -> Result<T> {
        match self {
            Return::Success(value) => Ok(value),
            Return::SuccessWithInfo(value) => {
                let mut i = 1;
                while let Some(diag) = odbc_object.get_diag_rec(i) {
                    warn!("{}", diag);
                    i += 1;
                }
                Ok(value)
            }
            Return::Error => {
                // Return the first record
                let diag = odbc_object.get_diag_rec(1).unwrap_or_else(DiagnosticRecord::empty);
                error!("{}", diag);
                let mut i = 2;
                // log the rest
                while let Some(diag) = odbc_object.get_diag_rec(i) {
                    error!("{}", diag);
                    i += 1;
                }
                Err(diag)
            }
        }
    }
}


// temporary glue code to odbc-safe
pub fn try_into_option<T, E, D>(ret: safe::ReturnOption<T, E>, handle: &D) -> Result<Option<T>>
where
    D: GetDiagRec,
{
    match ret {
        safe::ReturnOption::Success(value) => Ok(Some(value)),
        safe::ReturnOption::Info(value) => {
            let mut i = 1;
            while let Some(diag) = handle.get_diag_rec(i) {
                warn!("{}", diag);
                i += 1;
            }
            Ok(Some(value))
        }
        safe::ReturnOption::NoData(_) => Ok(None),
        safe::ReturnOption::Error(_) => {
            // Return the first record
            let diag = handle.get_diag_rec(1).unwrap_or_else(DiagnosticRecord::empty);
            error!("{}", diag);
            let mut i = 2;
            // log the rest
            while let Some(diag) = handle.get_diag_rec(i) {
                error!("{}", diag);
                i += 1;
            }
            Err(diag)
        }
    }
}

// temporary glue code to odbc-safe
pub fn into_result<T, E>(ret: safe::Return<T, E>) -> Result<T>
where
    T: GetDiagRec,
    E: GetDiagRec,
{
    match ret {
        safe::Return::Success(value) => Ok(value),
        safe::Return::Info(value) => {
            let mut i = 1;
            while let Some(diag) = value.get_diag_rec(i) {
                warn!("{}", diag);
                i += 1;
            }
            Ok(value)
        }
        safe::Return::Error(value) => {
            // Return the first record
            let diag = value.get_diag_rec(1).unwrap_or_else(DiagnosticRecord::empty);
            error!("{}", diag);
            let mut i = 2;
            // log the rest
            while let Some(diag) = value.get_diag_rec(i) {
                error!("{}", diag);
                i += 1;
            }
            Err(diag)
        }
    }
}

// temporary glue code to odbc-safe
pub fn into_result_with<D, T, E>(diag: &D, ret: safe::Return<T, E>) -> Result<T>
where
    D: GetDiagRec,
{
    match ret {
        safe::Return::Success(value) => Ok(value),
        safe::Return::Info(value) => {
            let mut i = 1;
            while let Some(diag_rec) = diag.get_diag_rec(i) {
                warn!("{}", diag_rec);
                i += 1;
            }
            Ok(value)
        }
        safe::Return::Error(_) => {
            // Return the first record
            let diag_rec = diag.get_diag_rec(1).unwrap_or_else(DiagnosticRecord::empty);
            error!("{}", diag_rec);
            let mut i = 2;
            // log the rest
            while let Some(diag_rec) = diag.get_diag_rec(i) {
                error!("{}", diag_rec);
                i += 1;
            }
            Err(diag_rec)
        }
    }
}