Fixed some typos, added some basic re-logicing (is that even a word?)
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator.pm
CommitLineData
16dc9970 1package SQL::Translator;
2
b346d8f1 3# ----------------------------------------------------------------------
38254289 4# $Id: Translator.pm,v 1.7 2002-06-11 12:09:13 dlc Exp $
b346d8f1 5# ----------------------------------------------------------------------
077ebf34 6# Copyright (C) 2002 Ken Y. Clark <kycl4rk@users.sourceforge.net>,
7# darren chamberlain <darren@cpan.org>
1fd8c91f 8#
077ebf34 9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as
11# published by the Free Software Foundation; version 2.
ca10f295 12#
077ebf34 13# This program is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16# General Public License for more details.
ca10f295 17#
077ebf34 18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21# 02111-1307 USA
ca10f295 22# -------------------------------------------------------------------
23
24=head1 NAME
25
26SQL::Translator - convert schema from one database to another
27
28=head1 SYNOPSIS
29
30 use SQL::Translator;
31 my $translator = SQL::Translator->new;
077ebf34 32
b346d8f1 33 my $output = $translator->translate(
34 from => "MySQL",
35 to => "Oracle",
36 filename => $file,
37 ) or die $translator->error;
ca10f295 38 print $output;
39
40=head1 DESCRIPTION
41
42This module attempts to simplify the task of converting one database
43create syntax to another through the use of Parsers and Producers.
44The idea is that any Parser can be used with any Producer in the
b346d8f1 45conversion process. So, if you wanted PostgreSQL-to-Oracle, you would
46use the PostgreSQL parser and the Oracle producer.
ca10f295 47
077ebf34 48Currently, the existing parsers use Parse::RecDescent, but this not
49a requirement, or even a recommendation. New parser modules don't
50necessarily have to use Parse::RecDescent, as long as the module
51implements the appropriate API. With this separation of code, it is
ca10f295 52hoped that developers will find it easy to add more database dialects
53by using what's written, writing only what they need, and then
54contributing their parsers or producers back to the project.
55
56=cut
16dc9970 57
58use strict;
ca10f295 59use vars qw($VERSION $DEFAULT_SUB $DEBUG);
38254289 60$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
ca10f295 61$DEBUG = 1 unless defined $DEBUG;
16dc9970 62
b346d8f1 63# ----------------------------------------------------------------------
64# The default behavior is to "pass through" values (note that the
65# SQL::Translator instance is the first value ($_[0]), and the stuff
66# to be parsed is the second value ($_[1])
67# ----------------------------------------------------------------------
68$DEFAULT_SUB = sub { $_[1] } unless defined $DEFAULT_SUB;
16dc9970 69
ca10f295 70*isa = \&UNIVERSAL::isa;
16dc9970 71
b346d8f1 72use Carp qw(carp);
1fd8c91f 73
ca10f295 74=head1 CONSTRUCTOR
16dc9970 75
077ebf34 76The constructor is called B<new>, and accepts a optional hash of options.
ca10f295 77Valid options are:
16dc9970 78
ca10f295 79=over 4
80
81=item parser (aka from)
82
9398955f 83=item parser_args
84
ca10f295 85=item producer (aka to)
86
9398955f 87=item producer_args
88
89=item filename (aka file)
90
91=item data
92
93=item debug
ca10f295 94
95=back
96
97All options are, well, optional; these attributes can be set via
b346d8f1 98instance methods. Internally, they are; no (non-syntactical)
99advantage is gained by passing options to the constructor.
ca10f295 100
101=cut
102
103# {{{ new
b346d8f1 104# ----------------------------------------------------------------------
105# new([ARGS])
106# The constructor.
dfb4c915 107#
b346d8f1 108# new takes an optional hash of arguments. These arguments may
109# include a parser, specified with the keys "parser" or "from",
110# and a producer, specified with the keys "producer" or "to".
dfb4c915 111#
b346d8f1 112# The values that can be passed as the parser or producer are
113# given directly to the parser or producer methods, respectively.
114# See the appropriate method description below for details about
115# what each expects/accepts.
b346d8f1 116# ----------------------------------------------------------------------
16dc9970 117sub new {
16dc9970 118 my $class = shift;
38254289 119 my $args = $_[0] && isa($_[0], 'HASH') ? shift : { @_ };
ca10f295 120 my $self = bless { } => $class;
1fd8c91f 121
b346d8f1 122 # ------------------------------------------------------------------
123 # Set the parser and producer.
ca10f295 124 #
b346d8f1 125 # If a 'parser' or 'from' parameter is passed in, use that as the
126 # parser; if a 'producer' or 'to' parameter is passed in, use that
127 # as the producer; both default to $DEFAULT_SUB.
128 # ------------------------------------------------------------------
ca10f295 129 $self->parser( $args->{'parser'} || $args->{'from'} || $DEFAULT_SUB);
130 $self->producer($args->{'producer'} || $args->{'to'} || $DEFAULT_SUB);
131
b346d8f1 132 # ------------------------------------------------------------------
e2158c40 133 # Set the parser_args and producer_args
134 # ------------------------------------------------------------------
135 for my $pargs (qw(parser_args producer_args)) {
136 $self->$pargs($args->{$pargs}) if defined $args->{$pargs};
137 }
138
139 # ------------------------------------------------------------------
9398955f 140 # Set the data source, if 'filename' or 'file' is provided.
141 # ------------------------------------------------------------------
142 $args->{'filename'} ||= $args->{'file'} || "";
143 $self->filename($args->{'filename'}) if $args->{'filename'};
144
145 # ------------------------------------------------------------------
146 # Finally, if there is a 'data' parameter, use that in preference
147 # to filename and file
148 # ------------------------------------------------------------------
149 if (my $data = $args->{'data'}) {
150 $self->data($data);
151 }
152
153 $self->{'debug'} = $DEBUG;
154 $self->{'debug'} = $args->{'debug'} if (defined $args->{'debug'});
155
156 # ------------------------------------------------------------------
ca10f295 157 # Clear the error
b346d8f1 158 # ------------------------------------------------------------------
ca10f295 159 $self->error_out("");
160
161 return $self;
dfb4c915 162}
ca10f295 163# }}}
1fd8c91f 164
ca10f295 165=head1 METHODS
166
ca10f295 167=head2 B<producer>
168
169The B<producer> method is an accessor/mutator, used to retrieve or
170define what subroutine is called to produce the output. A subroutine
b346d8f1 171defined as a producer will be invoked as a function (not a method) and
172passed 2 parameters: its container SQL::Translator instance and a
173data structure. It is expected that the function transform the data
174structure to a string. The SQL::Transformer instance is provided for
175informational purposes; for example, the type of the parser can be
176retrieved using the B<parser_type> method, and the B<error> and
177B<debug> methods can be called when needed.
178
179When defining a producer, one of several things can be passed
180in: A module name (e.g., My::Groovy::Producer), a module name
181relative to the SQL::Translator::Producer namespace (e.g., MySQL), a
182module name and function combination (My::Groovy::Producer::transmogrify),
183or a reference to an anonymous subroutine. If a full module name is
184passed in (for the purposes of this method, a string containing "::"
185is considered to be a module name), it is treated as a package, and a
186function called "produce" will be invoked: $modulename::produce. If
187$modulename cannot be loaded, the final portion is stripped off and
188treated as a function. In other words, if there is no file named
189My/Groovy/Producer/transmogrify.pm, SQL::Translator will attempt to load
190My/Groovy/Producer.pm and use transmogrify as the name of the function,
191instead of the default "produce".
ca10f295 192
193 my $tr = SQL::Translator->new;
194
077ebf34 195 # This will invoke My::Groovy::Producer::produce($tr, $data)
ca10f295 196 $tr->producer("My::Groovy::Producer");
197
077ebf34 198 # This will invoke SQL::Translator::Producer::Sybase::produce($tr, $data)
ca10f295 199 $tr->producer("Sybase");
200
b346d8f1 201 # This will invoke My::Groovy::Producer::transmogrify($tr, $data),
202 # assuming that My::Groovy::Producer::transmogrify is not a module
203 # on disk.
e2158c40 204 $tr->producer("My::Groovy::Producer::transmogrify");
b346d8f1 205
077ebf34 206 # This will invoke the referenced subroutine directly, as
207 # $subref->($tr, $data);
ca10f295 208 $tr->producer(\&my_producer);
209
077ebf34 210There is also a method named B<producer_type>, which is a string
211containing the classname to which the above B<produce> function
212belongs. In the case of anonymous subroutines, this method returns
213the string "CODE".
214
e2158c40 215Finally, there is a method named B<producer_args>, which is both an
216accessor and a mutator. Arbitrary data may be stored in name => value
217pairs for the producer subroutine to access:
218
219 sub My::Random::producer {
220 my ($tr, $data) = @_;
221 my $pr_args = $tr->producer_args();
222
223 # $pr_args is a hashref.
224
225Extra data passed to the B<producer> method is passed to
226B<producer_args>:
227
228 $tr->producer("xSV", delimiter => ',\s*');
229
230 # In SQL::Translator::Producer::xSV:
231 my $args = $tr->producer_args;
232 my $delimiter = $args->{'delimiter'}; # value is => ,\s*
233
ca10f295 234=cut
ca10f295 235
077ebf34 236# {{{ producer and producer_type
ca10f295 237sub producer {
1fd8c91f 238 my $self = shift;
b346d8f1 239
240 # {{{ producer as a mutator
ca10f295 241 if (@_) {
242 my $producer = shift;
b346d8f1 243
244 # {{{ Passed a module name (string containing "::")
ca10f295 245 if ($producer =~ /::/) {
077ebf34 246 my $func_name;
b346d8f1 247
248 # {{{ Module name was passed directly
249 # We try to load the name; if it doesn't load, there's
250 # a possibility that it has a function name attached to
251 # it.
077ebf34 252 if (load($producer)) {
253 $func_name = "produce";
b346d8f1 254 } # }}}
255
256 # {{{ Module::function was passed
257 else {
258 # Passed Module::Name::function; try to recover
077ebf34 259 my @func_parts = split /::/, $producer;
260 $func_name = pop @func_parts;
261 $producer = join "::", @func_parts;
b346d8f1 262
263 # If this doesn't work, then we have a legitimate
264 # problem.
077ebf34 265 load($producer) or die "Can't load $producer: $@";
b346d8f1 266 } # }}}
077ebf34 267
b346d8f1 268 # {{{ get code reference and assign
077ebf34 269 $self->{'producer'} = \&{ "$producer\::$func_name" };
270 $self->{'producer_type'} = $producer;
e2158c40 271 $self->debug("Got producer: $producer\::$func_name");
b346d8f1 272 # }}}
273 } # }}}
274
275 # {{{ passed an anonymous subroutine reference
276 elsif (isa($producer, 'CODE')) {
ca10f295 277 $self->{'producer'} = $producer;
077ebf34 278 $self->{'producer_type'} = "CODE";
9398955f 279 $self->debug("Got producer: code ref");
b346d8f1 280 } # }}}
281
282 # {{{ passed a string containing no "::"; relative package name
283 else {
ca10f295 284 my $Pp = sprintf "SQL::Translator::Producer::$producer";
285 load($Pp) or die "Can't load $Pp: $@";
077ebf34 286 $self->{'producer'} = \&{ "$Pp\::produce" };
287 $self->{'producer_type'} = $Pp;
ca10f295 288 $self->debug("Got producer: $Pp");
b346d8f1 289 } # }}}
290
ca10f295 291 # At this point, $self->{'producer'} contains a subroutine
b346d8f1 292 # reference that is ready to run
e2158c40 293
294 # {{{ Anything left? If so, it's producer_args
295 $self->produser_args(@_) if (@_);
296 # }}}
b346d8f1 297 } # }}}
298
ca10f295 299 return $self->{'producer'};
300};
077ebf34 301
e2158c40 302# {{{ producer_type
303# producer_type is an accessor that allows producer subs to get
304# information about their origin. This is poptentially important;
305# since all producer subs are called as subroutine refernces, there is
306# no way for a producer to find out which package the sub lives in
307# originally, for example.
308sub producer_type { $_[0]->{'producer_type'} } # }}}
309
310# {{{ producer_args
311# Arbitrary name => value pairs of paramters can be passed to a
312# producer using this method.
313sub producer_args {
314 my $self = shift;
315 if (@_) {
316 my $args = isa($_[0], 'HASH') ? shift : { @_ };
317 $self->{'producer_args'} = $args;
318 }
319 $self->{'producer_args'};
320} # }}}
ca10f295 321# }}}
322
323=head2 B<parser>
324
325The B<parser> method defines or retrieves a subroutine that will be
326called to perform the parsing. The basic idea is the same as that of
327B<producer> (see above), except the default subroutine name is
077ebf34 328"parse", and will be invoked as $module_name::parse($tr, $data).
329Also, the parser subroutine will be passed a string containing the
330entirety of the data to be parsed (or possibly a reference to a string?).
ca10f295 331
332 # Invokes SQL::Translator::Parser::MySQL::parse()
333 $tr->parser("MySQL");
334
335 # Invokes My::Groovy::Parser::parse()
336 $tr->parser("My::Groovy::Parser");
337
338 # Invoke an anonymous subroutine directly
339 $tr->parser(sub {
077ebf34 340 my $dumper = Data::Dumper->new([ $_[1] ], [ "SQL" ]);
ca10f295 341 $dumper->Purity(1)->Terse(1)->Deepcopy(1);
342 return $dumper->Dump;
343 });
344
e2158c40 345There is also B<parser_type> and B<parser_args>, which perform
346analogously to B<producer_type> and B<producer_args>
347
ca10f295 348=cut
349
e2158c40 350# {{{ parser, parser_type, and parser_args
ca10f295 351sub parser {
352 my $self = shift;
b346d8f1 353
354 # {{{ parser as a mutator
ca10f295 355 if (@_) {
356 my $parser = shift;
b346d8f1 357
358 # {{{ Passed a module name (string containing "::")
ca10f295 359 if ($parser =~ /::/) {
b346d8f1 360 my $func_name;
361
362 # {{{ Module name was passed directly
363 # We try to load the name; if it doesn't load, there's
364 # a possibility that it has a function name attached to
365 # it.
366 if (load($parser)) {
367 $func_name = "parse";
368 } # }}}
369
370 # {{{ Module::function was passed
371 else {
372 # Passed Module::Name::function; try to recover
373 my @func_parts = split /::/, $parser;
374 $func_name = pop @func_parts;
375 $parser = join "::", @func_parts;
376
377 # If this doesn't work, then we have a legitimate
378 # problem.
379 load($parser) or die "Can't load $parser: $@";
380 } # }}}
381
382 # {{{ get code reference and assign
383 $self->{'parser'} = \&{ "$parser\::$func_name" };
077ebf34 384 $self->{'parser_type'} = $parser;
b346d8f1 385 $self->debug("Got parser: $parser\::$func_name");
386 # }}}
387 } # }}}
388
389 # {{{ passed an anonymous subroutine reference
390 elsif (isa($parser, 'CODE')) {
ca10f295 391 $self->{'parser'} = $parser;
077ebf34 392 $self->{'parser_type'} = "CODE";
9398955f 393 $self->debug("Got parser: code ref");
b346d8f1 394 } # }}}
395
396 # {{{ passed a string containing no "::"; relative package name
397 else {
398 my $Pp = sprintf "SQL::Translator::Parser::$parser";
ca10f295 399 load($Pp) or die "Can't load $Pp: $@";
400 $self->{'parser'} = \&{ "$Pp\::parse" };
077ebf34 401 $self->{'parser_type'} = $Pp;
ca10f295 402 $self->debug("Got parser: $Pp");
b346d8f1 403 } # }}}
404
405 # At this point, $self->{'parser'} contains a subroutine
406 # reference that is ready to run
b346d8f1 407
e2158c40 408 $self->parser_args(@_) if (@_);
409 } # }}}
b346d8f1 410
ca10f295 411 return $self->{'parser'};
16dc9970 412}
1fd8c91f 413
077ebf34 414sub parser_type { $_[0]->{'parser_type'} }
e2158c40 415
416# {{{ parser_args
417sub parser_args {
418 my $self = shift;
419 if (@_) {
420 my $args = isa($_[0], 'HASH') ? shift : { @_ };
421 $self->{'parser_args'} = $args;
422 }
423 $self->{'parser_args'};
424} # }}}
ca10f295 425# }}}
16dc9970 426
ca10f295 427=head2 B<translate>
428
429The B<translate> method calls the subroutines referenced by the
430B<parser> and B<producer> data members (described above). It accepts
431as arguments a number of things, in key => value format, including
432(potentially) a parser and a producer (they are passed directly to the
433B<parser> and B<producer> methods).
434
435Here is how the parameter list to B<translate> is parsed:
436
437=over
438
439=item *
440
4411 argument means it's the data to be parsed; which could be a string
b346d8f1 442(filename) or a refernce to a scalar (a string stored in memory), or a
443reference to a hash, which is parsed as being more than one argument
444(see next section).
ca10f295 445
446 # Parse the file /path/to/datafile
447 my $output = $tr->translate("/path/to/datafile");
448
b346d8f1 449 # Parse the data contained in the string $data
ca10f295 450 my $output = $tr->translate(\$data);
451
452=item *
453
077ebf34 454More than 1 argument means its a hash of things, and it might be
455setting a parser, producer, or datasource (this key is named
b346d8f1 456"filename" or "file" if it's a file, or "data" for a SCALAR reference.
ca10f295 457
458 # As above, parse /path/to/datafile, but with different producers
459 for my $prod ("MySQL", "XML", "Sybase") {
460 print $tr->translate(
461 producer => $prod,
462 filename => "/path/to/datafile",
463 );
464 }
465
466 # The filename hash key could also be:
ca10f295 467 datasource => \$data,
468
469You get the idea.
470
471=back
472
9398955f 473=head2 B<filename>, B<data>
474
475Using the B<filename> method, the filename of the data to be parsed
476can be set. This method can be used in conjunction with the B<data>
477method, below. If both the B<filename> and B<data> methods are
478invoked as mutators, the data set in the B<data> method is used.
479
480 $tr->filename("/my/data/files/create.sql");
481
482or:
483
484 my $create_script = do {
485 local $/;
486 open CREATE, "/my/data/files/create.sql" or die $!;
487 <CREATE>;
488 };
489 $tr->data(\$create_script);
490
491B<filename> takes a string, which is interpreted as a filename.
38254289 492B<data> takes a reference to a string, which is used as the data to be
9398955f 493parsed. If a filename is set, then that file is opened and read when
494the B<translate> method is called, as long as the data instance
495variable is not set.
496
ca10f295 497=cut
498
9398955f 499# {{{ filename - get or set the filename
500sub filename {
501 my $self = shift;
502 if (@_) {
503 $self->{'filename'} = shift;
504 $self->debug("Got filename: $self->{'filename'}");
505 }
506 $self->{'filename'};
507} # }}}
508
509# {{{ data - get or set the data
510# if $self->{'data'} is not set, but $self->{'filename'} is, then
511# $self->{'filename'} is opened and read, whith the results put into
512# $self->{'data'}.
513sub data {
514 my $self = shift;
515
516 # {{{ Set $self->{'data'} to $_[0], if it is provided.
517 if (@_) {
518 my $data = shift;
519 if (isa($data, "SCALAR")) {
520 $self->{'data'} = $data;
521 }
522 elsif (! ref $data) {
523 $self->{'data'} = \$data;
524 }
525 }
526 # }}}
527
528 # {{{ If we have a filename but no data yet, populate.
529 if (not $self->{'data'} and my $filename = $self->filename) {
530 $self->debug("Opening '$filename' to get contents...");
531 local *FH;
532 local $/;
533 my $data;
534
535 unless (open FH, $filename) {
536 $self->error_out("Can't open $filename for reading: $!");
537 return;
538 }
539
540 $data = <FH>;
541 $self->{'data'} = \$data;
542
543 unless (close FH) {
544 $self->error_out("Can't close $filename: $!");
545 return;
546 }
547 }
548 # }}}
549
550 return $self->{'data'};
551} # }}}
552
ca10f295 553# {{{ translate
16dc9970 554sub translate {
ca10f295 555 my $self = shift;
556 my ($args, $parser, $producer);
557
9398955f 558 # {{{ Parse arguments
559 if (@_ == 1) {
560 # {{{ Passed a reference to a hash
ca10f295 561 if (isa($_[0], 'HASH')) {
562 # Passed a hashref
077ebf34 563 $self->debug("translate: Got a hashref");
ca10f295 564 $args = $_[0];
565 }
9398955f 566 # }}}
567
568 # {{{ Passed a reference to a string containing the data
ca10f295 569 elsif (isa($_[0], 'SCALAR')) {
9398955f 570 # passed a ref to a string
077ebf34 571 $self->debug("translate: Got a SCALAR reference (string)");
9398955f 572 $self->data($_[0]);
ca10f295 573 }
9398955f 574 # }}}
575
576 # {{{ Not a reference; treat it as a filename
b346d8f1 577 elsif (! ref $_[0]) {
ca10f295 578 # Not a ref, it's a filename
077ebf34 579 $self->debug("translate: Got a filename");
9398955f 580 $self->filename($_[0]);
ca10f295 581 }
9398955f 582 # }}}
583
584 # {{{ Passed something else entirely.
b346d8f1 585 else {
586 # We're not impressed. Take your empty string and leave.
38254289 587 # return "";
588
589 # Actually, if data, parser, and producer are set, then be can
590 # continue. Too bad, because I like my comment (above)...
591 return "" unless ($self->data &&
592 $self->producer &&
593 $self->parser);
b346d8f1 594 }
9398955f 595 # }}}
16dc9970 596 }
597 else {
b346d8f1 598 # You must pass in a hash, or you get nothing.
599 return "" if @_ % 2;
ca10f295 600 $args = { @_ };
9398955f 601 } # }}}
16dc9970 602
9398955f 603 # ----------------------------------------------------------------------
604 # Can specify the data to be transformed using "filename", "file",
605 # or "data"
606 # ----------------------------------------------------------------------
607 if (my $filename = $args->{'filename'} || $args->{'file'}) {
608 $self->filename($filename);
609 }
ca10f295 610
9398955f 611 if (my $data = $self->{'data'}) {
612 $self->data($data);
16dc9970 613 }
ca10f295 614
9398955f 615 # ----------------------------------------------------------------
616 # Get the data.
617 # ----------------------------------------------------------------
618 my $data = $self->data;
619 unless (defined $$data) {
620 $self->error_out("Empty data file!");
621 return "";
622 }
077ebf34 623
9398955f 624 # ----------------------------------------------------------------
ca10f295 625 # Local reference to the parser subroutine
9398955f 626 # ----------------------------------------------------------------
ca10f295 627 if ($parser = ($args->{'parser'} || $args->{'from'})) {
628 $self->parser($parser);
16dc9970 629 }
38254289 630 $parser = $self->parser;
16dc9970 631
9398955f 632 # ----------------------------------------------------------------
ca10f295 633 # Local reference to the producer subroutine
9398955f 634 # ----------------------------------------------------------------
ca10f295 635 if ($producer = ($args->{'producer'} || $args->{'to'})) {
636 $self->producer($producer);
16dc9970 637 }
38254289 638 $producer = $self->producer;
16dc9970 639
9398955f 640 # ----------------------------------------------------------------
ca10f295 641 # Execute the parser, then execute the producer with that output
9398955f 642 # ----------------------------------------------------------------
643 return $producer->($self, $parser->($self, $$data));
16dc9970 644}
ca10f295 645# }}}
646
647=head2 B<error>
16dc9970 648
ca10f295 649The error method returns the last error.
650
651=cut
652
653# {{{ error
16dc9970 654#-----------------------------------------------------
ca10f295 655sub error {
16dc9970 656#
ca10f295 657# Return the last error.
16dc9970 658#
ca10f295 659 return shift()->{'error'} || '';
660}
661# }}}
662
663=head2 B<error_out>
664
665Record the error and return undef. The error can be retrieved by
666calling programs using $tr->error.
667
668For Parser or Producer writers, primarily.
669
670=cut
671
672# {{{ error_out
673sub error_out {
16dc9970 674 my $self = shift;
ca10f295 675 if ( my $error = shift ) {
676 $self->{'error'} = $error;
16dc9970 677 }
ca10f295 678 return;
16dc9970 679}
ca10f295 680# }}}
1fd8c91f 681
ca10f295 682=head2 B<debug>
683
684If the global variable $SQL::Translator::DEBUG is set to a true value,
685then calls to $tr->debug($msg) will be carped to STDERR. If $DEBUG is
686not set, then this method does nothing.
687
688=cut
689
690# {{{ debug
ca10f295 691sub debug {
16dc9970 692 my $self = shift;
9398955f 693# if (ref $self) {
694# carp @_ if $self->{'debug'};
695# }
696# else {
697 if ($DEBUG) {
698 my $class = ref $self || $self;
699 carp "[$class] $_" for @_;
700 }
701# }
16dc9970 702}
ca10f295 703# }}}
704
705# {{{ load
706sub load {
707 my $module = do { my $m = shift; $m =~ s[::][/]g; "$m.pm" };
708 return 1 if $INC{$module};
709
710 eval { require $module };
711
712 return if ($@);
713 return 1;
1fd8c91f 714}
ca10f295 715# }}}
16dc9970 716
7171;
718
ca10f295 719__END__
16dc9970 720#-----------------------------------------------------
721# Rescue the drowning and tie your shoestrings.
722# Henry David Thoreau
723#-----------------------------------------------------
724
ca10f295 725=head1 AUTHOR
16dc9970 726
ca10f295 727Ken Y. Clark, E<lt>kclark@logsoft.comE<gt>,
728darren chamberlain E<lt>darren@cpan.orgE<gt>
dfb4c915 729
ca10f295 730=head1 COPYRIGHT
16dc9970 731
ca10f295 732This program is free software; you can redistribute it and/or modify
733it under the terms of the GNU General Public License as published by
734the Free Software Foundation; version 2.
dfb4c915 735
ca10f295 736This program is distributed in the hope that it will be useful, but
737WITHOUT ANY WARRANTY; without even the implied warranty of
738MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
739General Public License for more details.
16dc9970 740
ca10f295 741You should have received a copy of the GNU General Public License
742along with this program; if not, write to the Free Software
743Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
744USA
16dc9970 745
746=head1 SEE ALSO
747
ca10f295 748L<perl>, L<Parse::RecDescent>
16dc9970 749
750=cut