X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FSQLAHacks%2FOracle.pm;h=23c12857337c5c2a4ac823bcf520d713b0c95607;hb=7fca91be5671c61be5c766bd93df741bc00d9c15;hp=0ca54b62e836e5ac2e8cb8d3435a0456c047ed9a;hpb=2e4dd2413cf6add731083c60f493cc7374b3fcf8;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/SQLAHacks/Oracle.pm b/lib/DBIx/Class/SQLAHacks/Oracle.pm index 0ca54b6..23c1285 100644 --- a/lib/DBIx/Class/SQLAHacks/Oracle.pm +++ b/lib/DBIx/Class/SQLAHacks/Oracle.pm @@ -7,12 +7,6 @@ use strict; use base qw( DBIx::Class::SQLAHacks ); use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/; -# -# TODO: -# - Review by experienced DBIC/SQL:A developers :-) -# - Problem with count and connect_by look the TODO in t/73oracle.t -# - sub new { my $self = shift; my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_; @@ -24,23 +18,22 @@ sub new { $self->SUPER::new (\%opts); } -sub select { - my ($self, $table, $fields, $where, $rs_attrs, @rest) = @_; - - my ($sql, @bind) = $self->SUPER::select($table, $fields, $where, $rs_attrs, @rest); - push @bind, @{$self->{_oracle_connect_by_binds}}; - - return wantarray ? ($sql, @bind) : $sql; +sub _assemble_binds { + my $self = shift; + return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/from where oracle_connect_by having order/); } -sub _emulate_limit { - my ( $self, $syntax, $sql, $rs_attrs, $rows, $offset ) = @_; + +sub _parse_rs_attrs { + my $self = shift; + my ($rs_attrs) = @_; my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs); - $sql .= $cb_sql; - $self->{_oracle_connect_by_binds} = \@cb_bind; + push @{$self->{oracle_connect_by_bind}}, @cb_bind; + + my $sql = $self->SUPER::_parse_rs_attrs(@_); - return $self->SUPER::_emulate_limit($syntax, $sql, $rs_attrs, $rows, $offset); + return "$cb_sql $sql"; } sub _connect_by { @@ -55,18 +48,14 @@ sub _connect_by { $sql .= $self->_sqlcase(' start with ') . $ws; push @bind, @wb; } - if ( my $connect_by = $attrs->{'connect_by'} ) { - my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $attrs->{'connect_by'} ); + if ( my $connect_by = $attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'} ) { + my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by ); $sql .= sprintf(" %s %s", - ( $attrs->{'nocycle'} ) ? $self->_sqlcase('connect by nocycle') + ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle') : $self->_sqlcase('connect by'), $connect_by_sql, ); push @bind, @connect_by_sql_bind; - # $sql .= $self->_sqlcase(' connect by'); - # foreach my $key ( keys %$connect_by ) { - # $sql .= " $key = " . $connect_by->{$key}; - # } } if ( $attrs->{'order_siblings_by'} ) { $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} ); @@ -113,6 +102,87 @@ sub _where_field_PRIOR { return ($sql, @bind); } +# this takes an identifier and shortens it if necessary +# optionally keywords can be passed as an arrayref to generate useful +# identifiers +sub _shorten_identifier { + my ($self, $to_shorten, $keywords) = @_; + + # 30 characters is the identifier limit for Oracle + my $max_len = 30; + # we want at least 10 characters of the base36 md5 + my $min_entropy = 10; + + my $max_trunc = $max_len - $min_entropy - 1; + + return $to_shorten + if length($to_shorten) <= $max_len; + + croak 'keywords needs to be an arrayref' + if defined $keywords && ref $keywords ne 'ARRAY'; + + # if no keywords are passed use the identifier as one + my @keywords = @{$keywords || []}; + @keywords = $to_shorten unless @keywords; + + # get a base36 md5 of the identifier + require Digest::MD5; + require Math::BigInt; + require Math::Base36; + my $b36sum = Math::Base36::encode_base36( + Math::BigInt->from_hex ( + '0x' . Digest::MD5::md5_hex ($to_shorten) + ) + ); + + # switch from perl to java + # get run-length + my ($concat_len, @lengths); + for (@keywords) { + $_ = ucfirst (lc ($_)); + $_ =~ s/\_+(\w)/uc ($1)/eg; + + push @lengths, length ($_); + $concat_len += $lengths[-1]; + } + + # if we are still too long - try to disemvowel non-capitals (not keyword starts) + if ($concat_len > $max_trunc) { + $concat_len = 0; + @lengths = (); + + for (@keywords) { + $_ =~ s/[aeiou]//g; + + push @lengths, length ($_); + $concat_len += $lengths[-1]; + } + } + + # still too long - just start cuting proportionally + if ($concat_len > $max_trunc) { + my $trim_ratio = $max_trunc / $concat_len; + + for my $i (0 .. $#keywords) { + $keywords[$i] = substr ($keywords[$i], 0, int ($trim_ratio * $lengths[$i] ) ); + } + } + + my $fin = join ('', @keywords); + my $fin_len = length $fin; + + return sprintf ('%s_%s', + $fin, + substr ($b36sum, 0, $max_len - $fin_len - 1), + ); +} + +sub _unqualify_colname { + my ($self, $fqcn) = @_; + + return $self->_shorten_identifier($self->next::method($fqcn)); +} + 1; __END__ @@ -125,7 +195,7 @@ DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to S =head1 DESCRIPTION -See L for more informations about +See L for more information about how to use hierarchical queries with DBIx::Class. =cut