blib/lib/Class/DBI/SQL/Transformer/Quotify.pm

filter: Statementstmt 71.7% 56 / 78 Branchbran 75.0% 12 / 16 Conditioncond 38.1% 8 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 75.0% 6 / 8 Podpod n/a totaltotal 60.7% 82 / 135 CC 18 SCAR 23.4
CC
18
Cov
66%
_expand_join
30.0
_expand_table
8.9
_backtickify_arg
5.0
CRAP 10.4
↑ Summary
1 package Class::DBI::SQL::Transformer::Quotify;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base qw/Class::DBI::SQL::Transformer/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our $VERSION = '0.02';
8  
9 3 ▶sub _expand_table {
Subroutine _expand_table: called CC=8 SCAR=21.8
10 3 my $self = shift;
11 3 my $s = shift;
12 3 ▶ my ($class, $alias) = split /=/, defined($s)?$s:'', 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $s ? : 2 1 3
13 3 my $caller = $self->{_caller};
14 3 ▶ my $table = $class ? $class->table : $caller->table;
Branch: 1 branch
BranchTrueFalseTotal
$class ? : 2 1 3
15 3 ▶ $self->{cmap}{ $alias || $table } = $class || ref $caller || $caller;
Condition55%5/9
$class || ref $caller || $caller
l!l&&r!l&&!r
210
$class or ref $caller
l!l&&r!l&&!r
201
$alias || $table
l!l&&r!l&&!r
030
MC/DC0%0/3
$class || ref $caller || $caller
$class ref $caller $caller
MC/DC0%0/2
$alias || $table
$alias $table
Truth table50%2/4
A or B || C
A= $classB= ref $callerC= $caller
ABCresult
1XX1
01X1
0011
0000
Truth table33%1/3
A || B
A= $aliasB= $table
ABresult
1X1
011
000
16 3 ▶ ($alias ||= "") &&= " ".$caller->db_Main->quote_identifier($alias);
Condition50%2/4
($alias ||= "") &&= ' ' . $caller->db_Main->quote_identifier($alias)
!ll
30
$alias ||= ""
l!l
03
MC/DC0%0/1
($alias ||= "") &&= ' ' . $caller->db_Main->quote_identifier($alias)
($alias ||= "")
MC/DC0%0/1
$alias ||= ""
$alias
Truth table50%1/2
A
A= ($alias ||= "")
Aresult
00
11
Truth table50%1/2
A
A= $alias
Aresult
11
00
17 3 return $caller->db_Main->quote_identifier($table) . $alias;
18 }
19  
20 0 ▶sub _expand_join {
Subroutine _expand_join: not called CC=5 SCAR=34.0
21 0 my $self = shift;
22 0 my $joins = shift;
23 0 my @table = split /\s+/, $joins;
24  
25 0 my $caller = $self->{_caller};
26 0 my %tojoin = map { $table[$_] => $table[ $_ + 1 ] } 0 .. $#table - 1;
27 0 my @sql;
28 0 ▶ while (my ($t1, $t2) = each %tojoin) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %tojoin) 0 0 0
29 0 ▶ my ($c1, $c2) = map $self->{cmap}{$_}
Condition0%0/3
$self->{'cmap'}{$_} || $caller->_croak("Don't understand table '${_}' in JOIN")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'cmap'}{$_} || $caller->_croak("Don't understand table '${_}' in JOIN")
$self->{'cmap'}{$_} $caller->_croak("Don't understand table '${_}' in JOIN")
Truth table0%0/3
A || B
A= $self->{'cmap'}{$_}B= $caller->_croak("Don't understand table '${_}' in JOIN")
ABresult
1X1
011
000
30 || $caller->_croak("Don't understand table '$_' in JOIN"), ($t1, $t2);
31  
32 0 ▶ my $join_col = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 my ($c1, $c2) = @_;
34 0 my $meta = $c1->meta_info('has_a');
35 0 my ($col) = grep $meta->{$_}->foreign_class eq $c2, keys %$meta;
36 0 $col;
37 0 };
38  
39 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
40 0 ($c1, $c2) = ($c2, $c1);
41 0 ($t1, $t2) = ($t2, $t1);
42 0 $join_col->($c1 => $c2);
43 };
44  
45 0 ▶ $caller->_croak("Don't know how to join $c1 to $c2") unless $col;
Branch: 1 branch
BranchTrueFalseTotal
unless $col 0 0 0
46 0 push @sql, sprintf " %s = %s ",
47 $caller->db_Main->quote_identifier($t1, $col),
48 $caller->db_Main->quote_identifier($t2, $c2->primary_column);
49 }
50 0 return join " AND ", @sql;
51 }
52  
53 360 ▶sub _backtickify_arg {
Subroutine _backtickify_arg: called CC=5 SCAR=16.1
54 360 my $self = shift;
55 360 my $caller = $self->{_caller};
56 360 ▶ my $char = $caller->db_Main->get_info(29) || q{"}; # SQL_IDENTIFIER_QUOTE_CHAR
Condition50%1/2
$caller->db_Main->get_info(29) || '"'
l!l
3600
MC/DC0%0/1
$caller->db_Main->get_info(29) || '"'
$caller->db_Main->get_info(29)
Truth table50%1/2
A
A= $caller->db_Main->get_info(29)
Aresult
11
00
57 360 ▶ return $_[0] if $_[0] =~ /^$char[^$char]*$char$/; # return if already quoted
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^${char}[^$char]*$char$/ 36 324 360
58 my @cols = $_[1]
59 297 ? @{$_[1]} # use what's given us (in the recursion cases)
60 # or (the initial case) use all cols, sorted longest to shortest
61 # This is necessary so that 'foo bar' gets processed before 'foo',
62 # so that if you have "foo bar" it doesn't become "`foo` bar"
63 324 ▶ : sort { length $b <=> length $a } map { "$_" } $caller->all_columns
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 297 27 324
64 ;
65 324 ▶ return $_[0] unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 37 287 324
66 287 my $c = shift @cols; # process first col
67 287 my $quoted = $caller->db_Main->quote_identifier($c);
68 287 $_[0] =~ s/\b(?<!$char)$c(?!$char)\b/$quoted/g; # quote it where it's currently unquoted
69 # Recurse on all the pieces w/the remaining columns to process.
70 # Note the the quoted ones will just return right way.
71 287 my @s = map { $self->_backtickify_arg($_,\@cols) } split /($quoted)/, $_[0];
72 287 $_[0] = join '', @s;
73 287 return $_[0];
74 }
75  
76 59 ▶sub _do_transformation {
Subroutine _do_transformation: called CC=3 SCAR=11.0
77 59 my $me = shift;
78 59 my $sql = $me->{_sql};
79 59 my @args = @{ $me->{_args} };
80 59 my $caller = $me->{_caller};
81  
82 # Each entry in @args is a SQL fragment. This will bugger with fragments that
83 # contain strings that match column names but are not supposed to be column names.
84 59 $me->_backtickify_arg($_) for @args;
85  
86 59 $sql =~ s/__TABLE(?:\((.+?)\))?__/$me->_expand_table($1)/eg;
87 0 $sql =~ s/__JOIN\((.+?)\)__/$me->_expand_join($1)/eg;
88 59 $sql =~ s/__ESSENTIAL__/join ", ", map { $caller->db_Main->quote_identifier($_) } $caller->_essential/eg;
89 59 $sql =~
90 2 s/__ESSENTIAL\((.+?)\)__/join ", ", map $caller->db_Main->quote_identifier($1,$_), $caller->_essential/eg;
91 59 ▶ if ($sql =~ /__IDENTIFIER__/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /__IDENTIFIER__/) 1 58 59
92 1 my $key_sql = join " AND ", map $caller->db_Main->quote_identifier($_).'=?', $caller->primary_columns;
93 1 $sql =~ s/__IDENTIFIER__/$key_sql/g;
94 }
95  
96 59 $me->{_transformed_sql} = $sql;
97 59 $me->{_transformed_args} = [@args];
98 59 $me->{_transformed} = 1;
99 59 return 1;
100 }
101  
102 3 1;
103  
104 =pod
105  
106 =head1 NAME
107  
108 Class::DBI::SQL::Transformer::Quotify - Quote column and table names in Class::DBI-generated SQL
109  
110 =head1 VERSION
111  
112 Version 0.02
113  
114 =head1 SYNOPSIS
115  
116 package Foo;
117 use base qw/Class::DBI/;
118 __PACKAGE__->connection('DBI:Mock:', '', '');
119 __PACKAGE__->sql_transformer_class('Class::DBI::SQL::Transformer::Quotify');
120 __PACKAGE__->table('table name');
121 __PACKAGE__->columns( Essential => 'my id', 'my name' );
122 package main;
123 my $row = Foo->retrieve( 3 );
124  
125 =head1 DESCRIPTION
126  
127 This is an attempt to solve the problem of spaces and/or reserved words in table and/or column names. Normally, Class::DBI does not quote these, so it results in sql such as the following (which clearly will error out):
128  
129 SELECT my id, my name
130 FROM table name
131 WHERE my id = ?
132  
133 This is implemented by subclassing L<Class::DBI::SQL::Transformer> and notifying L<Class::DBI> via its C<sql_transformer_class()> attribute. Note that some of the methods are completely replaced.
134  
135 =head1 BACKGROUND/EVOLUTION
136  
137 I first came upon L<Class::DBI::Plugin::Backtickify>, which worked great, except the naming of the schema was so bad I hit an edge case that needed fixing first, which got me looking under the hood: L<http://rt.cpan.org/Public/Bug/Display.html?id=32133>
138  
139 Since that version of Class::DBI::Plugin::Backtickify, Class::DBI (as of v3.0.8) had refactored the Class::DBI::SQL::Transformer class and introduced the Class::DBIsql_transformer_class() method. Which is why this module has the namespace it does instead of Class::DBI::Plugin:: and why I didn't just submit a patch for Backtickify.
140  
141 Drawing heavily from Backtickify, i generalized it to this module by using L<DBI>::quote_identifier() instead of a hardcoded backtick.
142  
143 This potentially is (at least a partial) solution (or workaround) for Class::DBI RT ticket 7715 I<Class::DBI does not correctly quote column names (Pg, maybe others)>: L<http://rt.cpan.org/Ticket/Display.html?id=7715>
144  
145 In the course of investigation, also reported this Class::DBI issue, which this module also resolves: L<http://rt.cpan.org/Ticket/Display.html?id=32115>
146  
147 =head1 AUTHOR
148  
149 David Westbrook (CPAN: davidrw), C<< <dwestbrook at gmail.com> >>
150  
151 =head1 BUGS
152  
153 Please report any bugs or feature requests to C<bug-class-dbi-sql-transformer-quotify at rt.cpan.org>, or through
154 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-DBI-SQL-Transformer-Quotify>. I will be notified, and then you'll
155 automatically be notified of progress on your bug as I make changes.
156  
157 =head1 SUPPORT
158  
159 You can find documentation for this module with the perldoc command.
160  
161 perldoc Class::DBI::SQL::Transformer::Quotify
162  
163 You can also look for information at:
164  
165 =over 4
166  
167 =item * RT: CPAN's request tracker
168  
169 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-DBI-SQL-Transformer-Quotify>
170  
171 =item * AnnoCPAN: Annotated CPAN documentation
172  
173 L<http://annocpan.org/dist/Class-DBI-SQL-Transformer-Quotify>
174  
175 =item * CPAN Ratings
176  
177 L<http://cpanratings.perl.org/d/Class-DBI-SQL-Transformer-Quotify>
178  
179 =item * Search CPAN
180  
181 L<http://search.cpan.org/dist/Class-DBI-SQL-Transformer-Quotify>
182  
183 =back
184  
185 =head1 SEE ALSO
186  
187 L<Class::DBI>, L<Class::DBI::SQL::Transformer>, L<DBI>, L<Class::DBI::Plugin::Backtickify>
188  
189 =head1 ACKNOWLEDGEMENTS
190  
191 David Baird for the groundwork of Class::DBI::Plugin::Backtickify
192  
193 =head1 COPYRIGHT & LICENSE
194  
195 Copyright 2008 David Westbrook, all rights reserved.
196  
197 This program is free software; you can redistribute it and/or modify it
198 under the same terms as Perl itself.
199  
200 =cut
201  
↑ Summary