1 package SQL::Translator::Utils;
6 use Digest::SHA qw( sha1_hex );
10 our $DEFAULT_COMMENT = '-- ';
12 debug normalize_name header_comment parse_list_arg truncate_id_uniquely
13 $DEFAULT_COMMENT parse_mysql_version parse_dbms_version
15 use constant COLLISION_TAG_LENGTH => 8;
18 my ($pkg, $file, $line, $sub) = caller(0);
21 return unless ${"$pkg\::DEBUG"};
29 $x =~ s/\bPKG\b/$pkg/g;
30 $x =~ s/\bLINE\b/$line/g;
31 $x =~ s/\bSUB\b/$sub/g;
32 #warn '[' . $x . "]\n";
33 print STDERR '[' . $x . "]\n";
38 my $name = shift or return '';
40 # The name can only begin with a-zA-Z_; if there's anything
42 $name =~ s/^([^a-zA-Z_])/_$1/;
44 # anything other than a-zA-Z0-9_ in the non-first position
45 # needs to be turned into _
46 $name =~ tr/[a-zA-Z0-9_]/_/c;
48 # All duplicated _ need to be squashed into one.
58 my $producer = shift || caller;
59 my $comment_char = shift;
60 my $now = scalar localtime;
62 $comment_char = $DEFAULT_COMMENT
63 unless defined $comment_char;
65 my $header_comment =<<"HEADER_COMMENT";
67 ${comment_char}Created by $producer
68 ${comment_char}Created on $now
72 # Any additional stuff passed in
73 for my $additional_comment (@_) {
74 $header_comment .= "${comment_char}${additional_comment}\n";
77 return $header_comment;
81 my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ];
84 # This protects stringification of references.
86 if ( @$list && ref $list->[0] ) {
90 # This processes string-like arguments.
94 map { s/^\s+|\s+$//g; $_ }
96 grep { defined && length } @$list
101 sub truncate_id_uniquely {
102 my ( $desired_name, $max_symbol_length ) = @_;
105 unless defined $desired_name && length $desired_name > $max_symbol_length;
107 my $truncated_name = substr $desired_name, 0,
108 $max_symbol_length - COLLISION_TAG_LENGTH - 1;
110 # Hex isn't the most space-efficient, but it skirts around allowed
112 my $digest = sha1_hex($desired_name);
113 my $collision_tag = substr $digest, 0, COLLISION_TAG_LENGTH;
115 return $truncated_name
121 sub parse_mysql_version {
122 my ($v, $target) = @_;
124 return undef unless $v;
131 if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) {
132 push @vers, $1, $2, $3;
135 # XYYZZ (mysql) style
136 elsif ( $v =~ / ^ (\d) (\d{2}) (\d{2}) $ /x ) {
137 push @vers, $1, $2, $3;
140 # XX.YYYZZZ (perl) style or simply X
141 elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) {
142 push @vers, $1, $2, $3;
145 #how do I croak sanely here?
146 die "Unparseable MySQL version '$v'";
149 if ($target eq 'perl') {
150 return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) );
152 elsif ($target eq 'mysql') {
153 return sprintf ('%d%02d%02d', map { $_ || 0 } (@vers) );
156 #how do I croak sanely here?
157 die "Unknown version target '$target'";
161 sub parse_dbms_version {
162 my ($v, $target) = @_;
164 return undef unless $v;
169 if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) {
170 push @vers, $1, $2, $3;
173 # XX.YYYZZZ (perl) style or simply X
174 elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) {
175 push @vers, $1, $2, $3;
178 #how do I croak sanely here?
179 die "Unparseable database server version '$v'";
182 if ($target eq 'perl') {
183 return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) );
185 elsif ($target eq 'native') {
186 return join '.' => grep defined, @vers;
189 #how do I croak sanely here?
190 die "Unknown version target '$target'";
200 SQL::Translator::Utils - SQL::Translator Utility functions
204 use SQL::Translator::Utils qw(debug);
205 debug("PKG: Bad things happened");
209 C<SQL::Translator::Utils> contains utility functions designed to be
210 used from the other modules within the C<SQL::Translator> modules.
212 Nothing is exported by default.
214 =head1 EXPORTED FUNCTIONS AND CONSTANTS
218 C<debug> takes 0 or more messages, which will be sent to STDERR using
219 C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE>
220 will be replaced by the calling package, subroutine, and line number,
221 respectively, as reported by C<caller(1)>.
223 For example, from within C<foo> in F<SQL/Translator.pm>, at line 666:
225 debug("PKG: Error reading file at SUB/LINE");
229 [SQL::Translator: Error reading file at foo/666]
231 The entire message is enclosed within C<[> and C<]> for visual clarity
232 when STDERR is intermixed with STDOUT.
234 =head2 normalize_name
236 C<normalize_name> takes a string and ensures that it is suitable for
237 use as an identifier. This means: ensure that it starts with a letter
238 or underscore, and that the rest of the string consists of only
239 letters, numbers, and underscores. A string that begins with
240 something other than [a-zA-Z] will be prefixer with an underscore, and
241 all other characters in the string will be replaced with underscores.
242 Finally, a trailing underscore will be removed, because that's ugly.
244 normalize_name("Hello, world");
250 A more useful example, from the C<SQL::Translator::Parser::Excel> test
253 normalize_name("silly field (with random characters)");
257 silly_field_with_random_characters
259 =head2 header_comment
261 Create the header comment. Takes 1 mandatory argument (the producer
262 classname), an optional comment character (defaults to $DEFAULT_COMMENT),
263 and 0 or more additional comments, which will be appended to the header,
264 prefixed with the comment character. If additional comments are provided,
265 then a comment string must be provided ($DEFAULT_COMMENT is exported for
266 this use). For example, this:
268 package My::Producer;
270 use SQL::Translator::Utils qw(header_comment $DEFAULT_COMMENT);
272 print header_comment(__PACKAGE__,
279 -- Created by My::Prodcuer
280 -- Created on Fri Apr 25 06:56:02 2003
285 Note the gratuitous spacing.
287 =head2 parse_list_arg
289 Takes a string, list or arrayref (all of which could contain
290 comma-separated values) and returns an array reference of the values.
291 All of the following will return equivalent values:
293 parse_list_arg('id');
294 parse_list_arg('id', 'name');
295 parse_list_arg( 'id, name' );
296 parse_list_arg( [ 'id', 'name' ] );
297 parse_list_arg( qw[ id name ] );
299 =head2 truncate_id_uniquely
301 Takes a string ($desired_name) and int ($max_symbol_length). Truncates
302 $desired_name to $max_symbol_length by including part of the hash of
303 the full name at the end of the truncated name, giving a high
304 probability that the symbol will be unique. For example,
306 truncate_id_uniquely( 'a' x 100, 64 )
307 truncate_id_uniquely( 'a' x 99 . 'b', 64 );
308 truncate_id_uniquely( 'a' x 99, 64 )
310 Will give three different results; specifically:
312 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7f900025
313 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_6191e39a
314 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_8cd96af2
316 =head2 $DEFAULT_COMMENT
318 This is the default comment string, '-- ' by default. Useful for
321 =head2 parse_mysql_version
323 Used by both L<Parser::MySQL|SQL::Translator::Parser::MySQL> and
324 L<Producer::MySQL|SQL::Translator::Producer::MySQL> in order to provide a
325 consistent format for both C<< parser_args->{mysql_parser_version} >> and
326 C<< producer_args->{mysql_version} >> respectively. Takes any of the following
327 version specifications:
333 5.001005 (perl style)
336 =head2 parse_dbms_version
338 Takes a version string (X.Y.Z) or perl style (XX.YYYZZZ) and a target ('perl'
339 or 'native') transforms the string to the given target style.
344 Darren Chamberlain E<lt>darren@cpan.orgE<gt>,
345 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.