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