| 1 | package SQL::Translator::Producer::TT::Table; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | SQL::Translator::Producer::TT::Table - | ||||||||||||||||||||||||||||||||||||||||||
| 8 | Produces output using the Template Toolkit from a SQL schema, per table. | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # Normal STDOUT version | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||
| 14 | my $translator = SQL::Translator->new( | ||||||||||||||||||||||||||||||||||||||||||
| 15 | from => 'MySQL', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | filename => 'foo_schema.sql', | ||||||||||||||||||||||||||||||||||||||||||
| 17 | to => 'TT::Table', | ||||||||||||||||||||||||||||||||||||||||||
| 18 | producer_args => { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | tt_table => 'foo_table.tt', | ||||||||||||||||||||||||||||||||||||||||||
| 20 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | print $translator->translate; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # To generate a file per table | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||||||||||||||||||||||
| 26 | my $translator = SQL::Translator->new( | ||||||||||||||||||||||||||||||||||||||||||
| 27 | from => 'MySQL', | ||||||||||||||||||||||||||||||||||||||||||
| 28 | filename => 'foo_schema.sql', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | to => 'TT::Table', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | producer_args => { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | tt_table => 'foo_table.tt.html', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | mk_files => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | mk_files_base => "./doc/tables", | ||||||||||||||||||||||||||||||||||||||||||
| 34 | mk_file_ext => ".html", | ||||||||||||||||||||||||||||||||||||||||||
| 35 | on_exists => "replace", | ||||||||||||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # ./doc/tables/ now contains the templated tables as $tablename.html | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | Produces schema output using a given Template Tookit template, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | processing that template for each table in the schema. Optionally | ||||||||||||||||||||||||||||||||||||||||||
| 46 | allows you to write the result for each table to a separate file. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | It needs one additional producer_arg of C<tt_table> which is the file | ||||||||||||||||||||||||||||||||||||||||||
| 49 | name of the template to use. This template will be passed a template | ||||||||||||||||||||||||||||||||||||||||||
| 50 | var of C<table>, which is the current | ||||||||||||||||||||||||||||||||||||||||||
| 51 | L<SQL::Translator::Schema::Table> table we are producing, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | which you can then use to walk the schema via the methods documented | ||||||||||||||||||||||||||||||||||||||||||
| 53 | in that module. You also get C<schema> as a shortcut to the | ||||||||||||||||||||||||||||||||||||||||||
| 54 | L<SQL::Translator::Schema> for the table and C<translator>, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | the L<SQL::Translator> object for this parse in case you want to get | ||||||||||||||||||||||||||||||||||||||||||
| 56 | access to any of the options etc set here. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | Here's a brief example of what the template could look like: | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | [% table.name %] | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ================ | ||||||||||||||||||||||||||||||||||||||||||
| 62 | [% FOREACH field = table.get_fields %] | ||||||||||||||||||||||||||||||||||||||||||
| 63 | [% field.name %] [% field.data_type %]([% field.size %]) | ||||||||||||||||||||||||||||||||||||||||||
| 64 | [% END -%] | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | See F<t/data/template/table.tt> for a more complete example. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | You can also set any of the options used to initialize the Template | ||||||||||||||||||||||||||||||||||||||||||
| 69 | object by adding them to your producer_args. See Template Toolkit docs | ||||||||||||||||||||||||||||||||||||||||||
| 70 | for details of the options. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | $translator = SQL::Translator->new( | ||||||||||||||||||||||||||||||||||||||||||
| 73 | to => 'TT', | ||||||||||||||||||||||||||||||||||||||||||
| 74 | producer_args => { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | ttfile => 'foo_template.tt', | ||||||||||||||||||||||||||||||||||||||||||
| 76 | INCLUDE_PATH => '/foo/templates/tt', | ||||||||||||||||||||||||||||||||||||||||||
| 77 | INTERPOLATE => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | If you set C<mk_files> and its additional options the producer will | ||||||||||||||||||||||||||||||||||||||||||
| 82 | write a separate file for each table in the schema. This is useful for | ||||||||||||||||||||||||||||||||||||||||||
| 83 | producing things like HTML documentation where every table gets its | ||||||||||||||||||||||||||||||||||||||||||
| 84 | own page (you could also use TTSchema producer to add an index page). | ||||||||||||||||||||||||||||||||||||||||||
| 85 | It's also particularly good for code generation where you want to | ||||||||||||||||||||||||||||||||||||||||||
| 86 | produce a class file per table. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =item tt_table | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | File name of the template to run for each table. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =item mk_files | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | Set to true to output a file for each table in the schema (as well as | ||||||||||||||||||||||||||||||||||||||||||
| 99 | returning the whole lot back to the Translalor and hence STDOUT). The | ||||||||||||||||||||||||||||||||||||||||||
| 100 | file will be named after the table, with the optional C<mk_files_ext> | ||||||||||||||||||||||||||||||||||||||||||
| 101 | added and placed in the directory C<mk_files_base>. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =item mk_files_ext | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | Extension (without the dot) to add to the filename when using mk_files. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =item mk_files_base = DIR | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | Dir to build the table files into when using mk_files. Defaults to the | ||||||||||||||||||||||||||||||||||||||||||
| 110 | current directory. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =item mk_file_dir | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | Set true and if the file needs to written to a directory that doesn't | ||||||||||||||||||||||||||||||||||||||||||
| 115 | exist, it will be created first. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =item on_exists [Default:replace] | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | What to do if we are running with mk_files and a file already exists | ||||||||||||||||||||||||||||||||||||||||||
| 120 | where we want to write our output. One of "skip", "die", "replace", | ||||||||||||||||||||||||||||||||||||||||||
| 121 | "insert". The default is die. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | B<replace> - Over-write the existing file with the new one, clobbering | ||||||||||||||||||||||||||||||||||||||||||
| 124 | anything already there. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | B<skip> - Leave the original file as it was and don't write the new | ||||||||||||||||||||||||||||||||||||||||||
| 127 | version anywhere. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | B<die> - Die with an existing file error. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | B<insert> - Insert the generated output into the file between a set of | ||||||||||||||||||||||||||||||||||||||||||
| 132 | special comments (defined by the following options.) Any code between | ||||||||||||||||||||||||||||||||||||||||||
| 133 | the comments will be overwritten (ie the results from a previous | ||||||||||||||||||||||||||||||||||||||||||
| 134 | produce) but the rest of the file is left alone (your custom code). | ||||||||||||||||||||||||||||||||||||||||||
| 135 | This is particularly useful for code generation as it allows you to | ||||||||||||||||||||||||||||||||||||||||||
| 136 | generate schema derived code and then add your own custom code | ||||||||||||||||||||||||||||||||||||||||||
| 137 | to the file. Then when the schema changes you just re-produce to | ||||||||||||||||||||||||||||||||||||||||||
| 138 | insert the new code. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | =item insert_comment_start | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | The comment to look for in the file when on_exists is C<insert>. Default | ||||||||||||||||||||||||||||||||||||||||||
| 143 | is C<SQLF INSERT START>. Must appear on it own line, with only | ||||||||||||||||||||||||||||||||||||||||||
| 144 | whitespace either side, to be recognised. | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =item insert_comment_end | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | The end comment to look for in the file when on_exists is C<insert>. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | Default is C<SQLF INSERT END>. Must appear on it own line, with only | ||||||||||||||||||||||||||||||||||||||||||
| 150 | whitespace either side, to be recognised. | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | our ($DEBUG, @EXPORT_OK); | |||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | our $VERSION = '1.66'; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | ▶ | $DEBUG = 0 unless defined $DEBUG; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | use File::Path; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | ▶ | use Template; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | @EXPORT_OK = qw(produce); | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | ▶ | use SQL::Translator::Utils 'debug'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | my $Translator; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 1 | ▶ | sub produce { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
produce: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | $Translator = shift; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | local $DEBUG = $Translator->debug; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | my $scma = $Translator->schema; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | my $pargs = $Translator->producer_args; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 1 | ▶ | my $file = $pargs->{'tt_table'} or die "No template file given!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | $pargs->{on_exists} ||= "die"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$pargs->{"on_exists"} ||= "die"
MC/DC0%0/1
$pargs->{"on_exists"} ||= "die"
$pargs->{"on_exists"}
Truth table50%1/2
A
A= $pargs->{"on_exists"}
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | debug "Processing template $file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | my $out; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | ▶ | my $tt = Template->new( | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
'Template'->new('DEBUG', $SQL::Translator::Producer::TT::Table::DEBUG, 'ABSOLUTE', 1, 'RELATIVE', 1, %$pargs) || die('Failed to initialize Template object: ' . 'Template'->error)
MC/DC0%0/1
'Template'->new('DEBUG', $SQL::Translator::Producer::TT::Table::DEBUG, 'ABSOLUTE', 1, 'RELATIVE', 1, %$pargs) || die('Failed to initialize Template object: ' . 'Template'->error)
'Template'->new('DEBUG', $SQL::Translator::Producer::TT::Table::DEBUG, 'ABSOLUTE', 1, 'RELATIVE', 1, %$pargs)
Truth table50%1/2
A
A= 'Template'->new('DEBUG', $SQL::Translator::Producer::TT::Table::DEBUG, 'ABSOLUTE', 1, 'RELATIVE', 1, %$pargs)
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | DEBUG => $DEBUG, | ||||||||||||||||||||||||||||||||||||||||||
| 186 | ABSOLUTE => 1, # Set so we can use from the command line sensibly | ||||||||||||||||||||||||||||||||||||||||||
| 187 | RELATIVE => 1, # Maybe the cmd line code should set it! Security! | ||||||||||||||||||||||||||||||||||||||||||
| 188 | %$pargs, # Allow any TT opts to be passed in the producer_args | ||||||||||||||||||||||||||||||||||||||||||
| 189 | ) || die "Failed to initialize Template object: " . Template->error; | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | for my $tbl (sort { $a->order <=> $b->order } $scma->get_tables) { | |||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | my $outtmp; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | $tt->process( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | $file, | ||||||||||||||||||||||||||||||||||||||||||
| 195 | { | ||||||||||||||||||||||||||||||||||||||||||
| 196 | translator => $Translator, | ||||||||||||||||||||||||||||||||||||||||||
| 197 | schema => $scma, | ||||||||||||||||||||||||||||||||||||||||||
| 198 | table => $tbl, | ||||||||||||||||||||||||||||||||||||||||||
| 199 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 200 | \$outtmp | ||||||||||||||||||||||||||||||||||||||||||
| 201 | ) or die "Error processing template '$file' for table '" . $tbl->name . "': " . $tt->error; | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | $out .= $outtmp; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | # Write out the file... | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 2 | ▶ | write_file(table_file($tbl), $outtmp) if $pargs->{mk_files}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | return $out; | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | # Work out the filename for a given table. | ||||||||||||||||||||||||||||||||||||||||||
| 212 | 2 | ▶ | sub table_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table_file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | my ($tbl) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 2 | my $pargs = $Translator->producer_args; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 2 | my $root = $pargs->{mk_files_base}; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | my $ext = $pargs->{mk_file_ext}; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 2 | return "$root/$tbl.$ext"; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | # Write the src given to the file given, handling the on_exists arg. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 2 | ▶ | sub write_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=12 SCAR=37.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | my ($file, $src) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 2 | my $pargs = $Translator->producer_args; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 2 | my $root = $pargs->{mk_files_base}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | 2 | ▶ | if (-e $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if ($pargs->{on_exists} eq "skip") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | warn "Skipping existing $file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 230 | } elsif ($pargs->{on_exists} eq "die") { | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | die "File $file already exists.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } elsif ($pargs->{on_exists} eq "replace") { | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | warn "Replacing $file.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } elsif ($pargs->{on_exists} eq "insert") { | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | warn "Inserting into $file.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | $src = insert_code($file, $src); | |||||||||||||||||||||||||||||||||||||||||
| 237 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | die "Unknown on_exists action: $pargs->{on_exists}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 240 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 2 | ▶ | if (my $interactive = -t STDIN && -t STDOUT) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-t STDIN && -t STDOUT
MC/DC0%0/2
-t STDIN && -t STDOUT
-t STDIN
-t STDOUT
Truth table33%1/3
A && B
A= -t STDINB= -t STDOUT
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | warn "Creating $file.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 2 | my ($dir) = $file =~ m!^(.*)/!; # Want greedy, everything before the last / | |||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | if ($dir and not -d $dir and $pargs->{mk_file_dir}) { mkpath($dir); } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$dir and not -d $dir and $pargs->{"mk_file_dir"}
$dir and not -d $dir
MC/DC0%0/3
$dir and not -d $dir and $pargs->{"mk_file_dir"}
$dir
not -d $dir
$pargs->{"mk_file_dir"}
Truth table25%1/4
A and not(B) and C
A= $dirB= not -d $dirC= $pargs->{"mk_file_dir"}
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 2 | debug "Writing to $file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 250 | 2 | ▶ | open(FILE, ">$file") or die "Error opening file $file : $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | 2 | print FILE $src; | |||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | close(FILE); | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | # Reads file and inserts code between the insert comments and returns the new | ||||||||||||||||||||||||||||||||||||||||||
| 256 | # source. | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | sub insert_code { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert_code: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | my ($file, $src) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | my $pargs = $Translator->producer_args; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | my $cstart = $pargs->{insert_comment_start} || "SQLF_INSERT_START"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$pargs->{'insert_comment_start'} || 'SQLF_INSERT_START'
MC/DC0%0/1
$pargs->{'insert_comment_start'} || 'SQLF_INSERT_START'
$pargs->{'insert_comment_start'}
Truth table0%0/2
A
A= $pargs->{'insert_comment_start'}
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | my $cend = $pargs->{insert_comment_end} || "SQLF_INSERT_END"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$pargs->{'insert_comment_end'} || 'SQLF_INSERT_END'
MC/DC0%0/1
$pargs->{'insert_comment_end'} || 'SQLF_INSERT_END'
$pargs->{'insert_comment_end'}
Truth table0%0/2
A
A= $pargs->{'insert_comment_end'}
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | # Slurp in the original file | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | open(FILE, "<", "$file") or die "Error opening file $file : $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | local $/ = undef; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my $orig = <FILE>; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | close(FILE); | |||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | # Insert the new code between the insert comments | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | unless ($orig =~ s/^\s*?$cstart\s*?\n.*?^\s*?$cend\s*?\n/\n$cstart\n$src\n$cend\n/ms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | warn "No insert done\n"; | |||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | return $orig; | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | Mark Addison E<lt>grommit@users.sourceforge.netE<gt>. | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | - Some tests for the various on exists options (they have been tested | ||||||||||||||||||||||||||||||||||||||||||
| 288 | implicitly through use in a project but need some proper tests). | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | - More docs on code generation strategies. | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | - Better hooks for filename generation. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | - Integrate with L<TT::Base|SQL::Translator::Producer::TT::Base> and | ||||||||||||||||||||||||||||||||||||||||||
| 295 | L<TTSchema|SQL::Translator::Producer::TTSchema>. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | SQL::Translator. | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | =cut | ||||||||||||||||||||||||||||||||||||||||||