Commit | Line | Data |
6cedfc23 |
1 | package SQL::Translator::Filter::Names; |
2 | |
6cedfc23 |
3 | =head1 NAME |
4 | |
5 | SQL::Translator::Filter::Names - Tweak the names of schema objects. |
6 | |
7 | =head1 SYNOPSIS |
8 | |
9 | #! /usr/bin/perl -w |
10 | use SQL::Translator; |
11 | |
12 | # Lowercase all table names and upper case the first letter of all field |
13 | # names. (MySql style!) |
14 | # |
15 | my $sqlt = SQL::Translator->new( |
16 | filename => \@ARGV, |
17 | from => 'MySQL', |
18 | to => 'MySQL', |
19 | filters => [ |
20 | Names => { |
21 | 'tables' => 'lc', |
22 | 'fields' => 'ucfirst', |
23 | }, |
24 | ], |
25 | ) || die "SQLFairy error : ".SQL::Translator->error; |
26 | print($sqlt->translate) || die "SQLFairy error : ".$sqlt->error; |
27 | |
28 | =cut |
29 | |
30 | use strict; |
31 | use vars qw/$VERSION/; |
11ad2df9 |
32 | $VERSION = '1.59'; |
6cedfc23 |
33 | |
34 | sub filter { |
35 | my $schema = shift; |
36 | my %args = %{$_[0]}; |
37 | |
38 | # Tables |
39 | #if ( my $func = $args{tables} ) { |
40 | # _filtername($_,$func) foreach ( $schema->get_tables ); |
41 | #} |
42 | # , |
43 | foreach my $type ( qw/tables procedures triggers views/ ) { |
44 | if ( my $func = $args{$type} ) { |
45 | my $meth = "get_$type"; |
46 | _filtername($_,$func) foreach $schema->$meth; |
47 | } |
48 | } |
49 | |
50 | # Fields |
51 | if ( my $func = $args{fields} ) { |
52 | _filtername($_,$func) |
53 | foreach map { $_->get_fields } $schema->get_tables ; |
54 | } |
55 | |
56 | } |
57 | |
58 | # _filtername( OBJ, FUNC_NAME ) |
59 | # Update the name attribute on the schema object given using the named filter. |
60 | # Objects with no name are skipped. |
61 | # Returns true if the name was changed. Dies if there is an error running func. |
62 | sub _filtername { |
63 | my ($obj,$func) = @_; |
64 | return unless my $name = $obj->name; |
65 | $func = _getfunc($func); |
66 | my $newname = eval { $func->($name) }; |
67 | die "$@" if $@; # TODO - Better message! |
68 | return if $name eq $newname; |
69 | $_->name($newname); |
70 | } |
71 | |
72 | # _getfunc( NAME ) - Returns code ref to func NAME or dies. |
73 | sub _getfunc { |
74 | my ($name) = @_; |
75 | no strict 'refs'; |
76 | my $func = "SQL::Translator::Filter::Names::$name"; |
77 | die "Table name filter - unknown function '$name'\n" unless exists &$func; |
78 | \&$func; |
79 | } |
80 | |
81 | |
82 | |
83 | # The name munging functions |
11ad2df9 |
84 | #============================================================================= |
85 | # Get called with name to munge as first arg and return the new name. Die on |
86 | # errors. |
6cedfc23 |
87 | |
88 | sub lc { lc shift; } |
89 | sub uc { uc shift; } |
90 | sub ucfirst { ucfirst shift; } |
91 | |
11ad2df9 |
92 | 1; #========================================================================== |
6cedfc23 |
93 | |
94 | __END__ |
95 | |
96 | =head1 DESCRIPTION |
97 | |
98 | =head1 SEE ALSO |
99 | |
100 | L<perl(1)>, L<SQL::Translator> |
101 | |
11ad2df9 |
102 | =head1 BUGS |
103 | |
6cedfc23 |
104 | =head1 TODO |
105 | |
106 | =over 4 |
107 | |
108 | =item Name Groups |
109 | |
10f70490 |
110 | Define a bunch of useful groups to run the name filters over. e.g. all, fkeys, |
6cedfc23 |
111 | pkeys etc. |
112 | |
113 | =item More Functions |
114 | |
115 | e.g. camelcase, titlecase, single word etc. |
116 | Also a way to pass in a regexp. |
117 | |
118 | May also want a way to pass in arguments for the func e.g. prefix. |
119 | |
120 | =item Multiple Filters on the same name (filter order)? |
121 | |
122 | Do we actually need this, you could just run lots of filters. Would make adding |
123 | func args to the interface easier. |
124 | |
125 | filters => [ |
126 | [ 'Names', { all => 'lc' } ], |
127 | [ 'Names', { |
128 | tables => 'lc', |
129 | fields => 'ucfirst', |
130 | } ], |
131 | ], |
132 | |
133 | Mind you if you could give the filter a list this wouldn't be a problem! |
134 | |
135 | filters => [ |
136 | [ 'Names', |
137 | all => 'lc' |
138 | fields => 'ucfirst', |
139 | ], |
140 | ], |
141 | |
142 | Which is nice. Might have to change the calling conventions for filters. |
143 | Would also provide an order to run the filters in rather than having to hard |
144 | code it into the filter it's self. |
145 | |
146 | =back |
147 | |
148 | =head1 AUTHOR |
149 | |
150 | =cut |