Repository navigation
Expand file tree
/
Copy pathpostgres-macros.rs
More file actions
194 lines (151 loc) · 4.92 KB
/
Copy pathpostgres-macros.rs
File metadata and controls
194 lines (151 loc) · 4.92 KB
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
use sqlx::{Connect, PgConnection};
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_query() -> anyhow::Result<()> {
let mut conn = connect().await?;
let account = sqlx::query!(
"SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1",
1i32
)
.fetch_one(&mut conn)
.await?;
println!("account ID: {:?}", account.id);
Ok(())
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_no_result() -> anyhow::Result<()> {
let mut conn = connect().await?;
let _ = sqlx::query!("DELETE FROM pg_enum")
.execute(&mut conn)
.await?;
Ok(())
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn _file() -> anyhow::Result<()> {
let mut conn = connect().await?;
let account = sqlx::query_file!("tests/test-query.sql",)
.fetch_one(&mut conn)
.await?;
println!("{:?}", account);
Ok(())
}
#[derive(Debug)]
struct Account {
id: i32,
name: Option<String>,
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_query_as() -> anyhow::Result<()> {
let mut conn = connect().await?;
let name: Option<&str> = None;
let account = sqlx::query_as!(
Account,
"SELECT * from (VALUES (1, $1)) accounts(id, name)",
name
)
.fetch_one(&mut conn)
.await?;
assert_eq!(None, account.name);
println!("{:?}", account);
Ok(())
}
#[derive(Debug)]
struct RawAccount {
r#type: i32,
name: Option<String>,
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_query_as_raw() -> anyhow::Result<()> {
let mut conn = connect().await?;
let account = sqlx::query_as!(
RawAccount,
"SELECT * 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(())
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_query_file_as() -> anyhow::Result<()> {
let mut conn = connect().await?;
let account = sqlx::query_file_as!(Account, "tests/test-query.sql",)
.fetch_one(&mut conn)
.await?;
println!("{:?}", account);
Ok(())
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn query_by_string() -> anyhow::Result<()> {
let mut conn = connect().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, string);
Ok(())
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_nullable_err() -> anyhow::Result<()> {
#[derive(Debug)]
struct Account {
id: i32,
name: String,
}
let mut conn = connect().await?;
let err = sqlx::query_as!(
Account,
"SELECT * from (VALUES (1, null::text)) accounts(id, name)"
)
.fetch_one(&mut conn)
.await
.unwrap_err();
if let sqlx::Error::Decode(err) = &err {
if let Some(sqlx::error::UnexpectedNullError) = err.downcast_ref() {
return Ok(());
}
}
panic!("expected `UnexpectedNullError`, got {}", err)
}
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
async fn test_many_args() -> anyhow::Result<()> {
let mut conn = connect().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!(i as i32, row.id);
}
Ok(())
}
async fn connect() -> anyhow::Result<PgConnection> {
let _ = dotenv::dotenv();
let _ = env_logger::try_init();
Ok(PgConnection::connect(dotenv::var("DATABASE_URL")?).await?)
}