-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
mod.rs
188 lines (160 loc) · 5.85 KB
/
mod.rs
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
mod bind;
mod raw;
mod stmt;
mod url;
use self::raw::RawConnection;
use self::stmt::iterator::StatementIterator;
use self::stmt::Statement;
use self::url::ConnectionOptions;
use super::backend::Mysql;
use crate::connection::*;
use crate::expression::QueryMetadata;
use crate::query_builder::bind_collector::RawBytesBindCollector;
use crate::query_builder::*;
use crate::result::*;
#[allow(missing_debug_implementations, missing_copy_implementations)]
/// A connection to a MySQL database. Connection URLs should be in the form
/// `mysql://[user[:password]@]host/database_name`
pub struct MysqlConnection {
raw_connection: RawConnection,
transaction_state: AnsiTransactionManager,
statement_cache: StatementCache<Mysql, Statement>,
}
unsafe impl Send for MysqlConnection {}
impl SimpleConnection for MysqlConnection {
fn batch_execute(&mut self, query: &str) -> QueryResult<()> {
self.raw_connection
.enable_multi_statements(|| self.raw_connection.execute(query))
}
}
impl<'a> ConnectionGatWorkaround<'a, Mysql> for MysqlConnection {
type Cursor = self::stmt::iterator::StatementIterator<'a>;
type Row = self::stmt::iterator::MysqlRow;
}
impl Connection for MysqlConnection {
type Backend = Mysql;
type TransactionManager = AnsiTransactionManager;
fn establish(database_url: &str) -> ConnectionResult<Self> {
use crate::result::ConnectionError::CouldntSetupConfiguration;
let raw_connection = RawConnection::new();
let connection_options = ConnectionOptions::parse(database_url)?;
raw_connection.connect(&connection_options)?;
let mut conn = MysqlConnection {
raw_connection,
transaction_state: AnsiTransactionManager::default(),
statement_cache: StatementCache::new(),
};
conn.set_config_options()
.map_err(CouldntSetupConfiguration)?;
Ok(conn)
}
#[doc(hidden)]
fn execute(&mut self, query: &str) -> QueryResult<usize> {
self.raw_connection
.execute(query)
.map(|_| self.raw_connection.affected_rows())
}
#[doc(hidden)]
fn load<T>(
&mut self,
source: T,
) -> QueryResult<<Self as ConnectionGatWorkaround<Self::Backend>>::Cursor>
where
T: AsQuery,
T::Query: QueryFragment<Self::Backend> + QueryId,
Self::Backend: QueryMetadata<T::SqlType>,
{
let stmt = self.prepared_query(&source.as_query())?;
let mut metadata = Vec::new();
Mysql::row_metadata(&mut (), &mut metadata);
StatementIterator::from_stmt(stmt, &metadata)
}
#[doc(hidden)]
fn execute_returning_count<T>(&mut self, source: &T) -> QueryResult<usize>
where
T: QueryFragment<Self::Backend> + QueryId,
{
let stmt = self.prepared_query(source)?;
unsafe {
stmt.execute()?;
}
Ok(stmt.affected_rows())
}
#[doc(hidden)]
fn transaction_state(&mut self) -> &mut AnsiTransactionManager {
&mut self.transaction_state
}
}
impl MysqlConnection {
fn prepared_query<'a, T: QueryFragment<Mysql> + QueryId>(
&'a mut self,
source: &'_ T,
) -> QueryResult<MaybeCached<'a, Statement>> {
let cache = &mut self.statement_cache;
let conn = &mut self.raw_connection;
let mut stmt = cache.cached_statement(source, &[], |sql, _| conn.prepare(sql))?;
let mut bind_collector = RawBytesBindCollector::new();
source.collect_binds(&mut bind_collector, &mut ())?;
let binds = bind_collector
.metadata
.into_iter()
.zip(bind_collector.binds);
stmt.bind(binds)?;
Ok(stmt)
}
fn set_config_options(&mut self) -> QueryResult<()> {
self.execute("SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT'))")?;
self.execute("SET time_zone = '+00:00';")?;
self.execute("SET character_set_client = 'utf8mb4'")?;
self.execute("SET character_set_connection = 'utf8mb4'")?;
self.execute("SET character_set_results = 'utf8mb4'")?;
Ok(())
}
}
#[cfg(test)]
mod tests {
extern crate dotenv;
use super::*;
use std::env;
fn connection() -> MysqlConnection {
dotenv::dotenv().ok();
let database_url = env::var("MYSQL_UNIT_TEST_DATABASE_URL")
.or_else(|_| env::var("MYSQL_DATABASE_URL"))
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run unit tests");
MysqlConnection::establish(&database_url).unwrap()
}
#[test]
fn batch_execute_handles_single_queries_with_results() {
let connection = &mut connection();
assert!(connection.batch_execute("SELECT 1").is_ok());
assert!(connection.batch_execute("SELECT 1").is_ok());
}
#[test]
fn batch_execute_handles_multi_queries_with_results() {
let connection = &mut connection();
let query = "SELECT 1; SELECT 2; SELECT 3;";
assert!(connection.batch_execute(query).is_ok());
assert!(connection.batch_execute(query).is_ok());
}
#[test]
fn execute_handles_queries_which_return_results() {
let connection = &mut connection();
assert!(connection.execute("SELECT 1").is_ok());
assert!(connection.execute("SELECT 1").is_ok());
}
#[test]
fn check_client_found_rows_flag() {
let conn = &mut crate::test_helpers::connection();
conn.execute("DROP TABLE IF EXISTS update_test CASCADE")
.unwrap();
conn.execute("CREATE TABLE update_test(id INTEGER PRIMARY KEY, num INTEGER NOT NULL)")
.unwrap();
conn.execute("INSERT INTO update_test(id, num) VALUES (1, 5)")
.unwrap();
let output = conn
.execute("UPDATE update_test SET num = 5 WHERE id = 1")
.unwrap();
assert_eq!(output, 1);
}
}