Skip to content

Commit f79c290

Browse files
committed
Minor code cleanup
1 parent f902714 commit f79c290

File tree

3 files changed

+16
-30
lines changed

3 files changed

+16
-30
lines changed

src/mysql/mod.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ impl<'conn, 'query> AsyncConnectionGatWorkaround<'conn, 'query, Mysql> for Async
4545
type Row = MysqlRow;
4646
}
4747

48+
const CONNECTION_SETUP_QUERIES: &'static [&'static str] = &[
49+
"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT'))",
50+
"SET time_zone = '+00:00';",
51+
"SET character_set_client = 'utf8mb4'",
52+
"SET character_set_connection = 'utf8mb4'",
53+
"SET character_set_results = 'utf8mb4'",
54+
];
55+
4856
#[async_trait::async_trait]
4957
impl AsyncConnection for AsyncMysqlConnection {
5058
type Backend = Mysql;
@@ -55,13 +63,7 @@ impl AsyncConnection for AsyncMysqlConnection {
5563
let opts = Opts::from_url(database_url)
5664
.map_err(|e| diesel::result::ConnectionError::InvalidConnectionUrl(e.to_string()))?;
5765
let builder = OptsBuilder::from_opts(opts)
58-
.init(vec![
59-
"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT'))",
60-
"SET time_zone = '+00:00';",
61-
"SET character_set_client = 'utf8mb4'",
62-
"SET character_set_connection = 'utf8mb4'",
63-
"SET character_set_results = 'utf8mb4'",
64-
])
66+
.init(CONNECTION_SETUP_QUERIES.to_vec())
6567
.stmt_cache_size(0); // We have our own cache
6668

6769
let conn = mysql_async::Conn::new(builder).await.map_err(ErrorHelper)?;
@@ -86,7 +88,7 @@ impl AsyncConnection for AsyncMysqlConnection {
8688
+ 'query,
8789
{
8890
self.with_prepared_statement(source.as_query(), |conn, stmt, binds| async move {
89-
let res = conn.exec_iter(&*stmt, binds).await.map_err(ErrorHelper)?;
91+
let res = conn.exec_iter(stmt, binds).await.map_err(ErrorHelper)?;
9092

9193
let stream = res
9294
.stream_and_drop::<MysqlRow>()
@@ -116,7 +118,7 @@ impl AsyncConnection for AsyncMysqlConnection {
116118
+ 'query,
117119
{
118120
self.with_prepared_statement(source, |conn, stmt, binds| async move {
119-
conn.exec_drop(&*stmt, binds).await.map_err(ErrorHelper)?;
121+
conn.exec_drop(stmt, binds).await.map_err(ErrorHelper)?;
120122
Ok(conn.affected_rows() as usize)
121123
})
122124
}
@@ -169,16 +171,9 @@ impl AsyncMysqlConnection {
169171
transaction_manager: AnsiTransactionManager::default(),
170172
last_stmt: None,
171173
};
172-
let setup_statements = vec![
173-
"SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT'))",
174-
"SET time_zone = '+00:00';",
175-
"SET character_set_client = 'utf8mb4'",
176-
"SET character_set_connection = 'utf8mb4'",
177-
"SET character_set_results = 'utf8mb4'",
178-
];
179-
180-
for stmt in setup_statements {
181-
diesel::sql_query(stmt)
174+
175+
for stmt in CONNECTION_SETUP_QUERIES {
176+
diesel::sql_query(*stmt)
182177
.execute(&mut conn)
183178
.await
184179
.map_err(ConnectionError::CouldntSetupConfiguration)?;

src/mysql/row.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'a> diesel::row::Field<'a, Mysql> for MysqlField<'_> {
135135
fn value(&self) -> Option<diesel::backend::RawValue<Mysql>> {
136136
self.value.as_ref().map(|v| {
137137
MysqlValue::new(
138-
&*v,
138+
v,
139139
convert_type(self.column.column_type(), self.column.flags()),
140140
)
141141
})

src/pg/mod.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,7 @@ impl AsyncConnection for AsyncPgConnection {
128128
eprintln!("connection error: {}", e);
129129
}
130130
});
131-
let mut conn = AsyncPgConnection {
132-
conn: Arc::new(client),
133-
stmt_cache: Arc::new(Mutex::new(StmtCache::new())),
134-
transaction_state: AnsiTransactionManager::default(),
135-
metadata_cache: Arc::new(Mutex::new(Some(PgMetadataCache::new()))),
136-
};
137-
conn.set_config_options()
138-
.await
139-
.map_err(ConnectionError::CouldntSetupConfiguration)?;
140-
Ok(conn)
131+
Self::try_from(client).await
141132
}
142133

143134
fn load<'conn, 'query, T>(

0 commit comments

Comments
 (0)