Skip to content

Commit

Permalink
Merge pull request #660 from Cabbagec/mysql-text-type
Browse files Browse the repository at this point in the history
read mysql text column (utf8 charset) as string column
  • Loading branch information
wangxiaoying authored Jul 9, 2024
2 parents f54ad24 + 6417bcf commit 8dee3a6
Showing 1 changed file with 32 additions and 5 deletions.
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

0 comments on commit 8dee3a6

Please sign in to comment.