sqlx_postgres/types/
int.rs

1use byteorder::{BigEndian, ByteOrder};
2
3use crate::decode::Decode;
4use crate::encode::{Encode, IsNull};
5use crate::error::BoxDynError;
6use crate::types::Type;
7use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};
8
9impl Type<Postgres> for i8 {
10    fn type_info() -> PgTypeInfo {
11        PgTypeInfo::CHAR
12    }
13}
14
15impl PgHasArrayType for i8 {
16    fn array_type_info() -> PgTypeInfo {
17        PgTypeInfo::CHAR_ARRAY
18    }
19}
20
21impl Encode<'_, Postgres> for i8 {
22    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
23        buf.extend(&self.to_be_bytes());
24
25        IsNull::No
26    }
27}
28
29impl Decode<'_, Postgres> for i8 {
30    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
31        // note: in the TEXT encoding, a value of "0" here is encoded as an empty string
32        Ok(value.as_bytes()?.get(0).copied().unwrap_or_default() as i8)
33    }
34}
35
36impl Type<Postgres> for i16 {
37    fn type_info() -> PgTypeInfo {
38        PgTypeInfo::INT2
39    }
40}
41
42impl PgHasArrayType for i16 {
43    fn array_type_info() -> PgTypeInfo {
44        PgTypeInfo::INT2_ARRAY
45    }
46}
47
48impl Encode<'_, Postgres> for i16 {
49    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
50        buf.extend(&self.to_be_bytes());
51
52        IsNull::No
53    }
54}
55
56impl Decode<'_, Postgres> for i16 {
57    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
58        Ok(match value.format() {
59            PgValueFormat::Binary => BigEndian::read_i16(value.as_bytes()?),
60            PgValueFormat::Text => value.as_str()?.parse()?,
61        })
62    }
63}
64
65impl Type<Postgres> for i32 {
66    fn type_info() -> PgTypeInfo {
67        PgTypeInfo::INT4
68    }
69}
70
71impl PgHasArrayType for i32 {
72    fn array_type_info() -> PgTypeInfo {
73        PgTypeInfo::INT4_ARRAY
74    }
75}
76
77impl Encode<'_, Postgres> for i32 {
78    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
79        buf.extend(&self.to_be_bytes());
80
81        IsNull::No
82    }
83}
84
85impl Decode<'_, Postgres> for i32 {
86    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
87        Ok(match value.format() {
88            PgValueFormat::Binary => BigEndian::read_i32(value.as_bytes()?),
89            PgValueFormat::Text => value.as_str()?.parse()?,
90        })
91    }
92}
93
94impl Type<Postgres> for i64 {
95    fn type_info() -> PgTypeInfo {
96        PgTypeInfo::INT8
97    }
98}
99
100impl PgHasArrayType for i64 {
101    fn array_type_info() -> PgTypeInfo {
102        PgTypeInfo::INT8_ARRAY
103    }
104}
105
106impl Encode<'_, Postgres> for i64 {
107    fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
108        buf.extend(&self.to_be_bytes());
109
110        IsNull::No
111    }
112}
113
114impl Decode<'_, Postgres> for i64 {
115    fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
116        Ok(match value.format() {
117            PgValueFormat::Binary => BigEndian::read_i64(value.as_bytes()?),
118            PgValueFormat::Text => value.as_str()?.parse()?,
119        })
120    }
121}