Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read mysql text column (utf8 charset) as string column #660

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions connectorx/src/sources/mysql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ use fehler::{throw, throws};
use log::{debug, warn};
use r2d2::{Pool, PooledConnection};
use r2d2_mysql::{
mysql::{prelude::Queryable, Binary, Opts, OptsBuilder, QueryResult, Row, Text},
mysql::{
consts::{
ColumnFlags as MySQLColumnFlags, ColumnType as MySQLColumnType, UTF8MB4_GENERAL_CI,
UTF8_GENERAL_CI,
},
prelude::Queryable,
Binary, Opts, OptsBuilder, QueryResult, Row, Text,
},
MySqlConnectionManager,
};
use rust_decimal::Decimal;
Expand Down Expand Up @@ -96,6 +103,8 @@ where
assert!(!self.queries.is_empty());

let mut conn = self.pool.get()?;
let server_version_post_5_5_3 = conn.server_version() >= (5, 5, 3);

let first_query = &self.queries[0];

match conn.prep(first_query) {
Expand All @@ -104,10 +113,28 @@ where
.columns()
.iter()
.map(|col| {
(
col.name_str().to_string(),
MySQLTypeSystem::from((&col.column_type(), &col.flags())),
)
let col_name = col.name_str().to_string();
let col_type = col.column_type();
let col_flags = col.flags();
let charset = col.character_set();
let charset_is_utf8 = (server_version_post_5_5_3
&& charset == UTF8MB4_GENERAL_CI)
|| (!server_version_post_5_5_3 && charset == UTF8_GENERAL_CI);
if charset_is_utf8
&& (col_type == MySQLColumnType::MYSQL_TYPE_LONG_BLOB
|| col_type == MySQLColumnType::MYSQL_TYPE_BLOB
|| col_type == MySQLColumnType::MYSQL_TYPE_MEDIUM_BLOB
|| col_type == MySQLColumnType::MYSQL_TYPE_TINY_BLOB)
{
return (
col_name,
MySQLTypeSystem::Char(
!col_flags.contains(MySQLColumnFlags::NOT_NULL_FLAG),
),
);
}
let d = MySQLTypeSystem::from((&col_type, &col_flags));
(col_name, d)
})
.unzip();
self.names = names;
Expand Down
Loading