line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
31
|
1
|
|
|
1
|
|
11
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
330
|
|
32
|
|
|
|
|
|
|
our $VERSION = '1.63'; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub filter { |
35
|
1
|
|
|
1
|
0
|
10
|
my $schema = shift; |
36
|
1
|
|
|
|
|
2
|
my %args = %{$_[0]}; |
|
1
|
|
|
|
|
4
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Tables |
39
|
|
|
|
|
|
|
#if ( my $func = $args{tables} ) { |
40
|
|
|
|
|
|
|
# _filtername($_,$func) foreach ( $schema->get_tables ); |
41
|
|
|
|
|
|
|
#} |
42
|
|
|
|
|
|
|
# , |
43
|
1
|
|
|
|
|
3
|
foreach my $type ( qw/tables procedures triggers views/ ) { |
44
|
4
|
100
|
|
|
|
46
|
if ( my $func = $args{$type} ) { |
45
|
1
|
|
|
|
|
9
|
my $meth = "get_$type"; |
46
|
1
|
|
|
|
|
6
|
_filtername($_,$func) foreach $schema->$meth; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Fields |
51
|
1
|
50
|
|
|
|
4
|
if ( my $func = $args{fields} ) { |
52
|
|
|
|
|
|
|
_filtername($_,$func) |
53
|
1
|
|
|
|
|
4
|
foreach map { $_->get_fields } $schema->get_tables ; |
|
1
|
|
|
|
|
4
|
|
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
|
2
|
|
|
2
|
|
101
|
my ($obj,$func) = @_; |
64
|
2
|
50
|
|
|
|
46
|
return unless my $name = $obj->name; |
65
|
2
|
|
|
|
|
117
|
$func = _getfunc($func); |
66
|
2
|
|
|
|
|
3
|
my $newname = eval { $func->($name) }; |
|
2
|
|
|
|
|
4
|
|
67
|
2
|
50
|
|
|
|
5
|
die "$@" if $@; # TODO - Better message! |
68
|
2
|
50
|
|
|
|
4
|
return if $name eq $newname; |
69
|
2
|
|
|
|
|
45
|
$_->name($newname); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# _getfunc( NAME ) - Returns code ref to func NAME or dies. |
73
|
|
|
|
|
|
|
sub _getfunc { |
74
|
2
|
|
|
2
|
|
8
|
my ($name) = @_; |
75
|
1
|
|
|
1
|
|
8
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
150
|
|
76
|
2
|
|
|
|
|
4
|
my $func = "SQL::Translator::Filter::Names::$name"; |
77
|
2
|
50
|
|
|
|
11
|
die "Table name filter - unknown function '$name'\n" unless exists &$func; |
78
|
2
|
|
|
|
|
7
|
\&$func; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# The name munging functions |
84
|
|
|
|
|
|
|
#============================================================================= |
85
|
|
|
|
|
|
|
# Get called with name to munge as first arg and return the new name. Die on |
86
|
|
|
|
|
|
|
# errors. |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
1
|
0
|
5
|
sub lc { lc shift; } |
89
|
0
|
|
|
0
|
0
|
0
|
sub uc { uc shift; } |
90
|
1
|
|
|
1
|
0
|
8
|
sub ucfirst { ucfirst shift; } |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; #========================================================================== |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__END__ |