1 package # Hide from PAUSE
2 DBIx::Class::SQLAHacks::Oracle;
7 use base qw( DBIx::Class::SQLAHacks );
8 use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
12 my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
13 push @{$opts{special_ops}}, {
14 regex => qr/^prior$/i,
15 handler => '_where_field_PRIOR',
18 $self->SUPER::new (\%opts);
23 return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/from where oracle_connect_by having order/);
31 my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
32 push @{$self->{oracle_connect_by_bind}}, @cb_bind;
34 my $sql = $self->SUPER::_parse_rs_attrs(@_);
36 return "$cb_sql $sql";
40 my ($self, $attrs) = @_;
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;
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'),
58 push @bind, @connect_by_sql_bind;
60 if ( $attrs->{'order_siblings_by'} ) {
61 $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
65 return wantarray ? ($sql, @bind) : $sql;
68 sub _order_siblings_by {
69 my ( $self, $arg ) = @_;
72 for my $c ( $self->_order_by_chunks($arg) ) {
73 $self->_SWITCH_refkind(
76 SCALAR => sub { push @sql, $c },
77 ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c },
84 ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
87 return wantarray ? ( $sql, @bind ) : $sql;
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);
96 $sql = sprintf ('%s = %s %s ',
97 $self->_convert($self->_quote($lhs)),
98 $self->_sqlcase ($op),
102 return ($sql, @bind);
105 # this takes an identifier and shortens it if necessary
106 # optionally keywords can be passed as an arrayref to generate useful
108 sub _shorten_identifier {
109 my ($self, $to_shorten, $keywords) = @_;
111 # 30 characters is the identifier limit for Oracle
113 # we want at least 10 characters of the base36 md5
114 my $min_entropy = 10;
116 my $max_trunc = $max_len - $min_entropy - 1;
119 if length($to_shorten) <= $max_len;
121 croak 'keywords needs to be an arrayref'
122 if defined $keywords && ref $keywords ne 'ARRAY';
124 # if no keywords are passed use the identifier as one
125 my @keywords = @{$keywords || []};
126 @keywords = $to_shorten unless @keywords;
128 # get a base36 md5 of the identifier
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)
138 # switch from perl to java
140 my ($concat_len, @lengths);
142 $_ = ucfirst (lc ($_));
143 $_ =~ s/\_+(\w)/uc ($1)/eg;
145 push @lengths, length ($_);
146 $concat_len += $lengths[-1];
149 # if we are still too long - try to disemvowel non-capitals (not keyword starts)
150 if ($concat_len > $max_trunc) {
157 push @lengths, length ($_);
158 $concat_len += $lengths[-1];
162 # still too long - just start cuting proportionally
163 if ($concat_len > $max_trunc) {
164 my $trim_ratio = $max_trunc / $concat_len;
166 for my $i (0 .. $#keywords) {
167 $keywords[$i] = substr ($keywords[$i], 0, int ($trim_ratio * $lengths[$i] ) );
171 my $fin = join ('', @keywords);
172 my $fin_len = length $fin;
174 return sprintf ('%s_%s',
176 substr ($b36sum, 0, $max_len - $fin_len - 1),
180 sub _unqualify_colname {
181 my ($self, $fqcn) = @_;
183 return $self->_shorten_identifier($self->next::method($fqcn));
194 DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract
198 See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more information about
199 how to use hierarchical queries with DBIx::Class.