samling_clorinde/queries/
image.rs

1// This file was generated with `clorinde`. Do not modify.
2
3#[derive(Debug)]
4pub struct GetImageIdParams<T1: crate::StringSql> {
5    pub organization_id: i32,
6    pub id: Option<i32>,
7    pub external_id: Option<T1>,
8}
9#[derive(Debug)]
10pub struct GetImageUrlByExternalChecksumParams<T1: crate::StringSql> {
11    pub organization_id: i32,
12    pub external_checksum: T1,
13}
14#[derive(Debug)]
15pub struct GetImageParams<T1: crate::StringSql> {
16    pub organization_id: i32,
17    pub id: Option<i32>,
18    pub external_id: Option<T1>,
19}
20#[derive(Debug)]
21pub struct InsertImageParams<T1: crate::StringSql, T2: crate::StringSql, T3: crate::StringSql> {
22    pub color_id: i32,
23    pub external_id: Option<T1>,
24    pub url: T2,
25    pub organization_id: i32,
26    pub uploaded_by: i32,
27    pub external_checksum: Option<T3>,
28    pub position: i32,
29}
30#[derive(Debug)]
31pub struct UpdateImageParams<T1: crate::StringSql, T2: crate::StringSql, T3: crate::StringSql> {
32    pub color_id: i32,
33    pub external_id: Option<T1>,
34    pub url: Option<T2>,
35    pub external_checksum: Option<T3>,
36    pub position: Option<i32>,
37    pub id: i32,
38}
39#[derive(Clone, Copy, Debug)]
40pub struct DeleteImageParams {
41    pub organization_id: i32,
42    pub id: i32,
43}
44#[derive(Debug, Clone, PartialEq)]
45pub struct ImageRow {
46    pub id: i32,
47    pub organization_id: i32,
48    pub url: String,
49    pub external_id: Option<String>,
50    pub external_checksum: Option<String>,
51    pub position: i32,
52    pub color_id: i32,
53    pub uploaded_by: Option<i32>,
54    pub uploaded_at: chrono::DateTime<chrono::FixedOffset>,
55    pub updated_at: chrono::DateTime<chrono::FixedOffset>,
56    pub color: serde_json::Value,
57}
58pub struct ImageRowBorrowed<'a> {
59    pub id: i32,
60    pub organization_id: i32,
61    pub url: &'a str,
62    pub external_id: Option<&'a str>,
63    pub external_checksum: Option<&'a str>,
64    pub position: i32,
65    pub color_id: i32,
66    pub uploaded_by: Option<i32>,
67    pub uploaded_at: chrono::DateTime<chrono::FixedOffset>,
68    pub updated_at: chrono::DateTime<chrono::FixedOffset>,
69    pub color: postgres_types::Json<&'a serde_json::value::RawValue>,
70}
71impl<'a> From<ImageRowBorrowed<'a>> for ImageRow {
72    fn from(
73        ImageRowBorrowed {
74            id,
75            organization_id,
76            url,
77            external_id,
78            external_checksum,
79            position,
80            color_id,
81            uploaded_by,
82            uploaded_at,
83            updated_at,
84            color,
85        }: ImageRowBorrowed<'a>,
86    ) -> Self {
87        Self {
88            id,
89            organization_id,
90            url: url.into(),
91            external_id: external_id.map(|v| v.into()),
92            external_checksum: external_checksum.map(|v| v.into()),
93            position,
94            color_id,
95            uploaded_by,
96            uploaded_at,
97            updated_at,
98            color: serde_json::from_str(color.0.get()).unwrap(),
99        }
100    }
101}
102use crate::client::async_::GenericClient;
103use futures::{self, StreamExt, TryStreamExt};
104pub struct ImageRowQuery<'a, C: GenericClient, T, const N: usize> {
105    client: &'a C,
106    params: [&'a (dyn postgres_types::ToSql + Sync); N],
107    stmt: &'a mut crate::client::async_::Stmt,
108    extractor: fn(&tokio_postgres::Row) -> ImageRowBorrowed,
109    mapper: fn(ImageRowBorrowed) -> T,
110}
111impl<'a, C, T: 'a, const N: usize> ImageRowQuery<'a, C, T, N>
112where
113    C: GenericClient,
114{
115    pub fn map<R>(self, mapper: fn(ImageRowBorrowed) -> R) -> ImageRowQuery<'a, C, R, N> {
116        ImageRowQuery {
117            client: self.client,
118            params: self.params,
119            stmt: self.stmt,
120            extractor: self.extractor,
121            mapper,
122        }
123    }
124    pub async fn one(self) -> Result<T, tokio_postgres::Error> {
125        let stmt = self.stmt.prepare(self.client).await?;
126        let row = self.client.query_one(stmt, &self.params).await?;
127        Ok((self.mapper)((self.extractor)(&row)))
128    }
129    pub async fn all(self) -> Result<Vec<T>, tokio_postgres::Error> {
130        self.iter().await?.try_collect().await
131    }
132    pub async fn opt(self) -> Result<Option<T>, tokio_postgres::Error> {
133        let stmt = self.stmt.prepare(self.client).await?;
134        Ok(self
135            .client
136            .query_opt(stmt, &self.params)
137            .await?
138            .map(|row| (self.mapper)((self.extractor)(&row))))
139    }
140    pub async fn iter(
141        self,
142    ) -> Result<
143        impl futures::Stream<Item = Result<T, tokio_postgres::Error>> + 'a,
144        tokio_postgres::Error,
145    > {
146        let stmt = self.stmt.prepare(self.client).await?;
147        let it = self
148            .client
149            .query_raw(stmt, crate::slice_iter(&self.params))
150            .await?
151            .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
152            .into_stream();
153        Ok(it)
154    }
155}
156pub struct I32Query<'a, C: GenericClient, T, const N: usize> {
157    client: &'a C,
158    params: [&'a (dyn postgres_types::ToSql + Sync); N],
159    stmt: &'a mut crate::client::async_::Stmt,
160    extractor: fn(&tokio_postgres::Row) -> i32,
161    mapper: fn(i32) -> T,
162}
163impl<'a, C, T: 'a, const N: usize> I32Query<'a, C, T, N>
164where
165    C: GenericClient,
166{
167    pub fn map<R>(self, mapper: fn(i32) -> R) -> I32Query<'a, C, R, N> {
168        I32Query {
169            client: self.client,
170            params: self.params,
171            stmt: self.stmt,
172            extractor: self.extractor,
173            mapper,
174        }
175    }
176    pub async fn one(self) -> Result<T, tokio_postgres::Error> {
177        let stmt = self.stmt.prepare(self.client).await?;
178        let row = self.client.query_one(stmt, &self.params).await?;
179        Ok((self.mapper)((self.extractor)(&row)))
180    }
181    pub async fn all(self) -> Result<Vec<T>, tokio_postgres::Error> {
182        self.iter().await?.try_collect().await
183    }
184    pub async fn opt(self) -> Result<Option<T>, tokio_postgres::Error> {
185        let stmt = self.stmt.prepare(self.client).await?;
186        Ok(self
187            .client
188            .query_opt(stmt, &self.params)
189            .await?
190            .map(|row| (self.mapper)((self.extractor)(&row))))
191    }
192    pub async fn iter(
193        self,
194    ) -> Result<
195        impl futures::Stream<Item = Result<T, tokio_postgres::Error>> + 'a,
196        tokio_postgres::Error,
197    > {
198        let stmt = self.stmt.prepare(self.client).await?;
199        let it = self
200            .client
201            .query_raw(stmt, crate::slice_iter(&self.params))
202            .await?
203            .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
204            .into_stream();
205        Ok(it)
206    }
207}
208pub struct StringQuery<'a, C: GenericClient, T, const N: usize> {
209    client: &'a C,
210    params: [&'a (dyn postgres_types::ToSql + Sync); N],
211    stmt: &'a mut crate::client::async_::Stmt,
212    extractor: fn(&tokio_postgres::Row) -> &str,
213    mapper: fn(&str) -> T,
214}
215impl<'a, C, T: 'a, const N: usize> StringQuery<'a, C, T, N>
216where
217    C: GenericClient,
218{
219    pub fn map<R>(self, mapper: fn(&str) -> R) -> StringQuery<'a, C, R, N> {
220        StringQuery {
221            client: self.client,
222            params: self.params,
223            stmt: self.stmt,
224            extractor: self.extractor,
225            mapper,
226        }
227    }
228    pub async fn one(self) -> Result<T, tokio_postgres::Error> {
229        let stmt = self.stmt.prepare(self.client).await?;
230        let row = self.client.query_one(stmt, &self.params).await?;
231        Ok((self.mapper)((self.extractor)(&row)))
232    }
233    pub async fn all(self) -> Result<Vec<T>, tokio_postgres::Error> {
234        self.iter().await?.try_collect().await
235    }
236    pub async fn opt(self) -> Result<Option<T>, tokio_postgres::Error> {
237        let stmt = self.stmt.prepare(self.client).await?;
238        Ok(self
239            .client
240            .query_opt(stmt, &self.params)
241            .await?
242            .map(|row| (self.mapper)((self.extractor)(&row))))
243    }
244    pub async fn iter(
245        self,
246    ) -> Result<
247        impl futures::Stream<Item = Result<T, tokio_postgres::Error>> + 'a,
248        tokio_postgres::Error,
249    > {
250        let stmt = self.stmt.prepare(self.client).await?;
251        let it = self
252            .client
253            .query_raw(stmt, crate::slice_iter(&self.params))
254            .await?
255            .map(move |res| res.map(|row| (self.mapper)((self.extractor)(&row))))
256            .into_stream();
257        Ok(it)
258    }
259}
260pub fn list_images() -> ListImagesStmt {
261    ListImagesStmt(crate::client::async_::Stmt::new(
262        "SELECT
263    image.*,
264    jsonb_build_object(
265        'id',
266        color.id,
267        'style',
268        jsonb_build_object(
269            'id',
270            style.id,
271            'number',
272            style.number,
273            'name',
274            style.name,
275            'slug',
276            style.slug,
277            'external_id',
278            style.external_id
279        ),
280        'number',
281        color.number,
282        'name',
283        color.name,
284        'slug',
285        color.slug,
286        'external_id',
287        color.external_id
288    ) AS \"color\"
289FROM
290    image
291INNER JOIN color ON image.color_id = color.id
292INNER JOIN style ON color.style_id = style.id
293WHERE
294    image.organization_id = $1
295ORDER BY
296    image.id",
297    ))
298}
299pub struct ListImagesStmt(crate::client::async_::Stmt);
300impl ListImagesStmt {
301    pub fn bind<'a, C: GenericClient>(
302        &'a mut self,
303        client: &'a C,
304        organization_id: &'a i32,
305    ) -> ImageRowQuery<'a, C, ImageRow, 1> {
306        ImageRowQuery {
307            client,
308            params: [organization_id],
309            stmt: &mut self.0,
310            extractor: |row| ImageRowBorrowed {
311                id: row.get(0),
312                organization_id: row.get(1),
313                url: row.get(2),
314                external_id: row.get(3),
315                external_checksum: row.get(4),
316                position: row.get(5),
317                color_id: row.get(6),
318                uploaded_by: row.get(7),
319                uploaded_at: row.get(8),
320                updated_at: row.get(9),
321                color: row.get(10),
322            },
323            mapper: |it| <ImageRow>::from(it),
324        }
325    }
326}
327pub fn get_image_id() -> GetImageIdStmt {
328    GetImageIdStmt(crate::client::async_::Stmt::new(
329        "SELECT image.id
330FROM
331    image
332WHERE
333    image.organization_id = $1
334    AND (
335        image.id = coalesce($2, -1)
336        OR image.external_id = coalesce($3, '___NON_EXISTING___')
337    )",
338    ))
339}
340pub struct GetImageIdStmt(crate::client::async_::Stmt);
341impl GetImageIdStmt {
342    pub fn bind<'a, C: GenericClient, T1: crate::StringSql>(
343        &'a mut self,
344        client: &'a C,
345        organization_id: &'a i32,
346        id: &'a Option<i32>,
347        external_id: &'a Option<T1>,
348    ) -> I32Query<'a, C, i32, 3> {
349        I32Query {
350            client,
351            params: [organization_id, id, external_id],
352            stmt: &mut self.0,
353            extractor: |row| row.get(0),
354            mapper: |it| it,
355        }
356    }
357}
358impl<'a, C: GenericClient, T1: crate::StringSql>
359    crate::client::async_::Params<'a, GetImageIdParams<T1>, I32Query<'a, C, i32, 3>, C>
360    for GetImageIdStmt
361{
362    fn params(
363        &'a mut self,
364        client: &'a C,
365        params: &'a GetImageIdParams<T1>,
366    ) -> I32Query<'a, C, i32, 3> {
367        self.bind(
368            client,
369            &params.organization_id,
370            &params.id,
371            &params.external_id,
372        )
373    }
374}
375pub fn get_image_url_by_external_checksum() -> GetImageUrlByExternalChecksumStmt {
376    GetImageUrlByExternalChecksumStmt(crate::client::async_::Stmt::new(
377        "SELECT image.url
378FROM
379    image
380WHERE
381    image.organization_id = $1
382    AND image.external_checksum = $2",
383    ))
384}
385pub struct GetImageUrlByExternalChecksumStmt(crate::client::async_::Stmt);
386impl GetImageUrlByExternalChecksumStmt {
387    pub fn bind<'a, C: GenericClient, T1: crate::StringSql>(
388        &'a mut self,
389        client: &'a C,
390        organization_id: &'a i32,
391        external_checksum: &'a T1,
392    ) -> StringQuery<'a, C, String, 2> {
393        StringQuery {
394            client,
395            params: [organization_id, external_checksum],
396            stmt: &mut self.0,
397            extractor: |row| row.get(0),
398            mapper: |it| it.into(),
399        }
400    }
401}
402impl<'a, C: GenericClient, T1: crate::StringSql>
403    crate::client::async_::Params<
404        'a,
405        GetImageUrlByExternalChecksumParams<T1>,
406        StringQuery<'a, C, String, 2>,
407        C,
408    > for GetImageUrlByExternalChecksumStmt
409{
410    fn params(
411        &'a mut self,
412        client: &'a C,
413        params: &'a GetImageUrlByExternalChecksumParams<T1>,
414    ) -> StringQuery<'a, C, String, 2> {
415        self.bind(client, &params.organization_id, &params.external_checksum)
416    }
417}
418pub fn get_image() -> GetImageStmt {
419    GetImageStmt(crate::client::async_::Stmt::new(
420        "SELECT
421    image.*,
422    jsonb_build_object(
423        'id',
424        color.id,
425        'style',
426        jsonb_build_object(
427            'id',
428            style.id,
429            'number',
430            style.number,
431            'name',
432            style.name,
433            'slug',
434            style.slug,
435            'external_id',
436            style.external_id
437        ),
438        'number',
439        color.number,
440        'name',
441        color.name,
442        'slug',
443        color.slug,
444        'external_id',
445        color.external_id
446    ) AS \"color\"
447FROM
448    image
449INNER JOIN color ON image.color_id = color.id
450INNER JOIN style ON color.style_id = style.id
451WHERE
452    image.organization_id = $1
453    AND (
454        image.id = coalesce($2, -1)
455        OR image.external_id = coalesce($3, '___NON_EXISTING___')
456    )",
457    ))
458}
459pub struct GetImageStmt(crate::client::async_::Stmt);
460impl GetImageStmt {
461    pub fn bind<'a, C: GenericClient, T1: crate::StringSql>(
462        &'a mut self,
463        client: &'a C,
464        organization_id: &'a i32,
465        id: &'a Option<i32>,
466        external_id: &'a Option<T1>,
467    ) -> ImageRowQuery<'a, C, ImageRow, 3> {
468        ImageRowQuery {
469            client,
470            params: [organization_id, id, external_id],
471            stmt: &mut self.0,
472            extractor: |row| ImageRowBorrowed {
473                id: row.get(0),
474                organization_id: row.get(1),
475                url: row.get(2),
476                external_id: row.get(3),
477                external_checksum: row.get(4),
478                position: row.get(5),
479                color_id: row.get(6),
480                uploaded_by: row.get(7),
481                uploaded_at: row.get(8),
482                updated_at: row.get(9),
483                color: row.get(10),
484            },
485            mapper: |it| <ImageRow>::from(it),
486        }
487    }
488}
489impl<'a, C: GenericClient, T1: crate::StringSql>
490    crate::client::async_::Params<'a, GetImageParams<T1>, ImageRowQuery<'a, C, ImageRow, 3>, C>
491    for GetImageStmt
492{
493    fn params(
494        &'a mut self,
495        client: &'a C,
496        params: &'a GetImageParams<T1>,
497    ) -> ImageRowQuery<'a, C, ImageRow, 3> {
498        self.bind(
499            client,
500            &params.organization_id,
501            &params.id,
502            &params.external_id,
503        )
504    }
505}
506pub fn insert_image() -> InsertImageStmt {
507    InsertImageStmt(crate::client::async_::Stmt::new(
508        "INSERT INTO image (
509    color_id,
510    external_id,
511    url,
512    organization_id,
513    uploaded_by,
514    external_checksum,
515    position)
516VALUES (
517    $1,
518    $2,
519    $3,
520    $4,
521    $5,
522    $6,
523    $7)
524RETURNING
525id",
526    ))
527}
528pub struct InsertImageStmt(crate::client::async_::Stmt);
529impl InsertImageStmt {
530    pub fn bind<
531        'a,
532        C: GenericClient,
533        T1: crate::StringSql,
534        T2: crate::StringSql,
535        T3: crate::StringSql,
536    >(
537        &'a mut self,
538        client: &'a C,
539        color_id: &'a i32,
540        external_id: &'a Option<T1>,
541        url: &'a T2,
542        organization_id: &'a i32,
543        uploaded_by: &'a i32,
544        external_checksum: &'a Option<T3>,
545        position: &'a i32,
546    ) -> I32Query<'a, C, i32, 7> {
547        I32Query {
548            client,
549            params: [
550                color_id,
551                external_id,
552                url,
553                organization_id,
554                uploaded_by,
555                external_checksum,
556                position,
557            ],
558            stmt: &mut self.0,
559            extractor: |row| row.get(0),
560            mapper: |it| it,
561        }
562    }
563}
564impl<'a, C: GenericClient, T1: crate::StringSql, T2: crate::StringSql, T3: crate::StringSql>
565    crate::client::async_::Params<'a, InsertImageParams<T1, T2, T3>, I32Query<'a, C, i32, 7>, C>
566    for InsertImageStmt
567{
568    fn params(
569        &'a mut self,
570        client: &'a C,
571        params: &'a InsertImageParams<T1, T2, T3>,
572    ) -> I32Query<'a, C, i32, 7> {
573        self.bind(
574            client,
575            &params.color_id,
576            &params.external_id,
577            &params.url,
578            &params.organization_id,
579            &params.uploaded_by,
580            &params.external_checksum,
581            &params.position,
582        )
583    }
584}
585pub fn update_image() -> UpdateImageStmt {
586    UpdateImageStmt(crate::client::async_::Stmt::new(
587        "UPDATE
588image
589SET
590    color_id = coalesce($1, color_id),
591    external_id = coalesce($2, external_id),
592    url = coalesce($3, url),
593    external_checksum = coalesce($4, external_checksum),
594    position = coalesce($5, position)
595WHERE
596    id = $6",
597    ))
598}
599pub struct UpdateImageStmt(crate::client::async_::Stmt);
600impl UpdateImageStmt {
601    pub async fn bind<
602        'a,
603        C: GenericClient,
604        T1: crate::StringSql,
605        T2: crate::StringSql,
606        T3: crate::StringSql,
607    >(
608        &'a mut self,
609        client: &'a C,
610        color_id: &'a i32,
611        external_id: &'a Option<T1>,
612        url: &'a Option<T2>,
613        external_checksum: &'a Option<T3>,
614        position: &'a Option<i32>,
615        id: &'a i32,
616    ) -> Result<u64, tokio_postgres::Error> {
617        let stmt = self.0.prepare(client).await?;
618        client
619            .execute(
620                stmt,
621                &[color_id, external_id, url, external_checksum, position, id],
622            )
623            .await
624    }
625}
626impl<
627        'a,
628        C: GenericClient + Send + Sync,
629        T1: crate::StringSql,
630        T2: crate::StringSql,
631        T3: crate::StringSql,
632    >
633    crate::client::async_::Params<
634        'a,
635        UpdateImageParams<T1, T2, T3>,
636        std::pin::Pin<
637            Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
638        >,
639        C,
640    > for UpdateImageStmt
641{
642    fn params(
643        &'a mut self,
644        client: &'a C,
645        params: &'a UpdateImageParams<T1, T2, T3>,
646    ) -> std::pin::Pin<
647        Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
648    > {
649        Box::pin(self.bind(
650            client,
651            &params.color_id,
652            &params.external_id,
653            &params.url,
654            &params.external_checksum,
655            &params.position,
656            &params.id,
657        ))
658    }
659}
660pub fn delete_image() -> DeleteImageStmt {
661    DeleteImageStmt(crate::client::async_::Stmt::new(
662        "DELETE FROM image
663WHERE organization_id = $1
664      AND id = $2",
665    ))
666}
667pub struct DeleteImageStmt(crate::client::async_::Stmt);
668impl DeleteImageStmt {
669    pub async fn bind<'a, C: GenericClient>(
670        &'a mut self,
671        client: &'a C,
672        organization_id: &'a i32,
673        id: &'a i32,
674    ) -> Result<u64, tokio_postgres::Error> {
675        let stmt = self.0.prepare(client).await?;
676        client.execute(stmt, &[organization_id, id]).await
677    }
678}
679impl<'a, C: GenericClient + Send + Sync>
680    crate::client::async_::Params<
681        'a,
682        DeleteImageParams,
683        std::pin::Pin<
684            Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
685        >,
686        C,
687    > for DeleteImageStmt
688{
689    fn params(
690        &'a mut self,
691        client: &'a C,
692        params: &'a DeleteImageParams,
693    ) -> std::pin::Pin<
694        Box<dyn futures::Future<Output = Result<u64, tokio_postgres::Error>> + Send + 'a>,
695    > {
696        Box::pin(self.bind(client, &params.organization_id, &params.id))
697    }
698}