X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FProducer%2FTT%2FTable.pm;h=799ca54e4d18af19392162a44cf9568d22af21d8;hb=ac7adbab6451299d70b5ecc59c6bb7f8a6f9473b;hp=7ff5d5725cc9a53d3186b4aa56f1d34a4e26d75a;hpb=0be5b227cf41450dfbd47a634ace6efdd68b1d24;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Producer/TT/Table.pm b/lib/SQL/Translator/Producer/TT/Table.pm index 7ff5d57..799ca54 100644 --- a/lib/SQL/Translator/Producer/TT/Table.pm +++ b/lib/SQL/Translator/Producer/TT/Table.pm @@ -1,25 +1,5 @@ package SQL::Translator::Producer::TT::Table; -# ------------------------------------------------------------------- -# $Id: Table.pm,v 1.1 2004-04-01 19:02:39 grommit Exp $ -# ------------------------------------------------------------------- -# Copyright (C) 2002-4 SQLFairy Authors -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; version 2. -# -# This program is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -# 02111-1307 USA -# ------------------------------------------------------------------- - =pod =head1 NAME @@ -61,18 +41,19 @@ SQL::Translator::Producer::TT::Table - =head1 DESCRIPTION -Produces schema output using a given Template Tookit template, processing that -template for each table in the schema. Optionally allows you to write the result -for each table to a seperate file. +Produces schema output using a given Template Tookit template, +processing that template for each table in the schema. Optionally +allows you to write the result for each table to a separate file. It needs one additional producer_arg of C which is the file -name of the template to use. This template will be passed a template var of -C, which is the current C table we are -producing, which you can then use to walk the schema via the methods documented +name of the template to use. This template will be passed a template +var of C
, which is the current +L table we are producing, +which you can then use to walk the schema via the methods documented in that module. You also get C as a shortcut to the -C for the table and C, the -C object for this parse in case you want to get access to any -of the options etc set here. +L for the table and C, +the L object for this parse in case you want to get +access to any of the options etc set here. Here's a brief example of what the template could look like: @@ -84,9 +65,9 @@ Here's a brief example of what the template could look like: See F for a more complete example. -You can also set any of the options used to initiallize the Template object by -adding them to your producer_args. See Template Toolkit docs for details of -the options. +You can also set any of the options used to initialize the Template +object by adding them to your producer_args. See Template Toolkit docs +for details of the options. $translator = SQL::Translator->new( to => 'TT', @@ -97,11 +78,12 @@ the options. }, ); -If you set C and its additional options the producer will write a -seperate file for each table in the schema. This is usefull for producing -things like HTML documentation where every table gets its own page (you could -also use TTSchema producer to add an index page). Its also particulary good -for code generation where you want to produce a class file per table. +If you set C and its additional options the producer will +write a separate file for each table in the schema. This is useful for +producing things like HTML documentation where every table gets its +own page (you could also use TTSchema producer to add an index page). +It's also particularly good for code generation where you want to +produce a class file per table. =head1 OPTIONS @@ -113,10 +95,10 @@ File name of the template to run for each table. =item mk_files -Set to true to output a file for each table in the schema (as well as returning -the whole lot back to the Translalor and hence STDOUT). The file will be named -after the table, with the optional C added and placed in the -directory C. +Set to true to output a file for each table in the schema (as well as +returning the whole lot back to the Translalor and hence STDOUT). The +file will be named after the table, with the optional C +added and placed in the directory C. =item mk_files_ext @@ -124,58 +106,58 @@ Extension (without the dot) to add to the filename when using mk_files. =item mk_files_base = DIR -Dir to build the table files into when using mk_files. Defaults to the current -directory. +Dir to build the table files into when using mk_files. Defaults to the +current directory. =item mk_file_dir -Set true and if the file needs to written to a directory that doesn't exist, -it will be created first. +Set true and if the file needs to written to a directory that doesn't +exist, it will be created first. =item on_exists [Default:replace] -What to do if we are running with mk_files and a file already exists where we -want to write our output. One of "skip", "die", "replace", "insert". -The default is die. +What to do if we are running with mk_files and a file already exists +where we want to write our output. One of "skip", "die", "replace", +"insert". The default is die. -B - Over-write the existing file with the new one, clobbering anything -already there. +B - Over-write the existing file with the new one, clobbering +anything already there. -B - Leave the origional file as it was and don't write the new version -anywhere. +B - Leave the original file as it was and don't write the new +version anywhere. B - Die with an existing file error. -B - Insert the generated output into the file bewteen a set of special -comments (defined by the following options.) Any code between the comments will -be overwritten (ie the results from a previous produce) but the rest of the file -is left alone (your custom code). -This is particularly usefull for code generation as it allows you to -generate schema derived code and then add your own custom code using it to file. -Then when the scheam changes you just re-produce and insert the new code. +B - Insert the generated output into the file between a set of +special comments (defined by the following options.) Any code between +the comments will be overwritten (ie the results from a previous +produce) but the rest of the file is left alone (your custom code). +This is particularly useful for code generation as it allows you to +generate schema derived code and then add your own custom code +to the file. Then when the schema changes you just re-produce to +insert the new code. =item insert_comment_start -The comment to look for in the file when on_exists is insert. Default is -C. Must appear on it own line, with only whitespace either -side, to be recognised. +The comment to look for in the file when on_exists is C. Default +is C. Must appear on it own line, with only +whitespace either side, to be recognised. =item insert_comment_end -The end comment to look for in the file when on_exists is insert. Default is -C. Must appear on it own line, with only whitespace either -side, to be recognised. +The end comment to look for in the file when on_exists is C. +Default is C. Must appear on it own line, with only +whitespace either side, to be recognised. =back =cut -# ------------------------------------------------------------------- - use strict; +use warnings; -use vars qw[ $DEBUG $VERSION @EXPORT_OK ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/; +our ( $DEBUG, @EXPORT_OK ); +our $VERSION = '1.59'; $DEBUG = 0 unless defined $DEBUG; use File::Path; @@ -206,19 +188,19 @@ sub produce { %$pargs, # Allow any TT opts to be passed in the producer_args ) || die "Failed to initialize Template object: ".Template->error; - for my $tbl ( sort {$a->order <=> $b->order} $scma->get_tables ) { - my $outtmp; + for my $tbl ( sort {$a->order <=> $b->order} $scma->get_tables ) { + my $outtmp; $tt->process( $file, { translator => $Translator, schema => $scma, table => $tbl, - }, \$outtmp ) - or die "Error processing template '$file' for table '".$tbl->name - ."': ".$tt->error; + }, \$outtmp ) + or die "Error processing template '$file' for table '".$tbl->name + ."': ".$tt->error; $out .= $outtmp; # Write out the file... - write_file( table_file($tbl), $outtmp ) if $pargs->{mk_files}; + write_file( table_file($tbl), $outtmp ) if $pargs->{mk_files}; } return $out; @@ -235,7 +217,7 @@ sub table_file { # Write the src given to the file given, handling the on_exists arg. sub write_file { - my ($file, $src) = @_; + my ($file, $src) = @_; my $pargs = $Translator->producer_args; my $root = $pargs->{mk_files_base}; @@ -259,16 +241,18 @@ sub write_file { } } else { - warn "Creating $file.\n"; + if ( my $interactive = -t STDIN && -t STDOUT ) { + warn "Creating $file.\n"; + } } - my ($dir) = $file =~ m!^(.*)/!; # Want greedy, eveything before the last / - if ( $dir and not -d $dir and $pargs->{mk_file_dir} ) { mkpath($dir); } + my ($dir) = $file =~ m!^(.*)/!; # Want greedy, everything before the last / + if ( $dir and not -d $dir and $pargs->{mk_file_dir} ) { mkpath($dir); } debug "Writing to $file\n"; - open( FILE, ">$file") or die "Error opening file $file : $!\n"; - print FILE $src; - close(FILE); + open( FILE, ">$file") or die "Error opening file $file : $!\n"; + print FILE $src; + close(FILE); } # Reads file and inserts code between the insert comments and returns the new @@ -279,7 +263,7 @@ sub insert_code { my $cstart = $pargs->{insert_comment_start} || "SQLF_INSERT_START"; my $cend = $pargs->{insert_comment_end} || "SQLF_INSERT_END"; - # Slurp in the origional file + # Slurp in the original file open ( FILE, "<", "$file") or die "Error opening file $file : $!\n"; local $/ = undef; my $orig = ; @@ -297,8 +281,6 @@ sub insert_code { 1; -# ------------------------------------------------------------------- - =pod =head1 AUTHOR @@ -307,14 +289,15 @@ Mark Addison Egrommit@users.sourceforge.netE. =head1 TODO -+ Some tests for the various on exists options (they have been tested -implicitley through use in a project but need some proper tests). +- Some tests for the various on exists options (they have been tested +implicitly through use in a project but need some proper tests). -+ More docs code generation strategies. +- More docs on code generation strategies. -+ Better hooks for filename generation. +- Better hooks for filename generation. -+ Better integration with TTSchema. +- Integrate with L and + L. =head1 SEE ALSO