1 package # Hide from PAUSE
2 DBIx::Class::SQLMaker::Oracle;
4 use Module::Runtime ();
8 extends 'DBIx::Class::SQLMaker';
11 use DBIx::Class::Optional::Dependencies;
12 die('The following extra modules are required for Oracle-based Storages ' . DBIx::Class::Optional::Dependencies->req_missing_for ('id_shortener') . "\n" )
13 unless DBIx::Class::Optional::Dependencies->req_ok_for ('id_shortener');
16 sub _build_converter_class {
17 Module::Runtime::use_module('DBIx::Class::SQLMaker::Converter::Oracle');
20 around _build_renderer_roles => sub {
21 my ($orig, $self) = (shift, shift);
23 'Data::Query::Renderer::SQL::Extension::ConnectBy',
24 'Data::Query::Renderer::SQL::Dialect::ReturnInto',
31 return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where oracle_connect_by group having order limit/);
39 my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
40 push @{$self->{oracle_connect_by_bind}}, @cb_bind;
42 my $sql = $self->next::method(@_);
44 return "$cb_sql $sql";
48 my ($self, $attrs) = @_;
53 if ( ref($attrs) eq 'HASH' ) {
54 if ( $attrs->{'start_with'} ) {
55 my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
56 $sql .= $self->_sqlcase(' start with ') . $ws;
59 if ( my $connect_by = $attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'} ) {
60 my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by );
61 $sql .= sprintf(" %s %s",
62 ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
63 : $self->_sqlcase('connect by'),
66 push @bind, @connect_by_sql_bind;
68 if ( $attrs->{'order_siblings_by'} ) {
69 $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
73 return wantarray ? ($sql, @bind) : $sql;
76 sub _order_siblings_by {
77 my ( $self, $arg ) = @_;
80 for my $c ( $self->_order_by_chunks($arg) ) {
92 ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
95 return wantarray ? ( $sql, @bind ) : $sql;
98 # we need to add a '=' only when PRIOR is used against a column diretly
99 # i.e. when it is invoked by a special_op callback
100 sub _where_field_PRIOR {
101 my ($self, $lhs, $op, $rhs) = @_;
102 my ($sql, @bind) = $self->_recurse_where ($rhs);
104 $sql = sprintf ('%s = %s %s ',
105 $self->_convert($self->_quote($lhs)),
106 $self->_sqlcase ($op),
110 return ($sql, @bind);
113 # use this codepath to hook all identifiers and mangle them if necessary
114 # this is invoked regardless of quoting being on or off
116 my ($self, $label) = @_;
118 return '' unless defined $label;
119 return ${$label} if ref($label) eq 'SCALAR';
121 $label =~ s/ ( [^\.]{31,} ) /$self->_shorten_identifier($1)/gxe;
123 $self->next::method($label);
126 # this takes an identifier and shortens it if necessary
127 # optionally keywords can be passed as an arrayref to generate useful
129 sub _shorten_identifier {
130 my ($self, $to_shorten, $keywords) = @_;
132 # 30 characters is the identifier limit for Oracle
134 # we want at least 10 characters of the base36 md5
135 my $min_entropy = 10;
137 my $max_trunc = $max_len - $min_entropy - 1;
140 if length($to_shorten) <= $max_len;
142 $self->throw_exception("'keywords' needs to be an arrayref")
143 if defined $keywords && ref $keywords ne 'ARRAY';
145 # if no keywords are passed use the identifier as one
146 my @keywords = @{$keywords || []};
147 @keywords = $to_shorten unless @keywords;
149 # get a base36 md5 of the identifier
151 require Math::BigInt;
152 require Math::Base36;
153 my $b36sum = Math::Base36::encode_base36(
154 Math::BigInt->from_hex (
155 '0x' . Digest::MD5::md5_hex ($to_shorten)
159 # switch from perl to java
161 my ($concat_len, @lengths);
163 $_ = ucfirst (lc ($_));
164 $_ =~ s/\_+(\w)/uc ($1)/eg;
166 push @lengths, length ($_);
167 $concat_len += $lengths[-1];
170 # if we are still too long - try to disemvowel non-capitals (not keyword starts)
171 if ($concat_len > $max_trunc) {
178 push @lengths, length ($_);
179 $concat_len += $lengths[-1];
183 # still too long - just start cuting proportionally
184 if ($concat_len > $max_trunc) {
185 my $trim_ratio = $max_trunc / $concat_len;
187 for my $i (0 .. $#keywords) {
188 $keywords[$i] = substr ($keywords[$i], 0, int ($trim_ratio * $lengths[$i] ) );
192 my $fin = join ('', @keywords);
193 my $fin_len = length $fin;
195 return sprintf ('%s_%s',
197 substr ($b36sum, 0, $max_len - $fin_len - 1),
201 sub _unqualify_colname {
202 my ($self, $fqcn) = @_;
204 return $self->_shorten_identifier($self->next::method($fqcn));