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

Correct handling of mysql_enable_utf8mb4 #363

Merged
merged 3 commits into from
Oct 13, 2023
Merged

Conversation

dveeden
Copy link
Collaborator

@dveeden dveeden commented Oct 5, 2023

Depending on where in the DSN mysql_enable_utf8mb4 was specified it would set imp_dbh->enable_utf8mb4 or not. But it would always call mysql_options() to set the charset. This commit makes sure imp_dbh->enable_utf8mb4 is set consistently.

Closes #360

Depending on where in the DSN `mysql_enable_utf8mb4` was specified it
would set `imp_dbh->enable_utf8mb4` or not. But it would always call
`mysql_options()` to set the charset. This commit makes sure
`imp_dbh->enable_utf8mb4` is set consistently.

Closes perl5-dbi#360
@dveeden
Copy link
Collaborator Author

dveeden commented Oct 5, 2023

Tested with this:

#!/bin/perl
use strict;
use v5.36;
use DBI;
use Encode ();

-t(STDOUT) && binmode(STDOUT, ':utf8');
-t(STDERR) && binmode(STDERR, ':utf8');

my @dsns = (
	"DBI:mysql:database=test;host=localhost;mysql_enable_utf8mb4=1",
	"DBI:mysql(mysql_enable_utf8mb4=1):database=test;host=localhost"
);

for my $dsn (@dsns) {
	say "dsn: " . $dsn;
	my $dbh = DBI->connect($dsn, "xxxx", "xxxx");

	print "Connected to MySQL " . $dbh->selectrow_array("SELECT VERSION()") . "\n";
	say "mysql_enable_utf8mb4:" . $dbh->{mysql_enable_utf8mb4};

	my $sql = 'SELECT CHAR(0xC3BC USING utf8mb4), LENGTH(CHAR(0xC3BC USING utf8mb4)), CHAR_LENGTH(CHAR(0xC3BC USING utf8mb4))';	
	my ($char, $byte_length, $char_length) = $dbh->selectrow_array($sql);
	print 'character: ' . $char . "\n";
	print 'is_utf8?: ' . int(Encode::is_utf8($char)) . "\n";
	print 'mysql byte length: ' . $byte_length . "\n";
	print 'mysql char length: ' . $char_length . "\n";
	print 'perl byte length: ' . bytes::length($char) . "\n";
	print 'perl char length: ' . length($char) . "\n";
	$dbh->disconnect();
	print "--------------------\n";
}

Output without this PR:

dsn: DBI:mysql:database=test;host=localhost;mysql_enable_utf8mb4=1
Connected to MySQL 8.1.0
mysql_enable_utf8mb4:0
character: ü
is_utf8?: 0
mysql byte length: 2
mysql char length: 1
perl byte length: 2
perl char length: 2
--------------------
dsn: DBI:mysql(mysql_enable_utf8mb4=1):database=test;host=localhost
Connected to MySQL 8.1.0
mysql_enable_utf8mb4:1
character: ü
is_utf8?: 1
mysql byte length: 2
mysql char length: 1
perl byte length: 2
perl char length: 1
--------------------

And with this PR:

dsn: DBI:mysql:database=test;host=localhost;mysql_enable_utf8mb4=1
Connected to MySQL 8.1.0
mysql_enable_utf8mb4:1
character: ü
is_utf8?: 1
mysql byte length: 2
mysql char length: 1
perl byte length: 2
perl char length: 1
--------------------
dsn: DBI:mysql(mysql_enable_utf8mb4=1):database=test;host=localhost
Connected to MySQL 8.1.0
mysql_enable_utf8mb4:1
character: ü
is_utf8?: 1
mysql byte length: 2
mysql char length: 1
perl byte length: 2
perl char length: 1
--------------------

@dveeden dveeden merged commit 63a83d6 into perl5-dbi:master Oct 13, 2023
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Specifying mysql_enable_utf8mb4=1 doesn't always work depending on where it is specified in DSN
1 participant