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