Commit | Line | Data |
c0024355 |
1 | package # Hide from PAUSE |
d5dedbd6 |
2 | DBIx::Class::SQLMaker::Oracle; |
c0024355 |
3 | |
9ab1e5f0 |
4 | use warnings; |
5 | use strict; |
6 | |
d5dedbd6 |
7 | use base qw( DBIx::Class::SQLMaker ); |
c0024355 |
8 | use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/; |
9 | |
c7d50a7d |
10 | BEGIN { |
11 | use Carp::Clan qw/^DBIx::Class/; |
12 | use DBIx::Class::Optional::Dependencies; |
13 | croak('The following extra modules are required for Oracle-based Storages ' . DBIx::Class::Optional::Dependencies->req_missing_for ('id_shortener') ) |
14 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('id_shortener'); |
15 | } |
16 | |
9ab1e5f0 |
17 | sub new { |
18 | my $self = shift; |
19 | my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_; |
20 | push @{$opts{special_ops}}, { |
21 | regex => qr/^prior$/i, |
22 | handler => '_where_field_PRIOR', |
23 | }; |
24 | |
25 | $self->SUPER::new (\%opts); |
26 | } |
27 | |
583a0c65 |
28 | sub _assemble_binds { |
29 | my $self = shift; |
30 | return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/from where oracle_connect_by having order/); |
c0024355 |
31 | } |
32 | |
583a0c65 |
33 | |
49afd714 |
34 | sub _parse_rs_attrs { |
35 | my $self = shift; |
36 | my ($rs_attrs) = @_; |
c0024355 |
37 | |
2e4dd241 |
38 | my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs); |
49afd714 |
39 | push @{$self->{oracle_connect_by_bind}}, @cb_bind; |
40 | |
41 | my $sql = $self->SUPER::_parse_rs_attrs(@_); |
c0024355 |
42 | |
49afd714 |
43 | return "$cb_sql $sql"; |
c0024355 |
44 | } |
45 | |
46 | sub _connect_by { |
2e4dd241 |
47 | my ($self, $attrs) = @_; |
48 | |
c0024355 |
49 | my $sql = ''; |
50 | my @bind; |
51 | |
52 | if ( ref($attrs) eq 'HASH' ) { |
53 | if ( $attrs->{'start_with'} ) { |
54 | my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} ); |
55 | $sql .= $self->_sqlcase(' start with ') . $ws; |
56 | push @bind, @wb; |
57 | } |
6b2fbbf0 |
58 | if ( my $connect_by = $attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'} ) { |
59 | my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by ); |
43426175 |
60 | $sql .= sprintf(" %s %s", |
ef5db928 |
61 | ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle') |
2ba03b16 |
62 | : $self->_sqlcase('connect by'), |
43426175 |
63 | $connect_by_sql, |
64 | ); |
65 | push @bind, @connect_by_sql_bind; |
c0024355 |
66 | } |
67 | if ( $attrs->{'order_siblings_by'} ) { |
68 | $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} ); |
69 | } |
70 | } |
71 | |
72 | return wantarray ? ($sql, @bind) : $sql; |
73 | } |
74 | |
75 | sub _order_siblings_by { |
2a770efe |
76 | my ( $self, $arg ) = @_; |
77 | |
78 | my ( @sql, @bind ); |
79 | for my $c ( $self->_order_by_chunks($arg) ) { |
80 | $self->_SWITCH_refkind( |
81 | $c, |
82 | { |
83 | SCALAR => sub { push @sql, $c }, |
84 | ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c }, |
85 | } |
86 | ); |
87 | } |
c0024355 |
88 | |
2a770efe |
89 | my $sql = |
90 | @sql |
91 | ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) ) |
92 | : ''; |
c0024355 |
93 | |
2a770efe |
94 | return wantarray ? ( $sql, @bind ) : $sql; |
c0024355 |
95 | } |
96 | |
9ab1e5f0 |
97 | # we need to add a '=' only when PRIOR is used against a column diretly |
98 | # i.e. when it is invoked by a special_op callback |
99 | sub _where_field_PRIOR { |
100 | my ($self, $lhs, $op, $rhs) = @_; |
101 | my ($sql, @bind) = $self->_recurse_where ($rhs); |
102 | |
103 | $sql = sprintf ('%s = %s %s ', |
104 | $self->_convert($self->_quote($lhs)), |
105 | $self->_sqlcase ($op), |
106 | $sql |
107 | ); |
108 | |
109 | return ($sql, @bind); |
110 | } |
111 | |
63ca94e1 |
112 | # this takes an identifier and shortens it if necessary |
113 | # optionally keywords can be passed as an arrayref to generate useful |
114 | # identifiers |
115 | sub _shorten_identifier { |
116 | my ($self, $to_shorten, $keywords) = @_; |
117 | |
118 | # 30 characters is the identifier limit for Oracle |
119 | my $max_len = 30; |
120 | # we want at least 10 characters of the base36 md5 |
121 | my $min_entropy = 10; |
122 | |
123 | my $max_trunc = $max_len - $min_entropy - 1; |
124 | |
125 | return $to_shorten |
126 | if length($to_shorten) <= $max_len; |
127 | |
128 | croak 'keywords needs to be an arrayref' |
129 | if defined $keywords && ref $keywords ne 'ARRAY'; |
130 | |
131 | # if no keywords are passed use the identifier as one |
132 | my @keywords = @{$keywords || []}; |
133 | @keywords = $to_shorten unless @keywords; |
134 | |
135 | # get a base36 md5 of the identifier |
136 | require Digest::MD5; |
137 | require Math::BigInt; |
138 | require Math::Base36; |
139 | my $b36sum = Math::Base36::encode_base36( |
140 | Math::BigInt->from_hex ( |
141 | '0x' . Digest::MD5::md5_hex ($to_shorten) |
142 | ) |
143 | ); |
144 | |
145 | # switch from perl to java |
146 | # get run-length |
147 | my ($concat_len, @lengths); |
148 | for (@keywords) { |
149 | $_ = ucfirst (lc ($_)); |
150 | $_ =~ s/\_+(\w)/uc ($1)/eg; |
151 | |
152 | push @lengths, length ($_); |
153 | $concat_len += $lengths[-1]; |
154 | } |
155 | |
156 | # if we are still too long - try to disemvowel non-capitals (not keyword starts) |
157 | if ($concat_len > $max_trunc) { |
158 | $concat_len = 0; |
159 | @lengths = (); |
160 | |
161 | for (@keywords) { |
162 | $_ =~ s/[aeiou]//g; |
163 | |
164 | push @lengths, length ($_); |
165 | $concat_len += $lengths[-1]; |
166 | } |
167 | } |
168 | |
169 | # still too long - just start cuting proportionally |
170 | if ($concat_len > $max_trunc) { |
171 | my $trim_ratio = $max_trunc / $concat_len; |
172 | |
173 | for my $i (0 .. $#keywords) { |
174 | $keywords[$i] = substr ($keywords[$i], 0, int ($trim_ratio * $lengths[$i] ) ); |
175 | } |
176 | } |
177 | |
178 | my $fin = join ('', @keywords); |
179 | my $fin_len = length $fin; |
180 | |
181 | return sprintf ('%s_%s', |
182 | $fin, |
183 | substr ($b36sum, 0, $max_len - $fin_len - 1), |
184 | ); |
185 | } |
186 | |
187 | sub _unqualify_colname { |
188 | my ($self, $fqcn) = @_; |
189 | |
190 | return $self->_shorten_identifier($self->next::method($fqcn)); |
191 | } |
192 | |
bf51641f |
193 | # |
194 | # Oracle has a different INSERT...RETURNING syntax |
195 | # |
196 | |
197 | sub _insert_returning { |
198 | my ($self, $options) = @_; |
199 | |
200 | my $f = $options->{returning}; |
201 | |
202 | my ($f_list, @f_names) = $self->_SWITCH_refkind($f, { |
203 | ARRAYREF => sub { |
204 | (join ', ', map { $self->_quote($_) } @$f), |
205 | @$f |
206 | }, |
207 | SCALAR => sub { |
208 | $self->_quote($f), |
209 | $f, |
210 | }, |
211 | SCALARREF => sub { |
212 | $$f, |
213 | $$f, |
214 | }, |
215 | }); |
216 | |
217 | my $rc_ref = $options->{returning_container} |
218 | or croak ('No returning container supplied for IR values'); |
219 | |
220 | @$rc_ref = (undef) x @f_names; |
221 | |
222 | return ( |
223 | ( join (' ', |
224 | $self->_sqlcase(' returning'), |
225 | $f_list, |
226 | $self->_sqlcase('into'), |
227 | join (', ', ('?') x @f_names ), |
228 | )), |
229 | map { |
230 | $self->{bindtype} eq 'columns' |
231 | ? [ $f_names[$_] => \$rc_ref->[$_] ] |
232 | : \$rc_ref->[$_] |
233 | } (0 .. $#f_names), |
234 | ); |
235 | } |
236 | |
c0024355 |
237 | 1; |