Skip to content

Commit d855e2b

Browse files
MySQL: Set the character_set_results/character_set_client/character_set_connection session variables on connection setup (#334)
* Support custom mysql_character_set_results configuration in mysql connection pool * MySQL: Set character set to utf8 on connection setup * Remove error --------- Co-authored-by: Phillip LeBlanc <[email protected]>
1 parent c8b6ea3 commit d855e2b

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

src/sql/db_connection_pool/mysqlpool.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ pub struct MySQLConnectionPool {
5757
join_push_down: JoinPushDown,
5858
}
5959

60+
const SETUP_QUERIES: [&str; 4] = [
61+
"SET time_zone = '+00:00'",
62+
"SET character_set_results = 'utf8mb4'",
63+
"SET character_set_client = 'utf8mb4'",
64+
"SET character_set_connection = 'utf8mb4'",
65+
];
66+
6067
impl MySQLConnectionPool {
6168
/// Creates a new instance of `MySQLConnectionPool`.
6269
///
@@ -160,6 +167,8 @@ impl MySQLConnectionPool {
160167

161168
connection_string = connection_string.ssl_opts(ssl_opts);
162169

170+
connection_string = connection_string.setup(SETUP_QUERIES.to_vec());
171+
163172
let opts = mysql_async::Opts::from(connection_string);
164173

165174
verify_mysql_opts(&opts).await?;
@@ -209,13 +218,7 @@ impl MySQLConnectionPool {
209218
/// Returns an error if there is a problem creating the connection pool.
210219
pub async fn connect_direct(&self) -> super::Result<MySQLConnection> {
211220
let pool = Arc::clone(&self.pool);
212-
let mut conn = pool.get_conn().await.context(MySQLConnectionSnafu)?;
213-
214-
// Set MySQL session default time zone to UTC to match Datafusion
215-
let _: Vec<Row> = conn
216-
.exec("SET time_zone = '+00:00'", Params::Empty)
217-
.await
218-
.context(MySQLConnectionSnafu)?;
221+
let conn = pool.get_conn().await.context(MySQLConnectionSnafu)?;
219222

220223
Ok(MySQLConnection::new(conn))
221224
}
@@ -279,13 +282,7 @@ impl DbConnectionPool<mysql_async::Conn, &'static (dyn ToValue + Sync)> for MySQ
279282
) -> super::Result<Box<dyn DbConnection<mysql_async::Conn, &'static (dyn ToValue + Sync)>>>
280283
{
281284
let pool = Arc::clone(&self.pool);
282-
let mut conn = pool.get_conn().await.context(MySQLConnectionSnafu)?;
283-
284-
// Set MySQL session default time zone to UTC to match Datafusion
285-
let _: Vec<Row> = conn
286-
.exec("SET time_zone = '+00:00'", Params::Empty)
287-
.await
288-
.context(MySQLConnectionSnafu)?;
285+
let conn = pool.get_conn().await.context(MySQLConnectionSnafu)?;
289286

290287
Ok(Box::new(MySQLConnection::new(conn)))
291288
}

0 commit comments

Comments
 (0)