Commit | Line | Data |
1a24938d |
1 | package SQL::Translator::Utils; |
2 | |
3 | # ---------------------------------------------------------------------- |
d4f84dd1 |
4 | # $Id: Utils.pm 1440 2009-01-17 16:31:57Z jawnsy $ |
1a24938d |
5 | # ---------------------------------------------------------------------- |
478f608d |
6 | # Copyright (C) 2002-2009 SQLFairy Authors |
1a24938d |
7 | # |
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. |
11 | # |
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. |
16 | # |
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 |
20 | # 02111-1307 USA |
21 | # ------------------------------------------------------------------- |
22 | |
23 | use strict; |
24 | use base qw(Exporter); |
da06ac74 |
25 | use vars qw($VERSION $DEFAULT_COMMENT @EXPORT_OK); |
1a24938d |
26 | |
f5405d47 |
27 | use Digest::SHA1 qw( sha1_hex ); |
28 | |
1a24938d |
29 | use Exporter; |
30 | |
da06ac74 |
31 | $VERSION = '1.99'; |
a2ba36ba |
32 | $DEFAULT_COMMENT = '-- '; |
118bb73f |
33 | @EXPORT_OK = qw( |
5d666b31 |
34 | debug normalize_name header_comment parse_list_arg truncate_id_uniquely $DEFAULT_COMMENT parse_mysql_version |
118bb73f |
35 | ); |
1a24938d |
36 | |
37 | # ---------------------------------------------------------------------- |
38 | # debug(@msg) |
39 | # |
40 | # Will send debugging messages to STDERR, if the caller's $DEBUG global |
41 | # is set. |
42 | # |
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 |
45 | # from caller(): |
46 | # |
47 | # debug("PKG: Bad things happened on line LINE!"); |
48 | # |
49 | # Will be warned as: |
50 | # |
51 | # [SQL::Translator: Bad things happened on line 643] |
52 | # |
53 | # If called from Translator.pm, on line 643. |
54 | # ---------------------------------------------------------------------- |
55 | sub debug { |
a2ba36ba |
56 | my ($pkg, $file, $line, $sub) = caller(0); |
1a24938d |
57 | { |
58 | no strict qw(refs); |
59 | return unless ${"$pkg\::DEBUG"}; |
60 | } |
61 | |
62 | $sub =~ s/^$pkg\:://; |
63 | |
64 | while (@_) { |
65 | my $x = shift; |
66 | chomp $x; |
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"; |
72 | } |
73 | } |
74 | |
118bb73f |
75 | # ---------------------------------------------------------------------- |
93d12e9c |
76 | sub normalize_name { |
ae48473b |
77 | my $name = shift or return ''; |
93d12e9c |
78 | |
79 | # The name can only begin with a-zA-Z_; if there's anything |
80 | # else, prefix with _ |
81 | $name =~ s/^([^a-zA-Z_])/_$1/; |
82 | |
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; |
86 | |
87 | # All duplicated _ need to be squashed into one. |
88 | $name =~ tr/_/_/s; |
89 | |
90 | # Trim a trailing _ |
91 | $name =~ s/_$//; |
92 | |
93 | return $name; |
94 | } |
95 | |
118bb73f |
96 | # ---------------------------------------------------------------------- |
a2ba36ba |
97 | sub header_comment { |
98 | my $producer = shift || caller; |
99 | my $comment_char = shift; |
100 | my $now = scalar localtime; |
101 | |
102 | $comment_char = $DEFAULT_COMMENT |
103 | unless defined $comment_char; |
104 | |
105 | my $header_comment =<<"HEADER_COMMENT"; |
106 | ${comment_char} |
107 | ${comment_char}Created by $producer |
108 | ${comment_char}Created on $now |
109 | ${comment_char} |
110 | HEADER_COMMENT |
111 | |
112 | # Any additional stuff passed in |
113 | for my $additional_comment (@_) { |
114 | $header_comment .= "${comment_char}${additional_comment}\n"; |
115 | } |
116 | |
117 | return $header_comment; |
118 | } |
119 | |
118bb73f |
120 | # ---------------------------------------------------------------------- |
51bb6fe0 |
121 | # parse_list_arg |
122 | # |
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 | # ---------------------------------------------------------------------- |
e545d971 |
127 | sub parse_list_arg { |
128 | my $list = UNIVERSAL::isa( $_[0], 'ARRAY' ) ? shift : [ @_ ]; |
129 | |
51bb6fe0 |
130 | # |
131 | # This protects stringification of references. |
132 | # |
133 | if ( @$list && ref $list->[0] ) { |
134 | return $list; |
135 | } |
136 | # |
137 | # This processes string-like arguments. |
138 | # |
139 | else { |
140 | return [ |
141 | map { s/^\s+|\s+$//g; $_ } |
142 | map { split /,/ } |
143 | grep { defined && length } @$list |
144 | ]; |
145 | } |
118bb73f |
146 | } |
147 | |
f5405d47 |
148 | # ---------------------------------------------------------------------- |
149 | # truncate_id_uniquely( $desired_name, $max_symbol_length ) |
150 | # |
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 |
154 | # unique. |
155 | # ---------------------------------------------------------------------- |
156 | my $COLLISION_TAG_LENGTH = 8; |
157 | sub truncate_id_uniquely { |
158 | my ( $desired_name, $max_symbol_length ) = @_; |
159 | |
160 | return $desired_name unless defined $desired_name && length $desired_name > $max_symbol_length; |
161 | |
162 | my $truncated_name = substr $desired_name, 0, $max_symbol_length - $COLLISION_TAG_LENGTH - 1; |
163 | |
164 | # Hex isn't the most space-efficient, but it skirts around allowed |
165 | # charset issues |
166 | my $digest = sha1_hex($desired_name); |
167 | my $collision_tag = substr $digest, 0, $COLLISION_TAG_LENGTH; |
168 | |
169 | return $truncated_name |
170 | . '_' |
171 | . $collision_tag; |
172 | } |
173 | |
5d666b31 |
174 | |
175 | #--------------------------------------------------------------------- |
176 | # parse_mysql_version ( $version_string, $result_target) |
177 | # |
178 | # Attempts to parse an arbitrary string as a mysql version number. |
179 | # Returns either a floating point perl style string, or a mysql style |
180 | # 5 digit string, depending on the supplied $result_target |
181 | #--------------------------------------------------------------------- |
182 | sub parse_mysql_version { |
183 | my ($v, $target) = @_; |
184 | |
185 | return undef unless $v; |
186 | |
187 | $target ||= 'perl'; |
188 | |
189 | my @vers; |
190 | |
191 | # X.Y.Z style |
192 | if ( $v =~ / ^ (\d+) \. (\d{1,3}) (?: \. (\d{1,3}) )? $ /x ) { |
193 | push @vers, $1, $2, $3; |
194 | } |
195 | |
196 | # XYYZZ (mysql) style |
197 | elsif ( $v =~ / ^ (\d) (\d{2}) (\d{2}) $ /x ) { |
198 | push @vers, $1, $2, $3; |
199 | } |
200 | |
201 | # XX.YYYZZZ (perl) style or simply X |
202 | elsif ( $v =~ / ^ (\d+) (?: \. (\d{3}) (\d{3}) )? $ /x ) { |
203 | push @vers, $1, $2, $3; |
204 | } |
205 | else { |
206 | #how do I croak sanely here? |
207 | die "Unparseable MySQL version '$v'"; |
208 | } |
209 | |
210 | if ($target eq 'perl') { |
211 | return sprintf ('%d.%03d%03d', map { $_ || 0 } (@vers) ); |
212 | } |
213 | elsif ($target eq 'mysql') { |
214 | return sprintf ('%d%02d%02d', map { $_ || 0 } (@vers) ); |
215 | } |
216 | else { |
217 | #how do I croak sanely here? |
218 | die "Unknown version target '$target'"; |
219 | } |
220 | } |
221 | |
222 | |
1a24938d |
223 | 1; |
224 | |
118bb73f |
225 | # ---------------------------------------------------------------------- |
226 | |
227 | =pod |
1a24938d |
228 | |
229 | =head1 NAME |
230 | |
231 | SQL::Translator::Utils - SQL::Translator Utility functions |
232 | |
233 | =head1 SYNOPSIS |
234 | |
235 | use SQL::Translator::Utils qw(debug); |
236 | debug("PKG: Bad things happened"); |
237 | |
238 | =head1 DESCSIPTION |
239 | |
240 | C<SQL::Translator::Utils> contains utility functions designed to be |
241 | used from the other modules within the C<SQL::Translator> modules. |
242 | |
a2ba36ba |
243 | Nothing is exported by default. |
1a24938d |
244 | |
a2ba36ba |
245 | =head1 EXPORTED FUNCTIONS AND CONSTANTS |
1a24938d |
246 | |
247 | =head2 debug |
248 | |
249 | C<debug> takes 0 or more messages, which will be sent to STDERR using |
250 | C<warn>. Occurances of the strings I<PKG>, I<SUB>, and I<LINE> |
251 | will be replaced by the calling package, subroutine, and line number, |
e545d971 |
252 | respectively, as reported by C<caller(1)>. |
1a24938d |
253 | |
254 | For example, from within C<foo> in F<SQL/Translator.pm>, at line 666: |
255 | |
256 | debug("PKG: Error reading file at SUB/LINE"); |
257 | |
258 | Will warn |
259 | |
260 | [SQL::Translator: Error reading file at foo/666] |
261 | |
262 | The entire message is enclosed within C<[> and C<]> for visual clarity |
263 | when STDERR is intermixed with STDOUT. |
93d12e9c |
264 | |
265 | =head2 normalize_name |
266 | |
267 | C<normalize_name> takes a string and ensures that it is suitable for |
268 | use as an identifier. This means: ensure that it starts with a letter |
269 | or underscore, and that the rest of the string consists of only |
270 | letters, numbers, and underscores. A string that begins with |
271 | something other than [a-zA-Z] will be prefixer with an underscore, and |
272 | all other characters in the string will be replaced with underscores. |
273 | Finally, a trailing underscore will be removed, because that's ugly. |
274 | |
275 | normalize_name("Hello, world"); |
276 | |
277 | Produces: |
278 | |
279 | Hello_world |
280 | |
281 | A more useful example, from the C<SQL::Translator::Parser::Excel> test |
282 | suite: |
283 | |
284 | normalize_name("silly field (with random characters)"); |
285 | |
286 | returns: |
287 | |
288 | silly_field_with_random_characters |
289 | |
a2ba36ba |
290 | =head2 header_comment |
291 | |
292 | Create the header comment. Takes 1 mandatory argument (the producer |
293 | classname), an optional comment character (defaults to $DEFAULT_COMMENT), |
294 | and 0 or more additional comments, which will be appended to the header, |
295 | prefixed with the comment character. If additional comments are provided, |
296 | then a comment string must be provided ($DEFAULT_COMMENT is exported for |
297 | this use). For example, this: |
298 | |
299 | package My::Producer; |
300 | |
301 | use SQL::Translator::Utils qw(header_comment $DEFAULT_COMMENT); |
302 | |
303 | print header_comment(__PACKAGE__, |
e545d971 |
304 | $DEFAULT_COMMENT, |
a2ba36ba |
305 | "Hi mom!"); |
306 | |
307 | produces: |
308 | |
e545d971 |
309 | -- |
a2ba36ba |
310 | -- Created by My::Prodcuer |
311 | -- Created on Fri Apr 25 06:56:02 2003 |
e545d971 |
312 | -- |
a2ba36ba |
313 | -- Hi mom! |
e545d971 |
314 | -- |
a2ba36ba |
315 | |
316 | Note the gratuitous spacing. |
317 | |
118bb73f |
318 | =head2 parse_list_arg |
319 | |
320 | Takes a string, list or arrayref (all of which could contain |
321 | comma-separated values) and returns an array reference of the values. |
322 | All of the following will return equivalent values: |
323 | |
324 | parse_list_arg('id'); |
325 | parse_list_arg('id', 'name'); |
326 | parse_list_arg( 'id, name' ); |
327 | parse_list_arg( [ 'id', 'name' ] ); |
328 | parse_list_arg( qw[ id name ] ); |
329 | |
f5405d47 |
330 | =head2 truncate_id_uniquely |
331 | |
332 | Takes a string ($desired_name) and int ($max_symbol_length). Truncates |
333 | $desired_name to $max_symbol_length by including part of the hash of |
334 | the full name at the end of the truncated name, giving a high |
335 | probability that the symbol will be unique. For example, |
336 | |
337 | truncate_id_uniquely( 'a' x 100, 64 ) |
338 | truncate_id_uniquely( 'a' x 99 . 'b', 64 ); |
339 | truncate_id_uniquely( 'a' x 99, 64 ) |
340 | |
341 | Will give three different results; specifically: |
342 | |
343 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7f900025 |
344 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_6191e39a |
345 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_8cd96af2 |
346 | |
a2ba36ba |
347 | =head2 $DEFAULT_COMMENT |
348 | |
349 | This is the default comment string, '-- ' by default. Useful for |
350 | C<header_comment>. |
351 | |
5d666b31 |
352 | =head2 parse_mysql_version |
353 | |
354 | Used by both L<Parser::MySQL|SQL::Translator::Parser::MySQL> and |
355 | L<Producer::MySQL|SQL::Translator::Producer::MySQL> in order to provide a |
356 | consistent format for both C<< parser_args->{mysql_parser_version} >> and |
357 | C<< producer_args->{mysql_version} >> respectively. Takes any of the following |
358 | version specifications: |
359 | |
360 | 5.0.3 |
361 | 4.1 |
362 | 3.23.2 |
363 | 5 |
364 | 5.001005 (perl style) |
365 | 30201 (mysql style) |
366 | |
118bb73f |
367 | =head1 AUTHORS |
368 | |
369 | Darren Chamberlain E<lt>darren@cpan.orgE<gt>, |
370 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
371 | |
372 | =cut |