1 package DBIx::Class::CDBICompat::SQLTransformer;
6 use base 'DBIx::Class';
10 DBIx::Class::CDBICompat::SQLTransformer - Transform SQL
14 This is a copy of L<Class::DBI::SQL::Transformer> from Class::DBI 3.0.17.
15 It is here so we can be compatible with L<Class::DBI> without having it
21 my ($me, $caller, $sql, @args) = @_;
32 $self->_do_transformation if !$self->{_transformed};
33 return $self->{_transformed_sql};
38 $self->_do_transformation if !$self->{_transformed};
39 return @{ $self->{_transformed_args} };
44 my ($class, $alias) = split /=/, shift, 2;
45 my $caller = $self->{_caller};
46 my $table = $class ? $class->table : $caller->table;
47 $self->{cmap}{ $alias || $table } = $class || ref $caller || $caller;
48 ($alias ||= "") &&= " $alias";
49 return $table . $alias;
55 my @table = split /\s+/, $joins;
57 my $caller = $self->{_caller};
58 my %tojoin = map { $table[$_] => $table[ $_ + 1 ] } 0 .. $#table - 1;
60 while (my ($t1, $t2) = each %tojoin) {
61 my ($c1, $c2) = map $self->{cmap}{$_}
62 || $caller->_croak("Don't understand table '$_' in JOIN"), ($t1, $t2);
66 my $meta = $c1->meta_info('has_a');
67 my ($col) = grep $meta->{$_}->foreign_class eq $c2, keys %$meta;
71 my $col = $join_col->($c1 => $c2) || do {
72 ($c1, $c2) = ($c2, $c1);
73 ($t1, $t2) = ($t2, $t1);
74 $join_col->($c1 => $c2);
77 $caller->_croak("Don't know how to join $c1 to $c2") unless $col;
78 push @sql, sprintf " %s.%s = %s.%s ", $t1, $col, $t2, $c2->primary_column;
80 return join " AND ", @sql;
83 sub _do_transformation {
85 my $sql = $me->{_sql};
86 my @args = @{ $me->{_args} };
87 my $caller = $me->{_caller};
89 $sql =~ s/__TABLE\(?(.*?)\)?__/$me->_expand_table($1)/eg;
90 $sql =~ s/__JOIN\((.*?)\)__/$me->_expand_join($1)/eg;
91 $sql =~ s/__ESSENTIAL__/join ", ", $caller->_essential/eg;
93 s/__ESSENTIAL\((.*?)\)__/join ", ", map "$1.$_", $caller->_essential/eg;
94 if ($sql =~ /__IDENTIFIER__/) {
95 my $key_sql = join " AND ", map "$_=?", $caller->primary_columns;
96 $sql =~ s/__IDENTIFIER__/$key_sql/g;
99 $me->{_transformed_sql} = $sql;
100 $me->{_transformed_args} = [@args];
101 $me->{_transformed} = 1;
105 =head1 FURTHER QUESTIONS?
107 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
109 =head1 COPYRIGHT AND LICENSE
111 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
112 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
113 redistribute it and/or modify it under the same terms as the
114 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.