Skip to main content

sea_query_postgres/
lib.rs

1#![forbid(unsafe_code)]
2
3use std::error::Error;
4
5use bytes::BytesMut;
6use postgres_types::{IsNull, ToSql, Type, to_sql_checked};
7
8use sea_query::{ArrayType, OptionEnum, QueryBuilder, Value, query::*};
9
10#[derive(Clone, Debug, PartialEq)]
11pub struct PostgresValue(pub Value);
12#[derive(Clone, Debug, PartialEq)]
13pub struct PostgresValues(pub Vec<PostgresValue>);
14
15impl PostgresValues {
16    pub fn as_params(&self) -> Vec<&(dyn ToSql + Sync)> {
17        self.0
18            .iter()
19            .map(|x| {
20                let y: &(dyn ToSql + Sync) = x;
21                y
22            })
23            .collect()
24    }
25
26    pub fn as_types(&self) -> Vec<Type> {
27        self.0
28            .iter()
29            .map(|x| &x.0)
30            .map(value_to_postgres_type)
31            .collect()
32    }
33}
34
35pub trait PostgresBinder {
36    fn build_postgres<T: QueryBuilder>(&self, query_builder: T) -> (String, PostgresValues);
37}
38
39macro_rules! impl_postgres_binder {
40    ($l:ident) => {
41        impl PostgresBinder for $l {
42            fn build_postgres<T: QueryBuilder>(
43                &self,
44                query_builder: T,
45            ) -> (String, PostgresValues) {
46                let (query, values) = self.build(query_builder);
47                (
48                    query,
49                    PostgresValues(values.into_iter().map(PostgresValue).collect()),
50                )
51            }
52        }
53    };
54}
55
56impl_postgres_binder!(SelectStatement);
57impl_postgres_binder!(UpdateStatement);
58impl_postgres_binder!(InsertStatement);
59impl_postgres_binder!(DeleteStatement);
60impl_postgres_binder!(WithQuery);
61
62impl ToSql for PostgresValue {
63    fn to_sql(
64        &self,
65        ty: &Type,
66        out: &mut BytesMut,
67    ) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
68        macro_rules! to_sql {
69            ( $v: expr, $ty: ty ) => {
70                $v.map(|v| v as $ty).as_ref().to_sql(ty, out)
71            };
72        }
73        match &self.0 {
74            Value::Bool(v) => to_sql!(v, bool),
75            Value::TinyInt(v) => to_sql!(v, i8),
76            Value::SmallInt(v) => to_sql!(v, i16),
77            Value::Int(v) => to_sql!(v, i32),
78            Value::BigInt(v) => to_sql!(v, i64),
79            Value::TinyUnsigned(v) => to_sql!(v, u32),
80            Value::SmallUnsigned(v) => to_sql!(v, u32),
81            Value::Unsigned(v) => to_sql!(v, u32),
82            Value::BigUnsigned(v) => to_sql!(v, i64),
83            Value::Float(v) => to_sql!(v, f32),
84            Value::Double(v) => to_sql!(v, f64),
85            Value::String(v) => v.as_deref().to_sql(ty, out),
86            Value::Enum(v) => match v {
87                OptionEnum::Some(v) => Some(v.value.as_ref()).to_sql(ty, out),
88                OptionEnum::None(_) => Option::<&str>::None.to_sql(ty, out),
89            },
90            Value::Char(v) => v.map(|v| v.to_string()).to_sql(ty, out),
91            Value::Bytes(v) => v.as_deref().to_sql(ty, out),
92            #[cfg(feature = "with-json")]
93            Value::Json(v) => v.as_deref().to_sql(ty, out),
94            #[cfg(feature = "with-chrono")]
95            Value::ChronoDate(v) => v.to_sql(ty, out),
96            #[cfg(feature = "with-chrono")]
97            Value::ChronoTime(v) => v.to_sql(ty, out),
98            #[cfg(feature = "with-chrono")]
99            Value::ChronoDateTime(v) => v.to_sql(ty, out),
100            #[cfg(feature = "with-chrono")]
101            Value::ChronoDateTimeUtc(v) => v.to_sql(ty, out),
102            #[cfg(feature = "with-chrono")]
103            Value::ChronoDateTimeLocal(v) => v.to_sql(ty, out),
104            #[cfg(feature = "with-chrono")]
105            Value::ChronoDateTimeWithTimeZone(v) => v.to_sql(ty, out),
106            #[cfg(feature = "with-time")]
107            Value::TimeDate(v) => v.to_sql(ty, out),
108            #[cfg(feature = "with-time")]
109            Value::TimeTime(v) => v.to_sql(ty, out),
110            #[cfg(feature = "with-time")]
111            Value::TimeDateTime(v) => v.to_sql(ty, out),
112            #[cfg(feature = "with-time")]
113            Value::TimeDateTimeWithTimeZone(v) => v.to_sql(ty, out),
114            #[cfg(feature = "with-rust_decimal")]
115            Value::Decimal(v) => v.to_sql(ty, out),
116            #[cfg(feature = "with-bigdecimal")]
117            Value::BigDecimal(v) => {
118                use bigdecimal::ToPrimitive;
119                v.as_deref()
120                    .map(|v| v.to_f64().expect("Fail to convert bigdecimal as f64"))
121                    .to_sql(ty, out)
122            }
123            #[cfg(feature = "with-uuid")]
124            Value::Uuid(v) => v.to_sql(ty, out),
125            #[cfg(feature = "postgres-array")]
126            Value::Array(_, Some(v)) => v
127                .iter()
128                .map(|v| PostgresValue(v.clone()))
129                .collect::<Vec<PostgresValue>>()
130                .to_sql(ty, out),
131            #[cfg(feature = "postgres-array")]
132            Value::Array(_, None) => Ok(IsNull::Yes),
133            #[cfg(feature = "postgres-vector")]
134            Value::Vector(Some(v)) => v.to_sql(ty, out),
135            #[cfg(feature = "postgres-vector")]
136            Value::Vector(None) => Ok(IsNull::Yes),
137            #[cfg(feature = "with-ipnetwork")]
138            Value::IpNetwork(v) => {
139                use cidr::IpCidr;
140                v.map(|v| {
141                    IpCidr::new(v.network(), v.prefix())
142                        .expect("Fail to convert IpNetwork to IpCidr")
143                })
144                .to_sql(ty, out)
145            }
146            #[cfg(feature = "with-mac_address")]
147            Value::MacAddress(v) => {
148                use eui48::MacAddress;
149                v.map(|v| MacAddress::new(v.bytes())).to_sql(ty, out)
150            }
151            #[cfg(feature = "postgres-range")]
152            Value::Range(None) => Ok(IsNull::Yes),
153            #[cfg(feature = "postgres-range")]
154            Value::Range(Some(v)) => v.to_sql(ty, out),
155        }
156    }
157
158    fn accepts(_ty: &Type) -> bool {
159        true
160    }
161
162    to_sql_checked!();
163}
164
165fn value_to_postgres_type(value: &Value) -> Type {
166    match value {
167        Value::Bool(_) => Type::BOOL,
168        Value::TinyInt(_) => Type::INT2,
169        Value::TinyUnsigned(_) => Type::INT2,
170        Value::SmallInt(_) => Type::INT2,
171        Value::SmallUnsigned(_) => Type::INT4,
172        Value::Int(_) => Type::INT4,
173        Value::BigInt(_) => Type::INT8,
174        Value::Unsigned(_) => Type::INT8,
175        Value::BigUnsigned(_) => Type::NUMERIC,
176        Value::Float(_) => Type::FLOAT4,
177        Value::Double(_) => Type::FLOAT8,
178        Value::String(_) => Type::TEXT,
179        Value::Enum(_) => Type::TEXT,
180        #[cfg(feature = "postgres-range")]
181        Value::Range(_) => Type::INT8_RANGE,
182        Value::Char(_) => Type::CHAR,
183        Value::Bytes(_) => Type::BYTEA,
184        #[cfg(feature = "with-json")]
185        Value::Json(_) => Type::JSON,
186        #[cfg(feature = "with-chrono")]
187        Value::ChronoDate(_) => Type::DATE,
188        #[cfg(feature = "with-chrono")]
189        Value::ChronoTime(_) => Type::TIME,
190        #[cfg(feature = "with-chrono")]
191        Value::ChronoDateTime(_) => Type::TIMESTAMP,
192        #[cfg(feature = "with-chrono")]
193        Value::ChronoDateTimeUtc(_) => Type::TIMESTAMP,
194        #[cfg(feature = "with-chrono")]
195        Value::ChronoDateTimeLocal(_) => Type::TIMESTAMP,
196        #[cfg(feature = "with-chrono")]
197        Value::ChronoDateTimeWithTimeZone(_) => Type::TIMESTAMPTZ,
198        #[cfg(feature = "with-time")]
199        Value::TimeDate(_) => Type::DATE,
200        #[cfg(feature = "with-time")]
201        Value::TimeTime(_) => Type::TIME,
202        #[cfg(feature = "with-time")]
203        Value::TimeDateTime(_) => Type::TIMESTAMP,
204        #[cfg(feature = "with-time")]
205        Value::TimeDateTimeWithTimeZone(_) => Type::TIMESTAMPTZ,
206        #[cfg(feature = "with-uuid")]
207        Value::Uuid(_) => Type::UUID,
208        #[cfg(feature = "with-rust_decimal")]
209        Value::Decimal(_) => Type::NUMERIC,
210        #[cfg(feature = "with-bigdecimal")]
211        Value::BigDecimal(_) => Type::NUMERIC,
212        #[cfg(feature = "postgres-array")]
213        Value::Array(ty, _) => array_type_to_pg_type(ty),
214        #[cfg(feature = "postgres-vector")]
215        Value::Vector(_) => Type::FLOAT4_ARRAY,
216        #[cfg(feature = "with-ipnetwork")]
217        Value::IpNetwork(_) => Type::INET,
218        #[cfg(feature = "with-mac_address")]
219        Value::MacAddress(_) => Type::MACADDR,
220    }
221}
222
223fn array_type_to_pg_type(ty: &ArrayType) -> Type {
224    match ty {
225        ArrayType::Bool => Type::BOOL_ARRAY,
226        ArrayType::TinyInt => Type::INT2_ARRAY,
227        ArrayType::TinyUnsigned => Type::INT2_ARRAY,
228        ArrayType::SmallInt => Type::INT2_ARRAY,
229        ArrayType::SmallUnsigned => Type::INT4_ARRAY,
230        ArrayType::Int => Type::INT4_ARRAY,
231        ArrayType::Unsigned => Type::INT8_ARRAY,
232        ArrayType::BigInt => Type::INT8_ARRAY,
233        ArrayType::BigUnsigned => Type::NUMERIC_ARRAY,
234        ArrayType::Float => Type::FLOAT4_ARRAY,
235        ArrayType::Double => Type::FLOAT8_ARRAY,
236        ArrayType::String => Type::TEXT_ARRAY,
237        ArrayType::Char => Type::CHAR_ARRAY,
238        ArrayType::Bytes => Type::BYTEA_ARRAY,
239        #[cfg(feature = "with-json")]
240        ArrayType::Json => Type::JSON_ARRAY,
241        #[cfg(feature = "with-chrono")]
242        ArrayType::ChronoDate => Type::DATE_ARRAY,
243        #[cfg(feature = "with-chrono")]
244        ArrayType::ChronoTime => Type::TIME_ARRAY,
245        #[cfg(feature = "with-chrono")]
246        ArrayType::ChronoDateTime => Type::TIMESTAMP_ARRAY,
247        #[cfg(feature = "with-chrono")]
248        ArrayType::ChronoDateTimeUtc => Type::TIMESTAMP_ARRAY,
249        #[cfg(feature = "with-chrono")]
250        ArrayType::ChronoDateTimeLocal => Type::TIMESTAMP_ARRAY,
251        #[cfg(feature = "with-chrono")]
252        ArrayType::ChronoDateTimeWithTimeZone => Type::TIMESTAMPTZ_ARRAY,
253        #[cfg(feature = "with-time")]
254        ArrayType::TimeDate => Type::DATE_ARRAY,
255        #[cfg(feature = "with-time")]
256        ArrayType::TimeTime => Type::TIME_ARRAY,
257        #[cfg(feature = "with-time")]
258        ArrayType::TimeDateTime => Type::TIMESTAMP_ARRAY,
259        #[cfg(feature = "with-time")]
260        ArrayType::TimeDateTimeWithTimeZone => Type::TIMESTAMPTZ_ARRAY,
261        #[cfg(feature = "with-uuid")]
262        ArrayType::Uuid => Type::UUID_ARRAY,
263        #[cfg(feature = "with-rust_decimal")]
264        ArrayType::Decimal => Type::NUMERIC_ARRAY,
265        #[cfg(feature = "with-bigdecimal")]
266        ArrayType::BigDecimal => Type::NUMERIC_ARRAY,
267        #[cfg(feature = "with-ipnetwork")]
268        ArrayType::IpNetwork => Type::INET_ARRAY,
269        #[cfg(feature = "with-mac_address")]
270        ArrayType::MacAddress => Type::MACADDR_ARRAY,
271        ArrayType::Enum(_) => Type::TEXT_ARRAY,
272        #[cfg(feature = "postgres-range")]
273        ArrayType::Range => Type::INT8_RANGE_ARRAY,
274    }
275}