Skip to main content

diesel/pg/query_builder/copy/
copy_from.rs

1use alloc::borrow::Cow;
2use core::marker::PhantomData;
3
4use byteorder::NetworkEndian;
5use byteorder::WriteBytesExt;
6
7use super::CommonOptions;
8use super::CopyFormat;
9use super::CopyTarget;
10use crate::Connection;
11use crate::Insertable;
12use crate::QueryResult;
13use crate::expression::bound::Bound;
14use crate::insertable::ColumnInsertValue;
15use crate::pg::Pg;
16use crate::pg::PgMetadataLookup;
17use crate::pg::backend::FailedToLookupTypeError;
18use crate::pg::metadata_lookup::PgMetadataCacheKey;
19use crate::query_builder::BatchInsert;
20use crate::query_builder::QueryFragment;
21use crate::query_builder::QueryId;
22use crate::query_builder::ValuesClause;
23use crate::serialize::IsNull;
24use crate::serialize::ToSql;
25use crate::{Column, Table};
26
27/// Describes the different possible settings for the `HEADER` option
28/// for `COPY FROM` statements
29#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CopyHeader {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            CopyHeader::Set(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Set",
                    &__self_0),
            CopyHeader::Match =>
                ::core::fmt::Formatter::write_str(f, "Match"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::marker::Copy for CopyHeader { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CopyHeader {
    #[inline]
    fn clone(&self) -> CopyHeader {
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone)]
30pub enum CopyHeader {
31    /// Is the header set?
32    Set(bool),
33    /// Match the header with the targeted table names
34    /// and fail in the case of a mismatch
35    Match,
36}
37
38#[derive(#[automatically_derived]
impl ::core::fmt::Debug for CopyFromOptions {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f,
            "CopyFromOptions", "common", &self.common, "default",
            &self.default, "header", &&self.header)
    }
}Debug, #[automatically_derived]
impl ::core::default::Default for CopyFromOptions {
    #[inline]
    fn default() -> CopyFromOptions {
        CopyFromOptions {
            common: ::core::default::Default::default(),
            default: ::core::default::Default::default(),
            header: ::core::default::Default::default(),
        }
    }
}Default)]
39pub struct CopyFromOptions {
40    common: CommonOptions,
41    default: Option<String>,
42    header: Option<CopyHeader>,
43}
44
45impl QueryFragment<Pg> for CopyFromOptions {
46    fn walk_ast<'b>(
47        &'b self,
48        mut pass: crate::query_builder::AstPass<'_, 'b, Pg>,
49    ) -> crate::QueryResult<()> {
50        if self.any_set() {
51            let mut comma = "";
52            pass.push_sql(" WITH (");
53            self.common.walk_ast(pass.reborrow(), &mut comma);
54            if let Some(ref default) = self.default {
55                pass.push_sql(comma);
56                comma = ", ";
57                pass.push_sql("DEFAULT '");
58                // we cannot use binds here
59                // so we need to make sure quotes in
60                // the input are handled correctly
61                let default = default.replace('\'', "''");
62                pass.push_sql(&default);
63                pass.push_sql("'");
64            }
65            if let Some(ref header) = self.header {
66                pass.push_sql(comma);
67                // commented out because rustc complains otherwise
68                //comma = ", ";
69                pass.push_sql("HEADER ");
70                match header {
71                    CopyHeader::Set(true) => pass.push_sql("1"),
72                    CopyHeader::Set(false) => pass.push_sql("0"),
73                    CopyHeader::Match => pass.push_sql("MATCH"),
74                }
75            }
76
77            pass.push_sql(")");
78        }
79        Ok(())
80    }
81}
82
83impl CopyFromOptions {
84    fn any_set(&self) -> bool {
85        self.common.any_set() || self.default.is_some() || self.header.is_some()
86    }
87}
88
89#[derive(#[automatically_derived]
impl<S: ::core::fmt::Debug, F: ::core::fmt::Debug> ::core::fmt::Debug for
    CopyFrom<S, F> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "CopyFrom",
            "options", &self.options, "copy_callback", &self.copy_callback,
            "p", &&self.p)
    }
}Debug)]
90pub struct CopyFrom<S, F> {
91    options: CopyFromOptions,
92    copy_callback: F,
93    p: PhantomData<S>,
94}
95
96pub(crate) struct InternalCopyFromQuery<S, T> {
97    pub(crate) target: S,
98    p: PhantomData<T>,
99}
100
101#[cfg(feature = "postgres")]
102impl<S, T> InternalCopyFromQuery<S, T> {
103    pub(crate) fn new(target: S) -> Self {
104        Self {
105            target,
106            p: PhantomData,
107        }
108    }
109}
110
111impl<S, T> QueryId for InternalCopyFromQuery<S, T>
112where
113    S: CopyFromExpression<T>,
114{
115    const HAS_STATIC_QUERY_ID: bool = false;
116    type QueryId = ();
117}
118
119impl<S, T> QueryFragment<Pg> for InternalCopyFromQuery<S, T>
120where
121    S: CopyFromExpression<T>,
122{
123    fn walk_ast<'b>(
124        &'b self,
125        mut pass: crate::query_builder::AstPass<'_, 'b, Pg>,
126    ) -> crate::QueryResult<()> {
127        pass.unsafe_to_cache_prepared();
128        pass.push_sql("COPY ");
129        self.target.walk_target(pass.reborrow())?;
130        pass.push_sql(" FROM STDIN");
131        self.target.options().walk_ast(pass.reborrow())?;
132        Ok(())
133    }
134}
135
136pub trait CopyFromExpression<T> {
137    type Error: From<crate::result::Error> + core::error::Error;
138
139    fn callback(&mut self, copy: &mut impl std::io::Write) -> Result<(), Self::Error>;
140
141    fn walk_target<'b>(
142        &'b self,
143        pass: crate::query_builder::AstPass<'_, 'b, Pg>,
144    ) -> crate::QueryResult<()>;
145
146    fn options(&self) -> &CopyFromOptions;
147}
148
149impl<S, F, E> CopyFromExpression<S::Table> for CopyFrom<S, F>
150where
151    E: From<crate::result::Error> + core::error::Error,
152    S: CopyTarget,
153    F: Fn(&mut dyn std::io::Write) -> Result<(), E>,
154{
155    type Error = E;
156
157    fn callback(&mut self, copy: &mut impl std::io::Write) -> Result<(), Self::Error> {
158        (self.copy_callback)(copy)
159    }
160
161    fn options(&self) -> &CopyFromOptions {
162        &self.options
163    }
164
165    fn walk_target<'b>(
166        &'b self,
167        pass: crate::query_builder::AstPass<'_, 'b, Pg>,
168    ) -> crate::QueryResult<()> {
169        S::walk_target(pass)
170    }
171}
172
173struct Dummy;
174
175impl PgMetadataLookup for Dummy {
176    fn lookup_type(&mut self, type_name: &str, schema: Option<&str>) -> crate::pg::PgTypeMetadata {
177        let cache_key = PgMetadataCacheKey::new(
178            schema.map(Into::into).map(Cow::Owned),
179            Cow::Owned(type_name.into()),
180        );
181        crate::pg::PgTypeMetadata(Err(FailedToLookupTypeError::new_internal(cache_key)))
182    }
183}
184
185trait CopyFromInsertableHelper {
186    type Target: CopyTarget;
187    const COLUMN_COUNT: u16;
188
189    fn write_to_buffer(&self, idx: u16, out: &mut Vec<u8>) -> QueryResult<IsNull>;
190}
191
192macro_rules! impl_copy_from_insertable_helper_for_values_clause {
193    ($(
194        $Tuple:tt {
195            $(($idx:tt) -> $T:ident, $ST:ident, $TT:ident,)+
196        }
197    )+) => {
198        $(
199            impl<__T, $($ST,)* $($T,)* $($TT,)*> CopyFromInsertableHelper for ValuesClause<
200                ($(ColumnInsertValue<$ST, Bound<$T, $TT>>,)*),
201            __T>
202                where
203                __T: Table,
204                $($ST: Column<Table = __T>,)*
205                ($($ST,)*): CopyTarget,
206                $($TT: ToSql<$T, Pg>,)*
207            {
208                type Target = ($($ST,)*);
209
210                const COLUMN_COUNT: u16 = $Tuple;
211
212                fn write_to_buffer(&self, idx: u16, out: &mut Vec<u8>) -> QueryResult<IsNull> {
213                    use crate::query_builder::ByteWrapper;
214                    use crate::serialize::Output;
215
216                    let values = &self.values;
217                    match idx {
218                        $($idx =>{
219                            let item = &values.$idx.expr.item;
220                            let is_null = ToSql::<$T, Pg>::to_sql(
221                                item,
222                                &mut Output::new( ByteWrapper(out), &mut Dummy as _)
223                            ).map_err(crate::result::Error::SerializationError)?;
224                            return Ok(is_null);
225                        })*
226                        _ => unreachable!(),
227                    }
228                }
229            }
230
231            impl<'a, __T, $($ST,)* $($T,)* $($TT,)*> CopyFromInsertableHelper for ValuesClause<
232                ($(ColumnInsertValue<$ST, &'a Bound<$T, $TT>>,)*),
233            __T>
234                where
235                __T: Table,
236                $($ST: Column<Table = __T>,)*
237                ($($ST,)*): CopyTarget,
238                $($TT: ToSql<$T, Pg>,)*
239            {
240                type Target = ($($ST,)*);
241
242                // statically known to always fit
243                // as we don't support more than 128 columns
244                const COLUMN_COUNT: u16 = $Tuple;
245
246                fn write_to_buffer(&self, idx: u16, out: &mut Vec<u8>) -> QueryResult<IsNull> {
247                    use crate::query_builder::ByteWrapper;
248                    use crate::serialize::Output;
249
250                    let values = &self.values;
251                    match idx {
252                        $($idx =>{
253                            let item = &values.$idx.expr.item;
254                            let is_null = ToSql::<$T, Pg>::to_sql(
255                                item,
256                                &mut Output::new( ByteWrapper(out), &mut Dummy as _)
257                            ).map_err(crate::result::Error::SerializationError)?;
258                            return Ok(is_null);
259                        })*
260                        _ => unreachable!(),
261                    }
262                }
263            }
264        )*
265    }
266}
267
268impl<__T, ST, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11, ST12,
    ST13, ST14, ST15, ST16, ST17, ST18, ST19, ST20, ST21, ST22, ST23, ST24,
    ST25, ST26, ST27, ST28, ST29, ST30, ST31, T, T1, T2, T3, T4, T5, T6, T7,
    T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22,
    T23, T24, T25, T26, T27, T28, T29, T30, T31, TT, TT1, TT2, TT3, TT4, TT5,
    TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16, TT17, TT18,
    TT19, TT20, TT21, TT22, TT23, TT24, TT25, TT26, TT27, TT28, TT29, TT30,
    TT31> CopyFromInsertableHelper for
    ValuesClause<(ColumnInsertValue<ST, Bound<T, TT>>,
    ColumnInsertValue<ST1, Bound<T1, TT1>>,
    ColumnInsertValue<ST2, Bound<T2, TT2>>,
    ColumnInsertValue<ST3, Bound<T3, TT3>>,
    ColumnInsertValue<ST4, Bound<T4, TT4>>,
    ColumnInsertValue<ST5, Bound<T5, TT5>>,
    ColumnInsertValue<ST6, Bound<T6, TT6>>,
    ColumnInsertValue<ST7, Bound<T7, TT7>>,
    ColumnInsertValue<ST8, Bound<T8, TT8>>,
    ColumnInsertValue<ST9, Bound<T9, TT9>>,
    ColumnInsertValue<ST10, Bound<T10, TT10>>,
    ColumnInsertValue<ST11, Bound<T11, TT11>>,
    ColumnInsertValue<ST12, Bound<T12, TT12>>,
    ColumnInsertValue<ST13, Bound<T13, TT13>>,
    ColumnInsertValue<ST14, Bound<T14, TT14>>,
    ColumnInsertValue<ST15, Bound<T15, TT15>>,
    ColumnInsertValue<ST16, Bound<T16, TT16>>,
    ColumnInsertValue<ST17, Bound<T17, TT17>>,
    ColumnInsertValue<ST18, Bound<T18, TT18>>,
    ColumnInsertValue<ST19, Bound<T19, TT19>>,
    ColumnInsertValue<ST20, Bound<T20, TT20>>,
    ColumnInsertValue<ST21, Bound<T21, TT21>>,
    ColumnInsertValue<ST22, Bound<T22, TT22>>,
    ColumnInsertValue<ST23, Bound<T23, TT23>>,
    ColumnInsertValue<ST24, Bound<T24, TT24>>,
    ColumnInsertValue<ST25, Bound<T25, TT25>>,
    ColumnInsertValue<ST26, Bound<T26, TT26>>,
    ColumnInsertValue<ST27, Bound<T27, TT27>>,
    ColumnInsertValue<ST28, Bound<T28, TT28>>,
    ColumnInsertValue<ST29, Bound<T29, TT29>>,
    ColumnInsertValue<ST30, Bound<T30, TT30>>,
    ColumnInsertValue<ST31, Bound<T31, TT31>>), __T> where __T: Table,
    ST: Column<Table = __T>, ST1: Column<Table = __T>,
    ST2: Column<Table = __T>, ST3: Column<Table = __T>,
    ST4: Column<Table = __T>, ST5: Column<Table = __T>,
    ST6: Column<Table = __T>, ST7: Column<Table = __T>,
    ST8: Column<Table = __T>, ST9: Column<Table = __T>,
    ST10: Column<Table = __T>, ST11: Column<Table = __T>,
    ST12: Column<Table = __T>, ST13: Column<Table = __T>,
    ST14: Column<Table = __T>, ST15: Column<Table = __T>,
    ST16: Column<Table = __T>, ST17: Column<Table = __T>,
    ST18: Column<Table = __T>, ST19: Column<Table = __T>,
    ST20: Column<Table = __T>, ST21: Column<Table = __T>,
    ST22: Column<Table = __T>, ST23: Column<Table = __T>,
    ST24: Column<Table = __T>, ST25: Column<Table = __T>,
    ST26: Column<Table = __T>, ST27: Column<Table = __T>,
    ST28: Column<Table = __T>, ST29: Column<Table = __T>,
    ST30: Column<Table = __T>, ST31: Column<Table = __T>,
    (ST, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11, ST12, ST13,
    ST14, ST15, ST16, ST17, ST18, ST19, ST20, ST21, ST22, ST23, ST24, ST25,
    ST26, ST27, ST28, ST29, ST30, ST31): CopyTarget, TT: ToSql<T, Pg>,
    TT1: ToSql<T1, Pg>, TT2: ToSql<T2, Pg>, TT3: ToSql<T3, Pg>,
    TT4: ToSql<T4, Pg>, TT5: ToSql<T5, Pg>, TT6: ToSql<T6, Pg>,
    TT7: ToSql<T7, Pg>, TT8: ToSql<T8, Pg>, TT9: ToSql<T9, Pg>,
    TT10: ToSql<T10, Pg>, TT11: ToSql<T11, Pg>, TT12: ToSql<T12, Pg>,
    TT13: ToSql<T13, Pg>, TT14: ToSql<T14, Pg>, TT15: ToSql<T15, Pg>,
    TT16: ToSql<T16, Pg>, TT17: ToSql<T17, Pg>, TT18: ToSql<T18, Pg>,
    TT19: ToSql<T19, Pg>, TT20: ToSql<T20, Pg>, TT21: ToSql<T21, Pg>,
    TT22: ToSql<T22, Pg>, TT23: ToSql<T23, Pg>, TT24: ToSql<T24, Pg>,
    TT25: ToSql<T25, Pg>, TT26: ToSql<T26, Pg>, TT27: ToSql<T27, Pg>,
    TT28: ToSql<T28, Pg>, TT29: ToSql<T29, Pg>, TT30: ToSql<T30, Pg>,
    TT31: ToSql<T31, Pg> {
    type Target =
        (ST, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11, ST12,
        ST13, ST14, ST15, ST16, ST17, ST18, ST19, ST20, ST21, ST22, ST23,
        ST24, ST25, ST26, ST27, ST28, ST29, ST30, ST31);
    const COLUMN_COUNT: u16 = 32u16;
    fn write_to_buffer(&self, idx: u16, out: &mut Vec<u8>)
        -> QueryResult<IsNull> {
        use crate::query_builder::ByteWrapper;
        use crate::serialize::Output;
        let values = &self.values;
        match idx {
            0 => {
                let item = &values.0.expr.item;
                let is_null =
                    ToSql::<T,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            1 => {
                let item = &values.1.expr.item;
                let is_null =
                    ToSql::<T1,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            2 => {
                let item = &values.2.expr.item;
                let is_null =
                    ToSql::<T2,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            3 => {
                let item = &values.3.expr.item;
                let is_null =
                    ToSql::<T3,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            4 => {
                let item = &values.4.expr.item;
                let is_null =
                    ToSql::<T4,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            5 => {
                let item = &values.5.expr.item;
                let is_null =
                    ToSql::<T5,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            6 => {
                let item = &values.6.expr.item;
                let is_null =
                    ToSql::<T6,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            7 => {
                let item = &values.7.expr.item;
                let is_null =
                    ToSql::<T7,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            8 => {
                let item = &values.8.expr.item;
                let is_null =
                    ToSql::<T8,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            9 => {
                let item = &values.9.expr.item;
                let is_null =
                    ToSql::<T9,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            10 => {
                let item = &values.10.expr.item;
                let is_null =
                    ToSql::<T10,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            11 => {
                let item = &values.11.expr.item;
                let is_null =
                    ToSql::<T11,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            12 => {
                let item = &values.12.expr.item;
                let is_null =
                    ToSql::<T12,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            13 => {
                let item = &values.13.expr.item;
                let is_null =
                    ToSql::<T13,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            14 => {
                let item = &values.14.expr.item;
                let is_null =
                    ToSql::<T14,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            15 => {
                let item = &values.15.expr.item;
                let is_null =
                    ToSql::<T15,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            16 => {
                let item = &values.16.expr.item;
                let is_null =
                    ToSql::<T16,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            17 => {
                let item = &values.17.expr.item;
                let is_null =
                    ToSql::<T17,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            18 => {
                let item = &values.18.expr.item;
                let is_null =
                    ToSql::<T18,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            19 => {
                let item = &values.19.expr.item;
                let is_null =
                    ToSql::<T19,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            20 => {
                let item = &values.20.expr.item;
                let is_null =
                    ToSql::<T20,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            21 => {
                let item = &values.21.expr.item;
                let is_null =
                    ToSql::<T21,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            22 => {
                let item = &values.22.expr.item;
                let is_null =
                    ToSql::<T22,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            23 => {
                let item = &values.23.expr.item;
                let is_null =
                    ToSql::<T23,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            24 => {
                let item = &values.24.expr.item;
                let is_null =
                    ToSql::<T24,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            25 => {
                let item = &values.25.expr.item;
                let is_null =
                    ToSql::<T25,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            26 => {
                let item = &values.26.expr.item;
                let is_null =
                    ToSql::<T26,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            27 => {
                let item = &values.27.expr.item;
                let is_null =
                    ToSql::<T27,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            28 => {
                let item = &values.28.expr.item;
                let is_null =
                    ToSql::<T28,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            29 => {
                let item = &values.29.expr.item;
                let is_null =
                    ToSql::<T29,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            30 => {
                let item = &values.30.expr.item;
                let is_null =
                    ToSql::<T30,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            31 => {
                let item = &values.31.expr.item;
                let is_null =
                    ToSql::<T31,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            _ =>
                ::core::panicking::panic("internal error: entered unreachable code"),
        }
    }
}
impl<'a, __T, ST, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11,
    ST12, ST13, ST14, ST15, ST16, ST17, ST18, ST19, ST20, ST21, ST22, ST23,
    ST24, ST25, ST26, ST27, ST28, ST29, ST30, ST31, T, T1, T2, T3, T4, T5, T6,
    T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21,
    T22, T23, T24, T25, T26, T27, T28, T29, T30, T31, TT, TT1, TT2, TT3, TT4,
    TT5, TT6, TT7, TT8, TT9, TT10, TT11, TT12, TT13, TT14, TT15, TT16, TT17,
    TT18, TT19, TT20, TT21, TT22, TT23, TT24, TT25, TT26, TT27, TT28, TT29,
    TT30, TT31> CopyFromInsertableHelper for
    ValuesClause<(ColumnInsertValue<ST, &'a Bound<T, TT>>,
    ColumnInsertValue<ST1, &'a Bound<T1, TT1>>,
    ColumnInsertValue<ST2, &'a Bound<T2, TT2>>,
    ColumnInsertValue<ST3, &'a Bound<T3, TT3>>,
    ColumnInsertValue<ST4, &'a Bound<T4, TT4>>,
    ColumnInsertValue<ST5, &'a Bound<T5, TT5>>,
    ColumnInsertValue<ST6, &'a Bound<T6, TT6>>,
    ColumnInsertValue<ST7, &'a Bound<T7, TT7>>,
    ColumnInsertValue<ST8, &'a Bound<T8, TT8>>,
    ColumnInsertValue<ST9, &'a Bound<T9, TT9>>,
    ColumnInsertValue<ST10, &'a Bound<T10, TT10>>,
    ColumnInsertValue<ST11, &'a Bound<T11, TT11>>,
    ColumnInsertValue<ST12, &'a Bound<T12, TT12>>,
    ColumnInsertValue<ST13, &'a Bound<T13, TT13>>,
    ColumnInsertValue<ST14, &'a Bound<T14, TT14>>,
    ColumnInsertValue<ST15, &'a Bound<T15, TT15>>,
    ColumnInsertValue<ST16, &'a Bound<T16, TT16>>,
    ColumnInsertValue<ST17, &'a Bound<T17, TT17>>,
    ColumnInsertValue<ST18, &'a Bound<T18, TT18>>,
    ColumnInsertValue<ST19, &'a Bound<T19, TT19>>,
    ColumnInsertValue<ST20, &'a Bound<T20, TT20>>,
    ColumnInsertValue<ST21, &'a Bound<T21, TT21>>,
    ColumnInsertValue<ST22, &'a Bound<T22, TT22>>,
    ColumnInsertValue<ST23, &'a Bound<T23, TT23>>,
    ColumnInsertValue<ST24, &'a Bound<T24, TT24>>,
    ColumnInsertValue<ST25, &'a Bound<T25, TT25>>,
    ColumnInsertValue<ST26, &'a Bound<T26, TT26>>,
    ColumnInsertValue<ST27, &'a Bound<T27, TT27>>,
    ColumnInsertValue<ST28, &'a Bound<T28, TT28>>,
    ColumnInsertValue<ST29, &'a Bound<T29, TT29>>,
    ColumnInsertValue<ST30, &'a Bound<T30, TT30>>,
    ColumnInsertValue<ST31, &'a Bound<T31, TT31>>), __T> where __T: Table,
    ST: Column<Table = __T>, ST1: Column<Table = __T>,
    ST2: Column<Table = __T>, ST3: Column<Table = __T>,
    ST4: Column<Table = __T>, ST5: Column<Table = __T>,
    ST6: Column<Table = __T>, ST7: Column<Table = __T>,
    ST8: Column<Table = __T>, ST9: Column<Table = __T>,
    ST10: Column<Table = __T>, ST11: Column<Table = __T>,
    ST12: Column<Table = __T>, ST13: Column<Table = __T>,
    ST14: Column<Table = __T>, ST15: Column<Table = __T>,
    ST16: Column<Table = __T>, ST17: Column<Table = __T>,
    ST18: Column<Table = __T>, ST19: Column<Table = __T>,
    ST20: Column<Table = __T>, ST21: Column<Table = __T>,
    ST22: Column<Table = __T>, ST23: Column<Table = __T>,
    ST24: Column<Table = __T>, ST25: Column<Table = __T>,
    ST26: Column<Table = __T>, ST27: Column<Table = __T>,
    ST28: Column<Table = __T>, ST29: Column<Table = __T>,
    ST30: Column<Table = __T>, ST31: Column<Table = __T>,
    (ST, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11, ST12, ST13,
    ST14, ST15, ST16, ST17, ST18, ST19, ST20, ST21, ST22, ST23, ST24, ST25,
    ST26, ST27, ST28, ST29, ST30, ST31): CopyTarget, TT: ToSql<T, Pg>,
    TT1: ToSql<T1, Pg>, TT2: ToSql<T2, Pg>, TT3: ToSql<T3, Pg>,
    TT4: ToSql<T4, Pg>, TT5: ToSql<T5, Pg>, TT6: ToSql<T6, Pg>,
    TT7: ToSql<T7, Pg>, TT8: ToSql<T8, Pg>, TT9: ToSql<T9, Pg>,
    TT10: ToSql<T10, Pg>, TT11: ToSql<T11, Pg>, TT12: ToSql<T12, Pg>,
    TT13: ToSql<T13, Pg>, TT14: ToSql<T14, Pg>, TT15: ToSql<T15, Pg>,
    TT16: ToSql<T16, Pg>, TT17: ToSql<T17, Pg>, TT18: ToSql<T18, Pg>,
    TT19: ToSql<T19, Pg>, TT20: ToSql<T20, Pg>, TT21: ToSql<T21, Pg>,
    TT22: ToSql<T22, Pg>, TT23: ToSql<T23, Pg>, TT24: ToSql<T24, Pg>,
    TT25: ToSql<T25, Pg>, TT26: ToSql<T26, Pg>, TT27: ToSql<T27, Pg>,
    TT28: ToSql<T28, Pg>, TT29: ToSql<T29, Pg>, TT30: ToSql<T30, Pg>,
    TT31: ToSql<T31, Pg> {
    type Target =
        (ST, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, ST9, ST10, ST11, ST12,
        ST13, ST14, ST15, ST16, ST17, ST18, ST19, ST20, ST21, ST22, ST23,
        ST24, ST25, ST26, ST27, ST28, ST29, ST30, ST31);
    const COLUMN_COUNT: u16 = 32u16;
    fn write_to_buffer(&self, idx: u16, out: &mut Vec<u8>)
        -> QueryResult<IsNull> {
        use crate::query_builder::ByteWrapper;
        use crate::serialize::Output;
        let values = &self.values;
        match idx {
            0 => {
                let item = &values.0.expr.item;
                let is_null =
                    ToSql::<T,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            1 => {
                let item = &values.1.expr.item;
                let is_null =
                    ToSql::<T1,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            2 => {
                let item = &values.2.expr.item;
                let is_null =
                    ToSql::<T2,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            3 => {
                let item = &values.3.expr.item;
                let is_null =
                    ToSql::<T3,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            4 => {
                let item = &values.4.expr.item;
                let is_null =
                    ToSql::<T4,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            5 => {
                let item = &values.5.expr.item;
                let is_null =
                    ToSql::<T5,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            6 => {
                let item = &values.6.expr.item;
                let is_null =
                    ToSql::<T6,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            7 => {
                let item = &values.7.expr.item;
                let is_null =
                    ToSql::<T7,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            8 => {
                let item = &values.8.expr.item;
                let is_null =
                    ToSql::<T8,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            9 => {
                let item = &values.9.expr.item;
                let is_null =
                    ToSql::<T9,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            10 => {
                let item = &values.10.expr.item;
                let is_null =
                    ToSql::<T10,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            11 => {
                let item = &values.11.expr.item;
                let is_null =
                    ToSql::<T11,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            12 => {
                let item = &values.12.expr.item;
                let is_null =
                    ToSql::<T12,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            13 => {
                let item = &values.13.expr.item;
                let is_null =
                    ToSql::<T13,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            14 => {
                let item = &values.14.expr.item;
                let is_null =
                    ToSql::<T14,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            15 => {
                let item = &values.15.expr.item;
                let is_null =
                    ToSql::<T15,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            16 => {
                let item = &values.16.expr.item;
                let is_null =
                    ToSql::<T16,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            17 => {
                let item = &values.17.expr.item;
                let is_null =
                    ToSql::<T17,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            18 => {
                let item = &values.18.expr.item;
                let is_null =
                    ToSql::<T18,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            19 => {
                let item = &values.19.expr.item;
                let is_null =
                    ToSql::<T19,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            20 => {
                let item = &values.20.expr.item;
                let is_null =
                    ToSql::<T20,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            21 => {
                let item = &values.21.expr.item;
                let is_null =
                    ToSql::<T21,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            22 => {
                let item = &values.22.expr.item;
                let is_null =
                    ToSql::<T22,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            23 => {
                let item = &values.23.expr.item;
                let is_null =
                    ToSql::<T23,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            24 => {
                let item = &values.24.expr.item;
                let is_null =
                    ToSql::<T24,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            25 => {
                let item = &values.25.expr.item;
                let is_null =
                    ToSql::<T25,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            26 => {
                let item = &values.26.expr.item;
                let is_null =
                    ToSql::<T26,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            27 => {
                let item = &values.27.expr.item;
                let is_null =
                    ToSql::<T27,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            28 => {
                let item = &values.28.expr.item;
                let is_null =
                    ToSql::<T28,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            29 => {
                let item = &values.29.expr.item;
                let is_null =
                    ToSql::<T29,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            30 => {
                let item = &values.30.expr.item;
                let is_null =
                    ToSql::<T30,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            31 => {
                let item = &values.31.expr.item;
                let is_null =
                    ToSql::<T31,
                                    Pg>::to_sql(item,
                                &mut Output::new(ByteWrapper(out),
                                        &mut Dummy as
                                            _)).map_err(crate::result::Error::SerializationError)?;
                return Ok(is_null);
            }
            _ =>
                ::core::panicking::panic("internal error: entered unreachable code"),
        }
    }
}crate::for_each_tuple!(impl_copy_from_insertable_helper_for_values_clause);
269
270#[derive(#[automatically_derived]
impl<I: ::core::fmt::Debug> ::core::fmt::Debug for InsertableWrapper<I> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f,
            "InsertableWrapper", &&self.0)
    }
}Debug)]
271pub struct InsertableWrapper<I>(Option<I>);
272
273impl<I, T, V, QId, const STATIC_QUERY_ID: bool> CopyFromExpression<T> for InsertableWrapper<I>
274where
275    I: Insertable<T, Values = BatchInsert<Vec<V>, T, QId, STATIC_QUERY_ID>>,
276    V: CopyFromInsertableHelper,
277{
278    type Error = crate::result::Error;
279
280    fn callback(&mut self, copy: &mut impl std::io::Write) -> Result<(), Self::Error> {
281        let io_result_mapper = |e| crate::result::Error::DeserializationError(Box::new(e));
282        // see https://www.postgresql.org/docs/current/sql-copy.html for
283        // a description of the binary format
284        //
285        // We don't write oids
286
287        // write the header
288        copy.write_all(&super::COPY_MAGIC_HEADER)
289            .map_err(io_result_mapper)?;
290        copy.write_i32::<NetworkEndian>(0)
291            .map_err(io_result_mapper)?;
292        copy.write_i32::<NetworkEndian>(0)
293            .map_err(io_result_mapper)?;
294        // write the data
295        // we reuse the same buffer here again and again
296        // as we expect the data to be "similar"
297        // this skips reallocating
298        let mut buffer = Vec::<u8>::new();
299        let values = self
300            .0
301            .take()
302            .expect("We only call this callback once")
303            .values();
304        for i in values.values {
305            // column count
306            buffer
307                .write_u16::<NetworkEndian>(V::COLUMN_COUNT)
308                .map_err(io_result_mapper)?;
309            for idx in 0..V::COLUMN_COUNT {
310                // first write the null indicator as dummy value
311                buffer
312                    .write_i32::<NetworkEndian>(-1)
313                    .map_err(io_result_mapper)?;
314                let len_before = buffer.len();
315                let is_null = i.write_to_buffer(idx, &mut buffer)?;
316                if is_null == IsNull::No {
317                    // fill in the length afterwards
318                    let len_after = buffer.len();
319                    let diff = (len_after - len_before)
320                        .try_into()
321                        .map_err(|e| crate::result::Error::SerializationError(Box::new(e)))?;
322                    let bytes = i32::to_be_bytes(diff);
323                    for (b, t) in bytes.into_iter().zip(&mut buffer[len_before - 4..]) {
324                        *t = b;
325                    }
326                }
327            }
328            copy.write_all(&buffer).map_err(io_result_mapper)?;
329            buffer.clear();
330        }
331        // write the trailer
332        copy.write_i16::<NetworkEndian>(-1)
333            .map_err(io_result_mapper)?;
334        Ok(())
335    }
336
337    fn options(&self) -> &CopyFromOptions {
338        &CopyFromOptions {
339            common: CommonOptions {
340                format: Some(CopyFormat::Binary),
341                freeze: None,
342                delimiter: None,
343                null: None,
344                quote: None,
345                escape: None,
346            },
347            default: None,
348            header: None,
349        }
350    }
351
352    fn walk_target<'b>(
353        &'b self,
354        pass: crate::query_builder::AstPass<'_, 'b, Pg>,
355    ) -> crate::QueryResult<()> {
356        <V as CopyFromInsertableHelper>::Target::walk_target(pass)
357    }
358}
359
360/// The structure returned by [`copy_from`]
361///
362/// The [`from_raw_data`] and the [`from_insertable`] methods allow
363/// to configure the data copied into the database
364///
365/// The `with_*` methods allow to configure the settings used for the
366/// copy statement.
367///
368/// [`from_raw_data`]: CopyFromQuery::from_raw_data
369/// [`from_insertable`]: CopyFromQuery::from_insertable
370#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug, Action: ::core::fmt::Debug> ::core::fmt::Debug for
    CopyFromQuery<T, Action> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "CopyFromQuery",
            "table", &self.table, "action", &&self.action)
    }
}Debug)]
371#[must_use = "`COPY FROM` statements are only executed when calling `.execute()`."]
372#[cfg(feature = "postgres_backend")]
373pub struct CopyFromQuery<T, Action> {
374    table: T,
375    action: Action,
376}
377
378impl<T> CopyFromQuery<T, NotSet>
379where
380    T: Table,
381{
382    /// Copy data into the database by directly providing the data in the corresponding format
383    ///
384    /// `target` specifies the column selection that is the target of the `COPY FROM` statement
385    /// `action` expects a callback which accepts a [`std::io::Write`] argument. The necessary format
386    /// accepted by this writer sink depends on the options provided via the `with_*` methods
387    #[allow(clippy::wrong_self_convention)] // the sql struct is named that way
388    pub fn from_raw_data<F, C, E>(self, _target: C, action: F) -> CopyFromQuery<T, CopyFrom<C, F>>
389    where
390        C: CopyTarget<Table = T>,
391        F: Fn(&mut dyn std::io::Write) -> Result<(), E>,
392    {
393        CopyFromQuery {
394            table: self.table,
395            action: CopyFrom {
396                p: PhantomData,
397                options: Default::default(),
398                copy_callback: action,
399            },
400        }
401    }
402
403    /// Copy a set of insertable values into the database.
404    ///
405    /// The `insertable` argument is expected to be a `Vec<I>`, `&[I]` or similar, where `I`
406    /// needs to implement `Insertable<T>`. If you use the [`#[derive(Insertable)]`](derive@crate::prelude::Insertable)
407    /// derive macro make sure to also set the `#[diesel(treat_none_as_default_value = false)]` option
408    /// to disable the default value handling otherwise implemented by `#[derive(Insertable)]`.
409    ///
410    /// This uses the binary format. It internally configures the correct
411    /// set of settings and does not allow to set other options
412    #[allow(clippy::wrong_self_convention)] // the sql struct is named that way
413    pub fn from_insertable<I>(self, insertable: I) -> CopyFromQuery<T, InsertableWrapper<I>>
414    where
415        InsertableWrapper<I>: CopyFromExpression<T>,
416    {
417        CopyFromQuery {
418            table: self.table,
419            action: InsertableWrapper(Some(insertable)),
420        }
421    }
422}
423
424impl<T, C, F> CopyFromQuery<T, CopyFrom<C, F>> {
425    /// The format used for the copy statement
426    ///
427    /// See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html)
428    /// for more details.
429    pub fn with_format(mut self, format: CopyFormat) -> Self {
430        self.action.options.common.format = Some(format);
431        self
432    }
433
434    /// Whether or not the `freeze` option is set
435    ///
436    /// See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html)
437    /// for more details.
438    pub fn with_freeze(mut self, freeze: bool) -> Self {
439        self.action.options.common.freeze = Some(freeze);
440        self
441    }
442
443    /// Which delimiter should be used for textual input formats
444    ///
445    /// See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html)
446    /// for more details.
447    ///
448    /// Diesel will automatically escape the provided delimiter for the default
449    /// PostgreSQL setting `standard_conforming_strings=on`. If you use a non-standard
450    /// conforming setting you need to take care of escaping the value on your own
451    pub fn with_delimiter(mut self, delimiter: char) -> Self {
452        self.action.options.common.delimiter = Some(delimiter);
453        self
454    }
455
456    /// Which string should be used in place of a `NULL` value
457    /// for textual input formats
458    ///
459    /// See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html)
460    /// for more details.
461    ///
462    /// Diesel will automatically escape the provided delimiter for the default
463    /// PostgreSQL setting `standard_conforming_strings=on`. If you use a non-standard
464    /// conforming setting you need to take care of escaping the value on your own
465    pub fn with_null(mut self, null: impl Into<String>) -> Self {
466        self.action.options.common.null = Some(null.into());
467        self
468    }
469
470    /// Which quote character should be used for textual input formats
471    ///
472    /// See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html)
473    /// for more details.
474    ///
475    /// Diesel will automatically escape the provided delimiter for the default
476    /// PostgreSQL setting `standard_conforming_strings=on`. If you use a non-standard
477    /// conforming setting you need to take care of escaping the value on your own
478    pub fn with_quote(mut self, quote: char) -> Self {
479        self.action.options.common.quote = Some(quote);
480        self
481    }
482
483    /// Which escape character should be used for textual input formats
484    ///
485    /// See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html)
486    /// for more details.
487    ///
488    /// Diesel will automatically escape the provided delimiter for the default
489    /// PostgreSQL setting `standard_conforming_strings=on`. If you use a non-standard
490    /// conforming setting you need to take care of escaping the value on your own
491    pub fn with_escape(mut self, escape: char) -> Self {
492        self.action.options.common.escape = Some(escape);
493        self
494    }
495
496    /// Which string should be used to indicate that
497    /// the `default` value should be used in place of that string
498    /// for textual formats
499    ///
500    /// See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html)
501    /// for more details.
502    ///
503    /// (This parameter was added with PostgreSQL 16)
504    ///
505    /// Diesel will automatically escape the provided delimiter for the default
506    /// PostgreSQL setting `standard_conforming_strings=on`. If you use a non-standard
507    /// conforming setting you need to take care of escaping the value on your own
508    pub fn with_default(mut self, default: impl Into<String>) -> Self {
509        self.action.options.default = Some(default.into());
510        self
511    }
512
513    /// Is a header provided as part of the textual input or not
514    ///
515    /// See the [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-copy.html)
516    /// for more details.
517    pub fn with_header(mut self, header: CopyHeader) -> Self {
518        self.action.options.header = Some(header);
519        self
520    }
521}
522
523/// A custom execute function tailored for `COPY FROM` statements
524///
525/// This trait can be used to execute `COPY FROM` queries constructed
526/// via [`copy_from]`
527pub trait ExecuteCopyFromDsl<C>
528where
529    C: Connection<Backend = Pg>,
530{
531    /// The error type returned by the execute function
532    type Error: core::error::Error;
533
534    /// See the trait documentation for details
535    fn execute(self, conn: &mut C) -> Result<usize, Self::Error>;
536}
537
538#[cfg(feature = "postgres")]
539impl<T, A> ExecuteCopyFromDsl<crate::PgConnection> for CopyFromQuery<T, A>
540where
541    A: CopyFromExpression<T>,
542{
543    type Error = A::Error;
544
545    fn execute(self, conn: &mut crate::PgConnection) -> Result<usize, A::Error> {
546        conn.copy_from::<A, T>(self.action)
547    }
548}
549
550#[cfg(feature = "r2d2")]
551impl<T, A, C> ExecuteCopyFromDsl<crate::r2d2::PooledConnection<crate::r2d2::ConnectionManager<C>>>
552    for CopyFromQuery<T, A>
553where
554    A: CopyFromExpression<T>,
555    C: crate::r2d2::R2D2Connection<Backend = Pg> + 'static,
556    Self: ExecuteCopyFromDsl<C>,
557{
558    type Error = <Self as ExecuteCopyFromDsl<C>>::Error;
559
560    fn execute(
561        self,
562        conn: &mut crate::r2d2::PooledConnection<crate::r2d2::ConnectionManager<C>>,
563    ) -> Result<usize, Self::Error> {
564        self.execute(&mut **conn)
565    }
566}
567
568#[derive(#[automatically_derived]
impl ::core::fmt::Debug for NotSet {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "NotSet")
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for NotSet {
    #[inline]
    fn clone(&self) -> NotSet { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for NotSet { }Copy)]
569pub struct NotSet;
570
571/// Creates a `COPY FROM` statement
572///
573/// This function constructs `COPY FROM` statement which copies data
574/// *from* a source into the database. It's designed to move larger
575/// amounts of data into the database.
576///
577/// This function accepts a target table as argument.
578///
579/// There are two ways to construct a `COPY FROM` statement with
580/// diesel:
581///
582/// * By providing a `Vec<I>` where `I` implements `Insertable` for the
583///   given table
584/// * By providing a target selection (column list or table name)
585///   and a callback that provides the data
586///
587/// The first variant uses the `BINARY` format internally to send
588/// the provided data efficiently to the database. It automatically
589/// sets the right options and does not allow changing them.
590/// Use [`CopyFromQuery::from_insertable`] for this.
591///
592/// The second variant allows you to control the behaviour
593/// of the generated `COPY FROM` statement in detail. It can
594/// be setup via the [`CopyFromQuery::from_raw_data`] function.
595/// The callback accepts an opaque object as argument that allows
596/// to write the corresponding data to the database. The exact
597/// format depends on the settings chosen by the various
598/// `CopyFromQuery::with_*` methods. See
599/// [the postgresql documentation](https://www.postgresql.org/docs/current/sql-copy.html)
600/// for more details about the expected formats.
601///
602/// If you don't have any specific needs you should prefer
603/// using the more convenient first variant.
604///
605/// This functionality is postgresql specific.
606///
607/// # Examples
608///
609/// ## Via [`CopyFromQuery::from_insertable`]
610///
611/// ```rust
612/// # include!("../../../doctest_setup.rs");
613/// # use crate::schema::users;
614///
615/// #[derive(Insertable)]
616/// #[diesel(table_name = users)]
617/// #[diesel(treat_none_as_default_value = false)]
618/// struct NewUser {
619///     name: &'static str,
620/// }
621///
622/// # fn run_test() -> QueryResult<()> {
623/// # let connection = &mut establish_connection();
624///
625/// let data = vec![
626///     NewUser {
627///         name: "Diva Plavalaguna",
628///     },
629///     NewUser {
630///         name: "Father Vito Cornelius",
631///     },
632/// ];
633///
634/// let count = diesel::copy_from(users::table)
635///     .from_insertable(&data)
636///     .execute(connection)?;
637///
638/// assert_eq!(count, 2);
639/// # Ok(())
640/// # }
641/// # fn main() {
642/// #    run_test().unwrap();
643/// # }
644/// ```
645///
646/// ## Via [`CopyFromQuery::from_raw_data`]
647///
648/// ```rust
649/// # include!("../../../doctest_setup.rs");
650/// # fn run_test() -> QueryResult<()> {
651/// # use crate::schema::users;
652/// use diesel::pg::CopyFormat;
653/// # let connection = &mut establish_connection();
654/// let count = diesel::copy_from(users::table)
655///     .from_raw_data(users::table, |copy| {
656///         writeln!(copy, "3,Diva Plavalaguna").unwrap();
657///         writeln!(copy, "4,Father Vito Cornelius").unwrap();
658///         diesel::QueryResult::Ok(())
659///     })
660///     .with_format(CopyFormat::Csv)
661///     .execute(connection)?;
662///
663/// assert_eq!(count, 2);
664/// # Ok(())
665/// # }
666/// # fn main() {
667/// #    run_test().unwrap();
668/// # }
669/// ```
670#[cfg(feature = "postgres_backend")]
671pub fn copy_from<T>(table: T) -> CopyFromQuery<T, NotSet>
672where
673    T: Table,
674{
675    CopyFromQuery {
676        table,
677        action: NotSet,
678    }
679}