Merged fix-1007938-mysqlconfig-eol-comments and fixed it to only take # as a start of comment, and to respect quoted values

This commit is contained in:
Brian Fraser
2012-08-02 12:21:59 -03:00
4 changed files with 80 additions and 26 deletions

View File

@@ -1291,7 +1291,7 @@ sub get_dbh {
PTDEBUG && _d($dbh, $sql);
my ($sql_mode) = eval { $dbh->selectrow_array($sql) };
if ( $EVAL_ERROR ) {
die $EVAL_ERROR;
die "Error getting the current SQL_MODE: $EVAL_ERROR";
}
$sql = 'SET @@SQL_QUOTE_SHOW_CREATE = 1'
@@ -1301,15 +1301,17 @@ sub get_dbh {
PTDEBUG && _d($dbh, $sql);
eval { $dbh->do($sql) };
if ( $EVAL_ERROR ) {
die $EVAL_ERROR;
die "Error setting SQL_QUOTE_SHOW_CREATE, SQL_MODE"
. ($sql_mode ? " and $sql_mode" : '')
. ": $EVAL_ERROR";
}
if ( my ($charset) = $cxn_string =~ m/charset=(\w+)/ ) {
$sql = "/*!40101 SET NAMES $charset*/";
if ( my ($charset) = $cxn_string =~ m/charset=([\w]+)/ ) {
$sql = qq{/*!40101 SET NAMES "$charset"*/};
PTDEBUG && _d($dbh, ':', $sql);
eval { $dbh->do($sql) };
if ( $EVAL_ERROR ) {
die $EVAL_ERROR;
die "Error setting NAMES to $charset: $EVAL_ERROR";
}
PTDEBUG && _d('Enabling charset for STDOUT');
if ( $charset eq 'utf8' ) {
@@ -1321,12 +1323,12 @@ sub get_dbh {
}
}
if ( $self->prop('set-vars') ) {
$sql = "SET " . $self->prop('set-vars');
if ( my $var = $self->prop('set-vars') ) {
$sql = "SET $var";
PTDEBUG && _d($dbh, ':', $sql);
eval { $dbh->do($sql) };
if ( $EVAL_ERROR ) {
die $EVAL_ERROR;
die "Error setting $var: $EVAL_ERROR";
}
}
}
@@ -1451,6 +1453,7 @@ sub new {
dsn_name => $dp->as_string($dsn, [qw(h P S)]),
hostname => '',
set => $args{set},
NAME_lc => defined($args{NAME_lc}) ? $args{NAME_lc} : 1,
dbh_set => 0,
OptionParser => $o,
DSNParser => $dp,
@@ -1488,7 +1491,10 @@ sub set_dbh {
PTDEBUG && _d($dbh, 'Setting dbh');
$dbh->{FetchHashKeyName} = 'NAME_lc';
if ( !exists $self->{NAME_lc}
|| (defined $self->{NAME_lc} && $self->{NAME_lc}) ) {
$dbh->{FetchHashKeyName} = 'NAME_lc';
}
my $sql = 'SELECT @@hostname, @@server_id';
PTDEBUG && _d($dbh, $sql);
@@ -2158,6 +2164,8 @@ sub _parse_varvals {
$var =~ s/-/_/g;
$var =~ s/\s*#.*$//;
if ( exists $config{$var} && !$can_be_duplicate{$var} ) {
PTDEBUG && _d("Duplicate var:", $var);
$duplicate_var = 1; # flag on, save all the var's values
@@ -2171,13 +2179,20 @@ sub _parse_varvals {
$val = '';
}
else {
$val =~ s/
\A # Start of value
(['"]) # Opening quote
(.*) # Value
\1 # Closing quote
[\n\r]*\z # End of value
/$2/x;
my $quote_re = qr/
\A # Start of value
(['"]) # Opening quote
(.*) # Value
\1 # Closing quote
\s*(?:\#.*)? # End of line comment
[\n\r]*\z # End of value
/x;
if ( $val =~ $quote_re ) {
$val = $2;
}
else {
$val =~ s/\s*#.*//;
}
if ( my ($num, $factor) = $val =~ m/(\d+)([KMGT])b?$/i ) {
my %factor_for = (
k => 1_024,

View File

@@ -337,7 +337,7 @@ sub _parse_varvals {
ITEM:
foreach my $item ( @varvals ) {
if ( $item ) {
# Strip leading and trailing whitespace.
# Strip leading and trailing whitespace, and trailing comments.
$item =~ s/^\s+//;
$item =~ s/\s+$//;
}
@@ -350,6 +350,9 @@ sub _parse_varvals {
# but in SHOW VARIABLES they're all like "log_bin".
$var =~ s/-/_/g;
# Remove trailing comments
$var =~ s/\s*#.*$//;
# The var is a duplicate (in the bad sense, i.e. where user is
# probably unaware that there's two different values for this var
# but only the last is used) if we've seen it already and it cannot
@@ -371,13 +374,22 @@ sub _parse_varvals {
$val = '';
}
else {
$val =~ s/
\A # Start of value
(['"]) # Opening quote
(.*) # Value
\1 # Closing quote
[\n\r]*\z # End of value
/$2/x;
my $quote_re = qr/
\A # Start of value
(['"]) # Opening quote
(.*) # Value
\1 # Closing quote
\s*(?:\#.*)? # End of line comment
[\n\r]*\z # End of value
/x;
if ( $val =~ $quote_re ) {
# If it matches the quote re, then $2 holds the value
$val = $2;
}
else {
# Otherwise, remove possible trailing comments
$val =~ s/\s*#.*//;
}
if ( my ($num, $factor) = $val =~ m/(\d+)([KMGT])b?$/i ) {
# value is a size like 1k, 16M, etc.
my %factor_for = (

View File

@@ -9,7 +9,7 @@ BEGIN {
use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 30;
use Test::More;
use MySQLConfig;
use DSNParser;
@@ -827,6 +827,28 @@ SKIP: {
);
}
$config = new MySQLConfig(
file => "$trunk/t/lib/samples/configs/mycnf-kc-001.txt",
TextResultSetParser => $trp,
);
is(
$config->value_of('user'),
'mysql',
'end of line comment in option file'
);
is(
$config->value_of('password'),
'password # still part of it!',
'end of line comments respect quoted values'
);
is(
$config->value_of('something'),
'something ; or # another',
"..and removing comments doesn't leave trailing whitespace"
);
# #############################################################################
# Done.
# #############################################################################
@@ -841,4 +863,5 @@ like(
'_d() works'
);
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
exit;
done_testing;

View File

@@ -0,0 +1,4 @@
[mysqld]
user=mysql # comment
password="password # still part of it!"# comment
something='something ; or # another' # comment