Debian Codegraph @

tests/postgres/derives.rs

raw symbols SCIP
1 use futures::TryStreamExt;
2 use sqlx::postgres::types::PgRange;
3 use sqlx::{Connection, Executor, FromRow, Postgres};
4 use sqlx_postgres::PgHasArrayType;
5 use sqlx_test::{new, test_type};
6 use std::fmt::Debug;
7 use std::ops::Bound;
8
9 // Transparent types are rust-side wrappers over DB types
10 #[derive(PartialEq, Debug, sqlx::Type)]
11 #[sqlx(transparent)]
12 struct Transparent(i32);
13
14 #[derive(PartialEq, Debug, sqlx::Type)]
15 // https://github.com/launchbadge/sqlx/issues/2611
16 // Previously, the derive would generate a `PgHasArrayType` impl that errored on an
17 // impossible-to-satisfy `where` bound. This attribute allows the user to opt-out.
18 #[sqlx(transparent, no_pg_array)]
19 struct TransparentArray(Vec<i64>);
20
21 #[sqlx_macros::test]
22 async fn test_transparent_slice_to_array() -> anyhow::Result<()> {
23 let mut conn = new::<Postgres>().await?;
24
25 let values = vec![Transparent(1), Transparent(2), Transparent(3)];
26
27 sqlx::query("SELECT 2 = ANY($1);")
28 .bind(&values)
29 .fetch_one(&mut conn)
30 .await?;
31
32 Ok(())
33 }
34
35 // "Weak" enums map to an integer type indicated by #[repr]
36 #[derive(PartialEq, Copy, Clone, Debug, sqlx::Type)]
37 #[repr(i32)]
38 enum Weak {
39 One = 0,
40 Two = 2,
41 Three = 4,
42 }
43
44 // "Strong" enums can map to TEXT (25)
45 #[derive(PartialEq, Debug, sqlx::Type)]
46 #[sqlx(type_name = "text")]
47 #[sqlx(rename_all = "lowercase")]
48 enum Strong {
49 One,
50 Two,
51
52 #[sqlx(rename = "four")]
53 Three,
54 }
55
56 // rename_all variants
57 #[derive(PartialEq, Debug, sqlx::Type)]
58 #[sqlx(type_name = "color_lower")]
59 #[sqlx(rename_all = "lowercase")]
60 enum ColorLower {
61 Red,
62 Green,
63 Blue,
64 }
65
66 #[derive(PartialEq, Debug, sqlx::Type)]
67 #[sqlx(type_name = "color_snake")]
68 #[sqlx(rename_all = "snake_case")]
69 enum ColorSnake {
70 RedGreen,
71 BlueBlack,
72 }
73
74 #[derive(PartialEq, Debug, sqlx::Type)]
75 #[sqlx(type_name = "color_upper")]
76 #[sqlx(rename_all = "UPPERCASE")]
77 enum ColorUpper {
78 Red,
79 Green,
80 Blue,
81 }
82
83 #[derive(PartialEq, Debug, sqlx::Type)]
84 #[sqlx(type_name = "color_screaming_snake")]
85 #[sqlx(rename_all = "SCREAMING_SNAKE_CASE")]
86 enum ColorScreamingSnake {
87 RedGreen,
88 BlueBlack,
89 }
90
91 #[derive(PartialEq, Debug, sqlx::Type)]
92 #[sqlx(type_name = "color_kebab_case")]
93 #[sqlx(rename_all = "kebab-case")]
94 enum ColorKebabCase {
95 RedGreen,
96 BlueBlack,
97 }
98
99 #[derive(PartialEq, Debug, sqlx::Type)]
100 #[sqlx(type_name = "color_mixed_case")]
101 #[sqlx(rename_all = "camelCase")]
102 enum ColorCamelCase {
103 RedGreen,
104 BlueBlack,
105 }
106
107 #[derive(PartialEq, Debug, sqlx::Type)]
108 #[sqlx(type_name = "color_camel_case")]
109 #[sqlx(rename_all = "PascalCase")]
110 enum ColorPascalCase {
111 RedGreen,
112 BlueBlack,
113 }
114
115 // "Strong" enum can map to a custom type
116 #[derive(PartialEq, Debug, sqlx::Type)]
117 #[sqlx(type_name = "mood")]
118 #[sqlx(rename_all = "lowercase")]
119 enum Mood {
120 Ok,
121 Happy,
122 Sad,
123 }
124
125 // Records must map to a custom type
126 // Note that all types are types in Postgres
127 #[derive(PartialEq, Debug, sqlx::Type)]
128 #[sqlx(type_name = "inventory_item")]
129 struct InventoryItem {
130 name: String,
131 supplier_id: Option<i32>,
132 price: Option<i64>,
133 }
134
135 // Custom range type
136 #[derive(sqlx::Type, Debug, PartialEq)]
137 #[sqlx(type_name = "float_range")]
138 struct FloatRange(PgRange<f64>);
139
140 // Custom domain type
141 #[derive(sqlx::Type, Debug)]
142 #[sqlx(type_name = "int4rangeL0pC")]
143 struct RangeInclusive(PgRange<i32>);
144
145 test_type!(transparent<Transparent>(Postgres,
146 "0" == Transparent(0),
147 "23523" == Transparent(23523)
148 ));
149
150 test_type!(transparent_array<TransparentArray>(Postgres,
151 "'{}'::int8[]" == TransparentArray(vec![]),
152 "'{ 23523, 123456, 789 }'::int8[]" == TransparentArray(vec![23523, 123456, 789])
153 ));
154
155 test_type!(weak_enum<Weak>(Postgres,
156 "0::int4" == Weak::One,
157 "2::int4" == Weak::Two,
158 "4::int4" == Weak::Three,
159 ));
160
161 test_type!(weak_enum_array<Vec<Weak>>(Postgres,
162 "'{0, 2, 4}'::int4[]" == vec![Weak::One, Weak::Two, Weak::Three],
163 ));
164
165 test_type!(strong_enum<Strong>(Postgres,
166 "'one'::text" == Strong::One,
167 "'two'::text" == Strong::Two,
168 "'four'::text" == Strong::Three,
169 ));
170
171 test_type!(strong_enum_array<Vec<Strong>>(Postgres,
172 "ARRAY['one', 'two', 'four']" == vec![Strong::One, Strong::Two, Strong::Three],
173 ));
174
175 test_type!(floatrange<FloatRange>(Postgres,
176 "'[1.234, 5.678]'::float_range" == FloatRange(PgRange::from((Bound::Included(1.234), Bound::Included(5.678)))),
177 ));
178
179 #[sqlx_macros::test]
180 async fn test_enum_type() -> anyhow::Result<()> {
181 let mut conn = new::<Postgres>().await?;
182
183 conn.execute(
184 r#"
185 DROP TABLE IF EXISTS people;
186
187 DROP TYPE IF EXISTS mood CASCADE;
188
189 CREATE TYPE mood AS ENUM ( 'ok', 'happy', 'sad' );
190
191 DROP TYPE IF EXISTS color_lower CASCADE;
192 DROP TYPE IF EXISTS color_snake CASCADE;
193 DROP TYPE IF EXISTS color_upper CASCADE;
194 DROP TYPE IF EXISTS color_screaming_snake CASCADE;
195 DROP TYPE IF EXISTS color_kebab_case CASCADE;
196 DROP TYPE IF EXISTS color_mixed_case CASCADE;
197 DROP TYPE IF EXISTS color_camel_case CASCADE;
198
199
200 CREATE TYPE color_lower AS ENUM ( 'red', 'green', 'blue' );
201 CREATE TYPE color_snake AS ENUM ( 'red_green', 'blue_black' );
202 CREATE TYPE color_upper AS ENUM ( 'RED', 'GREEN', 'BLUE' );
203 CREATE TYPE color_screaming_snake AS ENUM ( 'RED_GREEN', 'BLUE_BLACK' );
204 CREATE TYPE color_kebab_case AS ENUM ( 'red-green', 'blue-black' );
205 CREATE TYPE color_mixed_case AS ENUM ( 'redGreen', 'blueBlack' );
206 CREATE TYPE color_camel_case AS ENUM ( 'RedGreen', 'BlueBlack' );
207
208
209 CREATE TABLE people (
210 id serial PRIMARY KEY,
211 mood mood not null
212 );
213 "#,
214 )
215 .await?;
216
217 // Drop and re-acquire the connection
218 conn.close().await?;
219 let mut conn = new::<Postgres>().await?;
220
221 // Select from table test
222 let (people_id,): (i32,) = sqlx::query_as(
223 "
224 INSERT INTO people (mood)
225 VALUES ($1)
226 RETURNING id
227 ",
228 )
229 .bind(Mood::Sad)
230 .fetch_one(&mut conn)
231 .await?;
232
233 // Drop and re-acquire the connection
234 conn.close().await?;
235 let mut conn = new::<Postgres>().await?;
236
237 #[derive(sqlx::FromRow)]
238 struct PeopleRow {
239 id: i32,
240 mood: Mood,
241 }
242
243 let rec: PeopleRow = sqlx::query_as(
244 "
245 SELECT id, mood FROM people WHERE id = $1
246 ",
247 )
248 .bind(people_id)
249 .fetch_one(&mut conn)
250 .await?;
251
252 assert_eq!(rec.id, people_id);
253 assert_eq!(rec.mood, Mood::Sad);
254
255 // Drop and re-acquire the connection
256 conn.close().await?;
257 let mut conn = new::<Postgres>().await?;
258
259 let stmt = format!("SELECT id, mood FROM people WHERE id = {people_id}");
260 dbg!(&stmt);
261
262 let mut cursor = conn.fetch(&*stmt);
263
264 let row = cursor.try_next().await?.unwrap();
265 let rec = PeopleRow::from_row(&row)?;
266
267 assert_eq!(rec.id, people_id);
268 assert_eq!(rec.mood, Mood::Sad);
269
270 drop(cursor);
271
272 // Normal type equivalency test
273
274 let rec: (bool, Mood) = sqlx::query_as(
275 "
276 SELECT $1 = 'happy'::mood, $1
277 ",
278 )
279 .bind(&Mood::Happy)
280 .fetch_one(&mut conn)
281 .await?;
282
283 assert!(rec.0);
284 assert_eq!(rec.1, Mood::Happy);
285
286 let rec: (bool, ColorLower) = sqlx::query_as(
287 "
288 SELECT $1 = 'green'::color_lower, $1
289 ",
290 )
291 .bind(&ColorLower::Green)
292 .fetch_one(&mut conn)
293 .await?;
294
295 assert!(rec.0);
296 assert_eq!(rec.1, ColorLower::Green);
297
298 let rec: (bool, ColorSnake) = sqlx::query_as(
299 "
300 SELECT $1 = 'red_green'::color_snake, $1
301 ",
302 )
303 .bind(&ColorSnake::RedGreen)
304 .fetch_one(&mut conn)
305 .await?;
306
307 assert!(rec.0);
308 assert_eq!(rec.1, ColorSnake::RedGreen);
309
310 let rec: (bool, ColorUpper) = sqlx::query_as(
311 "
312 SELECT $1 = 'RED'::color_upper, $1
313 ",
314 )
315 .bind(&ColorUpper::Red)
316 .fetch_one(&mut conn)
317 .await?;
318
319 assert!(rec.0);
320 assert_eq!(rec.1, ColorUpper::Red);
321
322 let rec: (bool, ColorScreamingSnake) = sqlx::query_as(
323 "
324 SELECT $1 = 'RED_GREEN'::color_screaming_snake, $1
325 ",
326 )
327 .bind(&ColorScreamingSnake::RedGreen)
328 .fetch_one(&mut conn)
329 .await?;
330
331 assert!(rec.0);
332 assert_eq!(rec.1, ColorScreamingSnake::RedGreen);
333
334 let rec: (bool, ColorKebabCase) = sqlx::query_as(
335 "
336 SELECT $1 = 'red-green'::color_kebab_case, $1
337 ",
338 )
339 .bind(&ColorKebabCase::RedGreen)
340 .fetch_one(&mut conn)
341 .await?;
342
343 assert!(rec.0);
344 assert_eq!(rec.1, ColorKebabCase::RedGreen);
345
346 let rec: (bool, ColorCamelCase) = sqlx::query_as(
347 "
348 SELECT $1 = 'redGreen'::color_mixed_case, $1
349 ",
350 )
351 .bind(&ColorCamelCase::RedGreen)
352 .fetch_one(&mut conn)
353 .await?;
354
355 assert!(rec.0);
356 assert_eq!(rec.1, ColorCamelCase::RedGreen);
357
358 let rec: (bool, ColorPascalCase) = sqlx::query_as(
359 "
360 SELECT $1 = 'RedGreen'::color_camel_case, $1
361 ",
362 )
363 .bind(&ColorPascalCase::RedGreen)
364 .fetch_one(&mut conn)
365 .await?;
366
367 assert!(rec.0);
368 assert_eq!(rec.1, ColorPascalCase::RedGreen);
369
370 Ok(())
371 }
372
373 #[sqlx_macros::test]
374 async fn test_record_type() -> anyhow::Result<()> {
375 let mut conn = new::<Postgres>().await?;
376
377 let value = InventoryItem {
378 name: "fuzzy dice".to_owned(),
379 supplier_id: Some(42),
380 price: Some(199),
381 };
382
383 let rec: (bool, InventoryItem) = sqlx::query_as(
384 "
385 SELECT $1 = ROW('fuzzy dice', 42, 199)::inventory_item, $1
386 ",
387 )
388 .bind(&value)
389 .fetch_one(&mut conn)
390 .await?;
391
392 assert!(rec.0);
393 assert_eq!(rec.1, value);
394
395 Ok(())
396 }
397
398 #[cfg(feature = "macros")]
399 #[sqlx_macros::test]
400 async fn test_new_type() {
401 struct NewType(i32);
402
403 impl From<i32> for NewType {
404 fn from(value: i32) -> Self {
405 NewType(value)
406 }
407 }
408
409 let mut conn = new::<Postgres>().await.unwrap();
410
411 struct NewTypeRow {
412 id: NewType,
413 }
414
415 let res = sqlx::query_as!(NewTypeRow, r#"SELECT 1 as "id!""#)
416 .fetch_one(&mut conn)
417 .await
418 .unwrap();
419 assert_eq!(res.id.0, 1);
420
421 struct NormalRow {
422 id: i32,
423 }
424
425 let res = sqlx::query_as!(NormalRow, r#"SELECT 1 as "id!""#)
426 .fetch_one(&mut conn)
427 .await
428 .unwrap();
429
430 assert_eq!(res.id, 1);
431 }
432
433 #[cfg(feature = "macros")]
434 #[sqlx_macros::test]
435 async fn test_from_row() -> anyhow::Result<()> {
436 let mut conn = new::<Postgres>().await?;
437
438 #[derive(sqlx::FromRow)]
439 struct Account {
440 id: i32,
441 name: String,
442 }
443
444 let account: Account = sqlx::query_as(
445 "SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
446 )
447 .bind(1_i32)
448 .fetch_one(&mut conn)
449 .await?;
450
451 assert_eq!(account.id, 1);
452 assert_eq!(account.name, "Herp Derpinson");
453
454 // A _single_ lifetime may be used but only when using the lowest-level API currently (Query::fetch)
455
456 #[derive(sqlx::FromRow)]
457 struct RefAccount<'a> {
458 id: i32,
459 name: &'a str,
460 }
461
462 let mut cursor = sqlx::query(
463 "SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
464 )
465 .bind(1_i32)
466 .fetch(&mut conn);
467
468 let row = cursor.try_next().await?.unwrap();
469 let account = RefAccount::from_row(&row)?;
470
471 assert_eq!(account.id, 1);
472 assert_eq!(account.name, "Herp Derpinson");
473
474 Ok(())
475 }
476
477 #[cfg(feature = "macros")]
478 #[sqlx_macros::test]
479 async fn test_from_row_with_keyword() -> anyhow::Result<()> {
480 #[derive(Debug, sqlx::FromRow)]
481 struct AccountKeyword {
482 r#type: i32,
483 r#static: String,
484 r#let: Option<String>,
485 r#struct: Option<String>,
486 name: Option<String>,
487 }
488
489 let mut conn = new::<Postgres>().await?;
490
491 let account: AccountKeyword = sqlx::query_as(
492 r#"SELECT * from (VALUES (1, 'foo', 'bar', null, null)) accounts(type, static, let, struct, name)"#
493 )
494 .fetch_one(&mut conn)
495 .await?;
496 println!("{account:?}");
497
498 assert_eq!(1, account.r#type);
499 assert_eq!("foo", account.r#static);
500 assert_eq!(None, account.r#struct);
501 assert_eq!(Some("bar".to_owned()), account.r#let);
502 assert_eq!(None, account.name);
503
504 Ok(())
505 }
506
507 #[cfg(feature = "macros")]
508 #[sqlx_macros::test]
509 async fn test_from_row_with_rename() -> anyhow::Result<()> {
510 #[derive(Debug, sqlx::FromRow)]
511 struct AccountKeyword {
512 #[sqlx(rename = "type")]
513 own_type: i32,
514
515 #[sqlx(rename = "static")]
516 my_static: String,
517
518 #[sqlx(rename = "let")]
519 custom_let: Option<String>,
520
521 #[sqlx(rename = "struct")]
522 def_struct: Option<String>,
523
524 name: Option<String>,
525 }
526
527 let mut conn = new::<Postgres>().await?;
528
529 let account: AccountKeyword = sqlx::query_as(
530 r#"SELECT * from (VALUES (1, 'foo', 'bar', null, null)) accounts(type, static, let, struct, name)"#
531 )
532 .fetch_one(&mut conn)
533 .await?;
534 println!("{account:?}");
535
536 assert_eq!(1, account.own_type);
537 assert_eq!("foo", account.my_static);
538 assert_eq!(None, account.def_struct);
539 assert_eq!(Some("bar".to_owned()), account.custom_let);
540 assert_eq!(None, account.name);
541
542 Ok(())
543 }
544
545 #[cfg(feature = "macros")]
546 #[sqlx_macros::test]
547 async fn test_from_row_with_rename_all() -> anyhow::Result<()> {
548 #[derive(Debug, sqlx::FromRow)]
549 #[sqlx(rename_all = "camelCase")]
550 struct AccountKeyword {
551 user_id: i32,
552 user_name: String,
553 user_surname: String,
554 }
555
556 let mut conn = new::<Postgres>().await?;
557
558 let account: AccountKeyword = sqlx::query_as(
559 r#"SELECT * from (VALUES (1, 'foo', 'bar')) accounts("userId", "userName", "userSurname")"#,
560 )
561 .fetch_one(&mut conn)
562 .await?;
563 println!("{account:?}");
564
565 assert_eq!(1, account.user_id);
566 assert_eq!("foo", account.user_name);
567 assert_eq!("bar", account.user_surname);
568
569 Ok(())
570 }
571
572 #[cfg(feature = "macros")]
573 #[sqlx_macros::test]
574 async fn test_from_row_tuple() -> anyhow::Result<()> {
575 let mut conn = new::<Postgres>().await?;
576
577 #[derive(Debug, sqlx::FromRow)]
578 struct Account(i32, String);
579
580 let account: Account = sqlx::query_as(
581 "SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
582 )
583 .bind(1_i32)
584 .fetch_one(&mut conn)
585 .await?;
586
587 assert_eq!(account.0, 1);
588 assert_eq!(account.1, "Herp Derpinson");
589
590 // A _single_ lifetime may be used but only when using the lowest-level API currently (Query::fetch)
591
592 #[derive(sqlx::FromRow)]
593 struct RefAccount<'a>(i32, &'a str);
594
595 let mut cursor = sqlx::query(
596 "SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
597 )
598 .bind(1_i32)
599 .fetch(&mut conn);
600
601 let row = cursor.try_next().await?.unwrap();
602 let account = RefAccount::from_row(&row)?;
603
604 assert_eq!(account.0, 1);
605 assert_eq!(account.1, "Herp Derpinson");
606
607 Ok(())
608 }
609
610 #[cfg(feature = "macros")]
611 #[sqlx_macros::test]
612 async fn test_default() -> anyhow::Result<()> {
613 #[derive(Debug, sqlx::FromRow)]
614 struct HasDefault {
615 not_default: i32,
616 #[sqlx(default)]
617 default: Option<i32>,
618 }
619
620 let mut conn = new::<Postgres>().await?;
621
622 let has_default: HasDefault = sqlx::query_as(r#"SELECT 1 AS not_default"#)
623 .fetch_one(&mut conn)
624 .await?;
625 println!("{has_default:?}");
626
627 assert_eq!(has_default.not_default, 1);
628 assert_eq!(has_default.default, None);
629
630 Ok(())
631 }
632
633 #[cfg(feature = "macros")]
634 #[sqlx_macros::test]
635 async fn test_struct_default() -> anyhow::Result<()> {
636 #[derive(Debug, sqlx::FromRow)]
637 #[sqlx(default)]
638 struct HasDefault {
639 not_default: Option<i32>,
640 default_a: Option<String>,
641 default_b: Option<i32>,
642 }
643
644 impl Default for HasDefault {
645 fn default() -> HasDefault {
646 HasDefault {
647 not_default: None,
648 default_a: None,
649 default_b: Some(0),
650 }
651 }
652 }
653
654 let mut conn = new::<Postgres>().await?;
655
656 let has_default: HasDefault = sqlx::query_as(r#"SELECT 1 AS not_default"#)
657 .fetch_one(&mut conn)
658 .await?;
659 println!("{has_default:?}");
660
661 assert_eq!(has_default.not_default, Some(1));
662 assert_eq!(has_default.default_a, None);
663 assert_eq!(has_default.default_b, Some(0));
664
665 Ok(())
666 }
667
668 #[cfg(feature = "macros")]
669 #[sqlx_macros::test]
670 async fn test_flatten() -> anyhow::Result<()> {
671 #[derive(Debug, Default, sqlx::FromRow)]
672 struct AccountDefault {
673 default: Option<i32>,
674 }
675
676 #[derive(Debug, sqlx::FromRow)]
677 struct UserInfo {
678 name: String,
679 surname: String,
680 }
681
682 #[derive(Debug, sqlx::FromRow)]
683 struct AccountKeyword {
684 id: i32,
685 #[sqlx(flatten)]
686 info: UserInfo,
687 #[sqlx(default)]
688 #[sqlx(flatten)]
689 default: AccountDefault,
690 }
691
692 let mut conn = new::<Postgres>().await?;
693
694 let account: AccountKeyword = sqlx::query_as(
695 r#"SELECT * from (VALUES (1, 'foo', 'bar')) accounts("id", "name", "surname")"#,
696 )
697 .fetch_one(&mut conn)
698 .await?;
699 println!("{account:?}");
700
701 assert_eq!(1, account.id);
702 assert_eq!("foo", account.info.name);
703 assert_eq!("bar", account.info.surname);
704 assert_eq!(None, account.default.default);
705
706 Ok(())
707 }
708
709 #[cfg(feature = "macros")]
710 #[sqlx_macros::test]
711 async fn test_skip() -> anyhow::Result<()> {
712 #[derive(Debug, Default, sqlx::FromRow)]
713 struct AccountDefault {
714 default: Option<i32>,
715 }
716
717 #[derive(Debug, sqlx::FromRow)]
718 struct AccountKeyword {
719 id: i32,
720 #[sqlx(skip)]
721 default: AccountDefault,
722 }
723
724 let mut conn = new::<Postgres>().await?;
725
726 let account: AccountKeyword = sqlx::query_as(r#"SELECT * from (VALUES (1)) accounts("id")"#)
727 .fetch_one(&mut conn)
728 .await?;
729 println!("{account:?}");
730
731 assert_eq!(1, account.id);
732 assert_eq!(None, account.default.default);
733
734 Ok(())
735 }
736
737 #[cfg(feature = "macros")]
738 #[sqlx_macros::test]
739 async fn test_enum_with_schema() -> anyhow::Result<()> {
740 #[derive(Debug, PartialEq, Eq, sqlx::Type)]
741 #[sqlx(type_name = "foo.\"Foo\"")]
742 enum Foo {
743 Bar,
744 Baz,
745 }
746
747 let mut conn = new::<Postgres>().await?;
748
749 let foo: Foo = sqlx::query_scalar("SELECT $1::foo.\"Foo\"")
750 .bind(Foo::Bar)
751 .fetch_one(&mut conn)
752 .await?;
753
754 assert_eq!(foo, Foo::Bar);
755
756 let foo: Foo = sqlx::query_scalar("SELECT $1::foo.\"Foo\"")
757 .bind(Foo::Baz)
758 .fetch_one(&mut conn)
759 .await?;
760
761 assert_eq!(foo, Foo::Baz);
762
763 let foos: Vec<Foo> = sqlx::query_scalar("SELECT ARRAY[$1::foo.\"Foo\", $2::foo.\"Foo\"]")
764 .bind(Foo::Bar)
765 .bind(Foo::Baz)
766 .fetch_one(&mut conn)
767 .await?;
768
769 assert_eq!(foos, [Foo::Bar, Foo::Baz]);
770
771 Ok(())
772 }
773
774 #[cfg(feature = "macros")]
775 #[sqlx_macros::test]
776 async fn test_from_row_hygiene() -> anyhow::Result<()> {
777 // A field named `row` previously would shadow the `row` parameter of `FromRow::from_row()`:
778 // https://github.com/launchbadge/sqlx/issues/3344
779 #[derive(Debug, sqlx::FromRow)]
780 pub struct Foo {
781 pub row: i32,
782 pub bar: i32,
783 }
784
785 let mut conn = new::<Postgres>().await?;
786
787 let foo: Foo = sqlx::query_as("SELECT 1234 as row, 5678 as bar")
788 .fetch_one(&mut conn)
789 .await?;
790
791 assert_eq!(foo.row, 1234);
792 assert_eq!(foo.bar, 5678);
793
794 Ok(())
795 }
796
797 #[sqlx_macros::test]
798 async fn test_custom_pg_array() -> anyhow::Result<()> {
799 #[derive(sqlx::Type)]
800 #[sqlx(no_pg_array)]
801 pub struct User {
802 pub id: i32,
803 pub username: String,
804 }
805
806 impl PgHasArrayType for User {
807 fn array_type_info() -> sqlx::postgres::PgTypeInfo {
808 sqlx::postgres::PgTypeInfo::array_of("Gebruiker")
809 }
810 }
811 Ok(())
812 }