POD fixes (RT#58247)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLAHacks / Oracle.pm
1 package # Hide from PAUSE
2   DBIx::Class::SQLAHacks::Oracle;
3
4 use warnings;
5 use strict;
6
7 use base qw( DBIx::Class::SQLAHacks );
8 use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
9
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
21 sub _assemble_binds {
22   my $self = shift;
23   return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/from where oracle_connect_by having order/);
24 }
25
26
27 sub _parse_rs_attrs {
28     my $self = shift;
29     my ($rs_attrs) = @_;
30
31     my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
32     push @{$self->{oracle_connect_by_bind}}, @cb_bind;
33
34     my $sql = $self->SUPER::_parse_rs_attrs(@_);
35
36     return "$cb_sql $sql";
37 }
38
39 sub _connect_by {
40     my ($self, $attrs) = @_;
41
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         }
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 );
53             $sql .= sprintf(" %s %s",
54                 ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
55                     : $self->_sqlcase('connect by'),
56                 $connect_by_sql,
57             );
58             push @bind, @connect_by_sql_bind;
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 {
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     }
81
82     my $sql =
83       @sql
84       ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
85       : '';
86
87     return wantarray ? ( $sql, @bind ) : $sql;
88 }
89
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
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
186 1;
187
188 __END__
189
190 =pod
191
192 =head1 NAME
193
194 DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract
195
196 =head1 DESCRIPTION
197
198 See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more information about
199 how to use hierarchical queries with DBIx::Class.
200
201 =cut