risingwave_common/types/
to_sql.rs

1// Copyright 2023 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::error::Error;
16
17use bytes::BytesMut;
18use postgres_types::{IsNull, ToSql, Type, to_sql_checked};
19use risingwave_common::types::ScalarRefImpl;
20
21use crate::types::ScalarImpl;
22
23impl ToSql for ScalarImpl {
24    to_sql_checked!();
25
26    fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>>
27    where
28        Self: Sized,
29    {
30        self.as_scalar_ref_impl().to_sql(ty, out)
31    }
32
33    // return true to accept all types
34    fn accepts(_ty: &Type) -> bool
35    where
36        Self: Sized,
37    {
38        true
39    }
40}
41
42impl ToSql for ScalarRefImpl<'_> {
43    to_sql_checked!();
44
45    fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>>
46    where
47        Self: Sized,
48    {
49        match self {
50            ScalarRefImpl::Int16(v) => v.to_sql(ty, out),
51            ScalarRefImpl::Int32(v) => v.to_sql(ty, out),
52            ScalarRefImpl::Int64(v) => v.to_sql(ty, out),
53            ScalarRefImpl::Serial(v) => v.to_sql(ty, out),
54            ScalarRefImpl::Float32(v) => v.to_sql(ty, out),
55            ScalarRefImpl::Float64(v) => v.to_sql(ty, out),
56            ScalarRefImpl::Utf8(v) => v.to_sql(ty, out),
57            ScalarRefImpl::Bool(v) => v.to_sql(ty, out),
58            ScalarRefImpl::Decimal(v) => v.to_sql(ty, out),
59            ScalarRefImpl::Interval(v) => v.to_sql(ty, out),
60            ScalarRefImpl::Date(v) => v.to_sql(ty, out),
61            ScalarRefImpl::Timestamp(v) => v.to_sql(ty, out),
62            ScalarRefImpl::Timestamptz(v) => v.to_sql(ty, out),
63            ScalarRefImpl::Time(v) => v.to_sql(ty, out),
64            ScalarRefImpl::Bytea(v) => (&**v).to_sql(ty, out),
65            ScalarRefImpl::Jsonb(v) => v.to_sql(ty, out),
66            ScalarRefImpl::Vector(_)
67            | ScalarRefImpl::Int256(_)
68            | ScalarRefImpl::Struct(_)
69            | ScalarRefImpl::List(_) => {
70                bail_not_implemented!("the postgres encoding for {ty} is unsupported")
71            }
72            ScalarRefImpl::Map(_) => todo!(),
73        }
74    }
75
76    // return true to accept all types
77    fn accepts(_ty: &Type) -> bool
78    where
79        Self: Sized,
80    {
81        true
82    }
83}