Merge 'trunk' into 'sybase_mssql'
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / ImaDBI.pm
CommitLineData
c0e7b4e5 1package # hide from PAUSE
2 DBIx::Class::CDBICompat::ImaDBI;
dbd7896f 3
4use strict;
5use warnings;
aea8af71 6use DBIx::ContextualFetch;
ddc0a6c8 7use Sub::Name ();
dbd7896f 8
5e85c671 9use base qw(Class::Data::Inheritable);
ef29a097 10
902133a3 11__PACKAGE__->mk_classdata('sql_transformer_class' =>
12 'DBIx::Class::CDBICompat::SQLTransformer');
13
12bbb339 14__PACKAGE__->mk_classdata('_transform_sql_handler_order'
e60dc79f 15 => [ qw/TABLE ESSENTIAL JOIN IDENTIFIER/ ] );
12bbb339 16
ef29a097 17__PACKAGE__->mk_classdata('_transform_sql_handlers' =>
18 {
12bbb339 19 'TABLE' =>
20 sub {
21 my ($self, $class, $data) = @_;
b98e75f6 22 return $class->result_source_instance->name unless $data;
12bbb339 23 my ($f_class, $alias) = split(/=/, $data);
24 $f_class ||= $class;
d2ff6175 25 $self->{_classes}{$alias} = $f_class;
b98e75f6 26 return $f_class->result_source_instance->name." ${alias}";
12bbb339 27 },
28 'ESSENTIAL' =>
29 sub {
30 my ($self, $class, $data) = @_;
e60dc79f 31 $class = $data ? $self->{_classes}{$data} : $class;
32 return join(', ', $class->columns('Essential'));
33 },
34 'IDENTIFIER' =>
35 sub {
36 my ($self, $class, $data) = @_;
37 $class = $data ? $self->{_classes}{$data} : $class;
38 return join ' AND ', map "$_ = ?", $class->primary_columns;
12bbb339 39 },
40 'JOIN' =>
41 sub {
42 my ($self, $class, $data) = @_;
43 my ($from, $to) = split(/ /, $data);
d2ff6175 44 my ($from_class, $to_class) = @{$self->{_classes}}{$from, $to};
12bbb339 45 my ($rel_obj) = grep { $_->{class} && $_->{class} eq $to_class }
4685e006 46 map { $from_class->relationship_info($_) }
47 $from_class->relationships;
12bbb339 48 unless ($rel_obj) {
49 ($from, $to) = ($to, $from);
50 ($from_class, $to_class) = ($to_class, $from_class);
51 ($rel_obj) = grep { $_->{class} && $_->{class} eq $to_class }
4685e006 52 map { $from_class->relationship_info($_) }
53 $from_class->relationships;
12bbb339 54 }
701da8c4 55 $self->throw_exception( "No relationship to JOIN from ${from_class} to ${to_class}" )
12bbb339 56 unless $rel_obj;
fef5d100 57 my $join = $from_class->storage->sql_maker->_join_condition(
8c49f629 58 $from_class->result_source_instance->resolve_condition(
3842b955 59 $rel_obj->{cond}, $to, $from) );
12bbb339 60 return $join;
61 }
62
ef29a097 63 } );
dbd7896f 64
65sub db_Main {
8b445e33 66 return $_[0]->storage->dbh;
dbd7896f 67}
68
8b445e33 69sub connection {
dbd7896f 70 my ($class, @info) = @_;
71 $info[3] = { %{ $info[3] || {}} };
72 $info[3]->{RootClass} = 'DBIx::ContextualFetch';
147dd158 73 return $class->next::method(@info);
dbd7896f 74}
75
76sub __driver {
8b445e33 77 return $_[0]->storage->dbh->{Driver}->{Name};
dbd7896f 78}
79
a3018bd3 80sub set_sql {
81 my ($class, $name, $sql) = @_;
a3018bd3 82 no strict 'refs';
ddc0a6c8 83 my $sql_name = "sql_${name}";
84 my $full_sql_name = join '::', $class, $sql_name;
85 *$full_sql_name = Sub::Name::subname $full_sql_name,
a3018bd3 86 sub {
87 my $sql = $sql;
88 my $class = shift;
8b445e33 89 return $class->storage->sth($class->transform_sql($sql, @_));
a3018bd3 90 };
510ca912 91 if ($sql =~ /select/i) {
ddc0a6c8 92 my $search_name = "search_${name}";
93 my $full_search_name = join '::', $class, $search_name;
94 *$full_search_name = Sub::Name::subname $full_search_name,
510ca912 95 sub {
96 my ($class, @args) = @_;
ddc0a6c8 97 my $sth = $class->$sql_name;
902133a3 98 return $class->sth_to_objects($sth, \@args);
510ca912 99 };
100 }
101}
102
223b8fe3 103sub sth_to_objects {
902133a3 104 my ($class, $sth, $execute_args) = @_;
105
106 $sth->execute(@$execute_args);
107
223b8fe3 108 my @ret;
b52e9bf8 109 while (my $row = $sth->fetchrow_hashref) {
8c49f629 110 push(@ret, $class->inflate_result($class->result_source_instance, $row));
223b8fe3 111 }
902133a3 112
223b8fe3 113 return @ret;
114}
115
510ca912 116sub transform_sql {
117 my ($class, $sql, @args) = @_;
902133a3 118
119 my $tclass = $class->sql_transformer_class;
120 $class->ensure_class_loaded($tclass);
121 my $t = $tclass->new($class, $sql, @args);
122
123 return sprintf($t->sql, $t->args);
a3018bd3 124}
125
a2800991 126package
127 DBIx::ContextualFetch::st; # HIDE FROM PAUSE THIS IS NOT OUR CLASS
aea8af71 128
129no warnings 'redefine';
130
131sub _untaint_execute {
132 my $sth = shift;
133 my $old_value = $sth->{Taint};
134 $sth->{Taint} = 0;
135 my $ret;
136 {
137 no warnings 'uninitialized';
138 $ret = $sth->SUPER::execute(@_);
139 }
140 $sth->{Taint} = $old_value;
141 return $ret;
142}
143
dbd7896f 1441;