sqlx-oldapi 0.6.53

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, SQLite, MSSQL, and ODBC.
Documentation
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
use core::f32;

use sqlx_oldapi::mssql::Mssql;
use sqlx_test::test_type;

test_type!(str<String>(Mssql,
    "'this is foo'" == "this is foo",
    "''" == "",
    "CAST('foo' AS VARCHAR(3))" == "foo",
    "CAST('foo' AS VARCHAR(max))" == "foo",
    "REPLICATE('a', 910)" == "a".repeat(910),
));

test_type!(str_unicode<String>(Mssql, "N'ï¿®'" == "ï¿®"));

test_type!(long_str<String>(Mssql,
    "REPLICATE(CAST('a' AS VARCHAR), 8000)" == "a".repeat(8000),
    "REPLICATE(CAST('a' AS VARCHAR(max)), 8192)" == "a".repeat(8192),
    "REPLICATE(CAST('a' AS NVARCHAR(max)), 8192)" == "a".repeat(8192),
    "REPLICATE(CAST('a' AS VARCHAR(max)), 100000)" == "a".repeat(100_000),
));

test_type!(null<Option<i32>>(Mssql,
    "CAST(NULL as INT)" == None::<i32>
));

test_type!(u8(
    Mssql,
    "CAST(5 AS TINYINT)" == 5_u8,
    "CAST(0 AS TINYINT)" == 0_u8,
    "CAST(255 AS TINYINT)" == 255_u8,
));

test_type!(i8(
    Mssql,
    "CAST(5 AS TINYINT)" == 5_i8,
    "CAST(0 AS TINYINT)" == 0_i8
));

test_type!(u8_edge_cases<u8>(
    Mssql,
    "CAST(0 AS TINYINT)" == 0_u8,
    "CAST(127 AS TINYINT)" == 127_u8,
    "CAST(128 AS TINYINT)" == 128_u8,
    "CAST(255 AS TINYINT)" == 255_u8,
));

test_type!(i16(Mssql, "CAST(21415 AS SMALLINT)" == 21415_i16));

test_type!(i16_edge_cases<i16>(
    Mssql,
    "CAST(-32768 AS SMALLINT)" == -32768_i16,
    "CAST(-1 AS SMALLINT)" == -1_i16,
    "CAST(0 AS SMALLINT)" == 0_i16,
    "CAST(32767 AS SMALLINT)" == 32767_i16,
));

test_type!(i32(Mssql, "CAST(2141512 AS INT)" == 2141512_i32));

test_type!(i32_edge_cases<i32>(
    Mssql,
    "CAST(-2147483648 AS INT)" == -2147483648_i32,
    "CAST(-1 AS INT)" == -1_i32,
    "CAST(0 AS INT)" == 0_i32,
    "CAST(2147483647 AS INT)" == 2147483647_i32,
));

test_type!(i64(Mssql, "CAST(32324324432 AS BIGINT)" == 32324324432_i64));

test_type!(i64_edge_cases<i64>(
    Mssql,
    "CAST(-9223372036854775808 AS BIGINT)" == -9223372036854775808_i64,
    "CAST(-1 AS BIGINT)" == -1_i64,
    "CAST(0 AS BIGINT)" == 0_i64,
    "CAST(9223372036854775807 AS BIGINT)" == 9223372036854775807_i64,
));

test_type!(f32(
    Mssql,
    "CAST(3.14159265358979323846264338327950288 AS REAL)" == f32::consts::PI,
    "CAST(0.5 AS DECIMAL(3,2))" == 0.5_f32,
));

test_type!(f64(
    Mssql,
    "CAST(939399419.1225182 AS FLOAT)" == 939399419.1225182_f64,
    "CAST(9.75 AS REAL)" == 9.75_f64,
));

test_type!(numeric<f64>(Mssql,
    "CAST(12 AS NUMERIC)" == 12_f64,
    "CAST(0.7 AS NUMERIC(2,1))" == 0.7_f64,
    "CAST(0.3 AS NUMERIC(2,1))" == 0.3_f64,
    "CAST(0.47 AS DECIMAL(3,2))" == 0.47_f64,
    "CAST(0.29 AS DECIMAL(3,2))" == 0.29_f64,
    "CAST(0.5678 AS NUMERIC(10,4))" == 0.5678_f64,
    "CAST(0.0003 AS NUMERIC(10,4))" == 0.0003_f64,
    "CAST(0.00003 AS NUMERIC(10,5))" == 0.00003_f64,
    "CAST(0.0000000000000001 AS NUMERIC(38,16))" == 0.0000000000000001_f64,
    "CAST(939399419.1225182 AS NUMERIC(15,2))" == 939399419.12_f64,
    "CAST(939399419.1225182 AS DECIMAL(15,2))" == 939399419.12_f64,
    "CAST(123456789.0123456789 AS NUMERIC(38,10))" == 123_456_789.012_345_67_f64,
    "CAST(123456789.0123456789012 AS NUMERIC(38,13))" == 123_456_789.012_345_67_f64,
    "CAST(123456789.012345678901234 AS NUMERIC(38,15))" == 123_456_789.012_345_67_f64,
    "CAST(1.0000000000000001 AS NUMERIC(18,16))" == 1.0000000000000001_f64,
    "CAST(0.99999999999999 AS NUMERIC(18,14))" == 0.99999999999999_f64,
    "CAST(2.00000000000001 AS NUMERIC(18,14))" == 2.00000000000001_f64,
    "CAST(333.33333333333333 AS NUMERIC(18,14))" == 333.333_333_333_333_3_f64,
    "CAST(0.14285714285714 AS NUMERIC(18,14))" == 0.14285714285714_f64,
    "CAST(9999999.99999999 AS NUMERIC(16,8))" == 9999999.99999999_f64, // Close to the precision limit
    "CAST(9007199254740992 AS NUMERIC(16,0))" == 9007199254740992_f64,    // 2^53, largest integer that can be exactly represented as a f64
    "CAST(0.000123456 AS NUMERIC(38,38))" == 0.000123456_f64,
    "CAST(1e-38 AS NUMERIC(38,38))" == 1e-38_f64,
));

test_type!(str_nvarchar<String>(Mssql,
    "CAST('this is foo' as NVARCHAR)" == "this is foo",
));

test_type!(bool(
    Mssql,
    "CAST(1 as BIT)" == true,
    "CAST(0 as BIT)" == false
));

test_type!(bytes<Vec<u8>>(Mssql,
    "0xDEADBEEF" == vec![0xDE_u8, 0xAD, 0xBE, 0xEF],
    "CAST(' ' AS VARBINARY)" == vec![0x20_u8],
    "CAST(REPLICATE(' ', 31) AS VARBINARY(max))" == vec![0x20_u8; 31],
));

test_type!(long_byte_buffer<Vec<u8>>(Mssql,
    "CAST(REPLICATE(CAST(' ' AS VARCHAR(max)), 100000) AS VARBINARY(max))" == vec![0x20_u8; 100000],
));

test_type!(empty_varbinary<Vec<u8>>(Mssql,
    "CAST('' AS VARBINARY)" == Vec::<u8>::new(),
));

test_type!(null_varbinary<Option<Vec<u8>>>(Mssql,
    "CAST(NULL AS VARBINARY)" == None::<Vec<u8>>,
    "CAST(NULL AS VARBINARY(max))" == None::<Vec<u8>>,
));

#[cfg(feature = "chrono")]
mod chrono {
    use super::*;
    use sqlx_core::types::chrono::{FixedOffset, NaiveTime, Utc};
    use sqlx_oldapi::types::chrono::{DateTime, NaiveDate, NaiveDateTime};

    test_type!(smalldatetime_type<DateTime<Utc>>(
        Mssql,
        "CAST('2023-07-31 23:59' as SmallDateTime)"
            == NaiveDateTime::parse_from_str("2023-07-31 23:59", "%Y-%m-%d %H:%M")
                .unwrap()
                .and_utc()
                .fixed_offset()
    ));

    test_type!(old_datetime_type<DateTime<Utc>>(
        Mssql,
        "CAST('1901-05-08 23:58:59' as DateTime)"
            == NaiveDateTime::parse_from_str("1901-05-08 23:58:59", "%Y-%m-%d %H:%M:%S")
                .unwrap()
                .and_utc()
                .fixed_offset()
    ));

    test_type!(old_datetime_type_as_naive<NaiveDateTime>(
        Mssql,
        "CAST('1901-05-08 23:58:59' as DateTime)"
            == NaiveDateTime::parse_from_str("1901-05-08 23:58:59", "%Y-%m-%d %H:%M:%S")
                .unwrap()
    ));

    test_type!(datetime2<NaiveDateTime>(
        Mssql,
        "CAST('2016-10-23 12:45:37.1234567' as DateTime2)"
            == NaiveDateTime::parse_from_str("2016-10-23 12:45:37.1234567", "%Y-%m-%d %H:%M:%S%.f")
                .unwrap()
    ));

    test_type!(datetimeoffset<DateTime<FixedOffset>>(
        Mssql,
        "CAST('2016-10-23 12:45:37.1234567 +02:00' as datetimeoffset(7))" == DateTime::parse_from_rfc3339("2016-10-23T12:45:37.1234567+02:00").unwrap()
    ));

    test_type!(NaiveDate(
        Mssql,
        "CAST('1789-07-14' AS DATE)"
            == NaiveDate::parse_from_str("1789-07-14", "%Y-%m-%d").unwrap()
    ));

    test_type!(NaiveTime(
        Mssql,
        "CAST('23:59:59.9999' AS TIME)"
            == NaiveTime::parse_from_str("23:59:59.9999", "%H:%M:%S%.f").unwrap(),
        "CAST('00:00' AS TIME)" == NaiveTime::default(),
    ));
}

#[cfg(feature = "decimal")]
mod decimal {
    use super::*;
    use sqlx_oldapi::types::Decimal;

    test_type!(Decimal(
        Mssql,
        "CAST('123456789.987654321' AS DECIMAL(18,9))"
            == Decimal::from_str_exact("123456789.987654321").unwrap(),
        "CAST('0' AS DECIMAL(1,0))" == Decimal::from_str_exact("0").unwrap(),
        "CAST('1' AS DECIMAL(1,0))" == Decimal::from_str_exact("1").unwrap(),
        "CAST('-1' AS DECIMAL(1,0))" == Decimal::from_str_exact("-1").unwrap(),
        "CAST('0.01234567890123456789' AS DECIMAL(38,20))"
            == Decimal::from_str_exact("0.01234567890123456789").unwrap(),
        "CAST('-12345678901234' AS DECIMAL(28,5))"
            == Decimal::from_str_exact("-12345678901234").unwrap(),
    ));

    test_type!(money_boundary_tests<Decimal>(
        Mssql,
        "CAST('922337203685477.5807' AS MONEY)" == Decimal::from_str_exact("922337203685477.5807").unwrap(),
        "CAST('-922337203685477.5808' AS MONEY)" == Decimal::from_str_exact("-922337203685477.5808").unwrap(),
        "CAST('922337203685477.5806' AS MONEY)" == Decimal::from_str_exact("922337203685477.5806").unwrap(),
        "CAST('-922337203685477.5807' AS MONEY)" == Decimal::from_str_exact("-922337203685477.5807").unwrap(),
        "CAST('922337203685477.0000' AS MONEY)" == Decimal::from_str_exact("922337203685477.0000").unwrap(),
        "CAST('-922337203685477.0000' AS MONEY)" == Decimal::from_str_exact("-922337203685477.0000").unwrap(),
    ));

    test_type!(smallmoney_boundary_tests<Decimal>(
        Mssql,
        "CAST('214748.3647' AS SMALLMONEY)" == Decimal::from_str_exact("214748.3647").unwrap(),
        "CAST('-214748.3648' AS SMALLMONEY)" == Decimal::from_str_exact("-214748.3648").unwrap(),
        "CAST('214748.3646' AS SMALLMONEY)" == Decimal::from_str_exact("214748.3646").unwrap(),
        "CAST('-214748.3647' AS SMALLMONEY)" == Decimal::from_str_exact("-214748.3647").unwrap(),
        "CAST('214748.0000' AS SMALLMONEY)" == Decimal::from_str_exact("214748.0000").unwrap(),
        "CAST('-214748.0000' AS SMALLMONEY)" == Decimal::from_str_exact("-214748.0000").unwrap(),
    ));

    test_type!(money_precision_tests<Decimal>(
        Mssql,
        "CAST('0.0000' AS MONEY)" == Decimal::from_str_exact("0.0000").unwrap(),
        "CAST('0.0001' AS MONEY)" == Decimal::from_str_exact("0.0001").unwrap(),
        "CAST('-0.0001' AS MONEY)" == Decimal::from_str_exact("-0.0001").unwrap(),
        "CAST('0.9999' AS MONEY)" == Decimal::from_str_exact("0.9999").unwrap(),
        "CAST('-0.9999' AS MONEY)" == Decimal::from_str_exact("-0.9999").unwrap(),
        "CAST('1.0000' AS MONEY)" == Decimal::from_str_exact("1.0000").unwrap(),
        "CAST('-1.0000' AS MONEY)" == Decimal::from_str_exact("-1.0000").unwrap(),
        "CAST('2.15' AS MONEY)" == Decimal::from_str_exact("2.15").unwrap(),
        "CAST('214748.3647' AS SMALLMONEY)" == Decimal::from_str_exact("214748.3647").unwrap(),
        "CAST('922337203685477.5807' AS MONEY)" == Decimal::from_str_exact("922337203685477.5807").unwrap(),
        "CAST('-922337203685477.5808' AS MONEY)" == Decimal::from_str_exact("-922337203685477.5808").unwrap(),
        "CAST('214748.3647' AS SMALLMONEY)" == Decimal::from_str_exact("214748.3647").unwrap(),
        "CAST('-214748.3648' AS SMALLMONEY)" == Decimal::from_str_exact("-214748.3648").unwrap(),
    ));

    test_type!(money_precision_tests_f64<f64>(
        Mssql,
        "CAST('0.0000' AS MONEY)" == 0.0000,
        "CAST('0.0001' AS MONEY)" == 0.0001,
        "CAST('-0.0001' AS MONEY)" == -0.0001,
        "CAST('0.9999' AS MONEY)" == 0.9999,
        "CAST('-0.9999' AS MONEY)" == -0.9999,
        "CAST('1.0000' AS MONEY)" == 1.0000,
        "CAST('-1.0000' AS MONEY)" == -1.0000,
        "CAST('2.15' AS MONEY)" == 2.15,
        "CAST('214748.3647' AS SMALLMONEY)" == 214748.3647,
        "CAST('922337203685477.5807' AS MONEY)" == 922_337_203_685_477.6,
        "CAST('-922337203685477.5808' AS MONEY)" == -922_337_203_685_477.6,
        "CAST('214748.3647' AS SMALLMONEY)" == 214748.3647,
        "CAST('-214748.3648' AS SMALLMONEY)" == -214748.3648,
    ));
}

#[cfg(feature = "bigdecimal")]
mod bigdecimal {
    use super::*;
    use sqlx_oldapi::types::BigDecimal;
    use std::str::FromStr;

    test_type!(BigDecimal(
        Mssql,
        "CAST('0' AS DECIMAL(1,0))" == BigDecimal::from_str("0").unwrap(),
        "CAST('1' AS DECIMAL(1,0))" == BigDecimal::from_str("1").unwrap(),
        "CAST('-1' AS DECIMAL(1,0))" == BigDecimal::from_str("-1").unwrap(),
        "CAST('-12345678901234' AS DECIMAL(28,5))"
            == BigDecimal::from_str("-12345678901234").unwrap(),
        "CAST('-12345678901234567890' AS DECIMAL(38,5))"
            == BigDecimal::from_str("-12345678901234567890").unwrap(),
        "CAST('-12345678901234567890.012345678901234' AS DECIMAL(38,15))"
            == BigDecimal::from_str("-12345678901234567890.012345678901234").unwrap(),
        "CAST('-1234567890.1234' AS MONEY)" == BigDecimal::from_str("-1234567890.1234").unwrap(),
        "CAST('-123456.1234' AS SMALLMONEY)" == BigDecimal::from_str("-123456.1234").unwrap(),
    ));
}

#[cfg(feature = "json")]
mod json {
    use super::*;
    use serde_json::Value;
    use sqlx_core::types::Json;

    test_type!(json_value<Json<Value>>(Mssql,
        r#"'123'"# == Json(Value::Number(123.into()))
    ));
}

test_type!(cross_type_tinyint_to_all_signed<i8>(
    Mssql,
    "CAST(0 AS TINYINT)" == 0_i8,
    "CAST(127 AS TINYINT)" == 127_i8,
));

test_type!(cross_type_tinyint_to_i16<i16>(
    Mssql,
    "CAST(0 AS TINYINT)" == 0_i16,
    "CAST(127 AS TINYINT)" == 127_i16,
    "CAST(255 AS TINYINT)" == 255_i16,
));

test_type!(cross_type_tinyint_to_i64<i64>(
    Mssql,
    "CAST(0 AS TINYINT)" == 0_i64,
    "CAST(127 AS TINYINT)" == 127_i64,
    "CAST(255 AS TINYINT)" == 255_i64,
));

test_type!(cross_type_tinyint_to_u16<u16>(
    Mssql,
    "CAST(0 AS TINYINT)" == 0_u16,
    "CAST(127 AS TINYINT)" == 127_u16,
    "CAST(255 AS TINYINT)" == 255_u16,
));

test_type!(cross_type_tinyint_to_u64<u64>(
    Mssql,
    "CAST(0 AS TINYINT)" == 0_u64,
    "CAST(127 AS TINYINT)" == 127_u64,
    "CAST(255 AS TINYINT)" == 255_u64,
));

test_type!(cross_type_smallint_to_i64<i64>(
    Mssql,
    "CAST(-32768 AS SMALLINT)" == -32768_i64,
    "CAST(0 AS SMALLINT)" == 0_i64,
    "CAST(32767 AS SMALLINT)" == 32767_i64,
));

test_type!(cross_type_smallint_to_u16<u16>(
    Mssql,
    "CAST(0 AS SMALLINT)" == 0_u16,
    "CAST(32767 AS SMALLINT)" == 32767_u16,
));

test_type!(cross_type_smallint_to_u64<u64>(
    Mssql,
    "CAST(0 AS SMALLINT)" == 0_u64,
    "CAST(32767 AS SMALLINT)" == 32767_u64,
));

test_type!(cross_type_int_to_i64<i64>(
    Mssql,
    "CAST(-2147483648 AS INT)" == -2147483648_i64,
    "CAST(0 AS INT)" == 0_i64,
    "CAST(2147483647 AS INT)" == 2147483647_i64,
));

test_type!(cross_type_int_to_u32<u32>(
    Mssql,
    "CAST(0 AS INT)" == 0_u32,
    "CAST(2147483647 AS INT)" == 2147483647_u32,
));

test_type!(cross_type_int_to_u64<u64>(
    Mssql,
    "CAST(0 AS INT)" == 0_u64,
    "CAST(2147483647 AS INT)" == 2147483647_u64,
));

test_type!(cross_type_bigint_to_u64<u64>(
    Mssql,
    "CAST(0 AS BIGINT)" == 0_u64,
    "CAST(9223372036854775807 AS BIGINT)" == 9223372036854775807_u64,
));

test_type!(cross_type_decimal_to_integers<i64>(
    Mssql,
    "CAST(123456789 AS DECIMAL(15,0))" == 123456789_i64,
    "CAST(-123456789 AS DECIMAL(15,0))" == -123456789_i64,
    "CAST(0 AS DECIMAL(15,0))" == 0_i64,
));

#[cfg(feature = "uuid")]
mod uuid {
    use super::*;
    use sqlx_oldapi::types::Uuid;

    test_type!(uuid<Uuid>(
        Mssql,
        "CAST('00000000-0000-0000-0000-000000000000' AS UNIQUEIDENTIFIER)" == Uuid::nil(),
        "CAST('6F9619FF-8B86-D011-B42D-00C04FC964FF' AS UNIQUEIDENTIFIER)" == Uuid::parse_str("6F9619FF-8B86-D011-B42D-00C04FC964FF").unwrap()
    ));
}