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