libpq/
status.rs

1#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2pub enum Status {
3    /** The server's response was not understood. */
4    BadResponse,
5    /** Successful completion of a command returning no data. */
6    CommandOk,
7    /**
8     * Copy In/Out (to and from server) data transfer started. This feature is currently used only
9     * for streaming replication, so this status should not occur in ordinary applications.
10     */
11    CopyBoth,
12    /** Copy In (to server) data transfer started. */
13    CopyIn,
14    /** Copy Out (from server) data transfer started. */
15    CopyOut,
16    /** The string sent to the server was empty. */
17    EmptyQuery,
18    /** A fatal error occurred. */
19    FatalError,
20    /** A nonfatal error (a notice or warning) occurred. */
21    NonFatalError,
22    /**
23     * The `libpq::PQResult` contains a single result tuple from the current command. This status
24     * occurs only when single-row mode has been selected for the query
25     */
26    SingleTuple,
27    /** Successful completion of a command returning data (such as a `SELECT` or `SHOW`). */
28    TuplesOk,
29    #[deprecated(since = "4.1.0", note = "Uses TuplesOk variant instead")]
30    TupplesOk,
31
32    /** Pipeline synchronization point. */
33    #[cfg(feature = "v14")]
34    PipelineSync,
35
36    /** Command didn't run because of an abort earlier in a pipeline. */
37    #[cfg(feature = "v14")]
38    PipelineAborted,
39}
40
41#[doc(hidden)]
42impl From<pq_sys::ExecStatusType> for Status {
43    fn from(status: pq_sys::ExecStatusType) -> Self {
44        match status {
45            pq_sys::ExecStatusType::PGRES_BAD_RESPONSE => Self::BadResponse,
46            pq_sys::ExecStatusType::PGRES_COMMAND_OK => Self::CommandOk,
47            pq_sys::ExecStatusType::PGRES_COPY_BOTH => Self::CopyBoth,
48            pq_sys::ExecStatusType::PGRES_COPY_IN => Self::CopyIn,
49            pq_sys::ExecStatusType::PGRES_COPY_OUT => Self::CopyOut,
50            pq_sys::ExecStatusType::PGRES_EMPTY_QUERY => Self::EmptyQuery,
51            pq_sys::ExecStatusType::PGRES_FATAL_ERROR => Self::FatalError,
52            pq_sys::ExecStatusType::PGRES_NONFATAL_ERROR => Self::NonFatalError,
53            pq_sys::ExecStatusType::PGRES_SINGLE_TUPLE => Self::SingleTuple,
54            pq_sys::ExecStatusType::PGRES_TUPLES_OK => Self::TuplesOk,
55            #[cfg(feature = "v14")]
56            pq_sys::ExecStatusType::PGRES_PIPELINE_SYNC => Self::PipelineSync,
57            #[cfg(feature = "v14")]
58            pq_sys::ExecStatusType::PGRES_PIPELINE_ABORTED => Self::PipelineAborted,
59            #[allow(unreachable_patterns)]
60            _ => unreachable!(),
61        }
62    }
63}
64
65#[doc(hidden)]
66impl From<Status> for pq_sys::ExecStatusType {
67    fn from(status: Status) -> pq_sys::ExecStatusType {
68        (&status).into()
69    }
70}
71
72#[doc(hidden)]
73impl From<&Status> for pq_sys::ExecStatusType {
74    fn from(status: &Status) -> Self {
75        match *status {
76            Status::BadResponse => pq_sys::ExecStatusType::PGRES_BAD_RESPONSE,
77            Status::CommandOk => pq_sys::ExecStatusType::PGRES_COMMAND_OK,
78            Status::CopyBoth => pq_sys::ExecStatusType::PGRES_COPY_BOTH,
79            Status::CopyIn => pq_sys::ExecStatusType::PGRES_COPY_IN,
80            Status::CopyOut => pq_sys::ExecStatusType::PGRES_COPY_OUT,
81            Status::EmptyQuery => pq_sys::ExecStatusType::PGRES_EMPTY_QUERY,
82            Status::FatalError => pq_sys::ExecStatusType::PGRES_FATAL_ERROR,
83            Status::NonFatalError => pq_sys::ExecStatusType::PGRES_NONFATAL_ERROR,
84            Status::SingleTuple => pq_sys::ExecStatusType::PGRES_SINGLE_TUPLE,
85            Status::TuplesOk => pq_sys::ExecStatusType::PGRES_TUPLES_OK,
86            #[allow(deprecated)]
87            Status::TupplesOk => pq_sys::ExecStatusType::PGRES_TUPLES_OK,
88            #[cfg(feature = "v14")]
89            Status::PipelineSync => pq_sys::ExecStatusType::PGRES_PIPELINE_SYNC,
90            #[cfg(feature = "v14")]
91            Status::PipelineAborted => pq_sys::ExecStatusType::PGRES_PIPELINE_ABORTED,
92            #[allow(unreachable_patterns)]
93            _ => unreachable!(),
94        }
95    }
96}
97
98impl std::fmt::Display for Status {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        let status = unsafe { pq_sys::PQresStatus(self.into()) };
101
102        let s = crate::connection::PqString::from_raw(status)
103            .to_string_lossy()
104            .to_string();
105
106        f.write_str(&s)
107    }
108}