File: Quoter.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (270 lines) | stat: -rw-r--r-- 8,163 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/perl

BEGIN {
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More;
use Data::Dumper;

use Quoter;
use PerconaTest;
use DSNParser;
use Sandbox;

my $dp  = new DSNParser(opts=>$dsn_opts);
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
my $dbh = $sb->get_dbh_for('master');

my $q = new Quoter;

is(
   $q->quote('a'),
   '`a`',
   'Simple quote OK',
);

is(
   $q->quote('a','b'),
   '`a`.`b`',
   'multi value',
);

is(
   $q->quote('`a`'),
   '```a```',
   'already quoted',
);

is(
   $q->quote('a`b'),
   '`a``b`',
   'internal quote',
);

is(
   $q->quote('my db', 'my tbl'),
   '`my db`.`my tbl`',
   'quotes db with space and tbl with space'
);

is( $q->quote_val(1), "'1'", 'number' );
is( $q->quote_val('001'), "'001'", 'number with leading zero' );
# is( $q->quote_val(qw(1 2 3)), '1, 2, 3', 'three numbers');
is( $q->quote_val(qw(a)), "'a'", 'letter');
is( $q->quote_val("a'"), "'a\\''", 'letter with quotes');
is( $q->quote_val(undef), 'NULL', 'NULL');
is( $q->quote_val(''), "''", 'Empty string');
is( $q->quote_val('\\\''), "'\\\\\\\''", 'embedded backslash');
# is( $q->quote_val(42, 0), "'42'", 'non-numeric number' );
# is( $q->quote_val(42, 1), "42", 'number is numeric' );
is( $q->quote_val('123-abc'), "'123-abc'", 'looks numeric but is string');
is( $q->quote_val('123abc'), "'123abc'", 'looks numeric but is string');
is( $q->quote_val('0x89504E470'), '0x89504E470', 'hex string');
is( $q->quote_val('0x89504E470', is_char => 0), '0x89504E470', 'hex string, with is_char => 0');
is( $q->quote_val('0x89504E470', is_char => 1), "'0x89504E470'", 'hex string, with is_char => 1');
is( $q->quote_val('0x89504I470'), "'0x89504I470'", 'looks like hex string');
is( $q->quote_val('eastside0x3'), "'eastside0x3'", 'looks like hex str (issue 1110');

# Splitting DB and tbl apart
is_deeply(
   [$q->split_unquote("`db`.`tbl`")],
   [qw(db tbl)],
   'splits with a quoted db.tbl',
);

is_deeply(
   [$q->split_unquote("db.tbl")],
   [qw(db tbl)],
   'splits with a db.tbl',
);

is_deeply(
   [$q->split_unquote("tbl")],
   [undef, 'tbl'],
   'splits without a db',
);

is_deeply(
   [$q->split_unquote("tbl", "db")],
   [qw(db tbl)],
   'splits with a db',
);

is_deeply(
   [$q->split_unquote("`db`.`tb``l```")],
   [qw(db tb`l`)],
   'splits with a quoted db.tbl ad embedded quotes',
);

#TODO: {
#   local $::TODO = "Embedded periods not yet supported";
#   is_deeply(
#      [$q->split_unquote("`d.b`.`tbl`")],
#      [qw(d.b tbl)],
#      'splits with embedded periods: `d.b`.`tbl`',
#   );
#}

is( $q->literal_like('foo'), "'foo'", 'LIKE foo');
is( $q->literal_like('foo_bar'), "'foo\\_bar'", 'LIKE foo_bar');
is( $q->literal_like('foo%bar'), "'foo\\%bar'", 'LIKE foo%bar');
is( $q->literal_like('v_b%a c_'), "'v\\_b\\%a c\\_'", 'LIKE v_b%a c_');

is( $q->join_quote('db', 'tbl'), '`db`.`tbl`', 'join_merge(db, tbl)' );
is( $q->join_quote(undef, 'tbl'), '`tbl`', 'join_merge(undef, tbl)'  );
is( $q->join_quote('db', 'foo.tbl'), '`foo`.`tbl`', 'join_merge(db, foo.tbl)' );
is( $q->join_quote('`db`', '`tbl`'), '`db`.`tbl`', 'join_merge(`db`, `tbl`)' );
is( $q->join_quote(undef, '`tbl`'), '`tbl`', 'join_merge(undef, `tbl`)'  );
is( $q->join_quote('`db`', '`foo`.`tbl`'), '`foo`.`tbl`', 'join_merge(`db`, `foo`.`tbl`)' );

# ###########################################################################
# (de)serialize_list
# ###########################################################################

is(
   $q->serialize_list( () ),
   undef,
   'Serialize empty list returns undef'
);
   
binmode(STDOUT, ':utf8')
   or die "Can't binmode(STDOUT, ':utf8'): $OS_ERROR";
binmode(STDERR, ':utf8')
   or die "Can't binmode(STDERR, ':utf8'): $OS_ERROR";

# Prevent "Wide character in print at Test/Builder.pm" warnings.
binmode Test::More->builder->$_(), ':encoding(UTF-8)'
   for qw(output failure_output);

my @latin1_serialize_tests = (
   [ 'a' ],
   [ 'a', 'b', ],
   [ 'a,', 'b', ],  # trailing comma
   [ ',a', 'b', ],  # leading comma
   [ 'a', ',b' ],
   [ 0 ],
   [ 0, 0 ],
   [ 1, 2 ],
   [ '' ],  # emptry string
   [ '', '', '', ],
   [ undef ],  # NULL
   [ undef, undef ],
   [ undef, '' ],
   [ '\N' ],  # literal \N
   [ "un caf\x{e9} na\x{ef}ve" ],  # Latin-1
   [ "\\," ],
   [ '\\' ],
   [ q/"abc\\", 'def'/ ],  # Brian's pathalogical case
);

my @utf8_serialize_tests = (
   [ "\x{30cb} \x{e8}" ],  # UTF-8
);

SKIP: {
   skip 'Cannot connect to sandbox master', scalar @latin1_serialize_tests
      unless $dbh;

   $dbh->do('CREATE DATABASE IF NOT EXISTS serialize_test');
   $dbh->do('DROP TABLE IF EXISTS serialize_test.serialize');
   $dbh->do('CREATE TABLE serialize_test.serialize (id INT, textval TEXT, blobval BLOB)');
   # Ensure we are using lantin1 as the default for the connection
   # From the documentation:
   # This statement sets the three session system variables character_set_client, 
   # character_set_connection, and character_set_results to the given character set. 
   $dbh->do("SET NAMES 'latin1'"); 
   warn Data::Dumper::Dumper($dbh);

   my $sth = $dbh->prepare(
      "INSERT INTO serialize_test.serialize VALUES (?, ?, ?)"
   );

   for my $test_index ( 0..$#latin1_serialize_tests ) {

      # Flat, friendly name for the test string
      my $flat_string
         =  "["
         . join( "][",
               map { defined($_) ? $_ : 'undef' }
               @{$latin1_serialize_tests[$test_index]})
         . "]";
      $flat_string =~ s/\n/\\n/g;

      # INSERT the serialized list of values.
      my $ser = $q->serialize_list( @{$latin1_serialize_tests[$test_index]} );
      $sth->execute($test_index, $ser, $ser);

      # SELECT back the values and deserialize them. 
      my ($text_string) = $dbh->selectrow_array(
         "SELECT textval FROM serialize_test.serialize WHERE id=$test_index");
      my @text_parts = $q->deserialize_list($text_string);

      is_deeply(
         \@text_parts,
         $latin1_serialize_tests[$test_index],
         "Serialize $flat_string"
      ) or diag(Dumper($text_string, \@text_parts));
   }
};

my $utf8_dbh = $sb->get_dbh_for('master');
$utf8_dbh->{mysql_enable_utf8} = 1;
$utf8_dbh->do("SET NAMES 'utf8'");
SKIP: {
   skip 'Cannot connect to sandbox master', scalar @utf8_serialize_tests
      unless $utf8_dbh;
   skip 'DBD::mysql 3.0007 has UTF-8 bug', scalar @utf8_serialize_tests
      if $DBD::mysql::VERSION le '3.0007';

   $utf8_dbh->do("DROP TABLE serialize_test.serialize");
   $utf8_dbh->do("CREATE TABLE serialize_test.serialize (id INT, textval TEXT, blobval BLOB) CHARSET='utf8'");

   my $sth = $utf8_dbh->prepare(
      "INSERT INTO serialize_test.serialize VALUES (?, ?, ?)"
   );

   for my $test_index ( 0..$#utf8_serialize_tests ) {

      # Flat, friendly name for the test string
      my $flat_string
         =  "["
         . join( "][",
               map { defined($_) ? $_ : 'undef' }
               @{$utf8_serialize_tests[$test_index]})
         . "]";
      $flat_string =~ s/\n/\\n/g;

      # INSERT the serialized list of values.
      my $ser = $q->serialize_list( @{$utf8_serialize_tests[$test_index]} );
      $sth->execute($test_index, $ser, $ser);

      # SELECT back the values and deserialize them. 
      my ($text_string) = $utf8_dbh->selectrow_array(
         "SELECT textval FROM serialize_test.serialize WHERE id=$test_index");
      my @text_parts = $q->deserialize_list($text_string);

      is_deeply(
         \@text_parts,
         $utf8_serialize_tests[$test_index],
         "Serialize UTF-8 $flat_string"
      ) or diag(Dumper($text_string, \@text_parts));
   }

   $utf8_dbh->disconnect();
};

# ###########################################################################
# Done.
# ###########################################################################
if ( $dbh ) {
   $sb->wipe_clean($dbh);
   $dbh->disconnect();
}
ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox");
done_testing;