1 package SQL::Translator::Utils;
3 # ----------------------------------------------------------------------
4 # $Id: Utils.pm,v 1.12 2004-02-09 23:04:26 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
24 use base qw(Exporter);
25 use vars qw($VERSION $DEFAULT_COMMENT @EXPORT_OK);
27 use Digest::SHA1 qw( sha1_hex );
31 $VERSION = sprintf "%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/;
32 $DEFAULT_COMMENT = '-- ';
34 debug normalize_name header_comment parse_list_arg truncate_id_uniquely $DEFAULT_COMMENT
37 # ----------------------------------------------------------------------
40 # Will send debugging messages to STDERR, if the caller's $DEBUG global
43 # This debug() function has a neat feature: Occurances of the strings
44 # PKG, LINE, and SUB in each message will be replaced with elements
47 # debug("PKG: Bad things happened on line LINE!");
51 # [SQL::Translator: Bad things happened on line 643]
53 # If called from Translator.pm, on line 643.
54 # ----------------------------------------------------------------------
56 my ($pkg, $file, $line, $sub) = caller(0);
59 return unless ${"$pkg\::DEBUG"};
67 $x =~ s/\bPKG\b/$pkg/g;
68 $x =~ s/\bLINE\b/$line/g;
69 $x =~ s/\bSUB\b/$sub/g;
70 #warn '[' . $x . "]\n";
71 print STDERR '[' . $x . "]\n";
75 # ----------------------------------------------------------------------
77 my $name = shift or return '';
79 # The name can only begin with a-zA-Z_; if there's anything
81 $name =~ s/^([^a-zA-Z_])/_$1/;
83 # anything other than a-zA-Z0-9_ in the non-first position
84 # needs to be turned into _
85 $name =~ tr/[a-zA-Z0-9_]/_/c;
87 # All duplicated _ need to be squashed into one.
96 # ----------------------------------------------------------------------
98 my $producer = shift || caller;
99 my $comment_char = shift;
100 my $now = scalar localtime;
102 $comment_char = $DEFAULT_COMMENT
103 unless defined $comment_char;
105 my $header_comment =<<"HEADER_COMMENT";
107 ${comment_char}Created by $producer
108 ${comment_char}Created on $now
112 # Any additional stuff passed in
113 for my $additional_comment (@_) {
114 $header_comment .= "${comment_char}${additional_comment}\n";
117 return $header_comment;
120 # ----------------------------------------------------------------------
123 # Meant to accept a list, an array reference, or a string of
124 # comma-separated values. Retuns an array reference of the
125 # arguments. Modified to also handle a list of references.
126 # ----------------------------------------------------------------------
128 my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ];
131 # This protects stringification of references.
133 if ( @$list && ref $list->[0] ) {
137 # This processes string-like arguments.
141 map { s/^\s+|\s+$//g; $_ }
143 grep { defined && length } @$list
148 # ----------------------------------------------------------------------
149 # truncate_id_uniquely( $desired_name, $max_symbol_length )
151 # Truncates the name $desired_name to the $max_symbol_length by
152 # including part of the hash of the full name at the end of the
153 # truncated name, giving a high probability that the symbol will be
155 # ----------------------------------------------------------------------
156 my $COLLISION_TAG_LENGTH = 8;
157 sub truncate_id_uniquely {
158 my ( $desired_name, $max_symbol_length ) = @_;
160 return $desired_name unless defined $desired_name && length $desired_name > $max_symbol_length;
162 my $truncated_name = substr $desired_name, 0, $max_symbol_length - $COLLISION_TAG_LENGTH - 1;
164 # Hex isn't the most space-efficient, but it skirts around allowed
166 my $digest = sha1_hex($desired_name);
167 my $collision_tag = substr $digest, 0, $COLLISION_TAG_LENGTH;
169 return $truncated_name
176 # ----------------------------------------------------------------------
182 SQL::Translator::Utils - SQL::Translator Utility functions
186 use SQL::Translator::Utils qw(debug);
187 debug("PKG: Bad things happened");
191 C<SQL::Translator::Utils> contains utility functions designed to be
192 used from the other modules within the C<SQL::Translator> modules.
194 Nothing is exported by default.
196 =head1 EXPORTED FUNCTIONS AND CONSTANTS
200 C<debug> takes 0 or more messages, which will be sent to STDERR using
201 C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE>
202 will be replaced by the calling package, subroutine, and line number,
203 respectively, as reported by C<caller(1)>.
205 For example, from within C<foo> in F<SQL/Translator.pm>, at line 666:
207 debug("PKG: Error reading file at SUB/LINE");
211 [SQL::Translator: Error reading file at foo/666]
213 The entire message is enclosed within C<[> and C<]> for visual clarity
214 when STDERR is intermixed with STDOUT.
216 =head2 normalize_name
218 C<normalize_name> takes a string and ensures that it is suitable for
219 use as an identifier. This means: ensure that it starts with a letter
220 or underscore, and that the rest of the string consists of only
221 letters, numbers, and underscores. A string that begins with
222 something other than [a-zA-Z] will be prefixer with an underscore, and
223 all other characters in the string will be replaced with underscores.
224 Finally, a trailing underscore will be removed, because that's ugly.
226 normalize_name("Hello, world");
232 A more useful example, from the C<SQL::Translator::Parser::Excel> test
235 normalize_name("silly field (with random characters)");
239 silly_field_with_random_characters
241 =head2 header_comment
243 Create the header comment. Takes 1 mandatory argument (the producer
244 classname), an optional comment character (defaults to $DEFAULT_COMMENT),
245 and 0 or more additional comments, which will be appended to the header,
246 prefixed with the comment character. If additional comments are provided,
247 then a comment string must be provided ($DEFAULT_COMMENT is exported for
248 this use). For example, this:
250 package My::Producer;
252 use SQL::Translator::Utils qw(header_comment $DEFAULT_COMMENT);
254 print header_comment(__PACKAGE__,
261 -- Created by My::Prodcuer
262 -- Created on Fri Apr 25 06:56:02 2003
267 Note the gratuitous spacing.
269 =head2 parse_list_arg
271 Takes a string, list or arrayref (all of which could contain
272 comma-separated values) and returns an array reference of the values.
273 All of the following will return equivalent values:
275 parse_list_arg('id');
276 parse_list_arg('id', 'name');
277 parse_list_arg( 'id, name' );
278 parse_list_arg( [ 'id', 'name' ] );
279 parse_list_arg( qw[ id name ] );
281 =head2 truncate_id_uniquely
283 Takes a string ($desired_name) and int ($max_symbol_length). Truncates
284 $desired_name to $max_symbol_length by including part of the hash of
285 the full name at the end of the truncated name, giving a high
286 probability that the symbol will be unique. For example,
288 truncate_id_uniquely( 'a' x 100, 64 )
289 truncate_id_uniquely( 'a' x 99 . 'b', 64 );
290 truncate_id_uniquely( 'a' x 99, 64 )
292 Will give three different results; specifically:
294 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7f900025
295 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_6191e39a
296 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_8cd96af2
298 =head2 $DEFAULT_COMMENT
300 This is the default comment string, '-- ' by default. Useful for
305 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
306 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.