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