forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathany.rs
More file actions
359 lines (298 loc) · 10.6 KB
/
any.rs
File metadata and controls
359 lines (298 loc) · 10.6 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
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
use sqlx_oldapi::any::AnyRow;
use sqlx_oldapi::{Any, Column, Connection, Decode, Executor, Row, Statement, Type};
use sqlx_test::new;
async fn get_val<T>(expr: &str) -> anyhow::Result<T>
where
for<'r> T: Decode<'r, Any> + Type<Any> + std::marker::Unpin + std::marker::Send + 'static,
{
let mut conn = new::<Any>().await?;
let val = sqlx_oldapi::query(&format!("select {}", expr))
.try_map(|row: AnyRow| row.try_get::<T, _>(0))
.fetch_one(&mut conn)
.await?;
conn.close().await?;
Ok(val)
}
#[sqlx_macros::test]
async fn it_has_all_the_types() -> anyhow::Result<()> {
assert_eq!(6, get_val::<i32>("5 + 1").await?);
assert_eq!(1234567890123, get_val::<i64>("1234567890123").await?);
assert_eq!(
"hello world".to_owned(),
get_val::<String>("'hello world'").await?
);
assert_eq!(None, get_val::<Option<i32>>("NULL").await?);
assert_eq!(1e-40, get_val::<f64>("1e-40").await?);
assert_eq!(12.5f64, get_val::<f64>("CAST(12.5 AS DECIMAL(9,2))").await?);
assert_eq!(
125125.125f64,
get_val::<f64>("CAST(125125.125 AS DECIMAL(10,3))").await?
);
assert_eq!(
-1234567890.125,
get_val::<f64>("CAST(-1234567890.125 AS DECIMAL(15,3))").await?
);
Ok(())
}
#[cfg(feature = "chrono")]
#[sqlx_macros::test]
async fn it_has_chrono() -> anyhow::Result<()> {
use sqlx_oldapi::types::chrono::NaiveDate;
let mut conn = crate::new::<sqlx_oldapi::Any>().await?;
let dbms_name = conn.dbms_name().await.unwrap_or_default();
let sql_date = if dbms_name.to_lowercase().contains("sqlite") {
"'2020-01-02'"
} else {
"CAST('2020-01-02' AS DATE)"
};
let expected_date = NaiveDate::from_ymd_opt(2020, 1, 2).unwrap();
let actual = conn.fetch_one(&*format!("SELECT {}", sql_date)).await?;
assert_eq!(expected_date, actual.try_get::<NaiveDate, _>(0)?);
Ok(())
}
#[cfg(feature = "chrono")]
#[sqlx_macros::test]
async fn it_has_chrono_fixed_offset() -> anyhow::Result<()> {
use sqlx_oldapi::types::chrono::{DateTime, FixedOffset};
assert_eq!(
DateTime::<FixedOffset>::parse_from_rfc3339("2020-01-02T12:00:00+02:00").unwrap(),
get_val::<DateTime<FixedOffset>>(if cfg!(feature = "sqlite") {
"'2020-01-02 12:00:00+02:00'"
} else if cfg!(feature = "mssql") {
"CAST('2020-01-02 12:00:00+02:00' AS DATETIMEOFFSET)"
} else if cfg!(feature = "postgres") {
"'2020-01-02 12:00:00+02:00'::timestamptz"
} else {
eprintln!("DBMS not supported");
return Ok(());
})
.await?
);
Ok(())
}
#[cfg(feature = "bigdecimal")]
#[sqlx_macros::test]
async fn it_has_bigdecimal() -> anyhow::Result<()> {
use sqlx_oldapi::types::BigDecimal;
use std::str::FromStr;
assert_eq!(
BigDecimal::from_str("1234567.25")?,
get_val::<BigDecimal>("CAST('1234567.25' AS DECIMAL(9,2))").await?
);
Ok(())
}
#[cfg(feature = "decimal")]
#[sqlx_macros::test]
async fn it_has_decimal() -> anyhow::Result<()> {
use sqlx_oldapi::types::Decimal;
use std::str::FromStr;
assert_eq!(
Decimal::from_str("1234567.25")?,
get_val::<Decimal>("CAST('1234567.25' AS DECIMAL(9,2))").await?
);
Ok(())
}
#[cfg(feature = "json")]
#[sqlx_macros::test]
async fn it_has_json() -> anyhow::Result<()> {
use serde_json::json;
let databases_without_json = ["sqlite", "microsoft sql server", "snowflake"];
let mut conn = crate::new::<sqlx_oldapi::Any>().await?;
let dbms_name = conn.dbms_name().await.unwrap_or_default();
let json_sql = if databases_without_json.contains(&dbms_name.to_lowercase().as_str()) {
"select '{\"foo\": \"bar\"}'"
} else {
"select CAST('{\"foo\": \"bar\"}' AS JSON)"
};
let expected_json = json!({"foo": "bar"});
let actual = conn
.fetch_one(json_sql)
.await?
.try_get::<serde_json::Value, _>(0)?;
assert_eq!(expected_json, actual, "Json value for {}", json_sql);
Ok(())
}
#[cfg(feature = "uuid")]
#[sqlx_macros::test]
async fn it_has_uuid() -> anyhow::Result<()> {
use sqlx_oldapi::types::Uuid;
let mut conn = new::<Any>().await?;
let dbms_name = conn.dbms_name().await?.to_lowercase();
let expected_uuid = Uuid::parse_str("123e4567-e89b-12d3-a456-426614174000")?;
let sql = if dbms_name.contains("mssql") || dbms_name.contains("sql server") {
"select CONVERT(uniqueidentifier, '123e4567-e89b-12d3-a456-426614174000')"
} else if dbms_name.contains("postgres") {
"select '123e4567-e89b-12d3-a456-426614174000'::uuid"
} else {
"select x'123e4567e89b12d3a456426614174000'"
};
let actual = conn.fetch_one(sql).await?.try_get::<Uuid, _>(0)?;
assert_eq!(expected_uuid, actual, "UUID value for {}", sql);
Ok(())
}
#[sqlx_macros::test]
async fn it_pings() -> anyhow::Result<()> {
let mut conn = new::<Any>().await?;
conn.ping().await?;
Ok(())
}
#[sqlx_macros::test]
async fn it_executes_one_statement_with_pool() -> anyhow::Result<()> {
let pool = sqlx_test::pool::<Any>().await?;
let rows = pool.fetch_all("SELECT 1").await?;
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].try_get::<i32, _>(0)?, 1);
Ok(())
}
/// ODBC does not support multiple statements in a single query
#[cfg(not(feature = "odbc"))]
#[sqlx_macros::test]
async fn it_executes_two_statements_with_pool() -> anyhow::Result<()> {
let pool = sqlx_test::pool::<Any>().await?;
let rows = pool.fetch_all("SELECT 1; SElECT 2").await?;
assert_eq!(rows.len(), 2);
Ok(())
}
#[sqlx_macros::test]
async fn it_does_not_stop_stream_after_decoding_error() -> anyhow::Result<()> {
use futures::stream::StreamExt;
// see https://github.com/launchbadge/sqlx/issues/1884
let pool = sqlx_test::pool::<Any>().await?;
#[derive(Debug, PartialEq)]
struct MyType;
impl<'a> sqlx_oldapi::FromRow<'a, AnyRow> for MyType {
fn from_row(row: &'a AnyRow) -> sqlx_oldapi::Result<Self> {
let n = row.try_get::<i32, _>(0)?;
if n == 1 {
Err(sqlx_oldapi::Error::RowNotFound)
} else {
Ok(MyType)
}
}
}
let rows = sqlx_oldapi::query_as("SELECT 0 UNION ALL SELECT 1 UNION ALL SELECT 2")
.fetch(&pool)
.map(|r| r.ok())
.collect::<Vec<_>>()
.await;
assert_eq!(rows, vec![Some(MyType), None, Some(MyType)]);
Ok(())
}
#[sqlx_macros::test]
async fn it_gets_by_name() -> anyhow::Result<()> {
let mut conn = new::<Any>().await?;
let row = conn.fetch_one("SELECT 1 as _1").await?;
let val: i32 = row.get("_1");
assert_eq!(val, 1);
Ok(())
}
#[sqlx_macros::test]
async fn it_can_fail_and_recover() -> anyhow::Result<()> {
let mut conn = new::<Any>().await?;
for i in 0..10 {
// make a query that will fail
let res = conn
.execute("INSERT INTO not_found (column) VALUES (10)")
.await;
assert!(res.is_err());
// now try and use the connection
let val: i32 = conn
.fetch_one(&*format!("SELECT {}", i))
.await?
.get_unchecked(0);
assert_eq!(val, i);
}
Ok(())
}
#[sqlx_macros::test]
async fn it_can_fail_and_recover_with_pool() -> anyhow::Result<()> {
let pool = sqlx_test::pool::<Any>().await?;
for i in 0..10 {
// make a query that will fail
let res = pool
.execute("INSERT INTO not_found (column) VALUES (10)")
.await;
assert!(res.is_err());
// now try and use the connection
let val: i32 = pool
.fetch_one(&*format!("SELECT {}", i))
.await?
.get_unchecked(0);
assert_eq!(val, i);
}
Ok(())
}
#[sqlx_macros::test]
async fn it_has_unsigned_integers() -> anyhow::Result<()> {
let max_value = 9223372036854775807;
let expr = if cfg!(feature = "mysql") {
format!("CAST({} AS UNSIGNED)", max_value)
} else {
max_value.to_string()
};
assert_eq!(max_value, get_val::<u64>(&expr).await?);
Ok(())
}
#[sqlx_macros::test]
async fn it_reports_rows_affected() -> anyhow::Result<()> {
let mut conn = new::<Any>().await?;
let dbms = conn.dbms_name().await.unwrap_or_default().to_lowercase();
let (create_sql, insert_sql, delete_sql) =
if dbms.contains("mssql") || dbms.contains("sql server") {
(
"CREATE TABLE #temp_rows_affected (id INT PRIMARY KEY)",
"INSERT INTO #temp_rows_affected (id) VALUES (1)",
"DELETE FROM #temp_rows_affected WHERE id = 1",
)
} else {
(
"CREATE TEMPORARY TABLE temp_rows_affected (id INTEGER PRIMARY KEY)",
"INSERT INTO temp_rows_affected (id) VALUES (1)",
"DELETE FROM temp_rows_affected WHERE id = 1",
)
};
conn.execute(create_sql).await?;
let insert_done = conn.execute(insert_sql).await?;
assert_eq!(insert_done.rows_affected(), 1);
let delete_done = conn.execute(delete_sql).await?;
assert_eq!(delete_done.rows_affected(), 1);
Ok(())
}
#[sqlx_macros::test]
async fn it_prepares_statements() -> anyhow::Result<()> {
let mut conn = new::<Any>().await?;
let stmt = conn.prepare("SELECT 42 AS answer").await?;
assert_eq!(stmt.columns().len(), 1);
assert_eq!(stmt.columns()[0].name(), "answer");
let row = stmt.query().fetch_one(&mut conn).await?;
let answer: i32 = row.try_get("answer")?;
assert_eq!(answer, 42);
Ok(())
}
#[sqlx_macros::test]
async fn it_fails_to_prepare_invalid_statements() -> anyhow::Result<()> {
let mut conn = new::<Any>().await?;
let result = conn.prepare("SELECT * FROM table_does_not_exist").await;
assert!(
result.is_err(),
"Expected error when preparing statement for non-existent table"
);
Ok(())
}
#[sqlx_macros::test]
#[cfg(feature = "postgres")]
async fn it_converts_any_value_ref_to_specific_postgres_types() -> anyhow::Result<()> {
let mut conn = new::<Any>().await?;
let dbms_name = conn.dbms_name().await.unwrap_or_default().to_lowercase();
if !dbms_name.contains("postgres") {
return Ok(());
}
use sqlx_oldapi::postgres::{types::PgRange, PgValueRef, Postgres};
use std::ops::Bound;
let row = conn.fetch_one("SELECT int4range(1, 10)").await?;
let pgrange: PgValueRef<'_> = row.try_get_raw(0)?.try_into()?;
let decoded: PgRange<i32> = <PgRange<i32> as Decode<'_, Postgres>>::decode(pgrange).unwrap();
assert_eq!(decoded.start, Bound::Included(1));
assert_eq!(decoded.end, Bound::Excluded(10));
Ok(())
}