|
41 | 41 | //! ## Example |
42 | 42 | //! |
43 | 43 | //! ```rust |
44 | | -//! # mysql::doctest_wrapper!(__result, { |
45 | 44 | //! use mysql::*; |
46 | 45 | //! use mysql::prelude::*; |
47 | 46 | //! |
|
52 | 51 | //! account_name: Option<String>, |
53 | 52 | //! } |
54 | 53 | //! |
55 | | -//! let url = "mysql://root:password@localhost:3307/db_name"; |
56 | | -//! # Opts::try_from(url)?; |
57 | | -//! # let url = get_opts(); |
58 | | -//! |
59 | | -//! let pool = Pool::new(url)?; |
60 | | -//! |
61 | | -//! let mut conn = pool.get_conn()?; |
62 | | -//! |
63 | | -//! // Let's create a table for payments. |
64 | | -//! conn.query_drop( |
65 | | -//! r"CREATE TEMPORARY TABLE payment ( |
66 | | -//! customer_id int not null, |
67 | | -//! amount int not null, |
68 | | -//! account_name text |
69 | | -//! )")?; |
70 | | -//! |
71 | | -//! let payments = vec![ |
72 | | -//! Payment { customer_id: 1, amount: 2, account_name: None }, |
73 | | -//! Payment { customer_id: 3, amount: 4, account_name: Some("foo".into()) }, |
74 | | -//! Payment { customer_id: 5, amount: 6, account_name: None }, |
75 | | -//! Payment { customer_id: 7, amount: 8, account_name: None }, |
76 | | -//! Payment { customer_id: 9, amount: 10, account_name: Some("bar".into()) }, |
77 | | -//! ]; |
78 | | -//! |
79 | | -//! // Now let's insert payments to the database |
80 | | -//! conn.exec_batch( |
81 | | -//! r"INSERT INTO payment (customer_id, amount, account_name) |
82 | | -//! VALUES (:customer_id, :amount, :account_name)", |
83 | | -//! payments.iter().map(|p| params! { |
84 | | -//! "customer_id" => p.customer_id, |
85 | | -//! "amount" => p.amount, |
86 | | -//! "account_name" => &p.account_name, |
87 | | -//! }) |
88 | | -//! )?; |
89 | | -//! |
90 | | -//! // Let's select payments from database. Type inference should do the trick here. |
91 | | -//! let selected_payments = conn |
92 | | -//! .query_map( |
93 | | -//! "SELECT customer_id, amount, account_name from payment", |
94 | | -//! |(customer_id, amount, account_name)| { |
95 | | -//! Payment { customer_id, amount, account_name } |
96 | | -//! }, |
| 54 | +//! # def_get_opts!(); |
| 55 | +//! |
| 56 | +//! fn main() -> std::result::Result<(), Box<dyn std::error::Error>> { |
| 57 | +//! let url = "mysql://root:password@localhost:3307/db_name"; |
| 58 | +//! # Opts::try_from(url)?; |
| 59 | +//! # let url = get_opts(); |
| 60 | +//! let pool = Pool::new(url)?; |
| 61 | +//! |
| 62 | +//! let mut conn = pool.get_conn()?; |
| 63 | +//! |
| 64 | +//! // Let's create a table for payments. |
| 65 | +//! conn.query_drop( |
| 66 | +//! r"CREATE TEMPORARY TABLE payment ( |
| 67 | +//! customer_id int not null, |
| 68 | +//! amount int not null, |
| 69 | +//! account_name text |
| 70 | +//! )")?; |
| 71 | +//! |
| 72 | +//! let payments = vec![ |
| 73 | +//! Payment { customer_id: 1, amount: 2, account_name: None }, |
| 74 | +//! Payment { customer_id: 3, amount: 4, account_name: Some("foo".into()) }, |
| 75 | +//! Payment { customer_id: 5, amount: 6, account_name: None }, |
| 76 | +//! Payment { customer_id: 7, amount: 8, account_name: None }, |
| 77 | +//! Payment { customer_id: 9, amount: 10, account_name: Some("bar".into()) }, |
| 78 | +//! ]; |
| 79 | +//! |
| 80 | +//! // Now let's insert payments to the database |
| 81 | +//! conn.exec_batch( |
| 82 | +//! r"INSERT INTO payment (customer_id, amount, account_name) |
| 83 | +//! VALUES (:customer_id, :amount, :account_name)", |
| 84 | +//! payments.iter().map(|p| params! { |
| 85 | +//! "customer_id" => p.customer_id, |
| 86 | +//! "amount" => p.amount, |
| 87 | +//! "account_name" => &p.account_name, |
| 88 | +//! }) |
97 | 89 | //! )?; |
98 | 90 | //! |
99 | | -//! // Let's make sure, that `payments` equals to `selected_payments`. |
100 | | -//! // Mysql gives no guaranties on order of returned rows |
101 | | -//! // without `ORDER BY`, so assume we are lucky. |
102 | | -//! assert_eq!(payments, selected_payments); |
103 | | -//! println!("Yay!"); |
104 | | -//! # }); |
| 91 | +//! // Let's select payments from database. Type inference should do the trick here. |
| 92 | +//! let selected_payments = conn |
| 93 | +//! .query_map( |
| 94 | +//! "SELECT customer_id, amount, account_name from payment", |
| 95 | +//! |(customer_id, amount, account_name)| { |
| 96 | +//! Payment { customer_id, amount, account_name } |
| 97 | +//! }, |
| 98 | +//! )?; |
| 99 | +//! |
| 100 | +//! // Let's make sure, that `payments` equals to `selected_payments`. |
| 101 | +//! // Mysql gives no guaranties on order of returned rows |
| 102 | +//! // without `ORDER BY`, so assume we are lucky. |
| 103 | +//! assert_eq!(payments, selected_payments); |
| 104 | +//! println!("Yay!"); |
| 105 | +//! |
| 106 | +//! Ok(()) |
| 107 | +//! } |
105 | 108 | //! ``` |
106 | 109 | //! |
107 | 110 | //! ## Crate Features |
|
129 | 132 | //! * **mysql_common/uuid** – the `uuid` is enabled by default |
130 | 133 | //! * **mysql_common/frunk** – the `frunk` is enabled by default |
131 | 134 | //! |
132 | | -//! Please note, that you'll need to reenable external features if you are using `no-default-features = true`: |
| 135 | +//! Please note, that you'll need to reenable external features if you are using `default-features = false`: |
133 | 136 | //! |
134 | 137 | //! ```toml |
135 | 138 | //! [dependencies] |
136 | 139 | //! # Lets say that we want to use the `rustls-tls` feature: |
137 | | -//! mysql = { version = "*", no-default-features = true, features = ["rustls-tls", "buffer-pool"] } |
| 140 | +//! mysql = { version = "*", default-features = false, features = ["rustls-tls", "buffer-pool"] } |
138 | 141 | //! # Previous line disables default mysql features, |
139 | 142 | //! # so now we have to choose the flate2 backend (this is necessary), |
140 | 143 | //! # as well as the desired set of mysql_common features: |
141 | | -//! flate2 = { version = "*", no-default-features = true, features = ["zlib"] } |
142 | | -//! mysql_common = { version = "*", no-default-features = true, features = ["bigdecimal03", "time03", "uuid"]} |
| 144 | +//! flate2 = { version = "*", default-features = false, features = ["zlib"] } |
| 145 | +//! mysql_common = { version = "*", default-features = false, features = ["bigdecimal03", "time03", "uuid"]} |
143 | 146 | //! ``` |
144 | 147 | //! |
145 | 148 | //! ## API Documentation |
|
425 | 428 | //! assert_eq!(row[i], Value::Int(i as i64)); |
426 | 429 | //! } |
427 | 430 | //! |
| 431 | +//! // Another way to handle wide rows is to use HList (requires `mysql_common/frunk` feature) |
| 432 | +//! use frunk::{HList, hlist, hlist_pat}; |
| 433 | +//! let query = "select 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"; |
| 434 | +//! type RowType = HList!(u8, u16, u32, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8, u8); |
| 435 | +//! let first_three_columns = conn.query_map(query, |row: RowType| { |
| 436 | +//! // do something with the row (see the `frunk` crate documentation) |
| 437 | +//! let hlist_pat![c1, c2, c3, ...] = row; |
| 438 | +//! (c1, c2, c3) |
| 439 | +//! }); |
| 440 | +//! assert_eq!(first_three_columns.unwrap(), vec![(0_u8, 1_u16, 2_u32)]); |
| 441 | +//! |
428 | 442 | //! // Some unknown row |
429 | 443 | //! let row: Row = conn.query_first( |
430 | 444 | //! // ... |
|
0 commit comments