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