blib/lib/Jifty/DBI/Handle/Oracle.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 26 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 148 CC 14 SCAR 33.1
CC
14
Cov
0%
insert
56.0
build_dsn
30.0
distinct_query
12.0
CRAP 27.3
1 package Jifty::DBI::Handle::Oracle;
2 use base qw/Jifty::DBI::Handle/;
3 use DBD::Oracle qw(:ora_types);
4  
5 use vars qw($VERSION $DBIHandle $DEBUG);
6  
7 =head1 NAME
8  
9 Jifty::DBI::Handle::Oracle - An oracle specific Handle object
10  
11 =head1 SYNOPSIS
12  
13 =head1 DESCRIPTION
14  
15 This module provides a subclass of L<Jifty::DBI::Handle> that
16 compensates for some of the idiosyncrasies of Oracle.
17  
18 =head1 METHODS
19  
20 =head2 connect PARAMHASH: Driver, Database, Host, User, Password
21  
22 Takes a paramhash and connects to your DBI datasource.
23  
24 =cut
25  
26 sub connect {
27 my $self = shift;
28  
29 my %args = (
30 driver => undef,
31 database => undef,
32 user => undef,
33 password => undef,
34 sid => undef,
35 host => undef,
36 @_
37 );
38  
39 $self->SUPER::connect(%args);
40  
41 $self->dbh->{LongTruncOk} = 1;
42 $self->dbh->{LongReadLen} = 8000;
43  
44 $self->simple_query(
45 "ALTER SESSION set NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'");
46  
47 return ($DBIHandle);
48 }
49  
50 =head2 insert
51  
52 Takes a table name as the first argument and assumes that the rest of
53 the arguments are an array of key-value pairs to be inserted.
54  
55 =cut
56  
57 sub insert {
58 my $self = shift;
59 my $table = shift;
60 my ($sth);
61  
62 # Oracle Hack to replace non-supported mysql_rowid call
63  
64 my %attribs = @_;
65 my ( $unique_id, $query_string );
66  
67 if ( $attribs{'Id'} || $attribs{'id'} ) {
68 $unique_id = ( $attribs{'Id'} ? $attribs{'Id'} : $attribs{'id'} );
69 } else {
70  
71 $query_string = "SELECT " . $table . "_seq.nextval FROM DUAL";
72  
73 $sth = $self->simple_query($query_string);
74 if ( !$sth ) {
75 if ($main::debug) {
76 die "Error with $query_string";
77 } else {
78 return (undef);
79 }
80 }
81  
82 #needs error checking
83 my @row = $sth->fetchrow_array;
84  
85 $unique_id = $row[0];
86  
87 }
88  
89 #TODO: don't hardcode this to id pull it from somewhere else
90 #call super::insert with the new column id.
91  
92 $attribs{'id'} = $unique_id;
93 delete $attribs{'Id'};
94 $sth = $self->SUPER::insert( $table, %attribs );
95  
96 unless ($sth) {
97 if ($main::debug) {
98 die "Error with $query_string: " . $self->dbh->errstr;
99 } else {
100 return (undef);
101 }
102 }
103  
104 $self->{'id'} = $unique_id;
105 return ( $self->{'id'} ); #Add Succeded. return the id
106 }
107  
108 =head2 build_dsn PARAMHASH
109  
110 Takes a bunch of parameters:
111  
112 Required: Driver, Database or Host/SID,
113 Optional: Port and RequireSSL
114  
115 Builds a dsn suitable for an Oracle DBI connection
116  
117 =cut
118  
119 sub build_dsn {
120 my $self = shift;
121 my %args = (
122 driver => undef,
123 database => undef,
124 host => undef,
125 port => undef,
126 sid => undef,
127 requiressl => undef,
128 @_
129 );
130  
131 my $dsn = "dbi:$args{'driver'}:";
132  
133 if ( defined $args{'host'}
134 && $args{'host'}
135 && defined $args{'sid'}
136 && $args{'sid'} )
137 {
138 $dsn .= "host=$args{'host'};sid=$args{'sid'}";
139 } else {
140 $dsn .= "$args{'database'}"
141 if ( defined $args{'database'} && $args{'database'} );
142 }
143 $dsn .= ";port=$args{'port'}"
144 if ( defined $args{'port'} && $args{'port'} );
145 $dsn .= ";requiressl=1"
146 if ( defined $args{'requiressl'} && $args{'requiressl'} );
147  
148 $self->{'dsn'} = $dsn;
149 }
150  
151 =head2 blob_params column_NAME column_type
152  
153 Returns a hash ref for the bind_param call to identify BLOB types used
154 by the current database for a particular column type. The current
155 Oracle implementation only supports ORA_CLOB types (112).
156  
157 =cut
158  
159 sub blob_params {
160 my $self = shift;
161 my $column = shift;
162  
163 # Don't assign to key 'value' as it is defined later.
164 return (
165 { ora_column => $column,
166 ora_type => ORA_CLOB,
167 }
168 );
169 }
170  
171 =head2 apply_limits STATEMENTREF ROWS_PER_PAGE FIRST_ROW
172  
173 takes an SQL SELECT statement and massages it to return ROWS_PER_PAGE
174 starting with FIRST_ROW;
175  
176 =cut
177  
178 sub apply_limits {
179 my $self = shift;
180 my $statementref = shift;
181 my $per_page = shift;
182 my $first = shift;
183  
184 # Transform an SQL query from:
185 #
186 # SELECT main.*
187 # FROM Tickets main
188 # WHERE ((main.EffectiveId = main.id))
189 # AND ((main.Type = 'ticket'))
190 # AND ( ( (main.Status = 'new')OR(main.Status = 'open') )
191 # AND ( (main.Queue = '1') ) )
192 #
193 # to:
194 #
195 # SELECT * FROM (
196 # SELECT limitquery.*,rownum limitrownum FROM (
197 # SELECT main.*
198 # FROM Tickets main
199 # WHERE ((main.EffectiveId = main.id))
200 # AND ((main.Type = 'ticket'))
201 # AND ( ( (main.Status = 'new')OR(main.Status = 'open') )
202 # AND ( (main.Queue = '1') ) )
203 # ) limitquery WHERE rownum <= 50
204 # ) WHERE limitrownum >= 1
205 #
206  
207 if ($per_page) {
208  
209 # Oracle orders from 1 not zero
210 $first++;
211  
212 # Make current query a sub select
213 $$statementref
214 = "SELECT * FROM ( SELECT limitquery.*,rownum limitrownum FROM ( $$statementref ) limitquery WHERE rownum <= "
215 . ( $first + $per_page - 1 )
216 . " ) WHERE limitrownum >= "
217 . $first;
218 }
219 }
220  
221 =head2 distinct_query STATEMENTREF
222  
223 takes an incomplete SQL SELECT statement and massages it to return a
224 DISTINCT result set.
225  
226 =cut
227  
228 sub distinct_query {
229 my $self = shift;
230 my $statementref = shift;
231 my $sb = shift;
232 my $table = $sb->Table;
233  
234 # Wrapp select query in a subselect as Oracle doesn't allow
235 # DISTINCT against CLOB/BLOB column types.
236 if ( $sb->_order_clause =~ /(?<!main)\./ ) {
237  
238 # If we are ordering by something not in 'main', we need to GROUP
239 # BY and adjust the ORDER_BY accordingly
240 local $sb->{group_by}
241 = [ @{ $sb->{group_by} || [] }, { column => 'id' } ];
242 local $sb->{order_by} = [
243 map {
244 ( $_->{alias} and $_->{alias} ne "main" )
245 ? { %{$_}, column => "min(" . $_->{column} . ")" }
246 : $_
247 } @{ $sb->{order_by} }
248 ];
249 my $group = $sb->_group_clause;
250 my $order = $sb->_order_clause;
251 $$statementref
252 = "SELECT main.* FROM ( SELECT main.id FROM $$statementref $group $order ) distinctquery, $table main WHERE (main.id = distinctquery.id)";
253 } else {
254 $$statementref
255 = "SELECT main.* FROM ( SELECT DISTINCT main.id FROM $$statementref ) distinctquery, $table main WHERE (main.id = distinctquery.id) ";
256 $$statementref .= $sb->_group_clause;
257 $$statementref .= $sb->_order_clause;
258 }
259 }
260  
261 1;
262  
263 __END__
264  
265 =head1 AUTHOR
266  
267 Jesse Vincent, jesse@fsck.com
268  
269 =head1 SEE ALSO
270  
271 L<Jifty::DBI>, L<Jifty::DBI::Handle>, L<DBD::Oracle>
272  
273 =cut