5f77a603918a4edc009a78024ca86047107574da
[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
11 #  TODO:
12 #   - Review by experienced DBIC/SQL:A developers :-)
13 #   - Problem with count and connect_by look the TODO in t/73oracle.t
14
15
16 sub new {
17   my $self = shift;
18   my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
19   push @{$opts{special_ops}}, {
20     regex => qr/^prior$/i,
21     handler => '_where_field_PRIOR',
22   };
23
24   $self->SUPER::new (\%opts);
25 }
26
27 sub _assemble_binds {
28   my $self = shift;
29   return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/from where oracle_connect_by having order/);
30 }
31
32
33 sub _emulate_limit {
34     my ( $self, $syntax, $sql, $rs_attrs, $rows, $offset ) = @_;
35
36     my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
37     $sql .= $cb_sql;
38     $self->{oracle_connect_by_bind} = \@cb_bind;
39
40     return $self->SUPER::_emulate_limit($syntax, $sql, $rs_attrs, $rows, $offset);
41 }
42
43 sub _connect_by {
44     my ($self, $attrs) = @_;
45
46     my $sql = '';
47     my @bind;
48
49     if ( ref($attrs) eq 'HASH' ) {
50         if ( $attrs->{'start_with'} ) {
51             my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
52             $sql .= $self->_sqlcase(' start with ') . $ws;
53             push @bind, @wb;
54         }
55         if ( my $connect_by = $attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'} ) {
56             my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by );
57             $sql .= sprintf(" %s %s",
58                 ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
59                     : $self->_sqlcase('connect by'),
60                 $connect_by_sql,
61             );
62             push @bind, @connect_by_sql_bind;
63         }
64         if ( $attrs->{'order_siblings_by'} ) {
65             $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
66         }
67     }
68
69     return wantarray ? ($sql, @bind) : $sql;
70 }
71
72 sub _order_siblings_by {
73     my ( $self, $arg ) = @_;
74
75     my ( @sql, @bind );
76     for my $c ( $self->_order_by_chunks($arg) ) {
77         $self->_SWITCH_refkind(
78             $c,
79             {
80                 SCALAR   => sub { push @sql, $c },
81                 ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c },
82             }
83         );
84     }
85
86     my $sql =
87       @sql
88       ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
89       : '';
90
91     return wantarray ? ( $sql, @bind ) : $sql;
92 }
93
94 # we need to add a '=' only when PRIOR is used against a column diretly
95 # i.e. when it is invoked by a special_op callback
96 sub _where_field_PRIOR {
97   my ($self, $lhs, $op, $rhs) = @_;
98   my ($sql, @bind) = $self->_recurse_where ($rhs);
99
100   $sql = sprintf ('%s = %s %s ',
101     $self->_convert($self->_quote($lhs)),
102     $self->_sqlcase ($op),
103     $sql
104   );
105
106   return ($sql, @bind);
107 }
108
109 1;
110
111 __END__
112
113 =pod
114
115 =head1 NAME
116
117 DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract
118
119 =head1 DESCRIPTION
120
121 See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more informations about
122 how to use hierarchical queries with DBIx::Class.
123
124 =cut