1 package # Hide from PAUSE
2 DBIx::Class::SQLMaker::Oracle;
8 require DBIx::Class::Optional::Dependencies;
9 if (my $missing = DBIx::Class::Optional::Dependencies->req_missing_for ('id_shortener') ) {
10 die "The following extra modules are required for Oracle-based Storages: $missing\n";
14 use base 'DBIx::Class::SQLMaker';
18 my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
19 push @{$opts{special_ops}}, {
20 regex => qr/^prior$/i,
21 handler => '_where_field_PRIOR',
24 $self->next::method(\%opts);
29 return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where oracle_connect_by group having order limit/);
37 my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
38 push @{$self->{oracle_connect_by_bind}}, @cb_bind;
40 my $sql = $self->next::method(@_);
42 return "$cb_sql $sql";
46 my ($self, $attrs) = @_;
51 if ( ref($attrs) eq 'HASH' ) {
52 if ( $attrs->{'start_with'} ) {
53 my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
54 $sql .= $self->_sqlcase(' start with ') . $ws;
57 if ( my $connect_by = $attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'} ) {
58 my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by );
59 $sql .= sprintf(" %s %s",
60 ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
61 : $self->_sqlcase('connect by'),
64 push @bind, @connect_by_sql_bind;
66 if ( $attrs->{'order_siblings_by'} ) {
67 $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
71 return wantarray ? ($sql, @bind) : $sql;
74 sub _order_siblings_by {
75 my ( $self, $arg ) = @_;
78 for my $c ( $self->_order_by_chunks($arg) ) {
90 ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
93 return wantarray ? ( $sql, @bind ) : $sql;
96 # we need to add a '=' only when PRIOR is used against a column directly
97 # i.e. when it is invoked by a special_op callback
98 sub _where_field_PRIOR {
99 my ($self, $lhs, $op, $rhs) = @_;
100 my ($sql, @bind) = $self->_recurse_where ($rhs);
102 $sql = sprintf ('%s = %s %s ',
103 $self->_convert($self->_quote($lhs)),
104 $self->_sqlcase ($op),
108 return ($sql, @bind);
111 # use this codepath to hook all identifiers and mangle them if necessary
112 # this is invoked regardless of quoting being on or off
114 my ($self, $label) = @_;
116 return '' unless defined $label;
117 return ${$label} if ref($label) eq 'SCALAR';
119 $label =~ s/ ( [^\.]{31,} ) /$self->_shorten_identifier($1)/gxe;
121 $self->next::method($label);
124 # this takes an identifier and shortens it if necessary
125 # optionally keywords can be passed as an arrayref to generate useful
127 sub _shorten_identifier {
128 my ($self, $to_shorten, $keywords) = @_;
130 # 30 characters is the identifier limit for Oracle
132 # we want at least 10 characters of the base36 md5
133 my $min_entropy = 10;
135 my $max_trunc = $max_len - $min_entropy - 1;
138 if length($to_shorten) <= $max_len;
140 $self->throw_exception("'keywords' needs to be an arrayref")
141 if defined $keywords && ref $keywords ne 'ARRAY';
143 # if no keywords are passed use the identifier as one
144 my @keywords = @{$keywords || []};
145 @keywords = $to_shorten unless @keywords;
147 # get a base36 md5 of the identifier
149 require Math::BigInt;
150 require Math::Base36;
151 my $b36sum = Math::Base36::encode_base36(
152 Math::BigInt->from_hex (
153 '0x' . Digest::MD5::md5_hex ($to_shorten)
157 # switch from perl to java
159 my ($concat_len, @lengths);
161 $_ = ucfirst (lc ($_));
162 $_ =~ s/\_+(\w)/uc ($1)/eg;
164 push @lengths, length ($_);
165 $concat_len += $lengths[-1];
168 # if we are still too long - try to disemvowel non-capitals (not keyword starts)
169 if ($concat_len > $max_trunc) {
176 push @lengths, length ($_);
177 $concat_len += $lengths[-1];
181 # still too long - just start cutting proportionally
182 if ($concat_len > $max_trunc) {
183 my $trim_ratio = $max_trunc / $concat_len;
185 for my $i (0 .. $#keywords) {
186 $keywords[$i] = substr ($keywords[$i], 0, int ($trim_ratio * $lengths[$i] ) );
190 my $fin = join ('', @keywords);
191 my $fin_len = length $fin;
193 return sprintf ('%s_%s',
195 substr ($b36sum, 0, $max_len - $fin_len - 1),
199 sub _unqualify_colname {
200 my ($self, $fqcn) = @_;
202 return $self->_shorten_identifier($self->next::method($fqcn));
206 # Oracle has a different INSERT...RETURNING syntax
209 sub _insert_returning {
210 my ($self, $options) = @_;
212 my $f = $options->{returning};
214 my ($f_list, @f_names) = do {
221 elsif (ref $f eq 'ARRAY') {
223 (join ', ', map { $self->_quote($_) } @$f),
227 elsif (ref $f eq 'SCALAR') {
234 $self->throw_exception("Unsupported INSERT RETURNING option $f");
238 my $rc_ref = $options->{returning_container}
239 or $self->throw_exception('No returning container supplied for IR values');
241 @$rc_ref = (undef) x @f_names;
245 $self->_sqlcase(' returning'),
247 $self->_sqlcase('into'),
248 join (', ', ('?') x @f_names ),
251 $self->{bindtype} eq 'columns'
252 ? [ $f_names[$_] => \$rc_ref->[$_] ]