1 package SQL::Translator::Utils;
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
22 use base qw(Exporter);
23 use vars qw($VERSION $DEFAULT_COMMENT @EXPORT_OK);
24 use Digest::SHA1 qw( sha1_hex );
28 $DEFAULT_COMMENT = '-- ';
30 debug normalize_name header_comment parse_list_arg truncate_id_uniquely
31 $DEFAULT_COMMENT parse_mysql_version parse_dbms_version
33 use constant COLLISION_TAG_LENGTH => 8;
36 my ($pkg, $file, $line, $sub) = caller(0);
39 return unless ${"$pkg\::DEBUG"};
47 $x =~ s/\bPKG\b/$pkg/g;
48 $x =~ s/\bLINE\b/$line/g;
49 $x =~ s/\bSUB\b/$sub/g;
50 #warn '[' . $x . "]\n";
51 print STDERR '[' . $x . "]\n";
56 my $name = shift or return '';
58 # The name can only begin with a-zA-Z_; if there's anything
60 $name =~ s/^([^a-zA-Z_])/_$1/;
62 # anything other than a-zA-Z0-9_ in the non-first position
63 # needs to be turned into _
64 $name =~ tr/[a-zA-Z0-9_]/_/c;
66 # All duplicated _ need to be squashed into one.
76 my $producer = shift || caller;
77 my $comment_char = shift;
78 my $now = scalar localtime;
80 $comment_char = $DEFAULT_COMMENT
81 unless defined $comment_char;
83 my $header_comment =<<"HEADER_COMMENT";
85 ${comment_char}Created by $producer
86 ${comment_char}Created on $now
90 # Any additional stuff passed in
91 for my $additional_comment (@_) {
92 $header_comment .= "${comment_char}${additional_comment}\n";
95 return $header_comment;
99 my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ];
102 # This protects stringification of references.
104 if ( @$list && ref $list->[0] ) {
108 # This processes string-like arguments.
112 map { s/^\s+|\s+$//g; $_ }
114 grep { defined && length } @$list
119 sub truncate_id_uniquely {
120 my ( $desired_name, $max_symbol_length ) = @_;
123 unless defined $desired_name && length $desired_name > $max_symbol_length;
125 my $truncated_name = substr $desired_name, 0,
126 $max_symbol_length - COLLISION_TAG_LENGTH - 1;
128 # Hex isn't the most space-efficient, but it skirts around allowed
130 my $digest = sha1_hex($desired_name);
131 my $collision_tag = substr $digest, 0, COLLISION_TAG_LENGTH;
133 return $truncated_name
139 sub parse_mysql_version {
140 my ($v, $target) = @_;
142 return undef unless $v;
149 if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) {
150 push @vers, $1, $2, $3;
153 # XYYZZ (mysql) style
154 elsif ( $v =~ / ^ (\d) (\d{2}) (\d{2}) $ /x ) {
155 push @vers, $1, $2, $3;
158 # XX.YYYZZZ (perl) style or simply X
159 elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) {
160 push @vers, $1, $2, $3;
163 #how do I croak sanely here?
164 die "Unparseable MySQL version '$v'";
167 if ($target eq 'perl') {
168 return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) );
170 elsif ($target eq 'mysql') {
171 return sprintf ('%d%02d%02d', map { $_ || 0 } (@vers) );
174 #how do I croak sanely here?
175 die "Unknown version target '$target'";
179 sub parse_dbms_version {
180 my ($v, $target) = @_;
182 return undef unless $v;
187 if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) {
188 push @vers, $1, $2, $3;
191 # XX.YYYZZZ (perl) style or simply X
192 elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) {
193 push @vers, $1, $2, $3;
196 #how do I croak sanely here?
197 die "Unparseable database server version '$v'";
200 if ($target eq 'perl') {
201 return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) );
203 elsif ($target eq 'native') {
204 return join '.' => grep defined, @vers;
207 #how do I croak sanely here?
208 die "Unknown version target '$target'";
218 SQL::Translator::Utils - SQL::Translator Utility functions
222 use SQL::Translator::Utils qw(debug);
223 debug("PKG: Bad things happened");
227 C<SQL::Translator::Utils> contains utility functions designed to be
228 used from the other modules within the C<SQL::Translator> modules.
230 Nothing is exported by default.
232 =head1 EXPORTED FUNCTIONS AND CONSTANTS
236 C<debug> takes 0 or more messages, which will be sent to STDERR using
237 C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE>
238 will be replaced by the calling package, subroutine, and line number,
239 respectively, as reported by C<caller(1)>.
241 For example, from within C<foo> in F<SQL/Translator.pm>, at line 666:
243 debug("PKG: Error reading file at SUB/LINE");
247 [SQL::Translator: Error reading file at foo/666]
249 The entire message is enclosed within C<[> and C<]> for visual clarity
250 when STDERR is intermixed with STDOUT.
252 =head2 normalize_name
254 C<normalize_name> takes a string and ensures that it is suitable for
255 use as an identifier. This means: ensure that it starts with a letter
256 or underscore, and that the rest of the string consists of only
257 letters, numbers, and underscores. A string that begins with
258 something other than [a-zA-Z] will be prefixer with an underscore, and
259 all other characters in the string will be replaced with underscores.
260 Finally, a trailing underscore will be removed, because that's ugly.
262 normalize_name("Hello, world");
268 A more useful example, from the C<SQL::Translator::Parser::Excel> test
271 normalize_name("silly field (with random characters)");
275 silly_field_with_random_characters
277 =head2 header_comment
279 Create the header comment. Takes 1 mandatory argument (the producer
280 classname), an optional comment character (defaults to $DEFAULT_COMMENT),
281 and 0 or more additional comments, which will be appended to the header,
282 prefixed with the comment character. If additional comments are provided,
283 then a comment string must be provided ($DEFAULT_COMMENT is exported for
284 this use). For example, this:
286 package My::Producer;
288 use SQL::Translator::Utils qw(header_comment $DEFAULT_COMMENT);
290 print header_comment(__PACKAGE__,
297 -- Created by My::Prodcuer
298 -- Created on Fri Apr 25 06:56:02 2003
303 Note the gratuitous spacing.
305 =head2 parse_list_arg
307 Takes a string, list or arrayref (all of which could contain
308 comma-separated values) and returns an array reference of the values.
309 All of the following will return equivalent values:
311 parse_list_arg('id');
312 parse_list_arg('id', 'name');
313 parse_list_arg( 'id, name' );
314 parse_list_arg( [ 'id', 'name' ] );
315 parse_list_arg( qw[ id name ] );
317 =head2 truncate_id_uniquely
319 Takes a string ($desired_name) and int ($max_symbol_length). Truncates
320 $desired_name to $max_symbol_length by including part of the hash of
321 the full name at the end of the truncated name, giving a high
322 probability that the symbol will be unique. For example,
324 truncate_id_uniquely( 'a' x 100, 64 )
325 truncate_id_uniquely( 'a' x 99 . 'b', 64 );
326 truncate_id_uniquely( 'a' x 99, 64 )
328 Will give three different results; specifically:
330 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7f900025
331 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_6191e39a
332 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_8cd96af2
334 =head2 $DEFAULT_COMMENT
336 This is the default comment string, '-- ' by default. Useful for
339 =head2 parse_mysql_version
341 Used by both L<Parser::MySQL|SQL::Translator::Parser::MySQL> and
342 L<Producer::MySQL|SQL::Translator::Producer::MySQL> in order to provide a
343 consistent format for both C<< parser_args->{mysql_parser_version} >> and
344 C<< producer_args->{mysql_version} >> respectively. Takes any of the following
345 version specifications:
351 5.001005 (perl style)
354 =head2 parse_dbms_version
356 Takes a version string (X.Y.Z) or perl style (XX.YYYZZZ) and a target ('perl'
357 or 'native') transforms the string to the given target style.
362 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
363 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.