Commit | Line | Data |
1a24938d |
1 | package SQL::Translator::Utils; |
2 | |
1a24938d |
3 | use strict; |
f27f9229 |
4 | use warnings; |
c092c5b3 |
5 | use Digest::SHA qw( sha1_hex ); |
bdf60588 |
6 | use File::Spec; |
7 | use Class::Unload; |
1a24938d |
8 | |
0c04c5a2 |
9 | our $VERSION = '1.59'; |
10 | our $DEFAULT_COMMENT = '-- '; |
bdf60588 |
11 | |
12 | use base qw(Exporter); |
0c04c5a2 |
13 | our @EXPORT_OK = qw( |
7b4b17aa |
14 | debug normalize_name header_comment parse_list_arg truncate_id_uniquely |
15 | $DEFAULT_COMMENT parse_mysql_version parse_dbms_version |
bdf60588 |
16 | ddl_parser_instance |
118bb73f |
17 | ); |
11ad2df9 |
18 | use constant COLLISION_TAG_LENGTH => 8; |
1a24938d |
19 | |
1a24938d |
20 | sub debug { |
a2ba36ba |
21 | my ($pkg, $file, $line, $sub) = caller(0); |
1a24938d |
22 | { |
23 | no strict qw(refs); |
24 | return unless ${"$pkg\::DEBUG"}; |
25 | } |
26 | |
27 | $sub =~ s/^$pkg\:://; |
28 | |
29 | while (@_) { |
30 | my $x = shift; |
31 | chomp $x; |
32 | $x =~ s/\bPKG\b/$pkg/g; |
33 | $x =~ s/\bLINE\b/$line/g; |
34 | $x =~ s/\bSUB\b/$sub/g; |
35 | #warn '[' . $x . "]\n"; |
36 | print STDERR '[' . $x . "]\n"; |
37 | } |
38 | } |
39 | |
93d12e9c |
40 | sub normalize_name { |
ae48473b |
41 | my $name = shift or return ''; |
93d12e9c |
42 | |
43 | # The name can only begin with a-zA-Z_; if there's anything |
44 | # else, prefix with _ |
45 | $name =~ s/^([^a-zA-Z_])/_$1/; |
46 | |
47 | # anything other than a-zA-Z0-9_ in the non-first position |
48 | # needs to be turned into _ |
49 | $name =~ tr/[a-zA-Z0-9_]/_/c; |
50 | |
51 | # All duplicated _ need to be squashed into one. |
52 | $name =~ tr/_/_/s; |
53 | |
54 | # Trim a trailing _ |
55 | $name =~ s/_$//; |
56 | |
57 | return $name; |
58 | } |
59 | |
a2ba36ba |
60 | sub header_comment { |
61 | my $producer = shift || caller; |
62 | my $comment_char = shift; |
63 | my $now = scalar localtime; |
64 | |
65 | $comment_char = $DEFAULT_COMMENT |
66 | unless defined $comment_char; |
67 | |
68 | my $header_comment =<<"HEADER_COMMENT"; |
69 | ${comment_char} |
70 | ${comment_char}Created by $producer |
71 | ${comment_char}Created on $now |
72 | ${comment_char} |
73 | HEADER_COMMENT |
74 | |
75 | # Any additional stuff passed in |
76 | for my $additional_comment (@_) { |
77 | $header_comment .= "${comment_char}${additional_comment}\n"; |
78 | } |
79 | |
80 | return $header_comment; |
81 | } |
82 | |
e545d971 |
83 | sub parse_list_arg { |
84 | my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ]; |
85 | |
51bb6fe0 |
86 | # |
87 | # This protects stringification of references. |
88 | # |
89 | if ( @$list && ref $list->[0] ) { |
90 | return $list; |
91 | } |
92 | # |
93 | # This processes string-like arguments. |
94 | # |
95 | else { |
ea93df61 |
96 | return [ |
51bb6fe0 |
97 | map { s/^\s+|\s+$//g; $_ } |
98 | map { split /,/ } |
99 | grep { defined && length } @$list |
100 | ]; |
101 | } |
118bb73f |
102 | } |
103 | |
f5405d47 |
104 | sub truncate_id_uniquely { |
105 | my ( $desired_name, $max_symbol_length ) = @_; |
106 | |
16fa91c0 |
107 | return $desired_name |
108 | unless defined $desired_name && length $desired_name > $max_symbol_length; |
f5405d47 |
109 | |
16fa91c0 |
110 | my $truncated_name = substr $desired_name, 0, |
11ad2df9 |
111 | $max_symbol_length - COLLISION_TAG_LENGTH - 1; |
f5405d47 |
112 | |
113 | # Hex isn't the most space-efficient, but it skirts around allowed |
114 | # charset issues |
115 | my $digest = sha1_hex($desired_name); |
11ad2df9 |
116 | my $collision_tag = substr $digest, 0, COLLISION_TAG_LENGTH; |
f5405d47 |
117 | |
118 | return $truncated_name |
119 | . '_' |
120 | . $collision_tag; |
121 | } |
122 | |
5d666b31 |
123 | |
5d666b31 |
124 | sub parse_mysql_version { |
125 | my ($v, $target) = @_; |
126 | |
127 | return undef unless $v; |
128 | |
129 | $target ||= 'perl'; |
130 | |
131 | my @vers; |
132 | |
ea93df61 |
133 | # X.Y.Z style |
5d666b31 |
134 | if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) { |
135 | push @vers, $1, $2, $3; |
136 | } |
137 | |
ea93df61 |
138 | # XYYZZ (mysql) style |
5d666b31 |
139 | elsif ( $v =~ / ^ (\d) (\d{2}) (\d{2}) $ /x ) { |
140 | push @vers, $1, $2, $3; |
141 | } |
142 | |
ea93df61 |
143 | # XX.YYYZZZ (perl) style or simply X |
5d666b31 |
144 | elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) { |
145 | push @vers, $1, $2, $3; |
146 | } |
147 | else { |
148 | #how do I croak sanely here? |
149 | die "Unparseable MySQL version '$v'"; |
150 | } |
151 | |
152 | if ($target eq 'perl') { |
153 | return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) ); |
154 | } |
155 | elsif ($target eq 'mysql') { |
156 | return sprintf ('%d%02d%02d', map { $_ || 0 } (@vers) ); |
157 | } |
158 | else { |
159 | #how do I croak sanely here? |
160 | die "Unknown version target '$target'"; |
161 | } |
162 | } |
163 | |
7b4b17aa |
164 | sub parse_dbms_version { |
165 | my ($v, $target) = @_; |
166 | |
167 | return undef unless $v; |
168 | |
169 | my @vers; |
170 | |
ea93df61 |
171 | # X.Y.Z style |
7b4b17aa |
172 | if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) { |
173 | push @vers, $1, $2, $3; |
174 | } |
175 | |
ea93df61 |
176 | # XX.YYYZZZ (perl) style or simply X |
7b4b17aa |
177 | elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) { |
178 | push @vers, $1, $2, $3; |
179 | } |
180 | else { |
181 | #how do I croak sanely here? |
182 | die "Unparseable database server version '$v'"; |
183 | } |
184 | |
185 | if ($target eq 'perl') { |
186 | return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) ); |
187 | } |
188 | elsif ($target eq 'native') { |
e0d18105 |
189 | return join '.' => grep defined, @vers; |
7b4b17aa |
190 | } |
191 | else { |
192 | #how do I croak sanely here? |
193 | die "Unknown version target '$target'"; |
194 | } |
195 | } |
5d666b31 |
196 | |
0eb3b94a |
197 | #my ($parsers_libdir, $checkout_dir); |
bdf60588 |
198 | sub ddl_parser_instance { |
0eb3b94a |
199 | |
bdf60588 |
200 | my $type = shift; |
201 | |
202 | # it may differ from our caller, even though currently this is not the case |
203 | eval "require SQL::Translator::Parser::$type" |
204 | or die "Unable to load grammar-spec container SQL::Translator::Parser::$type:\n$@"; |
205 | |
0eb3b94a |
206 | require Parse::RecDescent; |
207 | return Parse::RecDescent->new(do { |
208 | no strict 'refs'; |
209 | ${"SQL::Translator::Parser::${type}::GRAMMAR"} |
210 | || die "No \$SQL::Translator::Parser::${type}::GRAMMAR defined, unable to instantiate PRD parser\n" |
211 | }); |
212 | |
213 | # this is disabled until RT#74593 is resolved |
214 | =begin for general sadness |
215 | |
bdf60588 |
216 | unless ($parsers_libdir) { |
217 | |
218 | # are we in a checkout? |
219 | if ($checkout_dir = _find_co_root()) { |
220 | $parsers_libdir = File::Spec->catdir($checkout_dir, 'share', 'PrecompiledParsers'); |
221 | } |
222 | else { |
223 | require File::ShareDir; |
224 | $parsers_libdir = File::Spec->catdir( |
225 | File::ShareDir::dist_dir('SQL-Translator'), |
226 | 'PrecompiledParsers' |
227 | ); |
228 | } |
229 | |
230 | unshift @INC, $parsers_libdir; |
231 | } |
232 | |
233 | my $precompiled_mod = "Parse::RecDescent::DDL::SQLT::$type"; |
234 | |
235 | # FIXME FIXME FIXME |
236 | # Parse::RecDescent has horrible architecture where each precompiled parser |
237 | # instance shares global state with all its siblings |
238 | # What we do here is gross, but scarily efficient - the parser compilation |
239 | # is much much slower than an unload/reload cycle |
240 | Class::Unload->unload($precompiled_mod); |
241 | |
242 | # There is also a sub-namespace that P::RD uses, but simply unsetting |
243 | # $^W to stop redefine warnings seems to be enough |
244 | #Class::Unload->unload("Parse::RecDescent::$precompiled_mod"); |
245 | |
246 | eval "local \$^W; require $precompiled_mod" or do { |
247 | if ($checkout_dir) { |
248 | die "Unable to find precompiled grammar for $type - run Makefile.PL to generate it\n"; |
249 | } |
250 | else { |
251 | die "Unable to load precompiled grammar for $type... this is not supposed to happen if you are not in a checkout, please file a bugreport:\n$@" |
252 | } |
253 | }; |
254 | |
255 | my $grammar_spec_fn = $INC{"SQL/Translator/Parser/$type.pm"}; |
256 | my $precompiled_fn = $INC{"Parse/RecDescent/DDL/SQLT/$type.pm"}; |
257 | |
258 | if ( |
259 | (stat($grammar_spec_fn))[9] |
260 | > |
261 | (stat($precompiled_fn))[9] |
262 | ) { |
263 | die ( |
264 | "Grammar spec '$grammar_spec_fn' is newer than precompiled parser '$precompiled_fn'" |
265 | . ($checkout_dir |
266 | ? " - run Makefile.PL to regenerate stale versions\n" |
267 | : "... this is not supposed to happen if you are not in a checkout, please file a bugreport\n" |
268 | ) |
269 | ); |
270 | } |
271 | |
272 | return $precompiled_mod->new; |
0eb3b94a |
273 | =cut |
274 | |
bdf60588 |
275 | } |
276 | |
277 | # Try to determine the root of a checkout/untar if possible |
278 | # or return undef |
279 | sub _find_co_root { |
280 | |
281 | my @mod_parts = split /::/, (__PACKAGE__ . '.pm'); |
282 | my $rel_path = join ('/', @mod_parts); # %INC stores paths with / regardless of OS |
283 | |
284 | return undef unless ($INC{$rel_path}); |
285 | |
286 | # a bit convoluted, but what we do here essentially is: |
287 | # - get the file name of this particular module |
288 | # - do 'cd ..' as many times as necessary to get to lib/SQL/Translator/../../.. |
289 | |
290 | my $root = (File::Spec::Unix->splitpath($INC{$rel_path}))[1]; |
291 | for (1 .. @mod_parts) { |
292 | $root = File::Spec->catdir($root, File::Spec->updir); |
293 | } |
294 | |
295 | return ( -f File::Spec->catfile($root, 'Makefile.PL') ) |
296 | ? $root |
297 | : undef |
298 | ; |
299 | } |
300 | |
1a24938d |
301 | 1; |
302 | |
118bb73f |
303 | =pod |
1a24938d |
304 | |
305 | =head1 NAME |
306 | |
307 | SQL::Translator::Utils - SQL::Translator Utility functions |
308 | |
309 | =head1 SYNOPSIS |
310 | |
311 | use SQL::Translator::Utils qw(debug); |
312 | debug("PKG: Bad things happened"); |
313 | |
314 | =head1 DESCSIPTION |
315 | |
316 | C<SQL::Translator::Utils> contains utility functions designed to be |
317 | used from the other modules within the C<SQL::Translator> modules. |
318 | |
a2ba36ba |
319 | Nothing is exported by default. |
1a24938d |
320 | |
a2ba36ba |
321 | =head1 EXPORTED FUNCTIONS AND CONSTANTS |
1a24938d |
322 | |
323 | =head2 debug |
324 | |
325 | C<debug> takes 0 or more messages, which will be sent to STDERR using |
326 | C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE> |
327 | will be replaced by the calling package, subroutine, and line number, |
e545d971 |
328 | respectively, as reported by C<caller(1)>. |
1a24938d |
329 | |
330 | For example, from within C<foo> in F<SQL/Translator.pm>, at line 666: |
331 | |
332 | debug("PKG: Error reading file at SUB/LINE"); |
333 | |
334 | Will warn |
335 | |
336 | [SQL::Translator: Error reading file at foo/666] |
337 | |
338 | The entire message is enclosed within C<[> and C<]> for visual clarity |
339 | when STDERR is intermixed with STDOUT. |
93d12e9c |
340 | |
341 | =head2 normalize_name |
342 | |
343 | C<normalize_name> takes a string and ensures that it is suitable for |
344 | use as an identifier. This means: ensure that it starts with a letter |
345 | or underscore, and that the rest of the string consists of only |
346 | letters, numbers, and underscores. A string that begins with |
347 | something other than [a-zA-Z] will be prefixer with an underscore, and |
348 | all other characters in the string will be replaced with underscores. |
349 | Finally, a trailing underscore will be removed, because that's ugly. |
350 | |
351 | normalize_name("Hello, world"); |
352 | |
353 | Produces: |
354 | |
355 | Hello_world |
356 | |
357 | A more useful example, from the C<SQL::Translator::Parser::Excel> test |
358 | suite: |
359 | |
360 | normalize_name("silly field (with random characters)"); |
361 | |
362 | returns: |
363 | |
364 | silly_field_with_random_characters |
365 | |
a2ba36ba |
366 | =head2 header_comment |
367 | |
368 | Create the header comment. Takes 1 mandatory argument (the producer |
369 | classname), an optional comment character (defaults to $DEFAULT_COMMENT), |
370 | and 0 or more additional comments, which will be appended to the header, |
371 | prefixed with the comment character. If additional comments are provided, |
372 | then a comment string must be provided ($DEFAULT_COMMENT is exported for |
373 | this use). For example, this: |
374 | |
375 | package My::Producer; |
376 | |
377 | use SQL::Translator::Utils qw(header_comment $DEFAULT_COMMENT); |
378 | |
379 | print header_comment(__PACKAGE__, |
e545d971 |
380 | $DEFAULT_COMMENT, |
a2ba36ba |
381 | "Hi mom!"); |
382 | |
383 | produces: |
384 | |
e545d971 |
385 | -- |
a2ba36ba |
386 | -- Created by My::Prodcuer |
387 | -- Created on Fri Apr 25 06:56:02 2003 |
e545d971 |
388 | -- |
a2ba36ba |
389 | -- Hi mom! |
e545d971 |
390 | -- |
a2ba36ba |
391 | |
392 | Note the gratuitous spacing. |
393 | |
118bb73f |
394 | =head2 parse_list_arg |
395 | |
396 | Takes a string, list or arrayref (all of which could contain |
397 | comma-separated values) and returns an array reference of the values. |
398 | All of the following will return equivalent values: |
399 | |
400 | parse_list_arg('id'); |
401 | parse_list_arg('id', 'name'); |
402 | parse_list_arg( 'id, name' ); |
403 | parse_list_arg( [ 'id', 'name' ] ); |
404 | parse_list_arg( qw[ id name ] ); |
405 | |
f5405d47 |
406 | =head2 truncate_id_uniquely |
407 | |
408 | Takes a string ($desired_name) and int ($max_symbol_length). Truncates |
409 | $desired_name to $max_symbol_length by including part of the hash of |
410 | the full name at the end of the truncated name, giving a high |
411 | probability that the symbol will be unique. For example, |
412 | |
413 | truncate_id_uniquely( 'a' x 100, 64 ) |
414 | truncate_id_uniquely( 'a' x 99 . 'b', 64 ); |
415 | truncate_id_uniquely( 'a' x 99, 64 ) |
416 | |
417 | Will give three different results; specifically: |
418 | |
419 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7f900025 |
420 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_6191e39a |
421 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_8cd96af2 |
422 | |
a2ba36ba |
423 | =head2 $DEFAULT_COMMENT |
424 | |
425 | This is the default comment string, '-- ' by default. Useful for |
426 | C<header_comment>. |
427 | |
5d666b31 |
428 | =head2 parse_mysql_version |
429 | |
ea93df61 |
430 | Used by both L<Parser::MySQL|SQL::Translator::Parser::MySQL> and |
5d666b31 |
431 | L<Producer::MySQL|SQL::Translator::Producer::MySQL> in order to provide a |
432 | consistent format for both C<< parser_args->{mysql_parser_version} >> and |
433 | C<< producer_args->{mysql_version} >> respectively. Takes any of the following |
434 | version specifications: |
435 | |
436 | 5.0.3 |
437 | 4.1 |
438 | 3.23.2 |
439 | 5 |
440 | 5.001005 (perl style) |
441 | 30201 (mysql style) |
442 | |
282bf498 |
443 | =head2 parse_dbms_version |
444 | |
445 | Takes a version string (X.Y.Z) or perl style (XX.YYYZZZ) and a target ('perl' |
446 | or 'native') transforms the string to the given target style. |
447 | to |
448 | |
118bb73f |
449 | =head1 AUTHORS |
450 | |
451 | Darren Chamberlain E<lt>darren@cpan.orgE<gt>, |
11ad2df9 |
452 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
118bb73f |
453 | |
454 | =cut |