| 1 | package Jifty::DBI::Handle::Oracle; | ||
| 2 | use base qw/Jifty::DBI::Handle/; | ||
| 3 | use DBD::Oracle qw(:ora_types ORA_OCI); | ||
| 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 database_version | ||
| 51 | |||
| 52 | Returns value of ORA_OCI constant, see L<DBD::Oracle/Constants>. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | sub database_version { | ||
| 57 | return '' . ORA_OCI; | ||
| 58 | } | ||
| 59 | |||
| 60 | =head2 insert | ||
| 61 | |||
| 62 | Takes a table name as the first argument and assumes that the rest of | ||
| 63 | the arguments are an array of key-value pairs to be inserted. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub insert { | ||
| 68 | my $self = shift; | ||
| 69 | my $table = shift; | ||
| 70 | my ($sth); | ||
| 71 | |||
| 72 | # Oracle Hack to replace non-supported mysql_rowid call | ||
| 73 | |||
| 74 | my %attribs = @_; | ||
| 75 | my ( $unique_id, $query_string ); | ||
| 76 | |||
| 77 | if ( $attribs{'Id'} || $attribs{'id'} ) { | ||
| 78 | $unique_id = ( $attribs{'Id'} ? $attribs{'Id'} : $attribs{'id'} ); | ||
| 79 | } else { | ||
| 80 | |||
| 81 | $query_string = "SELECT " . $table . "_seq.nextval FROM DUAL"; | ||
| 82 | |||
| 83 | $sth = $self->simple_query($query_string); | ||
| 84 | if ( !$sth ) { | ||
| 85 | if ($main::debug) { | ||
| 86 | die "Error with $query_string"; | ||
| 87 | } else { | ||
| 88 | return (undef); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | #needs error checking | ||
| 93 | my @row = $sth->fetchrow_array; | ||
| 94 | |||
| 95 | $unique_id = $row[0]; | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | #TODO: don't hardcode this to id pull it from somewhere else | ||
| 100 | #call super::insert with the new column id. | ||
| 101 | |||
| 102 | $attribs{'id'} = $unique_id; | ||
| 103 | delete $attribs{'Id'}; | ||
| 104 | $sth = $self->SUPER::insert( $table, %attribs ); | ||
| 105 | |||
| 106 | unless ($sth) { | ||
| 107 | if ($main::debug) { | ||
| 108 | die "Error with $query_string: " . $self->dbh->errstr; | ||
| 109 | } else { | ||
| 110 | return (undef); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | $self->{'id'} = $unique_id; | ||
| 115 | return ( $self->{'id'} ); #Add Succeded. return the id | ||
| 116 | } | ||
| 117 | |||
| 118 | =head2 build_dsn PARAMHASH | ||
| 119 | |||
| 120 | Takes a bunch of parameters: | ||
| 121 | |||
| 122 | Required: Driver, Database or Host/SID, | ||
| 123 | Optional: Port and RequireSSL | ||
| 124 | |||
| 125 | Builds a dsn suitable for an Oracle DBI connection | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | sub build_dsn { | ||
| 130 | my $self = shift; | ||
| 131 | my %args = ( | ||
| 132 | driver => undef, | ||
| 133 | database => undef, | ||
| 134 | host => undef, | ||
| 135 | port => undef, | ||
| 136 | sid => undef, | ||
| 137 | requiressl => undef, | ||
| 138 | @_ | ||
| 139 | ); | ||
| 140 | |||
| 141 | my $dsn = "dbi:$args{'driver'}:"; | ||
| 142 | |||
| 143 | if ( defined $args{'host'} | ||
| 144 | && $args{'host'} | ||
| 145 | && defined $args{'sid'} | ||
| 146 | && $args{'sid'} ) | ||
| 147 | { | ||
| 148 | $dsn .= "host=$args{'host'};sid=$args{'sid'}"; | ||
| 149 | } else { | ||
| 150 | $dsn .= "$args{'database'}" | ||
| 151 | if ( defined $args{'database'} && $args{'database'} ); | ||
| 152 | } | ||
| 153 | $dsn .= ";port=$args{'port'}" | ||
| 154 | if ( defined $args{'port'} && $args{'port'} ); | ||
| 155 | $dsn .= ";requiressl=1" | ||
| 156 | if ( defined $args{'requiressl'} && $args{'requiressl'} ); | ||
| 157 | |||
| 158 | $self->{'dsn'} = $dsn; | ||
| 159 | } | ||
| 160 | |||
| 161 | =head2 blob_params column_NAME column_type | ||
| 162 | |||
| 163 | Returns a hash ref for the bind_param call to identify BLOB types used | ||
| 164 | by the current database for a particular column type. The current | ||
| 165 | Oracle implementation only supports ORA_CLOB types (112). | ||
| 166 | |||
| 167 | =cut | ||
| 168 | |||
| 169 | sub blob_params { | ||
| 170 | my $self = shift; | ||
| 171 | my $column = shift; | ||
| 172 | |||
| 173 | # Don't assign to key 'value' as it is defined later. | ||
| 174 | return ( | ||
| 175 | { ora_column => $column, | ||
| 176 | ora_type => ORA_CLOB, | ||
| 177 | } | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 apply_limits STATEMENTREF ROWS_PER_PAGE FIRST_ROW | ||
| 182 | |||
| 183 | takes an SQL SELECT statement and massages it to return ROWS_PER_PAGE | ||
| 184 | starting with FIRST_ROW; | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | sub apply_limits { | ||
| 189 | my $self = shift; | ||
| 190 | my $statementref = shift; | ||
| 191 | my $per_page = shift; | ||
| 192 | my $first = shift; | ||
| 193 | |||
| 194 | # Transform an SQL query from: | ||
| 195 | # | ||
| 196 | # SELECT main.* | ||
| 197 | # FROM Tickets main | ||
| 198 | # WHERE ((main.EffectiveId = main.id)) | ||
| 199 | # AND ((main.Type = 'ticket')) | ||
| 200 | # AND ( ( (main.Status = 'new')OR(main.Status = 'open') ) | ||
| 201 | # AND ( (main.Queue = '1') ) ) | ||
| 202 | # | ||
| 203 | # to: | ||
| 204 | # | ||
| 205 | # SELECT * FROM ( | ||
| 206 | # SELECT limitquery.*,rownum limitrownum FROM ( | ||
| 207 | # SELECT main.* | ||
| 208 | # FROM Tickets main | ||
| 209 | # WHERE ((main.EffectiveId = main.id)) | ||
| 210 | # AND ((main.Type = 'ticket')) | ||
| 211 | # AND ( ( (main.Status = 'new')OR(main.Status = 'open') ) | ||
| 212 | # AND ( (main.Queue = '1') ) ) | ||
| 213 | # ) limitquery WHERE rownum <= 50 | ||
| 214 | # ) WHERE limitrownum >= 1 | ||
| 215 | # | ||
| 216 | |||
| 217 | if ($per_page) { | ||
| 218 | |||
| 219 | # Oracle orders from 1 not zero | ||
| 220 | $first++; | ||
| 221 | |||
| 222 | # Make current query a sub select | ||
| 223 | $$statementref | ||
| 224 | = "SELECT * FROM ( SELECT limitquery.*,rownum limitrownum FROM ( $$statementref ) limitquery WHERE rownum <= " | ||
| 225 | . ( $first + $per_page - 1 ) | ||
| 226 | . " ) WHERE limitrownum >= " | ||
| 227 | . $first; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | =head2 distinct_query STATEMENTREF | ||
| 232 | |||
| 233 | takes an incomplete SQL SELECT statement and massages it to return a | ||
| 234 | DISTINCT result set. | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | sub distinct_query { | ||
| 239 | my $self = shift; | ||
| 240 | my $statementref = shift; | ||
| 241 | my $collection = shift; | ||
| 242 | my $table = $collection->Table; | ||
| 243 | |||
| 244 | # Wrapp select query in a subselect as Oracle doesn't allow | ||
| 245 | # DISTINCT against CLOB/BLOB column types. | ||
| 246 | if ( $collection->_order_clause =~ /(?<!main)\./ ) { | ||
| 247 | |||
| 248 | # If we are ordering by something not in 'main', we need to GROUP | ||
| 249 | # BY and adjust the ORDER_BY accordingly | ||
| 250 | local $collection->{group_by} | ||
| 251 | = [ @{ $collection->{group_by} || [] }, { column => 'id' } ]; | ||
| 252 | local $collection->{order_by} = [ | ||
| 253 | map { | ||
| 254 | my $alias = $_->{alias} || ''; | ||
| 255 | my $column = $_->{column}; | ||
| 256 | if ($column =~ /\W/) { | ||
| 257 | warn "Possible SQL injection in column '$column' in order_by\n"; | ||
| 258 | next; | ||
| 259 | } | ||
| 260 | $alias .= '.' if $alias; | ||
| 261 | |||
| 262 | ( ( !$alias or $alias eq 'main.' ) and $column eq 'id' ) | ||
| 263 | ? $_ | ||
| 264 | : { %{$_}, column => undef, function => "min($alias$column)" } | ||
| 265 | } @{ $collection->{order_by} } | ||
| 266 | ]; | ||
| 267 | my $group = $collection->_group_clause; | ||
| 268 | my $order = $collection->_order_clause; | ||
| 269 | $$statementref | ||
| 270 | = "SELECT " | ||
| 271 | . $collection->query_columns | ||
| 272 | . " FROM ( SELECT main.id FROM $$statementref $group $order ) distinctquery, $table main WHERE (main.id = distinctquery.id)"; | ||
| 273 | } else { | ||
| 274 | $$statementref | ||
| 275 | = "SELECT " | ||
| 276 | . $collection->query_columns | ||
| 277 | . " FROM ( SELECT DISTINCT main.id FROM $$statementref ) distinctquery, $table main WHERE (main.id = distinctquery.id) "; | ||
| 278 | $$statementref .= $collection->_group_clause; | ||
| 279 | $$statementref .= $collection->_order_clause; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | 1; | ||
| 284 | |||
| 285 | __END__ | ||
| 286 | |||
| 287 | =head1 AUTHOR | ||
| 288 | |||
| 289 | Jesse Vincent, jesse@fsck.com | ||
| 290 | |||
| 291 | =head1 SEE ALSO | ||
| 292 | |||
| 293 | L<Jifty::DBI>, L<Jifty::DBI::Handle>, L<DBD::Oracle> | ||
| 294 | |||
| 295 | =cut |