« blib/lib/SQL/Translator/Generator/Role/DDL.pm
↑ Summary
blib/lib/SQL/Translator/Schema/IndexField.pm »
| 1 | package SQL::Translator::Filter::Names; | ||||||||||
| 2 | |||||||||||
| 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 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 1 | our $VERSION = '1.66'; | |||||||||
| 33 | |||||||||||
| 34 | 1 | ▶ | sub filter { | ||||||||
|
Subroutine
filter: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||
| 35 | 1 | my $schema = shift; | |||||||||
| 36 | 1 | my %args = %{ $_[0] }; | |||||||||
| 37 | |||||||||||
| 38 | # Tables | ||||||||||
| 39 | #if ( my $func = $args{tables} ) { | ||||||||||
| 40 | # _filtername($_,$func) foreach ( $schema->get_tables ); | ||||||||||
| 41 | #} | ||||||||||
| 42 | # , | ||||||||||
| 43 | 1 | foreach my $type (qw/tables procedures triggers views/) { | |||||||||
| 44 | 4 | ▶ | if (my $func = $args{$type}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 1 | my $meth = "get_$type"; | |||||||||
| 46 | 1 | _filtername($_, $func) foreach $schema->$meth; | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | # Fields | ||||||||||
| 51 | 1 | ▶ | if (my $func = $args{fields}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 1 | _filtername($_, $func) foreach map { $_->get_fields } $schema->get_tables; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | # _filtername( OBJ, FUNC_NAME ) | ||||||||||
| 58 | # Update the name attribute on the schema object given using the named filter. | ||||||||||
| 59 | # Objects with no name are skipped. | ||||||||||
| 60 | # Returns true if the name was changed. Dies if there is an error running func. | ||||||||||
| 61 | 2 | ▶ | sub _filtername { | ||||||||
|
Subroutine
_filtername: called CC=4 SCAR=14.2
| |||||||||||
| 62 | 2 | my ($obj, $func) = @_; | |||||||||
| 63 | 2 | ▶ | return unless my $name = $obj->name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 2 | $func = _getfunc($func); | |||||||||
| 65 | 2 | my $newname = eval { $func->($name) }; | |||||||||
| 66 | 2 | ▶ | die "$@" if $@; # TODO - Better message! | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 2 | ▶ | return if $name eq $newname; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 2 | $_->name($newname); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | # _getfunc( NAME ) - Returns code ref to func NAME or dies. | ||||||||||
| 72 | 2 | ▶ | sub _getfunc { | ||||||||
|
Subroutine
_getfunc: called CC=2 SCAR=7.0
| |||||||||||
| 73 | 2 | my ($name) = @_; | |||||||||
| 74 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 75 | 2 | my $func = "SQL::Translator::Filter::Names::$name"; | |||||||||
| 76 | 2 | ▶ | die "Table name filter - unknown function '$name'\n" unless exists &$func; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 2 | \&$func; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | # The name munging functions | ||||||||||
| 81 | #============================================================================= | ||||||||||
| 82 | # Get called with name to munge as first arg and return the new name. Die on | ||||||||||
| 83 | # errors. | ||||||||||
| 84 | |||||||||||
| 85 | 1 | ▶ | sub lc { lc shift; } | ||||||||
|
Subroutine
lc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 86 | 0 | ▶ | sub uc { uc shift; } | ||||||||
|
Subroutine
uc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 87 | 1 | ▶ | sub ucfirst { ucfirst shift; } | ||||||||
|
Subroutine
ucfirst: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 88 | |||||||||||
| 89 | 1 | 1; #========================================================================== | |||||||||
| 90 | |||||||||||
| 91 | __END__ | ||||||||||