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