diesel/mariadb/connection.rs
1use crate::mariadb::Mariadb;
2
3/// A connection to a Mariadb database. Connection URLs should be in the form
4/// `mariadb://[user[:password]@]host/database_name[?unix_socket=socket-path]`
5///
6///* `host` can be an IP address or a hostname. If it is set to `localhost`, a connection
7/// will be attempted through the socket at `/tmp/mysql.sock`. If you want to connect to
8/// a local server via TCP (e.g. docker containers), use `0.0.0.0` or `127.0.0.1` instead.
9/// * `unix_socket` expects the path to the unix socket
10///
11/// # Supported loading model implementations
12///
13/// * [`DefaultLoadingMode`](crate::connection::DefaultLoadingMode)
14///
15/// As `MariadbConnection` only supports a single loading mode implementation
16/// it is **not required** to explicitly specify a loading mode
17/// when calling [`RunQueryDsl::load_iter()`](crate::query_dsl::RunQueryDsl::load_iter)
18/// or [`LoadConnection::load`](crate::connection::LoadConnection::load)
19///
20/// ## DefaultLoadingMode
21///
22/// `MariadbConnection` only supports a single loading mode, which loads
23/// values row by row from the result set.
24///
25/// ```rust
26/// # include!("../doctest_setup.rs");
27/// #
28/// # fn main() {
29/// # run_test().unwrap();
30/// # }
31/// #
32/// # fn run_test() -> QueryResult<()> {
33/// # use schema::users;
34/// # let connection = &mut establish_connection();
35/// use diesel::connection::DefaultLoadingMode;
36/// { // scope to restrict the lifetime of the iterator
37/// let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
38///
39/// for r in iter1 {
40/// let (id, name) = r?;
41/// println!("Id: {} Name: {}", id, name);
42/// }
43/// }
44///
45/// // works without specifying the loading mode
46/// let iter2 = users::table.load_iter::<(i32, String), _>(connection)?;
47///
48/// for r in iter2 {
49/// let (id, name) = r?;
50/// println!("Id: {} Name: {}", id, name);
51/// }
52/// # Ok(())
53/// # }
54/// ```
55///
56/// This mode does **not support** creating
57/// multiple iterators using the same connection.
58///
59/// ```compile_fail
60/// # include!("../../doctest_setup.rs");
61/// #
62/// # fn main() {
63/// # run_test().unwrap();
64/// # }
65/// #
66/// # fn run_test() -> QueryResult<()> {
67/// # use schema::users;
68/// # let connection = &mut establish_connection();
69/// use diesel::connection::DefaultLoadingMode;
70///
71/// let iter1 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
72/// let iter2 = users::table.load_iter::<(i32, String), DefaultLoadingMode>(connection)?;
73///
74/// for r in iter1 {
75/// let (id, name) = r?;
76/// println!("Id: {} Name: {}", id, name);
77/// }
78///
79/// for r in iter2 {
80/// let (id, name) = r?;
81/// println!("Id: {} Name: {}", id, name);
82/// }
83/// # Ok(())
84/// # }
85/// ```
86pub type MariadbConnection = crate::mysql_like::MysqlLikeConnection<Mariadb>;
87
88#[cfg(test)]
89mod tests {
90 extern crate dotenvy;
91
92 use super::*;
93 use std::env;
94
95 use crate::connection::Connection;
96 use crate::connection::SimpleConnection;
97 use crate::query_dsl::RunQueryDsl;
98
99 fn connection() -> MariadbConnection {
100 dotenvy::dotenv().ok();
101 let database_url = env::var("MARIADB_UNIT_TEST_DATABASE_URL")
102 .or_else(|_| env::var("MARIADB_DATABASE_URL"))
103 .or_else(|_| env::var("DATABASE_URL"))
104 .expect("DATABASE_URL must be set in order to run unit tests");
105 MariadbConnection::establish(&database_url).unwrap()
106 }
107
108 #[diesel_test_helper::test]
109 fn batch_execute_handles_single_queries_with_results() {
110 let connection = &mut connection();
111 assert!(connection.batch_execute("SELECT 1").is_ok());
112 assert!(connection.batch_execute("SELECT 1").is_ok());
113 }
114
115 #[diesel_test_helper::test]
116 fn batch_execute_handles_multi_queries_with_results() {
117 let connection = &mut connection();
118 let query = "SELECT 1; SELECT 2; SELECT 3;";
119 assert!(connection.batch_execute(query).is_ok());
120 assert!(connection.batch_execute(query).is_ok());
121 }
122
123 #[diesel_test_helper::test]
124 fn execute_handles_queries_which_return_results() {
125 let connection = &mut connection();
126 assert!(crate::sql_query("SELECT 1").execute(connection).is_ok());
127 assert!(crate::sql_query("SELECT 1").execute(connection).is_ok());
128 }
129
130 #[diesel_test_helper::test]
131 fn check_client_found_rows_flag() {
132 let conn = &mut crate::test_helpers::connection();
133 crate::sql_query("DROP TABLE IF EXISTS update_test CASCADE")
134 .execute(conn)
135 .unwrap();
136
137 crate::sql_query("CREATE TABLE update_test(id INTEGER PRIMARY KEY, num INTEGER NOT NULL)")
138 .execute(conn)
139 .unwrap();
140
141 crate::sql_query("INSERT INTO update_test(id, num) VALUES (1, 5)")
142 .execute(conn)
143 .unwrap();
144
145 let output = crate::sql_query("UPDATE update_test SET num = 5 WHERE id = 1")
146 .execute(conn)
147 .unwrap();
148
149 assert_eq!(output, 1);
150 }
151}