1 package # Hide from PAUSE
2 DBIx::Class::SQLMaker::Oracle;
7 use base qw( DBIx::Class::SQLMaker );
10 use DBIx::Class::Optional::Dependencies;
11 die('The following extra modules are required for Oracle-based Storages ' . DBIx::Class::Optional::Dependencies->req_missing_for ('id_shortener') . "\n" )
12 unless DBIx::Class::Optional::Dependencies->req_ok_for ('id_shortener');
17 my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
18 push @{$opts{special_ops}}, {
19 regex => qr/^prior$/i,
20 handler => '_where_field_PRIOR',
23 $self->next::method(\%opts);
28 return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where oracle_connect_by group having order limit/);
36 my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
37 push @{$self->{oracle_connect_by_bind}}, @cb_bind;
39 my $sql = $self->next::method(@_);
41 return "$cb_sql $sql";
45 my ($self, $attrs) = @_;
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;
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 );
58 $sql .= sprintf(" %s %s",
59 ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
60 : $self->_sqlcase('connect by'),
63 push @bind, @connect_by_sql_bind;
65 if ( $attrs->{'order_siblings_by'} ) {
66 $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
70 return wantarray ? ($sql, @bind) : $sql;
73 sub _order_siblings_by {
74 my ( $self, $arg ) = @_;
77 for my $c ( $self->_order_by_chunks($arg) ) {
89 ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
92 return wantarray ? ( $sql, @bind ) : $sql;
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);
101 $sql = sprintf ('%s = %s %s ',
102 $self->_convert($self->_quote($lhs)),
103 $self->_sqlcase ($op),
107 return ($sql, @bind);
110 # use this codepath to hook all identifiers and mangle them if necessary
111 # this is invoked regardless of quoting being on or off
113 my ($self, $label) = @_;
115 return '' unless defined $label;
116 return ${$label} if ref($label) eq 'SCALAR';
118 $label =~ s/ ( [^\.]{31,} ) /$self->_shorten_identifier($1)/gxe;
120 $self->next::method($label);
123 # this takes an identifier and shortens it if necessary
124 # optionally keywords can be passed as an arrayref to generate useful
126 sub _shorten_identifier {
127 my ($self, $to_shorten, $keywords) = @_;
129 # 30 characters is the identifier limit for Oracle
131 # we want at least 10 characters of the base36 md5
132 my $min_entropy = 10;
134 my $max_trunc = $max_len - $min_entropy - 1;
137 if length($to_shorten) <= $max_len;
139 $self->throw_exception("'keywords' needs to be an arrayref")
140 if defined $keywords && ref $keywords ne 'ARRAY';
142 # if no keywords are passed use the identifier as one
143 my @keywords = @{$keywords || []};
144 @keywords = $to_shorten unless @keywords;
146 # get a base36 md5 of the identifier
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)
156 # switch from perl to java
158 my ($concat_len, @lengths);
160 $_ = ucfirst (lc ($_));
161 $_ =~ s/\_+(\w)/uc ($1)/eg;
163 push @lengths, length ($_);
164 $concat_len += $lengths[-1];
167 # if we are still too long - try to disemvowel non-capitals (not keyword starts)
168 if ($concat_len > $max_trunc) {
175 push @lengths, length ($_);
176 $concat_len += $lengths[-1];
180 # still too long - just start cuting proportionally
181 if ($concat_len > $max_trunc) {
182 my $trim_ratio = $max_trunc / $concat_len;
184 for my $i (0 .. $#keywords) {
185 $keywords[$i] = substr ($keywords[$i], 0, int ($trim_ratio * $lengths[$i] ) );
189 my $fin = join ('', @keywords);
190 my $fin_len = length $fin;
192 return sprintf ('%s_%s',
194 substr ($b36sum, 0, $max_len - $fin_len - 1),
198 sub _unqualify_colname {
199 my ($self, $fqcn) = @_;
201 return $self->_shorten_identifier($self->next::method($fqcn));
205 # Oracle has a different INSERT...RETURNING syntax
208 sub _insert_returning {
209 my ($self, $options) = @_;
211 my $f = $options->{returning};
213 my ($f_list, @f_names) = do {
220 elsif (ref $f eq 'ARRAY') {
222 (join ', ', map { $self->_quote($_) } @$f),
226 elsif (ref $f eq 'SCALAR') {
233 $self->throw_exception("Unsupported INSERT RETURNING option $f");
237 my $rc_ref = $options->{returning_container}
238 or $self->throw_exception('No returning container supplied for IR values');
240 @$rc_ref = (undef) x @f_names;
244 $self->_sqlcase(' returning'),
246 $self->_sqlcase('into'),
247 join (', ', ('?') x @f_names ),
250 $self->{bindtype} eq 'columns'
251 ? [ $f_names[$_] => \$rc_ref->[$_] ]