mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-10 21:19:59 +00:00
Updqte Quoter in all tools.
This commit is contained in:
@@ -63,6 +63,11 @@ use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
|
||||
|
||||
use Data::Dumper;
|
||||
$Data::Dumper::Indent = 1;
|
||||
$Data::Dumper::Sortkeys = 1;
|
||||
$Data::Dumper::Quotekeys = 0;
|
||||
|
||||
sub new {
|
||||
my ( $class, %args ) = @_;
|
||||
return bless {}, $class;
|
||||
@@ -127,51 +132,64 @@ sub join_quote {
|
||||
|
||||
sub serialize_list {
|
||||
my ( $self, @args ) = @_;
|
||||
return unless @args;
|
||||
PTDEBUG && _d('Serializing', Dumper(\@args));
|
||||
die "Cannot serialize an empty array" unless scalar @args;
|
||||
|
||||
return $args[0] if @args == 1 && !defined $args[0];
|
||||
my @parts;
|
||||
foreach my $arg ( @args ) {
|
||||
if ( defined $arg ) {
|
||||
$arg =~ s/,/\\,/g; # escape commas
|
||||
$arg =~ s/\\N/\\\\N/g; # escape literal \N
|
||||
push @parts, $arg;
|
||||
}
|
||||
else {
|
||||
push @parts, '\N';
|
||||
}
|
||||
}
|
||||
|
||||
return join ',', map {
|
||||
my $c = $_;
|
||||
if ( defined($c) ) {
|
||||
$c =~ s/([^A-Za-z0-9])/\\$1/g;
|
||||
$c
|
||||
}
|
||||
else {
|
||||
'\\N'
|
||||
}
|
||||
} @args;
|
||||
my $string = join(',', @parts);
|
||||
PTDEBUG && _d('Serialized: <', $string, '>');
|
||||
return $string;
|
||||
}
|
||||
|
||||
sub deserialize_list {
|
||||
my ( $self, $string ) = @_;
|
||||
return $string unless defined $string;
|
||||
my @escaped_parts = $string =~ /
|
||||
\G # Start of string, or end of previous match.
|
||||
( # Each of these is an element in the original list.
|
||||
[^\\,]* # Anything not a backslash or a comma
|
||||
(?: # When we get here, we found one of the above.
|
||||
\\. # A backslash followed by something so we can continue
|
||||
[^\\,]* # Same as above.
|
||||
)* # Repeat zero of more times.
|
||||
)
|
||||
, # Comma dividing elements
|
||||
/sxgc;
|
||||
PTDEBUG && _d('Deserializing <', $string, '>');
|
||||
die "Cannot deserialize an undefined string" unless defined $string;
|
||||
|
||||
push @escaped_parts, pos($string) ? substr( $string, pos($string) ) : $string;
|
||||
|
||||
my @unescaped_parts = map {
|
||||
my $part = $_;
|
||||
if ($part eq '\\N') {
|
||||
undef
|
||||
my @parts;
|
||||
foreach my $arg ( split(/(?<!\\),/, $string) ) {
|
||||
if ( $arg eq '\N' ) {
|
||||
$arg = undef;
|
||||
}
|
||||
else {
|
||||
$part =~ s/\\([^A-Za-z0-9])/$1/g;
|
||||
$part;
|
||||
$arg =~ s/\\,/,/g;
|
||||
$arg =~ s/\\\\N/\\N/g;
|
||||
}
|
||||
} @escaped_parts;
|
||||
|
||||
return @unescaped_parts;
|
||||
push @parts, $arg;
|
||||
}
|
||||
|
||||
if ( !@parts ) {
|
||||
my $n_empty_strings = $string =~ tr/,//;
|
||||
$n_empty_strings++;
|
||||
PTDEBUG && _d($n_empty_strings, 'empty strings');
|
||||
map { push @parts, '' } 1..$n_empty_strings;
|
||||
}
|
||||
elsif ( $string =~ m/(?<!\\),$/ ) {
|
||||
PTDEBUG && _d('Last value is an empty string');
|
||||
push @parts, '';
|
||||
}
|
||||
|
||||
PTDEBUG && _d('Deserialized', Dumper(\@parts));
|
||||
return @parts;
|
||||
}
|
||||
|
||||
sub _d {
|
||||
my ($package, undef, $line) = caller 0;
|
||||
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
|
||||
map { defined $_ ? $_ : 'undef' }
|
||||
@_;
|
||||
print STDERR "# $package:$line $PID ", join(' ', @_), "\n";
|
||||
}
|
||||
|
||||
1;
|
||||
|
Reference in New Issue
Block a user