| 1 | =head1 NAME | ||
| 2 | |||
| 3 | DBIx::SQLEngine::Driver::Oracle - Support DBD::Oracle and DBD::ODBC/Oracle | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | B<DBI Wrapper>: Adds methods to a DBI database handle. | ||
| 8 | |||
| 9 | my $sqldb = DBIx::SQLEngine->new( 'dbi:oracle:test' ); | ||
| 10 | |||
| 11 | B<Portability Subclasses:> Uses driver's idioms or emulation. | ||
| 12 | |||
| 13 | $hash_ary = $sqldb->fetch_select( | ||
| 14 | table => 'students' | ||
| 15 | limit => 5, offset => 10 | ||
| 16 | ); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This package provides a subclass of DBIx::SQLEngine which compensates for Oracle's idiosyncrasies. | ||
| 21 | |||
| 22 | =head2 About Driver Subclasses | ||
| 23 | |||
| 24 | You do not need to use this package directly; when you connect to a database, the SQLEngine object is automatically re-blessed in to the appropriate subclass. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | package DBIx::SQLEngine::Driver::Oracle; | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use Carp; | ||
| 32 | |||
| 33 | ######################################################################## | ||
| 34 | |||
| 35 | ######################################################################## | ||
| 36 | |||
| 37 | =head1 FETCHING DATA (SQL DQL) | ||
| 38 | |||
| 39 | =head2 Methods Used By Complex Queries | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =item sql_limit() | ||
| 44 | |||
| 45 | Adds support for SQL select limit clause. | ||
| 46 | |||
| 47 | Implemented as a subselect with ROWNUM. | ||
| 48 | |||
| 49 | =back | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub sql_limit { | ||
| 54 | my $self = shift; | ||
| 55 | my ( $limit, $offset, $sql, @params ) = @_; | ||
| 56 | |||
| 57 | # remove tablealiases and group-functions from outer query properties | ||
| 58 | my ($properties) = ($sql =~ /^\s*SELECT\s(.*?)\sFROM\s/i); | ||
| 59 | $properties =~ s/[^\s]+\s+as\s+//ig; | ||
| 60 | $properties =~ s/DISTINCT//ig; | ||
| 61 | $properties =~ s/\w+\.//g; | ||
| 62 | |||
| 63 | $offset ||= 0; | ||
| 64 | my $position = ( $offset + $limit ); | ||
| 65 | |||
| 66 | $sql = "SELECT $properties FROM ( SELECT $properties, ROWNUM AS sqle_position FROM ( $sql ) WHERE ROWNUM <= $position ) WHERE sqle_position > $offset"; | ||
| 67 | |||
| 68 | return ($sql, @params); | ||
| 69 | } | ||
| 70 | |||
| 71 | ######################################################################## | ||
| 72 | |||
| 73 | ######################################################################## | ||
| 74 | |||
| 75 | =head1 EDITING DATA (SQL DML) | ||
| 76 | |||
| 77 | =head2 Insert to Add Data | ||
| 78 | |||
| 79 | =over 4 | ||
| 80 | |||
| 81 | =item do_insert_with_sequence() | ||
| 82 | |||
| 83 | $sqldb->do_insert_with_sequence( $sequence_name, %sql_clauses ) : $row_count | ||
| 84 | |||
| 85 | Implemented using _seq_do_insert_preinc and seq_increment. | ||
| 86 | |||
| 87 | =head2 seq_increment | ||
| 88 | |||
| 89 | $sqldb->seq_increment( $table, $field ) : $new_value | ||
| 90 | |||
| 91 | Increments the sequence, and returns the newly allocated value. | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | # $rows = $self->do_insert_with_sequence( $sequence, %clauses ); | ||
| 98 | sub do_insert_with_sequence { | ||
| 99 | (shift)->_seq_do_insert_preinc( @_ ) | ||
| 100 | } | ||
| 101 | |||
| 102 | # $current_id = $sqldb->seq_increment( $table, $field ); | ||
| 103 | sub seq_increment { | ||
| 104 | my ($self, $table, $field) = @_; | ||
| 105 | $self->fetch_one_value( | ||
| 106 | sql => "SELECT $field.NEXTVAL FROM DUAL')" | ||
| 107 | ); | ||
| 108 | } | ||
| 109 | |||
| 110 | ######################################################################## | ||
| 111 | |||
| 112 | ######################################################################## | ||
| 113 | |||
| 114 | =head1 DEFINING STRUCTURES (SQL DDL) | ||
| 115 | |||
| 116 | =head2 Detect Tables and Columns | ||
| 117 | |||
| 118 | =over 4 | ||
| 119 | |||
| 120 | =item sql_detect_table() | ||
| 121 | |||
| 122 | $sqldb->sql_detect_table ( $tablename ) : %sql_select_clauses | ||
| 123 | |||
| 124 | Implemented using Oracle's "select * from $tablename limit 1". | ||
| 125 | |||
| 126 | =back | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub sql_detect_table { | ||
| 131 | my ($self, $tablename) = @_; | ||
| 132 | return ( | ||
| 133 | table => $tablename, | ||
| 134 | criteria => '1 = 0', | ||
| 135 | limit => 1, | ||
| 136 | ) | ||
| 137 | } | ||
| 138 | |||
| 139 | ######################################################################## | ||
| 140 | |||
| 141 | =head2 Column Type Methods | ||
| 142 | |||
| 143 | The following methods are used by sql_create_table to specify column information in a DBMS-specific fashion. | ||
| 144 | |||
| 145 | =over 4 | ||
| 146 | |||
| 147 | =item dbms_create_column_types() | ||
| 148 | |||
| 149 | $sqldb->dbms_create_column_types () : %column_type_codes | ||
| 150 | |||
| 151 | Implemented using Oracle's blob and number types. | ||
| 152 | |||
| 153 | I<Portability:> Note that this capability is currently limited, and | ||
| 154 | additional steps need to be taken to manually define sequences in Oracle. | ||
| 155 | |||
| 156 | =item dbms_create_column_text_long_type() | ||
| 157 | |||
| 158 | $sqldb->dbms_create_column_text_long_type () : $col_type_str | ||
| 159 | |||
| 160 | Implemented using Oracle's clob type. | ||
| 161 | |||
| 162 | =back | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | sub dbms_create_column_types { | ||
| 167 | # sequences have to be defined extra manually with Oracle :-| | ||
| 168 | 'sequential' => 'number not null', | ||
| 169 | 'binary' => 'blob', | ||
| 170 | } | ||
| 171 | |||
| 172 | sub dbms_create_column_text_long_type { | ||
| 173 | 'clob' | ||
| 174 | } | ||
| 175 | |||
| 176 | ######################################################################## | ||
| 177 | |||
| 178 | ######################################################################## | ||
| 179 | |||
| 180 | =head1 ADVANCED CAPABILITIES | ||
| 181 | |||
| 182 | =head2 Call, Create and Drop Stored Procedures | ||
| 183 | |||
| 184 | Note: this feature has been added recently, and not yet tested in real-world conditions. | ||
| 185 | |||
| 186 | =over 4 | ||
| 187 | |||
| 188 | =item fetch_storedproc() | ||
| 189 | |||
| 190 | $sqldb->fetch_storedproc( $proc_name, @arguments ) : $rows | ||
| 191 | |||
| 192 | Not yet supported. | ||
| 193 | |||
| 194 | See L<DBD::Oracle/"Binding Cursors"> for more information. | ||
| 195 | |||
| 196 | =item do_storedproc() | ||
| 197 | |||
| 198 | $sqldb->do_storedproc( $proc_name, @arguments ) : $row_count | ||
| 199 | |||
| 200 | Calls do_sql with "execute procedure", the procedure name, and the arguments using placeholders. | ||
| 201 | |||
| 202 | =item create_storedproc() | ||
| 203 | |||
| 204 | $sqldb->create_storedproc( $proc_name, $definition ) | ||
| 205 | |||
| 206 | Calls do_sql with "create or replace procedure", the procedure name, and the definition. | ||
| 207 | |||
| 208 | =item drop_storedproc() | ||
| 209 | |||
| 210 | $sqldb->drop_storedproc( $proc_name ) | ||
| 211 | |||
| 212 | Calls do_sql with "drop procedure" and the procedure name. | ||
| 213 | |||
| 214 | =back | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub fetch_storedproc { | ||
| 219 | confess("Oracle fetch_storedproc: Not yet implemented") | ||
| 220 | } | ||
| 221 | sub do_storedproc { | ||
| 222 | (shift)->do_sql(join("\n", | ||
| 223 | "begin" . | ||
| 224 | (shift) . "(" . join(', ', ('?') x scalar(@_) ) . ")", | ||
| 225 | "end"), @_) | ||
| 226 | } | ||
| 227 | sub create_storedproc { | ||
| 228 | (shift)->do_sql(join("\n", | ||
| 229 | "create or replace procedure $_[0] is begin", @_, "end" ) ) | ||
| 230 | } | ||
| 231 | sub drop_storedproc { | ||
| 232 | (shift)->do_sql( "drop procedure $_[0]" ) | ||
| 233 | } | ||
| 234 | |||
| 235 | ######################################################################## | ||
| 236 | |||
| 237 | ######################################################################## | ||
| 238 | |||
| 239 | =head1 INTERNAL CONNECTION METHODS (DBI DBH) | ||
| 240 | |||
| 241 | =head2 Checking For Connection | ||
| 242 | |||
| 243 | =over 4 | ||
| 244 | |||
| 245 | =item sql_detect_any() | ||
| 246 | |||
| 247 | $sqldb->sql_detect_any : %sql_select_clauses | ||
| 248 | |||
| 249 | Implemented using Oracle's "select 1 from dual". | ||
| 250 | |||
| 251 | =back | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | sub sql_detect_any { | ||
| 256 | return ( sql => 'select 1 from dual' ) | ||
| 257 | } | ||
| 258 | |||
| 259 | ######################################################################## | ||
| 260 | |||
| 261 | =head2 Statement Error Handling | ||
| 262 | |||
| 263 | =over 4 | ||
| 264 | |||
| 265 | =item recoverable_query_exceptions() | ||
| 266 | |||
| 267 | $sqldb->recoverable_query_exceptions() : @common_error_messages | ||
| 268 | |||
| 269 | Provides a list of error messages which represent common | ||
| 270 | communication failures or other incidental errors. | ||
| 271 | |||
| 272 | =back | ||
| 273 | |||
| 274 | =cut | ||
| 275 | |||
| 276 | sub recoverable_query_exceptions { | ||
| 277 | 'ORA-03111', # ORA-03111 break received on communication channel | ||
| 278 | 'ORA-03113', # ORA-03113 end-of-file on communication channel | ||
| 279 | 'ORA-03114', # ORA-03114 not connected to ORACLE | ||
| 280 | } | ||
| 281 | |||
| 282 | ######################################################################## | ||
| 283 | |||
| 284 | ######################################################################## | ||
| 285 | |||
| 286 | =head1 SEE ALSO | ||
| 287 | |||
| 288 | See L<DBIx::SQLEngine> for the overall interface and developer documentation. | ||
| 289 | |||
| 290 | See L<DBIx::SQLEngine::Docs::ReadMe> for general information about | ||
| 291 | this distribution, including installation and license information. | ||
| 292 | |||
| 293 | =cut | ||
| 294 | |||
| 295 | ######################################################################## | ||
| 296 | |||
| 297 | 1; |