zero_postgres/protocol/
types.rs

1//! Common PostgreSQL wire protocol types.
2
3/// PostgreSQL Object Identifier (OID)
4pub type Oid = u32;
5
6/// Data format code in PostgreSQL protocol.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8#[repr(u16)]
9pub enum FormatCode {
10    /// Text format (human-readable)
11    #[default]
12    Text = 0,
13    /// Binary format (type-specific packed representation)
14    Binary = 1,
15}
16
17impl FormatCode {
18    /// Create a FormatCode from a raw u16 value.
19    pub fn from_u16(value: u16) -> Self {
20        match value {
21            0 => FormatCode::Text,
22            1 => FormatCode::Binary,
23            _ => FormatCode::Text, // Default to text for unknown values
24        }
25    }
26}
27
28impl From<u16> for FormatCode {
29    fn from(value: u16) -> Self {
30        Self::from_u16(value)
31    }
32}
33
34/// Returns the preferred format code for a given OID.
35///
36/// Most types use binary format for efficiency, but some types
37/// (like NUMERIC) use text format because the binary encoding
38/// is complex and text is equally efficient.
39pub fn preferred_format(oid: Oid) -> FormatCode {
40    match oid {
41        oid::NUMERIC => FormatCode::Text,
42        _ => FormatCode::Binary,
43    }
44}
45
46/// Transaction status indicator from ReadyForQuery message.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
48#[repr(u8)]
49pub enum TransactionStatus {
50    /// Idle (not in transaction block)
51    #[default]
52    Idle = b'I',
53    /// In transaction block
54    InTransaction = b'T',
55    /// In failed transaction block (queries will be rejected until rollback)
56    Failed = b'E',
57}
58
59impl TransactionStatus {
60    /// Create a TransactionStatus from a raw byte value.
61    pub fn from_byte(value: u8) -> Option<Self> {
62        match value {
63            b'I' => Some(TransactionStatus::Idle),
64            b'T' => Some(TransactionStatus::InTransaction),
65            b'E' => Some(TransactionStatus::Failed),
66            _ => None,
67        }
68    }
69
70    /// Returns true if currently in a transaction (either active or failed).
71    pub fn in_transaction(self) -> bool {
72        matches!(
73            self,
74            TransactionStatus::InTransaction | TransactionStatus::Failed
75        )
76    }
77
78    /// Returns true if the transaction has failed.
79    pub fn is_failed(self) -> bool {
80        matches!(self, TransactionStatus::Failed)
81    }
82}
83
84/// Well-known PostgreSQL type OIDs.
85pub mod oid {
86    use super::Oid;
87
88    /// boolean, format 't'/'f'
89    pub const BOOL: Oid = 16;
90    /// variable-length string, binary values escaped
91    pub const BYTEA: Oid = 17;
92    /// single character
93    pub const CHAR: Oid = 18;
94    /// 63-byte type for storing system identifiers
95    pub const NAME: Oid = 19;
96    /// ~18 digit integer, 8-byte storage
97    pub const INT8: Oid = 20;
98    /// -32 thousand to 32 thousand, 2-byte storage
99    pub const INT2: Oid = 21;
100    /// array of int2, used in system tables
101    pub const INT2VECTOR: Oid = 22;
102    /// -2 billion to 2 billion integer, 4-byte storage
103    pub const INT4: Oid = 23;
104    /// registered procedure
105    pub const REGPROC: Oid = 24;
106    /// variable-length string, no limit specified
107    pub const TEXT: Oid = 25;
108    /// object identifier(oid), maximum 4 billion
109    pub const OID: Oid = 26;
110    /// tuple physical location, format '(block,offset)'
111    pub const TID: Oid = 27;
112    /// transaction id
113    pub const XID: Oid = 28;
114    /// command identifier type, sequence in transaction id
115    pub const CID: Oid = 29;
116    /// array of oids, used in system tables
117    pub const OIDVECTOR: Oid = 30;
118    /// internal type for passing CollectedCommand
119    pub const PG_DDL_COMMAND: Oid = 32;
120    /// pg_type system catalog
121    pub const PG_TYPE: Oid = 71;
122    /// pg_attribute system catalog
123    pub const PG_ATTRIBUTE: Oid = 75;
124    /// pg_proc system catalog
125    pub const PG_PROC: Oid = 81;
126    /// pg_class system catalog
127    pub const PG_CLASS: Oid = 83;
128    /// JSON stored as text
129    pub const JSON: Oid = 114;
130    /// XML content
131    pub const XML: Oid = 142;
132    /// string representing an internal node tree
133    pub const PG_NODE_TREE: Oid = 194;
134    /// pseudo-type for the result of a table AM handler function
135    pub const TABLE_AM_HANDLER: Oid = 269;
136    /// pseudo-type for the result of an index AM handler function
137    pub const INDEX_AM_HANDLER: Oid = 325;
138    /// geometric point, format '(x,y)'
139    pub const POINT: Oid = 600;
140    /// geometric line segment, format '\[point1,point2\]'
141    pub const LSEG: Oid = 601;
142    /// geometric path, format '(point1,...)'
143    pub const PATH: Oid = 602;
144    /// geometric box, format 'lower left point,upper right point'
145    pub const BOX: Oid = 603;
146    /// geometric polygon, format '(point1,...)'
147    pub const POLYGON: Oid = 604;
148    /// geometric line, formats '{A,B,C}'/'\[point1,point2\]'
149    pub const LINE: Oid = 628;
150    /// network IP address/netmask, network address
151    pub const CIDR: Oid = 650;
152    /// single-precision floating point number, 4-byte storage
153    pub const FLOAT4: Oid = 700;
154    /// double-precision floating point number, 8-byte storage
155    pub const FLOAT8: Oid = 701;
156    /// pseudo-type representing an undetermined type
157    pub const UNKNOWN: Oid = 705;
158    /// geometric circle, format '<center point,radius>'
159    pub const CIRCLE: Oid = 718;
160    /// XX:XX:XX:XX:XX:XX:XX:XX, MAC address
161    pub const MACADDR8: Oid = 774;
162    /// monetary amounts, $d,ddd.cc
163    pub const MONEY: Oid = 790;
164    /// XX:XX:XX:XX:XX:XX, MAC address
165    pub const MACADDR: Oid = 829;
166    /// IP address/netmask, host address, netmask optional
167    pub const INET: Oid = 869;
168    /// access control list
169    pub const ACLITEM: Oid = 1033;
170    /// 'char(length)' blank-padded string, fixed storage length
171    pub const BPCHAR: Oid = 1042;
172    /// 'varchar(length)' non-blank-padded string, variable storage length
173    pub const VARCHAR: Oid = 1043;
174    /// date
175    pub const DATE: Oid = 1082;
176    /// time of day
177    pub const TIME: Oid = 1083;
178    /// date and time
179    pub const TIMESTAMP: Oid = 1114;
180    /// date and time with time zone
181    pub const TIMESTAMPTZ: Oid = 1184;
182    /// time interval, format 'number units ...'
183    pub const INTERVAL: Oid = 1186;
184    /// time of day with time zone
185    pub const TIMETZ: Oid = 1266;
186    /// fixed-length bit string
187    pub const BIT: Oid = 1560;
188    /// variable-length bit string
189    pub const VARBIT: Oid = 1562;
190    /// 'numeric(precision, scale)' arbitrary precision number
191    pub const NUMERIC: Oid = 1700;
192    /// reference to cursor (portal name)
193    pub const REFCURSOR: Oid = 1790;
194    /// registered procedure (with args)
195    pub const REGPROCEDURE: Oid = 2202;
196    /// registered operator
197    pub const REGOPER: Oid = 2203;
198    /// registered operator (with args)
199    pub const REGOPERATOR: Oid = 2204;
200    /// registered class
201    pub const REGCLASS: Oid = 2205;
202    /// registered type
203    pub const REGTYPE: Oid = 2206;
204    /// pseudo-type representing any composite type
205    pub const RECORD: Oid = 2249;
206    /// array of records
207    pub const RECORD_ARRAY: Oid = 2287;
208    /// C-style string
209    pub const CSTRING: Oid = 2275;
210    /// pseudo-type representing any type
211    pub const ANY: Oid = 2276;
212    /// pseudo-type representing a polymorphic array type
213    pub const ANYARRAY: Oid = 2277;
214    /// pseudo-type for the result of a function with no real result
215    pub const VOID: Oid = 2278;
216    /// pseudo-type for the result of a trigger function
217    pub const TRIGGER: Oid = 2279;
218    /// pseudo-type for the result of a language handler function
219    pub const LANGUAGE_HANDLER: Oid = 2280;
220    /// pseudo-type representing an internal data structure
221    pub const INTERNAL: Oid = 2281;
222    /// pseudo-type representing a polymorphic base type
223    pub const ANYELEMENT: Oid = 2283;
224    /// pseudo-type representing a polymorphic base type that is not an array
225    pub const ANYNONARRAY: Oid = 2776;
226    /// UUID
227    pub const UUID: Oid = 2950;
228    /// transaction snapshot
229    pub const TXID_SNAPSHOT: Oid = 2970;
230    /// pseudo-type for the result of an FDW handler function
231    pub const FDW_HANDLER: Oid = 3115;
232    /// PostgreSQL LSN
233    pub const PG_LSN: Oid = 3220;
234    /// pseudo-type for the result of a tablesample method function
235    pub const TSM_HANDLER: Oid = 3310;
236    /// multivariate ndistinct coefficients
237    pub const PG_NDISTINCT: Oid = 3361;
238    /// multivariate dependencies
239    pub const PG_DEPENDENCIES: Oid = 3402;
240    /// pseudo-type representing a polymorphic base type that is an enum
241    pub const ANYENUM: Oid = 3500;
242    /// text representation for text search
243    pub const TSVECTOR: Oid = 3614;
244    /// query representation for text search
245    pub const TSQUERY: Oid = 3615;
246    /// GiST index internal text representation for text search
247    pub const GTSVECTOR: Oid = 3642;
248    /// registered text search configuration
249    pub const REGCONFIG: Oid = 3734;
250    /// registered text search dictionary
251    pub const REGDICTIONARY: Oid = 3769;
252    /// Binary JSON
253    pub const JSONB: Oid = 3802;
254    /// pseudo-type representing a range over a polymorphic base type
255    pub const ANYRANGE: Oid = 3831;
256    /// pseudo-type for the result of an event trigger function
257    pub const EVENT_TRIGGER: Oid = 3838;
258    /// range of integers
259    pub const INT4RANGE: Oid = 3904;
260    /// range of numerics
261    pub const NUMRANGE: Oid = 3906;
262    /// range of timestamps without time zone
263    pub const TSRANGE: Oid = 3908;
264    /// range of timestamps with time zone
265    pub const TSTZRANGE: Oid = 3910;
266    /// range of dates
267    pub const DATERANGE: Oid = 3912;
268    /// range of bigints
269    pub const INT8RANGE: Oid = 3926;
270    /// JSON path
271    pub const JSONPATH: Oid = 4072;
272    /// registered namespace
273    pub const REGNAMESPACE: Oid = 4089;
274    /// registered role
275    pub const REGROLE: Oid = 4096;
276    /// registered collation
277    pub const REGCOLLATION: Oid = 4191;
278    /// multirange of integers
279    pub const INT4MULTIRANGE: Oid = 4451;
280    /// multirange of numerics
281    pub const NUMMULTIRANGE: Oid = 4532;
282    /// multirange of timestamps without time zone
283    pub const TSMULTIRANGE: Oid = 4533;
284    /// multirange of timestamps with time zone
285    pub const TSTZMULTIRANGE: Oid = 4534;
286    /// multirange of dates
287    pub const DATEMULTIRANGE: Oid = 4535;
288    /// multirange of bigints
289    pub const INT8MULTIRANGE: Oid = 4536;
290    /// pseudo-type representing a polymorphic base type that is a multirange
291    pub const ANYMULTIRANGE: Oid = 4537;
292    /// pseudo-type representing a multirange over a polymorphic common type
293    pub const ANYCOMPATIBLEMULTIRANGE: Oid = 4538;
294    /// pseudo-type representing BRIN bloom summary
295    pub const PG_BRIN_BLOOM_SUMMARY: Oid = 4600;
296    /// pseudo-type representing BRIN minmax-multi summary
297    pub const PG_BRIN_MINMAX_MULTI_SUMMARY: Oid = 4601;
298    /// multivariate MCV list
299    pub const PG_MCV_LIST: Oid = 5017;
300    /// transaction snapshot
301    pub const PG_SNAPSHOT: Oid = 5038;
302    /// full transaction id
303    pub const XID8: Oid = 5069;
304    /// pseudo-type representing a polymorphic common type
305    pub const ANYCOMPATIBLE: Oid = 5077;
306    /// pseudo-type representing an array of polymorphic common type elements
307    pub const ANYCOMPATIBLEARRAY: Oid = 5078;
308    /// pseudo-type representing a polymorphic common type that is not an array
309    pub const ANYCOMPATIBLENONARRAY: Oid = 5079;
310    /// pseudo-type representing a range over a polymorphic common type
311    pub const ANYCOMPATIBLERANGE: Oid = 5080;
312    /// registered database
313    pub const REGDATABASE: Oid = 8326;
314}