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