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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#![warn(missing_docs)]
//! This crate provides a Rust wrapper to the [Oracle Call Interface][1] (OCI) library.
//! The Oracle site describes OCI as a "...comprehensive, high performance, native C
//! language interface to Oracle Database for custom or packaged applications...".
//!
//! # Overview
//!
//! The OCI library is the original Oracle C API for interacting with their database. It is the one
//! that later versions of JDBC is built on for example. Recently Oracle has released a new API
//! called the [Oracle Database Programming Interface for Drivers and Applications][2] (ODPI-C)
//! that is supposed to simplify use of OCI, however the documentation for OCI
//! is more extensive and therefore easier to build a wrapper on top of.
//!
//! The OCI library is large and supports many use cases for interacting with a database. This
//! crate is currently concerned with support for executing SQL statements and so is limited when
//! compared to the whole of OCI.
//!
//! The overall design will be familiar to anyone who has used Java's JDBC, Haskell's HDBC or
//! Rust's [postgres][3] crate. Indeed, most design decisions were
//! made based on reviewing the API of these libraries.
//!
//! The basics are simple: a [`Connection`][4] represents a connection to a database, this connection
//! can be used to prepare one or more [`Statement`][5]s which are then used to execute SQL against the
//! database. If there are results then they can be returned all at once or lazily via an iterator.
//! Datatypes are represented using [`SqlValue`][6] and allow type conversion from Oracle
//! to Rust types.
//!
//! ## Missing type conversions
//!
//! Currently only `String`, `i64` and `f64` are supported. In Oracle terms this means that anything
//! held in columns as `VARCHAR`, `VARCHAR2` and `Number` can be retrieved. As Oracle uses `Number` to
//! respresent all number types then this is less restricting that it first appears. More types
//! will be added.
//!
//! # Setup
//!
//! This crate is developed against version 12.2 of the OCI library. It is expected to work with
//! 12.x.x but is not tested. The OCI client library needs to be installed on your machine and can be
//! downloaded [here][7].
//!
//! If you are on Linux then you are likely to need to tell the linker where
//! to find the files. Adding this to my `.bashrc` file worked for me, however the details may vary
//! according to your distro, mine is [OpenSuse][8].
//!
//! ```text
//! export LIBRARY_PATH=$LIBRARY_PATH:/usr/lib/oracle/12.2/client64/lib/
//! ```
//!
//! This crate has not been tested against Windows and so the setup will be different.
//!
//! Testing has been done against a local installation of [Oracle 11g Express Edition][9].
//! In order to run the crate tests then a local database needs to be
//! available on `localhost:1521/xe` with a user `oci_rs` and password `test`.
//!
//! In order to use `oci_rs` add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! oci_rs = "0.3.0"
//! ```
//! and this to your crate root:
//!
//! ```rust
//! extern crate oci_rs;
//! ```
//!
//! # Examples
//!
//! In the following example we will create a connection to a database and then create a table,
//! insert a couple of rows using bind variables and then execute a query to fetch them back again.
//! There is a lot of error handling needed. Every OCI function call can fail and so `Result` and
//! `Option` are used extensively. The below code takes the usual documentation shortcut of calling
//! `unwrap()` a lot but doing so in real client code will prove ill-fated. Any remote database connection is
//! inherently unreliable.
//!
//! ```rust
//! use oci_rs::connection::Connection;
//!
//! let conn = Connection::new("localhost:1521/xe", "oci_rs", "test").unwrap();
//!
//! # let mut drop = conn.create_prepared_statement("DROP TABLE Toys").unwrap();
//! # drop.execute().ok();
//!
//! // Create a table
//! let sql_create = "CREATE TABLE Toys (ToyId int,
//!                                      Name varchar(20),
//!                                      Price float)";
//! let mut create = conn.create_prepared_statement(sql_create).unwrap();
//!
//! // Execute the create statement
//! create.execute().unwrap();
//!
//! // Commit in case we lose connection (an abnormal disconnection would result
//! // in an automatic roll-back.)
//! create.commit().unwrap();
//!
//! // Insert some values using bind variables
//! let sql_insert = "INSERT INTO Toys (ToyId, Name, Price)
//!                   VALUES (:id, :name, :price)";
//! let mut insert = conn.create_prepared_statement(sql_insert).unwrap();
//!
//! let values = [(1, "Barbie", 23.45),
//!               (2, "Dinosaurs", -5.21)];
//!
//! // Run through the list of values, bind them and execute the statement
//! for value in values.iter() {
//!     insert.bind(&[&value.0, &value.1, &value.2]).unwrap();
//!     insert.execute().unwrap()
//! }
//!
//! insert.commit().unwrap();
//!
//! // Create a query
//! let sql_select = "SELECT * FROM Toys
//!                   WHERE Name='Barbie'";
//!
//! let mut select = conn.create_prepared_statement(sql_select).unwrap();
//!
//! // Execute
//! select.execute().unwrap();
//!
//! // Get the result set
//! let result_set = select.result_set().unwrap();
//! assert_eq!(result_set.len(), 1);
//! let first_row = &result_set[0];
//!
//! // Types are automatically converted
//! let id: i64 = first_row[0].value().unwrap();
//! let name: String = first_row[1].value().unwrap();
//! let price: f64 = first_row[2].value().unwrap();
//!
//! assert_eq!(id, 1);
//! assert_eq!(name, "Barbie");
//! assert_eq!(price, 23.45);
//!
//! ```
//! # OCI docs
//!
//! Documentation for the underlying OCI library can be found [here][10] and error codes and their
//! descriptions [here][11]. The error descriptions are useful because they often contain
//! additional information that is not included in the text returned from the library.
//!
//! [1]: http://www.oracle.com/technetwork/database/features/oci/index-090945.html
//! [2]: https://github.com/oracle/odpi
//! [3]: https://crates.io/crates/postgres
//! [4]: connection/struct.Connection.html
//! [5]: statement/struct.Statement.html
//! [6]: types/enum.SqlValue.html
//! [7]: http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
//! [8]: https://www.opensuse.org/
//! [9]: http://www.oracle.com/technetwork/database/database-technologies/express-edition/overview/index.html
//! [10]: http://docs.oracle.com/database/122/LNOCI/toc.htm
//! [11]: https://docs.oracle.com/database/122/ERRMG/toc.htm
//!

extern crate libc;
extern crate byteorder;

/// Connections to a database.
///
/// The current implementation only supports a simple connection to the database. There is
/// one user session, but multiple statements can be created. Multiple connections can be created
/// safely because it defaults to setting the OCI environment mode as multi-threaded and
/// therefore the OCI library takes care of concurrency. The cost of this is that a purely single
/// threaded client application might run slower.
///
/// More advanced connection options such as connection and statement pooling are not yet
/// available.
///
/// # Examples
///
/// Connection and statement creation with error handling:
///
/// ```rust
/// use oci_rs::connection::Connection;
///
/// let connection = match Connection::new("localhost:1521/xe",
///                                        "oci_rs",
///                                        "test") {
///     Ok(conn) => conn,
///     Err(err) => panic!("Failed to create a connection: {}", err),
/// };
///
/// let sql_create = "CREATE TABLE Cats (CatId INTEGER,
///                                      Name VARCHAR(20))";
///
/// let create = match connection.create_prepared_statement(sql_create) {
///     Ok(stmt) => stmt,
///     Err(err) => panic!("Failed to create a statement: {}", err),
/// };
/// ```
///
pub mod connection;

/// Errors.
///
/// Any errors arising from interaction with the OCI library will be returned as an `OciError`. All
/// Oracle errors will be returned as the `OciError::Oracle` type. The Oracle error
/// code and description can be seen through this.
///
/// # Examples
///
/// Here is an example of how an Oracle error will reach you if you choose to display it:
///
/// ```rust,should_panic
/// use oci_rs::connection::Connection;
///
/// let conn = Connection::new("localhost:1521/xe", "oci_rs", "test").unwrap();
///
/// // Create a table
/// let sql_create = "CREATE TABLE BrokenToys (ToyId int,
///                                            Name varchar(20),
///                                            Price sink)";
/// let mut create = conn.create_prepared_statement(sql_create).unwrap();
/// if let Err(err) = create.execute() {
///     panic!("Execution failed: {}", err)
/// }
/// ```
///
/// The above code will produce the following (this specific output comes from
/// running this documentation test without the `should_panic` attribute):
///
/// ```text
/// thread 'main' panicked at 'Execution failed: Executing statement
/// Error number: 1
/// Error code: ORA-902
/// Error text: ORA-00902: invalid datatype
/// ', <anon>:13
/// note: Run with `RUST_BACKTRACE=1` for a backtrace.
/// ```
/// In this case "sink" is not a valid SQL data type.
///
/// Note that there might be more than one error and in such cases all errors will be listed, this
/// is why there is an error number.
///
pub mod oci_error;

/// Types used in conversion between OCI and Rust types.
///
/// This module provides a type `SqlValue` and two traits `ToSqlValue` and `FromSqlValue` that
/// allow the underlying OCI types to be converted into Rust types. They do not map exactly to the
/// corresponding SQL standard types.
///
/// For number types in particular there are less `SqlValue`s than SQL types. Inside Oracle all
/// numbers are stored as a `NUMBER`. This is an Oracle format that can handle all integer and
/// float values with a precision of 38 digits. Regardless of whether the SQL statement specifies
/// an `INTEGER` or `FLOAT` or `LONG`, Oracle will store it as a `NUMBER`. The OCI library then allows you
/// to convert it into any numeric type you like, but that forces you to explicitly state the type
/// of the columns when retrieving the values. To avoid this, this crate makes some executive
/// decisions based on the `NUMBER` value. As per the OCI documentation the basic type of a number can be
/// determined by the scale and precision of the `NUMBER` value. If the precision is non-zero and
/// scale is -127 then the number is a `FLOAT` otherwise we can consider it an `INTEGER`. 
/// So, according to this logic the caller will receive either `SqlValue::Integer` or `SqlValue::Float`. 
/// These two variants contain an `i64` and `f64` respectively. If a smaller type is needed in Rust code, 
/// then further conversions can be made. This appears to be sufficient to allow retrieval of data in 
/// queries, without having specify column types on the Rust side ahead of time.
///
/// Note: Oracle also supports types known as `BINARY_FLOAT` and `BINARY_DOUBLE`. These can also be
/// used to store numbers inside the database as an alternative to `NUMBER`. They are not currently
/// supported.
///
/// The traits allow conversion to and from Rust types into `SqlValue`.
///
/// # Examples
///
/// This example highlights the automatic conversion. If it is confusing then I suggest reading
/// [Communicating Intent][1] as it explains very well how Rust's trait system makes this work,
/// the [`postgres`][2] crate also makes use of the same process to
/// convert the column values in a result row into Rust. This crate copies `postgres`'s approach except that it
/// makes use of an intermediary `SqlValue` instead of returning a trait. I think that it is fair
/// to argue that `SqlValue` is not needed, `postgres` skips such an intermediary value, but using
/// it simplifies the current implementation.
///
/// ```rust
/// use oci_rs::connection::Connection;
///
/// let conn = Connection::new("localhost:1521/xe", "oci_rs", "test").unwrap();
/// # let mut drop = conn.create_prepared_statement("DROP TABLE Men").unwrap();
/// # drop.execute().ok();
///
/// // Create a table
/// let sql_create = "CREATE TABLE Men (ManId INTEGER,
///                                     Name VARCHAR2(20),
///                                     Height FLOAT)";
///
/// let mut create = conn.create_prepared_statement(sql_create).unwrap();
///
/// // Execute the create statement
/// create.execute().unwrap();
///
/// // Commit in case we lose connection (an abnormal disconnection would result
/// // in an automatic roll-back.)
/// create.commit().unwrap();
///
/// // Insert some values
/// let sql_insert = "INSERT INTO Men (ManId, Name, Height)
///                   VALUES (1, 'Roger', 183.4)";
///  let mut insert = conn.create_prepared_statement(sql_insert).unwrap();
///
/// insert.execute().unwrap();
/// insert.commit().unwrap();
///
/// // Create a query
/// let sql_select = "SELECT * FROM Men";
///
/// let mut select = conn.create_prepared_statement(sql_select).unwrap();
///
/// // Execute
/// select.execute().unwrap();
///
/// // Get the result set
/// let result_set = select.result_set().unwrap();
/// assert_eq!(result_set.len(), 1);
/// let first_row = &result_set[0];
///
/// // Types are automatically converted
/// let id: i64 = first_row[0].value().unwrap();
/// let name: String = first_row[1].value().unwrap();
/// let height: f64 = first_row[2].value().unwrap();
///
/// assert_eq!(id, 1);
/// assert_eq!(name, "Roger");
/// assert_eq!(height, 183.4);
///
/// // Integer and Float can also be turned into Strings
/// let id_as_string: String = first_row[0].value().unwrap();
/// let height_as_string: String = first_row[2].value().unwrap();
///
/// assert_eq!(id_as_string, "1");
/// assert_eq!(height_as_string, "183.4");
/// ```
///
/// [1]: https://github.com/jaheba/stuff/blob/master/communicating_intent.md
/// [2]: https://crates.io/crates/postgres
pub mod types;

/// Rows of data returned from a query
///
/// A `Row` represents a row of data returned from a SQL query. Internally it holds the columns
/// and their values. It implements the `Index` trait and so columns can be accessed via an index
/// number.
///
pub mod row;

/// SQL statements run against the database.
///
/// `Statement`s are created to run a SQL Statement against a database. They prepare the statement
/// for execution and allow bind variables to be set. If there are results then these these can be
/// returned all in one go or lazily through an iterator.
///
/// # Overview
///
/// The process is as follows:
///
/// 1. Create a `Statement` from a connection with a given SQL statement. This will create a
///    prepared statement on the Oracle side.
/// 2. If the SQL contains bind variable placeholders then these values should now be set via a
///    call to `.bind`. Although OCI supports both positional and named bind variables, only
///    positional are curently support by `Statement`. Oracle uses the form `:name` where `name`is
///    the bind variable.
/// 3. Execute the statement.
/// 4. Commit the transaction if data was changed. Oracle implicitly creates a transaction when data
///    is changed and commits automatically with a normal session close and log-off. If we
///    disconnect abnormally however, a rollback is initiated.
/// 5. If there are results i.e. it was a `SELECT` statement, then fetch the results. The entire
///    result set can be returned as a `Vec<Row>` or instead an iterator can be used to return the
///    `Row`s one by one. These are fetched from OCI by the iterator as needed.
///
/// A connection can create multiple `Statement`s. In the examples in this document there is
/// usually one for each of the `DROP`, `CREATE`, `INSERT` and `SELECT` SQL statements used in the
/// examples.
///
/// # Examples
///
/// We will run through the above process to create a table, add some values and then return them
/// lazily. Every OCI call can fail and the below example avoids handling errors in order to make
/// the example easier to read, notice how many `.unwrap`s are here.
///
/// ```rust
/// use oci_rs::connection::Connection;
/// use oci_rs::row::Row;
///
/// let conn = Connection::new("localhost:1521/xe", "oci_rs", "test").unwrap();
/// # let mut drop = conn.create_prepared_statement("DROP TABLE Cities").unwrap();
/// # drop.execute().ok();
///
/// // Create a table
/// let sql_create = "CREATE TABLE Cities (CityId INTEGER,
///                                        Name VARCHAR(20))";
///
/// let mut create = conn.create_prepared_statement(sql_create).unwrap();
///
/// // Execute the create statement
/// create.execute().unwrap();
///
/// // Commit in case we lose connection (an abnormal disconnection would result
/// // in an automatic roll-back.)
/// create.commit().unwrap();
///
/// // Insert some values using bind variables
/// let sql_insert = "INSERT INTO Cities (CityId, Name)
///                   VALUES (:id, :name)";
/// let mut insert = conn.create_prepared_statement(sql_insert).unwrap();
///
/// let values = vec![(1, "Paris"),
///                   (2, "London"),
///                   (3, "Hamburg"),
///                   (4, "Miami")];
///
/// // Run through the list of values, bind them and execute the statement
/// for value in values.iter() {
///     insert.bind(&[&value.0, &value.1]).unwrap();
///     insert.execute().unwrap()
/// }
///
/// insert.commit().unwrap();
///
/// // Create a query
/// let sql_select = "SELECT * FROM Cities";
///
/// let mut select = conn.create_prepared_statement(sql_select).unwrap();
///
/// // Execute
/// select.execute().unwrap();
///
/// // Get the result set row by row from an iterator
/// for (index, row_result) in select.lazy_result_set().enumerate(){
///     let row = row_result.unwrap();
///     let city_id: i64 = row[0].value().unwrap();
///     let city_name: String = row[1].value().unwrap();
///     assert_eq!(city_id, values[index].0);
///     assert_eq!(city_name, values[index].1);
/// }
///
/// // Or perhaps something a bit more convoluted just to make use of iterator adapters
///
/// // Execute again to get fresh results
/// select.execute().unwrap();
///
/// // Get cities containing an 'a':
/// let results: Vec<String> = select.lazy_result_set()
///                                  .map(|row_result| {
///                                           let row = row_result.unwrap();
///                                           row[1].value::<String>().unwrap()
///                                       })
///                                  .filter(|city| city.contains("a"))
///                                  .collect();
///
/// let correct_result = vec!["Paris".to_string(),
///                           "Hamburg".to_string(),
///                           "Miami".to_string()];
/// assert_eq!(results, correct_result);
/// ```
/// The final example is a bit awkard because we have `Result`s and `Option`s to deal with
/// (or ignored as in this case) but it is added as a reminder that iterator methods can be used.
///
pub mod statement;
mod oci_bindings;


#[cfg(test)]
mod tests {
    use connection::Connection;
    const CONNECTION: &str = "localhost:1521/xe";
    const USER: &str = "oci_rs";
    const PASSWORD: &str = "test";

    #[test]
    #[allow(unused_variables)]
    fn create_connection() {
        let conn = match Connection::new(CONNECTION, USER, PASSWORD) {
            Ok(conn) => conn,
            Err(err) => panic!("Failed to create a connection: {}", err),
        };
    }

    #[test]
    fn create_prepared_statement() {
        let conn = match Connection::new(CONNECTION, USER, PASSWORD) {
            Ok(conn) => conn,
            Err(err) => panic!("Failed to create a connection: {}", err),
        };
        let sql_drop = "DROP TABLE Books";
        let mut drop = match conn.create_prepared_statement(sql_drop) {
            Ok(s) => s,
            Err(err) => panic!("Failed to prepare drop Books: {}", err),
        };
        drop.execute().ok();
        let sql_create = "CREATE TABLE Books(BookID int, Name varchar(200))";
        let mut stmt = match conn.create_prepared_statement(sql_create) {
            Ok(s) => s,
            Err(err) => panic!("Failed to create a statement: {}", err),
        };
        if let Err(err) = stmt.execute() {
            panic!("Failed to execute: {}", err)
        }
        if let Err(err) = stmt.commit() {
            panic!("Failed to commit: {}", err)
        }
    }

    #[test]
    fn bind() {
        let conn = match Connection::new(CONNECTION, USER, PASSWORD) {
            Ok(conn) => conn,
            Err(err) => panic!("Failed to create a connection: {}", err),
        };
        let sql_drop = "DROP TABLE Fruit";
        let mut drop = match conn.create_prepared_statement(sql_drop) {
            Ok(s) => s,
            Err(err) => panic!("{}", err),
        };
        drop.execute().ok();
        let sql_create = "CREATE TABLE Fruit(FruitId integer, Name varchar(20))";
        let mut create = match conn.create_prepared_statement(sql_create) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = create.execute() {
            panic!("{}", err)
        }
        let sql_insert = "INSERT INTO Fruit(FruitId, Name) VALUES(:id, :name)";
        let mut insert = match conn.create_prepared_statement(sql_insert) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        let name = String::from("Apple");
        let id: i64 = 22;
        if let Err(err) = insert.bind(&[&id, &name]) {
            panic!("{}", err)
        }
        if let Err(err) = insert.execute() {
            panic!("{}", err)
        }
        let name = String::from("Pear");
        let id: i64 = 23;
        if let Err(err) = insert.bind(&[&id, &name]) {
            panic!("{}", err)
        }
        if let Err(err) = insert.execute() {
            panic!("{}", err)
        }
        if let Err(err) = insert.bind(&[&24, &"Banana".to_string()]) {
            panic!("{}", err)
        }
        if let Err(err) = insert.execute() {
            panic!("{}", err)
        }
        if let Err(err) = insert.commit() {
            panic!("{}", err)
        }
    }

    #[test]
    fn query() {
        let conn = match Connection::new(CONNECTION, USER, PASSWORD) {
            Ok(conn) => conn,
            Err(err) => panic!("Failed to create a connection: {}", err),
        };
        let sql_drop = "DROP TABLE Cars";
        let mut drop = match conn.create_prepared_statement(sql_drop) {
            Ok(s) => s,
            Err(err) => panic!("{}", err),
        };
        drop.execute().ok();
        let sql_create = "CREATE TABLE Cars(CarId integer, Name varchar(20))";
        let mut create = match conn.create_prepared_statement(sql_create) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = create.execute() {
            panic!("{}", err)
        }
        let sql_insert = "INSERT INTO Cars(CarId, Name) VALUES('12', 'BMW')";
        let mut insert = match conn.create_prepared_statement(sql_insert) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = insert.execute() {
            panic!("{}", err)
        }
        let sql_query = "SELECT * FROM Cars";
        let mut select = match conn.create_prepared_statement(sql_query) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = select.execute() {
            panic!("{}", err)
        }
        let result_set = match select.result_set() {
            Ok(res) => res,
            Err(err) => panic!("{}", err),
        };
        if result_set.is_empty() {
            panic!("Should not have an empty result")
        }
        let row = &result_set[0];
        let car_id: i64 = row[0].value().expect("Not an i64");
        assert_eq!(car_id, 12);
        let car_name: String = row[1].value().expect("Not a string");
        assert_eq!(car_name, "BMW")
    }

    #[test]
    fn multi_row_query() {
        let conn = match Connection::new(CONNECTION, USER, PASSWORD) {
            Ok(conn) => conn,
            Err(err) => panic!("Failed to create a connection: {}", err),
        };
        let sql_drop = "DROP TABLE Flowers";
        let mut drop = match conn.create_prepared_statement(sql_drop) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        drop.execute().ok();
        let sql_create = "CREATE TABLE Flowers(FlowerId integer, Name varchar(20))";
        let mut create = match conn.create_prepared_statement(sql_create) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = create.execute() {
            panic!("{}", err)
        }
        let sql_insert = "INSERT INTO Flowers(FlowerId, Name) VALUES(:id, :name)";
        let mut insert = match conn.create_prepared_statement(sql_insert) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = insert.bind(&[&1, &"Rose".to_string()]) {
            panic!("{}", err)
        }
        if let Err(err) = insert.execute() {
            panic!("{}", err)
        }
        if let Err(err) = insert.bind(&[&2, &"Tulip".to_string()]) {
            panic!("{}", err)
        }
        if let Err(err) = insert.execute() {
            panic!("{}", err)
        }
        let sql_query = "SELECT * FROM Flowers";
        let mut select = match conn.create_prepared_statement(sql_query) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = select.execute() {
            panic!("{}", err)
        }
        let result_set = match select.result_set() {
            Ok(res) => res,
            Err(err) => panic!("{}", err),
        };
        if result_set.is_empty() {
            panic!("Should not have an empty result")
        }
        let pairs = [(1, "Rose"), (2, "Tulip")];
        for (index, pair) in pairs.iter().enumerate() {
            let row = &result_set[index];
            let flower_id: i64 = row[0].value().expect("Not an i64");
            let flower_name: String = row[1].value().expect("Not a string");
            assert_eq!(flower_id, pair.0);
            assert_eq!(flower_name, pair.1);
        }
    }

    #[test]
    fn lazy_multi_row_query() {
        let conn = match Connection::new(CONNECTION, USER, PASSWORD) {
            Ok(conn) => conn,
            Err(err) => panic!("Failed to create a connection: {}", err),
        };
        let sql_drop = "DROP TABLE Birds";
        let mut drop = match conn.create_prepared_statement(sql_drop) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        drop.execute().ok();
        let sql_create = "CREATE TABLE Birds(BirdId integer, Name varchar(20))";
        let mut create = match conn.create_prepared_statement(sql_create) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = create.execute() {
            panic!("{}", err)
        }
        let sql_insert = "INSERT INTO Birds(BirdId, Name) VALUES(:id, :name)";
        let mut insert = match conn.create_prepared_statement(sql_insert) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = insert.bind(&[&1, &"Chafinch".to_string()]) {
            panic!("{}", err)
        }
        if let Err(err) = insert.execute() {
            panic!("{}", err)
        }
        if let Err(err) = insert.bind(&[&2, &"Eagle".to_string()]) {
            panic!("{}", err)
        }
        if let Err(err) = insert.execute() {
            panic!("{}", err)
        }
        let sql_query = "SELECT * FROM Birds";
        let mut select = match conn.create_prepared_statement(sql_query) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = select.execute() {
            panic!("{}", err)
        }
        let mut result_set = Vec::new();
        for row_result in select.lazy_result_set() {
            match row_result {
                Ok(row) => result_set.push(row),
                Err(err) => panic!("{}", err),
            }
        }
        if result_set.is_empty() {
            panic!("Should not have an empty result")
        }
        let pairs = [(1, "Chafinch"), (2, "Eagle")];
        for (index, pair) in pairs.iter().enumerate() {
            let row = &result_set[index];
            let bird_id: i64 = row[0].value().expect("Not an i64");
            let bird_name: String = row[1].value().expect("Not a string");
            assert_eq!(bird_id, pair.0);
            assert_eq!(bird_name, pair.1);
        }
    }

    #[test]
    fn number_conversion() {
        let conn = match Connection::new(CONNECTION, USER, PASSWORD) {
            Ok(conn) => conn,
            Err(err) => panic!("Failed to create a connection: {}", err),
        };
        let sql_drop = "DROP TABLE Sweets";
        let mut drop = match conn.create_prepared_statement(sql_drop) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        drop.execute().ok();
        let sql_create = "CREATE TABLE Sweets(SweetId integer, Name varchar(20), Price float)";
        let mut create = match conn.create_prepared_statement(sql_create) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = create.execute() {
            panic!("{}", err)
        }
        let sql_insert = "INSERT INTO Sweets(SweetId, Name, Price) VALUES(:id, :name, :price)";
        let mut insert = match conn.create_prepared_statement(sql_insert) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        let values = [(1, "Toffee", 22.5), (2, "Haribo", -4.0), (3, "Gobstoppers", 34.5657)];
        for value in values.iter() {
            if let Err(err) = insert.bind(&[&value.0, &value.1.to_string(), &value.2]) {
                panic!("{}", err)
            }
            if let Err(err) = insert.execute() {
                panic!("{}", err)
            }
        }
        let sql_query = "SELECT * FROM Sweets";
        let mut query = match conn.create_prepared_statement(sql_query) {
            Ok(stmt) => stmt,
            Err(err) => panic!("{}", err),
        };
        if let Err(err) = query.execute() {
            panic!("{}", err)
        }
        let result_set = match query.result_set() {
            Ok(res) => res,
            Err(err) => panic!("{}", err),
        };
        if result_set.is_empty() {
            panic!("Should not have an empty result")
        }
        for (index, value) in values.iter().enumerate() {
            let row = &result_set[index];
            let sweet_id: i64 = row[0].value().expect("Not an i64");
            let sweet_name: String = row[1].value().expect("Not a string");
            let sweet_price: f64 = match row[2].value() {
                Some(p) => p,
                None => panic!("{:?}", row[2]),
            };
            assert_eq!(sweet_id, value.0);
            assert_eq!(sweet_name, value.1);
            assert_eq!(sweet_price, value.2);
        }
    }
}