Commit | Line | Data |
c0024355 |
1 | package # Hide from PAUSE |
2 | DBIx::Class::SQLAHacks::Oracle; |
3 | |
9ab1e5f0 |
4 | use warnings; |
5 | use strict; |
6 | |
c0024355 |
7 | use base qw( DBIx::Class::SQLAHacks ); |
8 | use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/; |
9 | |
9ab1e5f0 |
10 | sub new { |
11 | my $self = shift; |
12 | my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_; |
13 | push @{$opts{special_ops}}, { |
14 | regex => qr/^prior$/i, |
15 | handler => '_where_field_PRIOR', |
16 | }; |
17 | |
18 | $self->SUPER::new (\%opts); |
19 | } |
20 | |
583a0c65 |
21 | sub _assemble_binds { |
22 | my $self = shift; |
23 | return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/from where oracle_connect_by having order/); |
c0024355 |
24 | } |
25 | |
583a0c65 |
26 | |
49afd714 |
27 | sub _parse_rs_attrs { |
28 | my $self = shift; |
29 | my ($rs_attrs) = @_; |
c0024355 |
30 | |
2e4dd241 |
31 | my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs); |
49afd714 |
32 | push @{$self->{oracle_connect_by_bind}}, @cb_bind; |
33 | |
34 | my $sql = $self->SUPER::_parse_rs_attrs(@_); |
c0024355 |
35 | |
49afd714 |
36 | return "$cb_sql $sql"; |
c0024355 |
37 | } |
38 | |
39 | sub _connect_by { |
2e4dd241 |
40 | my ($self, $attrs) = @_; |
41 | |
c0024355 |
42 | my $sql = ''; |
43 | my @bind; |
44 | |
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; |
49 | push @bind, @wb; |
50 | } |
6b2fbbf0 |
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 ); |
43426175 |
53 | $sql .= sprintf(" %s %s", |
ef5db928 |
54 | ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle') |
2ba03b16 |
55 | : $self->_sqlcase('connect by'), |
43426175 |
56 | $connect_by_sql, |
57 | ); |
58 | push @bind, @connect_by_sql_bind; |
c0024355 |
59 | } |
60 | if ( $attrs->{'order_siblings_by'} ) { |
61 | $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} ); |
62 | } |
63 | } |
64 | |
65 | return wantarray ? ($sql, @bind) : $sql; |
66 | } |
67 | |
68 | sub _order_siblings_by { |
2a770efe |
69 | my ( $self, $arg ) = @_; |
70 | |
71 | my ( @sql, @bind ); |
72 | for my $c ( $self->_order_by_chunks($arg) ) { |
73 | $self->_SWITCH_refkind( |
74 | $c, |
75 | { |
76 | SCALAR => sub { push @sql, $c }, |
77 | ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c }, |
78 | } |
79 | ); |
80 | } |
c0024355 |
81 | |
2a770efe |
82 | my $sql = |
83 | @sql |
84 | ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) ) |
85 | : ''; |
c0024355 |
86 | |
2a770efe |
87 | return wantarray ? ( $sql, @bind ) : $sql; |
c0024355 |
88 | } |
89 | |
9ab1e5f0 |
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); |
95 | |
96 | $sql = sprintf ('%s = %s %s ', |
97 | $self->_convert($self->_quote($lhs)), |
98 | $self->_sqlcase ($op), |
99 | $sql |
100 | ); |
101 | |
102 | return ($sql, @bind); |
103 | } |
104 | |
c0024355 |
105 | 1; |
106 | |
107 | __END__ |
108 | |
109 | =pod |
110 | |
111 | =head1 NAME |
112 | |
113 | DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract |
114 | |
115 | =head1 DESCRIPTION |
116 | |
117 | See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more informations about |
118 | how to use hierarchical queries with DBIx::Class. |
119 | |
120 | =cut |