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
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Status {
    /** The server's response was not understood. */
    BadResponse,
    /** Successful completion of a command returning no data. */
    CommandOk,
    /**
     * Copy In/Out (to and from server) data transfer started. This feature is currently used only
     * for streaming replication, so this status should not occur in ordinary applications.
     */
    CopyBoth,
    /** Copy In (to server) data transfer started. */
    CopyIn,
    /** Copy Out (from server) data transfer started. */
    CopyOut,
    /** The string sent to the server was empty. */
    EmptyQuery,
    /** A fatal error occurred. */
    FatalError,
    /** A nonfatal error (a notice or warning) occurred. */
    NonFatalError,
    /**
     * The `libpq::Result` contains a single result tuple from the current command. This status
     * occurs only when single-row mode has been selected for the query
     */
    SingleTuble,
    /** Successful completion of a command returning data (such as a `SELECT` or `SHOW`). */
    TupplesOk,
}

#[doc(hidden)]
impl From<pq_sys::ExecStatusType> for Status {
    fn from(status: pq_sys::ExecStatusType) -> Self {
        match status {
            pq_sys::ExecStatusType::PGRES_BAD_RESPONSE => Self::BadResponse,
            pq_sys::ExecStatusType::PGRES_COMMAND_OK => Self::CommandOk,
            pq_sys::ExecStatusType::PGRES_COPY_BOTH => Self::CopyBoth,
            pq_sys::ExecStatusType::PGRES_COPY_IN => Self::CopyIn,
            pq_sys::ExecStatusType::PGRES_COPY_OUT => Self::CopyOut,
            pq_sys::ExecStatusType::PGRES_EMPTY_QUERY => Self::EmptyQuery,
            pq_sys::ExecStatusType::PGRES_FATAL_ERROR => Self::FatalError,
            pq_sys::ExecStatusType::PGRES_NONFATAL_ERROR => Self::NonFatalError,
            pq_sys::ExecStatusType::PGRES_SINGLE_TUPLE => Self::SingleTuble,
            pq_sys::ExecStatusType::PGRES_TUPLES_OK => Self::TupplesOk,
        }
    }
}

#[doc(hidden)]
impl From<Status> for pq_sys::ExecStatusType {
    fn from(status: Status) -> pq_sys::ExecStatusType {
        (&status).into()
    }
}

#[doc(hidden)]
impl From<&Status> for pq_sys::ExecStatusType {
    fn from(status: &Status) -> Self {
        match *status {
            Status::BadResponse => pq_sys::ExecStatusType::PGRES_BAD_RESPONSE,
            Status::CommandOk => pq_sys::ExecStatusType::PGRES_COMMAND_OK,
            Status::CopyBoth => pq_sys::ExecStatusType::PGRES_COPY_BOTH,
            Status::CopyIn => pq_sys::ExecStatusType::PGRES_COPY_IN,
            Status::CopyOut => pq_sys::ExecStatusType::PGRES_COPY_OUT,
            Status::EmptyQuery => pq_sys::ExecStatusType::PGRES_EMPTY_QUERY,
            Status::FatalError => pq_sys::ExecStatusType::PGRES_FATAL_ERROR,
            Status::NonFatalError => pq_sys::ExecStatusType::PGRES_NONFATAL_ERROR,
            Status::SingleTuble => pq_sys::ExecStatusType::PGRES_SINGLE_TUPLE,
            Status::TupplesOk => pq_sys::ExecStatusType::PGRES_TUPLES_OK,
        }
    }
}

impl ToString for Status {
    fn to_string(&self) -> String {
        crate::ffi::to_string(unsafe { pq_sys::PQresStatus(self.into()) })
    }
}