Skip to content

Commit 7fdcca5

Browse files
MySQL: Set character set to utf8 on connection setup
1 parent c578639 commit 7fdcca5

1 file changed

Lines changed: 10 additions & 31 deletions

File tree

src/sql/db_connection_pool/mysqlpool.rs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ pub struct MySQLConnectionPool {
6060
join_push_down: JoinPushDown,
6161
}
6262

63+
const SETUP_QUERIES: [&str; 4] = [
64+
"SET time_zone = '+00:00'",
65+
"SET character_set_results = 'utf8mb4'",
66+
"SET character_set_client = 'utf8mb4'",
67+
"SET character_set_connection = 'utf8mb4'",
68+
];
69+
6370
impl MySQLConnectionPool {
6471
/// Creates a new instance of `MySQLConnectionPool`.
6572
///
@@ -163,23 +170,7 @@ impl MySQLConnectionPool {
163170

164171
connection_string = connection_string.ssl_opts(ssl_opts);
165172

166-
if let Some(mysql_character_set_results) = params
167-
.get("character_set_results")
168-
.map(SecretBox::expose_secret)
169-
{
170-
match mysql_character_set_results {
171-
"utf8mb3" | "utf8mb4" => {
172-
connection_string = connection_string.setup(vec![format!(
173-
"SET character_set_results = {mysql_character_set_results}"
174-
)]);
175-
}
176-
_ => {
177-
return Err(Error::InvalidCharacterSetResults {
178-
value: mysql_character_set_results.to_string(),
179-
})
180-
}
181-
}
182-
}
173+
connection_string = connection_string.setup(SETUP_QUERIES.to_vec());
183174

184175
let opts = mysql_async::Opts::from(connection_string);
185176

@@ -230,13 +221,7 @@ impl MySQLConnectionPool {
230221
/// Returns an error if there is a problem creating the connection pool.
231222
pub async fn connect_direct(&self) -> super::Result<MySQLConnection> {
232223
let pool = Arc::clone(&self.pool);
233-
let mut conn = pool.get_conn().await.context(MySQLConnectionSnafu)?;
234-
235-
// Set MySQL session default time zone to UTC to match Datafusion
236-
let _: Vec<Row> = conn
237-
.exec("SET time_zone = '+00:00'", Params::Empty)
238-
.await
239-
.context(MySQLConnectionSnafu)?;
224+
let conn = pool.get_conn().await.context(MySQLConnectionSnafu)?;
240225

241226
Ok(MySQLConnection::new(conn))
242227
}
@@ -300,13 +285,7 @@ impl DbConnectionPool<mysql_async::Conn, &'static (dyn ToValue + Sync)> for MySQ
300285
) -> super::Result<Box<dyn DbConnection<mysql_async::Conn, &'static (dyn ToValue + Sync)>>>
301286
{
302287
let pool = Arc::clone(&self.pool);
303-
let mut conn = pool.get_conn().await.context(MySQLConnectionSnafu)?;
304-
305-
// Set MySQL session default time zone to UTC to match Datafusion
306-
let _: Vec<Row> = conn
307-
.exec("SET time_zone = '+00:00'", Params::Empty)
308-
.await
309-
.context(MySQLConnectionSnafu)?;
288+
let conn = pool.get_conn().await.context(MySQLConnectionSnafu)?;
310289

311290
Ok(Box::new(MySQLConnection::new(conn)))
312291
}

0 commit comments

Comments
 (0)