rsfbclient 0.26.0

Binds to official firebird client lib
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
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
//!
//! Rust Firebird Client
//!
//! Parameter tests
//!

mk_tests_default! {
    use crate::{prelude::*, FbError, SqlType, EngineVersion, SystemInfos};
    use chrono::{NaiveDate, NaiveTime};
    use rand::{distributions::Standard, Rng};

    #[test]
    fn optional_named_support() -> Result<(), FbError> {
        let exec_block_select : &str = "
            EXECUTE BLOCK RETURNS (outval bigint) as
            declare loopvar int = 0;
            begin
                while (loopvar < 100) do begin
                    for select
                        :loopvar
                    from
                        rdb$database
                    into
                        :outval
                    do begin
                        loopvar = loopvar + 1;
                        suspend;
                    end
                end
            end;";

        let mut conn = cbuilder().connect()?;

        let rows = conn.query::<(), (i64,)>(exec_block_select,())?;

        assert_eq!(100, rows.len());

        Ok(())
    }

    #[test]
    fn struct_namedparams_optional() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PNAMED_TEST", ()).ok();
        conn.execute("CREATE TABLE PNAMED_TEST (id int, num1 int, str1 varchar(50))", ())?;

        #[derive(Clone, IntoParams)]
        struct ParamTest {
            pub num1: Option<i32>,
            pub str1: Option<String>
        }

        let ptest = ParamTest {
            num1: Some(10),
            str1: None
        };

        let res1: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where 10 = :num1",
            ptest.clone(),
        )?;
        assert!(res1.is_some());

        conn.execute("insert into pnamed_test (id, str1) values (1, :str1)", ptest.clone())?;
        conn.execute("insert into pnamed_test (id, num1) values (2, :num1)", ptest)?;

        let res2: Option<(i32,)> = conn.query_first("select 1 from pnamed_test where id = 1 and str1 is null", ())?;
        assert!(res2.is_some());

        let res3: Option<(i32,)> = conn.query_first("select 1 from pnamed_test where id = 2 and num1 is not null", ())?;
        assert!(res3.is_some());

        Ok(())
    }

    #[test]
    fn struct_namedparams_insert() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PNAMED_USER", ()).ok();
        conn.execute("CREATE TABLE PNAMED_USER (name varchar(50), age int)", ())?;

        #[derive(Clone, IntoParams)]
        struct User {
            pub name: String,
            pub age: i32
        }

        let user1 = User {
            name: "Pedro".to_string(),
            age: 20
        };

        conn.execute("insert into pnamed_user (name, age) values (:name, :age)", user1.clone())?;

        let suser1: Option<(String,i32,)> = conn.query_first(
            "select name, age from pnamed_user where age >= :age",
            user1,
        )?;
        assert!(suser1.is_some());
        assert_eq!("Pedro", suser1.unwrap().0);

        Ok(())
    }

    #[test]
    fn struct_namedparams() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        #[derive(Clone, IntoParams)]
        struct ParamTest {
            pub num: i32,
            pub num2: f64,
            pub str1: String
        }

        let ptest = ParamTest {
            num: 10,
            num2: 11.11,
            str1: "olá mundo".to_string()
        };

        let res1: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where 10 = :num ",
            ptest.clone(),
        )?;
        assert!(res1.is_some());

        let res2: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where 11.11 = :num2 ",
            ptest.clone(),
        )?;
        assert!(res2.is_some());

        let res3: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where 10 = :num and 11.11 = :num2 ",
            ptest.clone(),
        )?;
        assert!(res3.is_some());

        let res4: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where 'olá mundo' = :str1 ",
            ptest,
        )?;
        assert!(res4.is_some());

        Ok(())
    }

    #[test]
    fn multi_struct_namedparams() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        #[derive(Clone, IntoParams)]
        struct ParamTest1 {
            pub num: i32,
        }

        let ptest1 = ParamTest1 {
            num: 10,
        };

        let res1: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where 10 = :num ",
            ptest1.clone(),
        )?;
        assert!(res1.is_some());

        #[derive(Clone, IntoParams)]
        struct ParamTest2 {
            pub num: i32,
        }

        let ptest2 = ParamTest2 {
            num: 10,
        };

        let res2: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where 10 = :num ",
            ptest2.clone(),
        )?;
        assert!(res2.is_some());

        Ok(())
    }

    #[test]
    fn boolean() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        if conn.server_engine()? <= EngineVersion::V2 {
            return Ok(());
        }

        let res: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where true = ? ",
            (true,),
        )?;
        assert!(res.is_some());


        let res: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where true = ? ",
            (false,),
        )?;
        assert!(res.is_none());

        Ok(())
    }

    #[test]
    fn blob_binary_subtype() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PBLOBBIN", ()).ok();
        conn.execute("CREATE TABLE PBLOBBIN (content blob sub_type 0)", ())?;

        let bin: Vec<u8> = Vec::from("abc äbç 123".as_bytes());
        conn.execute("insert into pblobbin (content) values (?)", (bin,))?;
        let val_exists: Option<(i16,)> = conn.query_first("select 1 from pblobbin where content = x'61626320c3a462c3a720313233'", ())?;
        assert!(val_exists.is_some());

        Ok(())
    }

    #[test]
    fn blob_text_subtype() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PBLOBTEXT", ()).ok();
        conn.execute("CREATE TABLE PBLOBTEXT (content blob sub_type 1)", ())?;

        conn.execute("insert into pblobtext (content) values (?)", ("abc äbç 123",))?;
        let val_exists: Option<(i16,)> = conn.query_first("select 1 from pblobtext where content = 'abc äbç 123'", ())?;
        assert!(val_exists.is_some());

        Ok(())
    }

    #[test]
    fn big_blob_binary() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        let rstr: Vec<u8> = rand::thread_rng()
            .sample_iter::<u8, _>(Standard)
            .take(1024 * 1024)
            .collect();

        conn.execute("DROP TABLE PBIGBLOBBIN", ()).ok();
        conn.execute("CREATE TABLE PBIGBLOBBIN (content blob sub_type 0)", ())?;

        conn.execute("insert into pbigblobbin (content) values (?)", (rstr,))?;

        Ok(())
    }

    #[test]
    fn big_blob_text() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        let rstr: String = rand::thread_rng()
            .sample_iter::<char, _>(Standard)
            .take(1024 * 1024)
            .collect();

        conn.execute("DROP TABLE PBIGBLOBTEXT", ()).ok();
        conn.execute("CREATE TABLE PBIGBLOBTEXT (content blob sub_type 1)", ())?;

        conn.execute("insert into pbigblobtext (content) values (?)", (rstr,))?;

        Ok(())
    }

    #[test]
    fn dates() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PDATES", ()).ok();
        conn.execute(
            "CREATE TABLE PDATES (ref char(1), a date, b timestamp, c time)",
            (),
        )?;

        conn.execute(
            "insert into pdates (ref, a) values ('a', ?)",
            (NaiveDate::from_ymd(2009, 8, 7),),
        )?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pdates where ref = 'a' and a = '2009-08-07'",
            (),
        )?;
        assert!(val_exists.is_some());

        conn.execute(
            "insert into pdates (ref, b) values ('b', ?)",
            (NaiveDate::from_ymd(2009, 8, 7).and_hms(11, 32, 25),),
        )?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pdates where ref = 'b' and b = '2009-08-07 11:32:25'",
            (),
        )?;
        assert!(val_exists.is_some());

        conn.execute(
            "insert into pdates (ref, c) values ('c', ?)",
            (NaiveTime::from_hms(11, 22, 33),),
        )?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pdates where ref = 'c' and c = '11:22:33'",
            (),
        )?;
        assert!(val_exists.is_some());

        Ok(())
    }

    #[test]
    fn strings() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PSTRINGS", ()).ok();
        conn.execute(
            "CREATE TABLE PSTRINGS (ref char(1), a varchar(10), b varchar(10))",
            (),
        )?;

        conn.execute(
            "insert into pstrings (ref, a) values ('a', ?)",
            ("firebird",),
        )?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pstrings where ref = 'a' and a = 'firebird'",
            (),
        )?;
        assert!(val_exists.is_some());

        conn.execute(
            "insert into pstrings (ref, b) values ('b', ?)",
            ("firebird",),
        )?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pstrings where ref = 'b' and b = 'firebird  '",
            (),
        )?;
        assert!(val_exists.is_some());

        Ok(())
    }

    #[test]
    fn fixed_points() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PFIXEDS", ()).ok();
        conn.execute(
            "CREATE TABLE PFIXEDS (ref char(1), a numeric(2, 2), b decimal(2, 2))",
            (),
        )?;

        conn.execute("insert into pfixeds (ref, a) values ('a', ?)", (22.33,))?;
        let val_exists: Option<(i16,)> =
            conn.query_first("select 1 from pfixeds where ref = 'a' and a = 22.33", ())?;
        assert!(val_exists.is_some());

        conn.execute("insert into pfixeds (ref, b) values ('b', ?)", (22.33,))?;
        let val_exists: Option<(i16,)> =
            conn.query_first("select 1 from pfixeds where ref = 'b' and b = 22.33", ())?;
        assert!(val_exists.is_some());

        Ok(())
    }

    #[test]
    fn float_points() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PFLOATS", ()).ok();
        conn.execute(
            "CREATE TABLE PFLOATS (ref char(1), a float, b double precision)",
            (),
        )?;

        conn.execute("insert into pfloats (ref, a) values ('a', ?)", (3.402E38,))?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pfloats where ref = 'a' and a = cast(3.402E38 as float)",
            (),
        )?;
        assert!(val_exists.is_some());

        conn.execute(
            "insert into pfloats (ref, b) values ('b', ?)",
            (2.225e-300,),
        )?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pfloats where ref = 'b' and b = 2.225E-300",
            (),
        )?;
        assert!(val_exists.is_some());

        Ok(())
    }

    #[test]
    fn ints() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        conn.execute("DROP TABLE PINTEGERS", ()).ok();
        conn.execute(
            "CREATE TABLE PINTEGERS (ref char(1), a smallint, b int, c bigint)",
            (),
        )?;

        conn.execute(
            "insert into pintegers (ref, a) values ('a', ?)",
            (i16::MIN,),
        )?;
        let val_exists: Option<(i16,)> =
            conn.query_first("select 1 from pintegers where ref = 'a' and a = -32768", ())?;
        assert!(val_exists.is_some());

        conn.execute(
            "insert into pintegers (ref, b) values ('b', ?)",
            (i32::MIN,),
        )?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pintegers where ref = 'b' and b = -2147483648",
            (),
        )?;
        assert!(val_exists.is_some());

        conn.execute(
            "insert into pintegers (ref, c) values ('c', ?)",
            (i64::MIN,),
        )?;
        let val_exists: Option<(i16,)> = conn.query_first(
            "select 1 from pintegers where ref = 'c' and c = -9223372036854775808",
            (),
        )?;
        assert!(val_exists.is_some());

        Ok(())
    }

    #[test]
    fn null() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        let res: Option<(i32,)> = conn.query_first(
            "select 1 from rdb$database where 1 = ? ",
            (Option::<i32>::None,),
        )?;

        assert!(res.is_none());

        Ok(())
    }

    #[test]
    fn lots_of_params() -> Result<(), FbError> {
        let mut conn = cbuilder().connect()?;

        let vals = -250..250;

        let params: Vec<SqlType> = vals.clone().map(|v| v.into()).collect();

        let sql = format!("select 1 from rdb$database where {}", vals.fold(String::new(), |mut acc, v| {
            if acc.is_empty() {
                acc += &format!("{} = ?", v);
            }else{
                acc += &format!(" and {} = ?", v);
            }
            acc
        }));

        let resp = conn.query_first(&sql, params)?;

        assert_eq!(resp, Some((1,)));

        Ok(())
    }
}