1 package SQL::Translator::Filter::Names;
3 # -------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
23 SQL::Translator::Filter::Names - Tweak the names of schema objects.
30 # Lowercase all table names and upper case the first letter of all field
31 # names. (MySql style!)
33 my $sqlt = SQL::Translator->new(
40 'fields' => 'ucfirst',
43 ) || die "SQLFairy error : ".SQL::Translator->error;
44 print($sqlt->translate) || die "SQLFairy error : ".$sqlt->error;
49 use vars qw/$VERSION/;
57 #if ( my $func = $args{tables} ) {
58 # _filtername($_,$func) foreach ( $schema->get_tables );
61 foreach my $type ( qw/tables procedures triggers views/ ) {
62 if ( my $func = $args{$type} ) {
63 my $meth = "get_$type";
64 _filtername($_,$func) foreach $schema->$meth;
69 if ( my $func = $args{fields} ) {
71 foreach map { $_->get_fields } $schema->get_tables ;
76 # _filtername( OBJ, FUNC_NAME )
77 # Update the name attribute on the schema object given using the named filter.
78 # Objects with no name are skipped.
79 # Returns true if the name was changed. Dies if there is an error running func.
82 return unless my $name = $obj->name;
83 $func = _getfunc($func);
84 my $newname = eval { $func->($name) };
85 die "$@" if $@; # TODO - Better message!
86 return if $name eq $newname;
90 # _getfunc( NAME ) - Returns code ref to func NAME or dies.
94 my $func = "SQL::Translator::Filter::Names::$name";
95 die "Table name filter - unknown function '$name'\n" unless exists &$func;
101 # The name munging functions
102 # -------------------------------------------------------------------------
103 # Get called with name to munge as first arg and return the new name. Die
108 sub ucfirst { ucfirst shift; }
112 # -------------------------------------------------------------------------
120 L<perl(1)>, L<SQL::Translator>
128 Define a bunch of usefull groups to run the name filters over. e.g. all, fkeys,
133 e.g. camelcase, titlecase, single word etc.
134 Also a way to pass in a regexp.
136 May also want a way to pass in arguments for the func e.g. prefix.
138 =item Multiple Filters on the same name (filter order)?
140 Do we actually need this, you could just run lots of filters. Would make adding
141 func args to the interface easier.
144 [ 'Names', { all => 'lc' } ],
151 Mind you if you could give the filter a list this wouldn't be a problem!
160 Which is nice. Might have to change the calling conventions for filters.
161 Would also provide an order to run the filters in rather than having to hard
162 code it into the filter it's self.