forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.rs
678 lines (500 loc) · 17.1 KB
/
macros.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
use sqlx::{Connection, PgConnection, Postgres, Transaction};
use sqlx_postgres::types::PgHstore;
use sqlx_test::new;
use futures::TryStreamExt;
#[sqlx_macros::test]
async fn test_query() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let account = sqlx::query!(
"SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
1i32
)
.fetch_one(&mut conn)
.await?;
assert_eq!(account.id, Some(1));
assert_eq!(account.name.as_deref(), Some("Herp Derpinson"));
Ok(())
}
#[sqlx_macros::test]
async fn test_query_tuple() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let account = sqlx::query_tuple!(
"SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
1i32
)
.fetch_one(&mut conn)
.await?;
assert_eq!(account.0, Some(1));
assert_eq!(account.1.as_deref(), Some("Herp Derpinson"));
Ok(())
}
#[sqlx_macros::test]
async fn test_non_null() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut tx = conn.begin().await?;
let _ = sqlx::query!("INSERT INTO tweet (text) VALUES ('Hello')")
.execute(&mut *tx)
.await?;
let row = sqlx::query!("SELECT id, text, owner_id FROM tweet LIMIT 1")
.fetch_one(&mut *tx)
.await?;
assert!(row.id > 0);
assert_eq!(row.text, "Hello");
assert_eq!(row.owner_id, None);
// let the transaction rollback so we don't actually insert the tweet
Ok(())
}
#[sqlx_macros::test]
async fn test_no_result() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut tx = conn.begin().await?;
let _ = sqlx::query!("DELETE FROM tweet").execute(&mut *tx).await?;
// let the transaction rollback so we don't actually delete the tweets
Ok(())
}
#[sqlx_macros::test]
async fn test_text_var_char_char_n() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
// TEXT
// we cannot infer nullability from an expression
let rec = sqlx::query!("SELECT 'Hello'::text as greeting")
.fetch_one(&mut conn)
.await?;
assert_eq!(rec.greeting.as_deref(), Some("Hello"));
// VARCHAR(N)
let rec = sqlx::query!("SELECT 'Hello'::varchar(5) as greeting")
.fetch_one(&mut conn)
.await?;
assert_eq!(rec.greeting.as_deref(), Some("Hello"));
// CHAR(N)
let rec = sqlx::query!("SELECT 'Hello'::char(5) as greeting")
.fetch_one(&mut conn)
.await?;
assert_eq!(rec.greeting.as_deref(), Some("Hello"));
Ok(())
}
#[sqlx_macros::test]
async fn test_void() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let _ = sqlx::query!(r#"select pg_notify('chan', 'message')"#)
.execute(&mut conn)
.await?;
Ok(())
}
#[sqlx_macros::test]
async fn test_call_procedure() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let row = sqlx::query!(r#"CALL forty_two(null)"#)
.fetch_one(&mut conn)
.await?;
assert_eq!(row.forty_two, Some(42));
Ok(())
}
#[sqlx_macros::test]
async fn test_query_file() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
// keep trailing comma as a test
let account = sqlx::query_file!("tests/postgres/test-query.sql")
.fetch_one(&mut conn)
.await?;
assert_eq!(account.id, 1);
assert_eq!(account.name, Option::<String>::None);
Ok(())
}
#[derive(Debug)]
struct Account {
id: i32,
name: Option<String>,
}
#[sqlx_macros::test]
async fn test_query_as() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let name: Option<&str> = None;
let account = sqlx::query_as!(
Account,
r#"SELECT id "id!", name from (VALUES (1, $1)) accounts(id, name)"#,
name
)
.fetch_one(&mut conn)
.await?;
assert_eq!(1, account.id);
assert_eq!(None, account.name);
println!("{account:?}");
Ok(())
}
#[derive(Debug)]
struct RawAccount {
r#type: i32,
name: Option<String>,
}
#[sqlx_macros::test]
async fn test_query_as_raw() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let account = sqlx::query_as!(
RawAccount,
r#"SELECT type "type!", name from (VALUES (1, null)) accounts(type, name)"#
)
.fetch_one(&mut conn)
.await?;
assert_eq!(None, account.name);
assert_eq!(1, account.r#type);
println!("{account:?}");
Ok(())
}
#[sqlx_macros::test]
async fn test_query_file_as() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let account = sqlx::query_file_as!(Account, "tests/postgres/test-query.sql")
.fetch_one(&mut conn)
.await?;
println!("{account:?}");
Ok(())
}
#[sqlx_macros::test]
async fn test_query_scalar() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let id = sqlx::query_scalar!("select 1").fetch_one(&mut conn).await?;
// nullability inference can't handle expressions
assert_eq!(id, Some(1i32));
// invalid column names are ignored
let id = sqlx::query_scalar!(r#"select 1 as "&foo""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, Some(1i32));
let id = sqlx::query_scalar!(r#"select 1 as "foo!""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, 1i32);
let id = sqlx::query_scalar!(r#"select 1 as "foo?""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, Some(1i32));
let id = sqlx::query_scalar!(r#"select 1 as "foo: MyInt4""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, Some(MyInt4(1i32)));
let id = sqlx::query_scalar!(r#"select 1 as "foo?: MyInt4""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, Some(MyInt4(1i32)));
let id = sqlx::query_scalar!(r#"select 1 as "foo!: MyInt4""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, MyInt4(1i32));
let id: MyInt4 = sqlx::query_scalar!(r#"select 1 as "foo: _""#)
.fetch_one(&mut conn)
.await?
// don't hint that it should be `Option<MyInt4>`
.unwrap();
assert_eq!(id, MyInt4(1i32));
let id: MyInt4 = sqlx::query_scalar!(r#"select 1 as "foo?: _""#)
.fetch_one(&mut conn)
.await?
// don't hint that it should be `Option<MyInt4>`
.unwrap();
assert_eq!(id, MyInt4(1i32));
let id: MyInt4 = sqlx::query_scalar!(r#"select 1 as "foo!: _""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(id, MyInt4(1i32));
Ok(())
}
#[sqlx_macros::test]
async fn query_by_string() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let string = "Hello, world!".to_string();
let ref tuple = ("Hello, world!".to_string(),);
let result = sqlx::query!(
"SELECT * from (VALUES('Hello, world!')) strings(string)\
where string in ($1, $2, $3, $4, $5, $6, $7)",
string, // make sure we don't actually take ownership here
&string[..],
Some(&string),
Some(&string[..]),
Option::<String>::None,
string.clone(),
tuple.0 // make sure we're not trying to move out of a field expression
)
.fetch_one(&mut conn)
.await?;
assert_eq!(result.string, Some(string));
Ok(())
}
#[sqlx_macros::test]
#[cfg(feature = "bigdecimal")]
async fn query_by_bigdecimal() -> anyhow::Result<()> {
use sqlx::types::BigDecimal;
let mut conn = new::<Postgres>().await?;
// this tests querying by a non-`Copy` type that doesn't have special reborrow semantics
let decimal = "1234".parse::<BigDecimal>()?;
let ref tuple = ("51245.121232".parse::<BigDecimal>()?,);
let result = sqlx::query!(
"SELECT * from (VALUES(1234.0)) decimals(decimal)\
where decimal in ($1, $2, $3, $4, $5, $6, $7)",
decimal, // make sure we don't actually take ownership here
&decimal, // allow query-by-reference
Some(&decimal),
Some(&decimal),
Option::<BigDecimal>::None,
decimal.clone(),
tuple.0 // make sure we're not trying to move out of a field expression
)
.fetch_one(&mut conn)
.await?;
assert_eq!(result.decimal, Some(decimal));
Ok(())
}
#[sqlx_macros::test]
async fn test_nullable_err() -> anyhow::Result<()> {
#[allow(dead_code)]
#[derive(Debug)]
struct Account {
id: i32,
name: String,
}
let mut conn = new::<Postgres>().await?;
let err = sqlx::query_as!(
Account,
r#"SELECT id "id!", name "name!" from (VALUES (1, null::text)) accounts(id, name)"#
)
.fetch_one(&mut conn)
.await
.unwrap_err();
if let sqlx::Error::ColumnDecode { source, .. } = &err {
if let Some(sqlx::error::UnexpectedNullError) = source.downcast_ref() {
return Ok(());
}
}
panic!("expected `UnexpectedNullError`, got {err}")
}
#[sqlx_macros::test]
async fn test_many_args() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
// previous implementation would only have supported 10 bind parameters
// (this is really gross to test in MySQL)
let rows = sqlx::query!(
"SELECT * from unnest(array[$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12]::int[]) ids(id);",
0i32, 1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32, 8i32, 9i32, 10i32, 11i32
)
.fetch_all(&mut conn)
.await?;
for (i, row) in rows.iter().enumerate() {
assert_eq!(Some(i as i32), row.id);
}
Ok(())
}
#[sqlx_macros::test]
async fn test_array_from_slice() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let list: &[i32] = &[1, 2, 3, 4i32];
let result = sqlx::query!("SELECT $1::int[] as my_array", list)
.fetch_one(&mut conn)
.await?;
assert_eq!(result.my_array, Some(vec![1, 2, 3, 4]));
println!("result ID: {:?}", result.my_array);
let account = sqlx::query!("SELECT ARRAY[4,3,2,1] as my_array")
.fetch_one(&mut conn)
.await?;
assert_eq!(account.my_array, Some(vec![4, 3, 2, 1]));
println!("account ID: {:?}", account.my_array);
Ok(())
}
#[sqlx_macros::test]
async fn fetch_is_usable_issue_224() -> anyhow::Result<()> {
// ensures that the stream returned by `query::Map::fetch()` is usable with `TryStreamExt`
let mut conn = new::<Postgres>().await?;
let mut stream =
sqlx::query!("select * from generate_series(1, 3) as series(num);").fetch(&mut conn);
// `num` is generated by a function so we can't assume it's non-null
assert_eq!(stream.try_next().await?.unwrap().num, Some(1));
assert_eq!(stream.try_next().await?.unwrap().num, Some(2));
assert_eq!(stream.try_next().await?.unwrap().num, Some(3));
assert!(stream.try_next().await?.is_none());
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_not_null() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let record = sqlx::query!(r#"select 1 as "id!""#)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, 1);
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_nullable() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
// workaround for https://github.com/launchbadge/sqlx/issues/367
// declare a `NOT NULL` column from a left-joined table to be nullable
let record = sqlx::query!(
r#"select text as "text?" from (values (1)) foo(id) left join tweet on false"#
)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.text, None);
Ok(())
}
async fn with_test_row<'a>(
conn: &'a mut PgConnection,
) -> anyhow::Result<Transaction<'a, Postgres>> {
let mut transaction = conn.begin().await?;
sqlx::query!("INSERT INTO tweet(id, text, owner_id) VALUES (1, '#sqlx is pretty cool!', 1)")
.execute(&mut *transaction)
.await?;
Ok(transaction)
}
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
#[sqlx(transparent)]
struct MyInt(i64);
#[derive(PartialEq, Eq, Debug, sqlx::Type)]
#[sqlx(transparent)]
struct MyInt4(i32);
struct Record {
id: MyInt,
}
struct OptionalRecord {
id: Option<MyInt>,
}
#[sqlx_macros::test]
async fn test_column_override_wildcard() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut conn = with_test_row(&mut conn).await?;
let record = sqlx::query_as!(Record, r#"select id as "id: _" from tweet"#)
.fetch_one(&mut *conn)
.await?;
assert_eq!(record.id, MyInt(1));
let record = sqlx::query_as!(OptionalRecord, r#"select owner_id as "id: _" from tweet"#)
.fetch_one(&mut *conn)
.await?;
assert_eq!(record.id, Some(MyInt(1)));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_wildcard_not_null() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut conn = with_test_row(&mut conn).await?;
let record = sqlx::query_as!(Record, r#"select owner_id as "id!: _" from tweet"#)
.fetch_one(&mut *conn)
.await?;
assert_eq!(record.id, MyInt(1));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_wildcard_nullable() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut conn = with_test_row(&mut conn).await?;
let record = sqlx::query_as!(OptionalRecord, r#"select id as "id?: _" from tweet"#)
.fetch_one(&mut *conn)
.await?;
assert_eq!(record.id, Some(MyInt(1)));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_exact() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut conn = with_test_row(&mut conn).await?;
let record = sqlx::query!(r#"select id as "id: MyInt" from tweet"#)
.fetch_one(&mut *conn)
.await?;
assert_eq!(record.id, MyInt(1));
let record = sqlx::query!(r#"select owner_id as "id: MyInt" from tweet"#)
.fetch_one(&mut *conn)
.await?;
assert_eq!(record.id, Some(MyInt(1)));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_exact_not_null() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut conn = with_test_row(&mut conn).await?;
let record = sqlx::query!(r#"select owner_id as "id!: MyInt" from tweet"#)
.fetch_one(&mut *conn)
.await?;
assert_eq!(record.id, MyInt(1));
Ok(())
}
#[sqlx_macros::test]
async fn test_column_override_exact_nullable() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut conn = with_test_row(&mut conn).await?;
let record = sqlx::query!(r#"select id as "id?: MyInt" from tweet"#)
.fetch_one(&mut *conn)
.await?;
assert_eq!(record.id, Some(MyInt(1)));
Ok(())
}
#[sqlx_macros::test]
async fn test_bind_arg_override_exact() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let my_int = MyInt4(1);
// this query should require a bind parameter override as we would otherwise expect the bind
// to be the same type
let record = sqlx::query!(
"select * from (select 1::int4) records(id) where id = $1",
my_int as MyInt4
)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, Some(1i32));
// test that we're actually emitting the typecast by requiring the bound type to be the same
let record = sqlx::query!("select $1::int8 as id", 1i32 as i64)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, Some(1i64));
// test the override with `Option`
let my_opt_int = Some(MyInt4(1));
let record = sqlx::query!(
"select * from (select 1::int4) records(id) where id = $1",
my_opt_int as Option<MyInt4>
)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, Some(1i32));
Ok(())
}
#[sqlx_macros::test]
async fn test_bind_arg_override_wildcard() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let my_int = MyInt4(1);
let record = sqlx::query!(
"select * from (select 1::int4) records(id) where id = $1",
my_int as _
)
.fetch_one(&mut conn)
.await?;
assert_eq!(record.id, Some(1i32));
Ok(())
}
#[sqlx_macros::test]
async fn test_to_from_citext() -> anyhow::Result<()> {
// Ensure that the macros consider `CITEXT` to be compatible with `String` and friends
let mut conn = new::<Postgres>().await?;
let mut tx = conn.begin().await?;
let foo_in = "Hello, world!";
sqlx::query!("insert into test_citext(foo) values ($1)", foo_in)
.execute(&mut *tx)
.await?;
let foo_out: String = sqlx::query_scalar!("select foo from test_citext")
.fetch_one(&mut *tx)
.await?;
assert_eq!(foo_in, foo_out);
tx.rollback().await?;
Ok(())
}
#[sqlx_macros::test]
async fn pghstore_tests() -> anyhow::Result<()> {
let mut conn = new::<Postgres>().await?;
let mut store = PgHstore::default();
let stores = vec![store.clone(), store.clone()];
store.insert("key".into(), Some("value".to_string()));
sqlx::query!(" insert into mytable(f) values ($1)", store)
.execute(&mut conn)
.await?;
sqlx::query!(
" insert into mytable(f) select * from unnest($1::hstore[])",
&stores
)
.execute(&mut conn)
.await?;
Ok(())
}