Commit | Line | Data |
c0e7b4e5 |
1 | package # hide from PAUSE |
2 | DBIx::Class::CDBICompat::ImaDBI; |
dbd7896f |
3 | |
4 | use strict; |
5 | use warnings; |
aea8af71 |
6 | use DBIx::ContextualFetch; |
dbd7896f |
7 | |
75a23b3e |
8 | use base qw/DBIx::Class/; |
ef29a097 |
9 | |
12bbb339 |
10 | __PACKAGE__->mk_classdata('_transform_sql_handler_order' |
11 | => [ qw/TABLE ESSENTIAL JOIN/ ] ); |
12 | |
ef29a097 |
13 | __PACKAGE__->mk_classdata('_transform_sql_handlers' => |
14 | { |
12bbb339 |
15 | 'TABLE' => |
16 | sub { |
17 | my ($self, $class, $data) = @_; |
b98e75f6 |
18 | return $class->result_source_instance->name unless $data; |
12bbb339 |
19 | my ($f_class, $alias) = split(/=/, $data); |
20 | $f_class ||= $class; |
d2ff6175 |
21 | $self->{_classes}{$alias} = $f_class; |
b98e75f6 |
22 | return $f_class->result_source_instance->name." ${alias}"; |
12bbb339 |
23 | }, |
24 | 'ESSENTIAL' => |
25 | sub { |
26 | my ($self, $class, $data) = @_; |
27 | return join(' ', $class->columns('Essential')) unless $data; |
d2ff6175 |
28 | return join(' ', $self->{_classes}{$data}->columns('Essential')); |
12bbb339 |
29 | }, |
30 | 'JOIN' => |
31 | sub { |
32 | my ($self, $class, $data) = @_; |
33 | my ($from, $to) = split(/ /, $data); |
d2ff6175 |
34 | my ($from_class, $to_class) = @{$self->{_classes}}{$from, $to}; |
12bbb339 |
35 | my ($rel_obj) = grep { $_->{class} && $_->{class} eq $to_class } |
4685e006 |
36 | map { $from_class->relationship_info($_) } |
37 | $from_class->relationships; |
12bbb339 |
38 | unless ($rel_obj) { |
39 | ($from, $to) = ($to, $from); |
40 | ($from_class, $to_class) = ($to_class, $from_class); |
41 | ($rel_obj) = grep { $_->{class} && $_->{class} eq $to_class } |
4685e006 |
42 | map { $from_class->relationship_info($_) } |
43 | $from_class->relationships; |
12bbb339 |
44 | } |
701da8c4 |
45 | $self->throw_exception( "No relationship to JOIN from ${from_class} to ${to_class}" ) |
12bbb339 |
46 | unless $rel_obj; |
fef5d100 |
47 | my $join = $from_class->storage->sql_maker->_join_condition( |
8c49f629 |
48 | $from_class->result_source_instance->resolve_condition( |
3842b955 |
49 | $rel_obj->{cond}, $to, $from) ); |
12bbb339 |
50 | return $join; |
51 | } |
52 | |
ef29a097 |
53 | } ); |
dbd7896f |
54 | |
55 | sub db_Main { |
8b445e33 |
56 | return $_[0]->storage->dbh; |
dbd7896f |
57 | } |
58 | |
8b445e33 |
59 | sub connection { |
dbd7896f |
60 | my ($class, @info) = @_; |
61 | $info[3] = { %{ $info[3] || {}} }; |
62 | $info[3]->{RootClass} = 'DBIx::ContextualFetch'; |
147dd158 |
63 | return $class->next::method(@info); |
dbd7896f |
64 | } |
65 | |
66 | sub __driver { |
8b445e33 |
67 | return $_[0]->storage->dbh->{Driver}->{Name}; |
dbd7896f |
68 | } |
69 | |
a3018bd3 |
70 | sub set_sql { |
71 | my ($class, $name, $sql) = @_; |
a3018bd3 |
72 | no strict 'refs'; |
73 | *{"${class}::sql_${name}"} = |
74 | sub { |
75 | my $sql = $sql; |
76 | my $class = shift; |
8b445e33 |
77 | return $class->storage->sth($class->transform_sql($sql, @_)); |
a3018bd3 |
78 | }; |
510ca912 |
79 | if ($sql =~ /select/i) { |
80 | my $meth = "sql_${name}"; |
81 | *{"${class}::search_${name}"} = |
82 | sub { |
83 | my ($class, @args) = @_; |
223b8fe3 |
84 | my $sth = $class->$meth; |
85 | $sth->execute(@args); |
86 | return $class->sth_to_objects($sth); |
510ca912 |
87 | }; |
88 | } |
89 | } |
90 | |
223b8fe3 |
91 | sub sth_to_objects { |
92 | my ($class, $sth) = @_; |
223b8fe3 |
93 | my @ret; |
b52e9bf8 |
94 | while (my $row = $sth->fetchrow_hashref) { |
8c49f629 |
95 | push(@ret, $class->inflate_result($class->result_source_instance, $row)); |
223b8fe3 |
96 | } |
97 | return @ret; |
98 | } |
99 | |
510ca912 |
100 | sub transform_sql { |
101 | my ($class, $sql, @args) = @_; |
12bbb339 |
102 | my $attrs = { }; |
103 | foreach my $key (@{$class->_transform_sql_handler_order}) { |
ef29a097 |
104 | my $h = $class->_transform_sql_handlers->{$key}; |
12bbb339 |
105 | $sql =~ s/__$key(?:\(([^\)]+)\))?__/$h->($attrs, $class, $1)/eg; |
ef29a097 |
106 | } |
438adc0e |
107 | #warn $sql; |
510ca912 |
108 | return sprintf($sql, @args); |
a3018bd3 |
109 | } |
110 | |
aea8af71 |
111 | package DBIx::ContextualFetch::st; |
112 | |
113 | no warnings 'redefine'; |
114 | |
115 | sub _untaint_execute { |
116 | my $sth = shift; |
117 | my $old_value = $sth->{Taint}; |
118 | $sth->{Taint} = 0; |
119 | my $ret; |
120 | { |
121 | no warnings 'uninitialized'; |
122 | $ret = $sth->SUPER::execute(@_); |
123 | } |
124 | $sth->{Taint} = $old_value; |
125 | return $ret; |
126 | } |
127 | |
dbd7896f |
128 | 1; |