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