blib/lib/Class/DBI/Plugin/Backtickify.pm

filter: Statementstmt 26.0% 18 / 69 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 50.0% 5 / 10 Podpod 100% 1 / 1 totaltotal 19.2% 24 / 125 CC 17 SCAR 34.4
CC
17
Cov
18%
__ANON__
56.0
transform_sql
42.0
__ANON__
30.0
CRAP 31.3
↑ Summary
1 package Class::DBI::Plugin::Backtickify;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Class::ISA;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = 0.02;
9  
10 =head1 NAME
11  
12 Class::DBI::Plugin::Backtickify - surround column and table names with backticks
13  
14 =head1 SYNOPSIS
15  
16 package Film;
17 use base qw( Class::DBI );
18 use Class::DBI::Plugin::Backtickify; # must come after the use base
19
20 =head1 DESCRIPTION
21  
22 Puts backticks around table and column names. This allows reserved words to be used
23 as column (and table?) names in MySQL (and others?).
24  
25 =head1 CAVEATS
26  
27 It works by installing a C<transform_sql> method into your CDBI class. Other modules and
28 plugins maybe do the same thing, in which case they may not play nicely with this. It does
29 go through some hoops however to try and call other C<transform_sql> methods, but all the
30 replacement tags will already have been removed so this might not help anyway. YMMV.
31  
32 The installed C<transform_sql> finds column names using a regex over each C<@args> passed in. If
33 strings matching column names (but not supposed to represent column names) exist as words
34 in the input to the method, they will also get wrapped. Not sure how likely this is.
35  
36 I haven't tested if this works with joins, but it should.
37  
38 No tests yet.
39  
40 =cut
41  
42 sub import
43 1 ▶{
Subroutine import: called CC=1 SCAR=0.0
44 1 my ( $class ) = @_;
45
46 1 my $caller = caller( 0 );
47
48 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 *{"$caller\::transform_sql"} = \&transform_sql;
50 }
51  
52 =head1 METHODS
53  
54 =over 4
55  
56 =item transform_sql
57  
58 =back
59  
60 =cut
61  
62 sub transform_sql
63 0 ▶{
Subroutine transform_sql: not called CC=6 SCAR=37.4
Pod documented
64 0 my ( $self, $sql, @args ) = @_;
65
66 #warn "TRANSFORM_SQL: SQL IN: $sql - @args\n";
67
68 # Each entry in @args is a SQL fragment. This will bugger with fragments that
69 # contain strings that match column names but are not supposed to be column names.
70 0 ▶ my $backtickify_arg = sub { $_[0] =~ s/\b$_\b/`$_`/g for $self->all_columns };
Subroutine __ANON__: not called CC=2 SCAR=17.9
71 0 $backtickify_arg->( $_ ) for @args;
72
73 # -------------------
74 0 my %cmap;
75 0 ▶ my $expand_table = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
76 0 my ($class, $alias) = split /=/, shift, 2;
77 0 ▶ my $table = $class ? $class->table : $self->table;
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 0 0 0
78 0 ▶ $cmap{ $alias || $table } = $class || ref $self || $self;
Condition0%0/9
$class || ref $self || $self
l!l&&r!l&&!r
000
$class or ref $self
l!l&&r!l&&!r
000
$alias || $table
l!l&&r!l&&!r
000
MC/DC0%0/3
$class || ref $self || $self
$class ref $self $self
MC/DC0%0/2
$alias || $table
$alias $table
Truth table0%0/4
A or B || C
A= $classB= ref $selfC= $self
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/3
A || B
A= $aliasB= $table
ABresult
1X1
011
000
79 0 ▶ ($alias ||= "") &&= " AS `$alias`";
Condition0%0/4
($alias ||= "") &&= " AS `$alias`"
!ll
00
$alias ||= ""
l!l
00
MC/DC0%0/1
($alias ||= "") &&= " AS `$alias`"
($alias ||= "")
MC/DC0%0/1
$alias ||= ""
$alias
Truth table0%0/2
A
A= ($alias ||= "")
Aresult
00
11
Truth table0%0/2
A
A= $alias
Aresult
11
00
80 0 return "`$table`$alias";
81 0 };
82
83 # -------------------
84 0 ▶ my $expand_join = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
85 0 my $joins = shift;
86 0 my @table = split /\s+/, $joins;
87 0 my %tojoin = map { $table[$_] => $table[ $_ + 1 ] } 0 .. $#table - 1;
88 0 my @sql;
89 0 ▶ while (my ($t1, $t2) = each %tojoin) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %tojoin) 0 0 0
90 0 ▶ my ($c1, $c2) = map $cmap{$_}
Condition0%0/3
$cmap{$_} || $self->_croak("Don't understand table '${_}' in JOIN")
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmap{$_} || $self->_croak("Don't understand table '${_}' in JOIN")
$cmap{$_} $self->_croak("Don't understand table '${_}' in JOIN")
Truth table0%0/3
A || B
A= $cmap{$_}B= $self->_croak("Don't understand table '${_}' in JOIN")
ABresult
1X1
011
000
91 || $self->_croak("Don't understand table '$_' in JOIN"), ($t1, $t2);
92  
93 0 ▶ my $join_col = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 0 my ($c1, $c2) = @_;
95 0 my $meta = $c1->meta_info('has_a');
96 0 my ($col) = grep $meta->{$_}->foreign_class eq $c2, keys %$meta;
97 0 $col;
98 0 };
99  
100 0 ▶ my $col = $join_col->($c1 => $c2) || do {
Condition0%0/3
&$join_col($c1, $c2) || do { () = ($c2, $c1); () = ($t2, $t1); &$join_col($c1, $c2) }
l!l&&r!l&&!r
000
MC/DC0%0/2
&$join_col($c1, $c2) || do { () = ($c2, $c1); () = ($t2, $t1); &$join_col($c1, $c2) }
&$join_col($c1, $c2) do { () = ($c2, $c1); () = ($t2, $t1); &$join_col($c1, $c2) }
Truth table0%0/3
A || B
A= &$join_col($c1, $c2)B= do { () = ($c2, $c1); () = ($t2, $t1); &$join_col($c1, $c2) }
ABresult
1X1
011
000
101 0 ($c1, $c2) = ($c2, $c1);
102 0 ($t1, $t2) = ($t2, $t1);
103 0 $join_col->($c1 => $c2);
104 };
105  
106 0 ▶ $self->_croak("Don't know how to join $c1 to $c2") unless $col;
Branch: 1 branch
BranchTrueFalseTotal
unless $col 0 0 0
107 0 push @sql, sprintf " `%s`.`%s` = `%s`.`%s` ", $t1, $col, $t2,
108 $c2->primary_column;
109 }
110 0 return join " AND ", @sql;
111 0 };
112
113 # -------------------
114 0 $sql =~ s/__TABLE\(?(.*?)\)?__/$expand_table->($1)/eg;
115 0 $sql =~ s/__JOIN\((.*?)\)__/$expand_join->($1)/eg;
116 0 $sql =~ s/__ESSENTIAL__/join ", ", map { "`$_`" } $self->_essential/eg;
117 0 $sql =~ s/__ESSENTIAL\((.*?)\)__/join ", ", map { "`$1`.`$_`" } $self->_essential/eg;
118
119 0 ▶ if ( $sql =~ /__IDENTIFIER__/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /__IDENTIFIER__/) 0 0 0
120 {
121 0 my $key_sql = join " AND ", map "`$_`=?", $self->primary_columns;
122 0 $sql =~ s/__IDENTIFIER__/$key_sql/g;
123 }
124
125 # nasty hack
126 0 ▶ my $super = ( Class::ISA::super_path( ref( $self ) || $self ) )[0];
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
127
128 0 my $eval = '{ package %s; $self->SUPER::transform_sql( q(%s), ';
129 0 $eval .= 'q(%s), ' for @args;
130 0 $eval .= ') }';
131
132 0 my $return = eval sprintf $eval, $super, $sql, @args;
133
134 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
135
136 0 return $return;
137  
138 #my $out = $self->SUPER::transform_sql($sql => @args);
139 #warn "TRANSFORM_SQL: SQL OUT: $out\n";
140 #return $out;
141 }
142  
143  
144 =head1 AUTHOR
145  
146 David Baird, C<< <cpan@riverside-cms.co.uk> >>
147  
148 =head1 BUGS
149  
150 Please report any bugs or feature requests to
151 C<bug-class-dbi-plugin-backtickify@rt.cpan.org>, or through the web interface at
152 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-DBI-Plugin-Backtickify>.
153 I will be notified, and then you'll automatically be notified of progress on
154 your bug as I make changes.
155  
156 =head1 ACKNOWLEDGEMENTS
157  
158 =head1 COPYRIGHT & LICENSE
159  
160 Copyright 2005 David Baird, All Rights Reserved.
161  
162 This program is free software; you can redistribute it and/or modify it
163 under the same terms as Perl itself.
164  
165 =cut
166  
167 2 1; # End of Class::DBI::Plugin::Backtickify
↑ Summary