| 1 | package DBIx::Class::CDBICompat::SQLTransformer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | DBIx::Class::CDBICompat::SQLTransformer - Transform SQL | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | This is a copy of L<Class::DBI::SQL::Transformer> from Class::DBI 3.0.17. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | It is here so we can be compatible with L<Class::DBI> without having it | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | installed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my ($me, $caller, $sql, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | bless { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | _caller => $caller, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | _sql => $sql, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | _args => [@args], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | _transformed => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | } => $me; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub sql { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sql: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $self->_do_transformation if !$self->{_transformed}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | return $self->{_transformed_sql}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub args { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
args: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | $self->_do_transformation if !$self->{_transformed}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return @{ $self->{_transformed_args} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub _expand_table { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_table: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my ($class, $alias) = split /=/, shift, 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $caller = $self->{_caller}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $table = $class ? $class->table : $caller->table; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $self->{cmap}{ $alias || $table } = $class || ref $caller || $caller; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$class || ref $caller || $caller
$class or ref $caller
$alias || $table
MC/DC0%0/3
$class || ref $caller || $caller
$class
ref $caller
$caller
MC/DC0%0/2
$alias || $table
$alias
$table
Truth table0%0/4
A or B || C
A= $classB= ref $callerC= $caller
Truth table0%0/3
A || B
A= $aliasB= $table
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | ($alias ||= "") &&= " $alias"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
($alias ||= "") &&= " $alias"
$alias ||= ""
MC/DC0%0/1
($alias ||= "") &&= " $alias"
($alias ||= "")
MC/DC0%0/1
$alias ||= ""
$alias
Truth table0%0/2
A
A= ($alias ||= "")
Truth table0%0/2
A
A= $alias
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return $table . $alias; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub _expand_join { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expand_join: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $joins = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my @table = split /\s+/, $joins; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $caller = $self->{_caller}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my %tojoin = map { $table[$_] => $table[ $_ + 1 ] } 0 .. $#table - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my @sql; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | while (my ($t1, $t2) = each %tojoin) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my ($c1, $c2) = map $self->{cmap}{$_} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{'cmap'}{$_} || $caller->_croak("Don't understand table '${_}' in JOIN")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | || $caller->_croak("Don't understand table '$_' in JOIN"), ($t1, $t2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my $join_col = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my ($c1, $c2) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $meta = $c1->meta_info('has_a'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my ($col) = grep $meta->{$_}->foreign_class eq $c2, keys %$meta; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $col; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $col = $join_col->($c1 => $c2) || do { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
&$join_col($c1, $c2) || do {
() = ($c2, $c1);
() = ($t2, $t1);
&$join_col($c1, $c2)
}
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)
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ($c1, $c2) = ($c2, $c1); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ($t1, $t2) = ($t2, $t1); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $join_col->($c1 => $c2); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $caller->_croak("Don't know how to join $c1 to $c2") unless $col; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | push @sql, sprintf " %s.%s = %s.%s ", $t1, $col, $t2, $c2->primary_column; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | return join " AND ", @sql; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub _do_transformation { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_do_transformation: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $me = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $sql = $me->{_sql}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my @args = @{ $me->{_args} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $caller = $me->{_caller}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $sql =~ s/__TABLE\(?(.*?)\)?__/$me->_expand_table($1)/eg; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $sql =~ s/__JOIN\((.*?)\)__/$me->_expand_join($1)/eg; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $sql =~ s/__ESSENTIAL__/join ", ", $caller->_essential/eg; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $sql =~ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | s/__ESSENTIAL\((.*?)\)__/join ", ", map "$1.$_", $caller->_essential/eg; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ($sql =~ /__IDENTIFIER__/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $key_sql = join " AND ", map "$_=?", $caller->primary_columns; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $sql =~ s/__IDENTIFIER__/$key_sql/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $me->{_transformed_sql} = $sql; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $me->{_transformed_args} = [@args]; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $me->{_transformed} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | =head1 FURTHER QUESTIONS? | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | redistribute it and/or modify it under the same terms as the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||