| 1 | package DBIO::Oracle::Storage; | ||
| 2 | # ABSTRACT: Oracle Support for DBIO | ||
| 3 | |||
| 4 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | # Compose cross-cutting Storage behaviour via ISA (not Exporter import), so the | ||
| 8 | # role methods live in the MRO and their next::method calls resolve. Roles that | ||
| 9 | # override a DBIO::Storage::DBI method (LOBSupport::_prep_for_execute, | ||
| 10 | # Savepoints::_dbh_execute_for_fetch) come BEFORE DBIO::Storage::DBI so the | ||
| 11 | # override wins and chains forward to the base. | ||
| 12 | 4 | ▶ | use base qw/ |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | DBIO::Oracle::Storage::LOBSupport | ||
| 14 | DBIO::Oracle::Storage::AutoIncrement | ||
| 15 | DBIO::Oracle::Storage::Savepoints | ||
| 16 | DBIO::Oracle::Storage::FKDeferral | ||
| 17 | DBIO::Oracle::Storage::ConnectSetup | ||
| 18 | DBIO::Storage::DBI | ||
| 19 | 4 | /; | |
| 20 | use mro 'c3'; | ||
| 21 | |||
| 22 | __PACKAGE__->register_driver('Oracle' => __PACKAGE__); | ||
| 23 | |||
| 24 | use DBIO::Carp; | ||
| 25 | use Try::Tiny; | ||
| 26 | use namespace::clean; | ||
| 27 | |||
| 28 | __PACKAGE__->sql_quote_char ('"'); | ||
| 29 | __PACKAGE__->sql_maker_class('DBIO::Oracle::SQLMaker'); | ||
| 30 | __PACKAGE__->datetime_parser_type('DateTime::Format::Oracle'); | ||
| 31 | |||
| 32 | use DBIO::Oracle::Type (); | ||
| 33 | |||
| 34 | sub __cache_queries_with_max_lob_parts { 2 } | ||
| 35 | |||
| 36 | sub dbio_deploy_class { 'DBIO::Oracle::Deploy' } | ||
| 37 | sub deploy_setup { } | ||
| 38 | |||
| 39 | |||
| 40 | sub _determine_supports_insert_returning { | ||
| 41 | my $self = shift; | ||
| 42 | # TODO find out which version supports the RETURNING syntax | ||
| 43 | # 8i has it and earlier docs are a 404 on oracle.com | ||
| 44 | return 1 if $self->_server_info->{normalized_dbms_version} >= 8.001; | ||
| 45 | return 0; | ||
| 46 | } | ||
| 47 | |||
| 48 | __PACKAGE__->_use_insert_returning_bound (1); | ||
| 49 | |||
| 50 | sub deployment_statements { | ||
| 51 | my $self = shift;; | ||
| 52 | my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; | ||
| 53 | $sqltargs ||= {}; | ||
| 54 | if (! exists $sqltargs->{producer_args}{oracle_version} | ||
| 55 | and my $dver = $self->_server_info->{dbms_version}) { | ||
| 56 | $sqltargs->{producer_args}{oracle_version} = $dver; | ||
| 57 | } | ||
| 58 | $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _ping { | ||
| 62 | my $self = shift; | ||
| 63 | my $dbh = $self->_dbh or return 0; | ||
| 64 | local $dbh->{RaiseError} = 1; | ||
| 65 | local $dbh->{PrintError} = 0; | ||
| 66 | return try { | ||
| 67 | $dbh->do('select 1 from dual'); | ||
| 68 | 1; | ||
| 69 | } catch { 0 }; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _dbh_execute { | ||
| 73 | my ($self, $sql, $bind) = @_[0,2,3]; | ||
| 74 | |||
| 75 | # Turn off sth caching for multi-part LOBs. See _prep_for_execute below | ||
| 76 | local $self->{disable_sth_caching} = 1 if grep { | ||
| 77 | ($_->[0]{_ora_lob_autosplit_part}||0) > (__cache_queries_with_max_lob_parts - 1) | ||
| 78 | } @$bind; | ||
| 79 | |||
| 80 | my $next = $self->next::can; | ||
| 81 | |||
| 82 | # if we are already in a txn we can't retry anything | ||
| 83 | return shift->$next(@_) if $self->transaction_depth; | ||
| 84 | |||
| 85 | local $self->{_in_do_block}; | ||
| 86 | |||
| 87 | return DBIO::Storage::BlockRunner->new( | ||
| 88 | storage => $self, | ||
| 89 | wrap_txn => 0, | ||
| 90 | retry_handler => sub { | ||
| 91 | if ($_[0]->failed_attempt_count == 1 | ||
| 92 | and $_[0]->last_exception =~ /ORA-01003/ | ||
| 93 | and my $dbh = $_[0]->storage->_dbh) { | ||
| 94 | delete $dbh->{CachedKids}{$sql}; | ||
| 95 | return 1; | ||
| 96 | } | ||
| 97 | return 0; | ||
| 98 | }, | ||
| 99 | )->run($next, @_); | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | sub relname_to_table_alias { | ||
| 104 | my $self = shift; | ||
| 105 | my ($relname, $join_count) = @_; | ||
| 106 | my $alias = $self->next::method(@_); | ||
| 107 | return $self->sql_maker->_shorten_identifier($alias); | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | # Internal helpers for LOBSupport role (called by the composed subs). | ||
| 112 | # The LOB-type predicates live in DBIO::Oracle::Type as the single source; | ||
| 113 | # these thin wrappers preserve the $self->_is_lob_type calling convention. | ||
| 114 | sub _is_lob_type { DBIO::Oracle::Type::is_lob_type(@_) } | ||
| 115 | sub _is_text_lob_type { DBIO::Oracle::Type::is_text_lob_type(@_) } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =encoding UTF-8 | ||
| 124 | |||
| 125 | =head1 NAME | ||
| 126 | |||
| 127 | DBIO::Oracle::Storage - Oracle Support for DBIO | ||
| 128 | |||
| 129 | =head1 VERSION | ||
| 130 | |||
| 131 | version 0.900001 | ||
| 132 | |||
| 133 | =head1 SYNOPSIS | ||
| 134 | |||
| 135 | # In your result (table) classes | ||
| 136 | use base 'DBIO::Core'; | ||
| 137 | __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } }); | ||
| 138 | __PACKAGE__->set_primary_key('id'); | ||
| 139 | |||
| 140 | # Somewhere in your Code | ||
| 141 | # add some data to a table with a hierarchical relationship | ||
| 142 | $schema->resultset('Person')->create ({ | ||
| 143 | firstname => 'foo', | ||
| 144 | lastname => 'bar', | ||
| 145 | children => [ | ||
| 146 | { | ||
| 147 | firstname => 'child1', | ||
| 148 | lastname => 'bar', | ||
| 149 | children => [ | ||
| 150 | { | ||
| 151 | firstname => 'grandchild', | ||
| 152 | lastname => 'bar', | ||
| 153 | } | ||
| 154 | ], | ||
| 155 | }, | ||
| 156 | { | ||
| 157 | firstname => 'child2', | ||
| 158 | lastname => 'bar', | ||
| 159 | }, | ||
| 160 | ], | ||
| 161 | }); | ||
| 162 | |||
| 163 | # select from the hierarchical relationship | ||
| 164 | my $rs = $schema->resultset('Person')->search({}, | ||
| 165 | { | ||
| 166 | 'start_with' => { 'firstname' => 'foo', 'lastname' => 'bar' }, | ||
| 167 | 'connect_by' => { 'parentid' => { '-prior' => { -ident => 'personid' } }, | ||
| 168 | 'order_siblings_by' => { -asc => 'name' }, | ||
| 169 | }; | ||
| 170 | ); | ||
| 171 | |||
| 172 | # this will select the whole tree starting from person "foo bar", creating | ||
| 173 | # following query: | ||
| 174 | # SELECT | ||
| 175 | # me.persionid me.firstname, me.lastname, me.parentid | ||
| 176 | # FROM | ||
| 177 | # person me | ||
| 178 | # START WITH | ||
| 179 | # firstname = 'foo' and lastname = 'bar' | ||
| 180 | # CONNECT BY | ||
| 181 | # parentid = prior personid | ||
| 182 | # ORDER SIBLINGS BY | ||
| 183 | # firstname ASC | ||
| 184 | |||
| 185 | =head1 DESCRIPTION | ||
| 186 | |||
| 187 | This class implements base Oracle support. The subclass | ||
| 188 | L<DBIO::Oracle::Storage::WhereJoins> is for C<(+)> joins in Oracle | ||
| 189 | versions before 9.0. | ||
| 190 | |||
| 191 | =head1 METHODS | ||
| 192 | |||
| 193 | =head2 relname_to_table_alias | ||
| 194 | |||
| 195 | L<DBIO> uses L<DBIO::Relationship> names as table aliases in queries. | ||
| 196 | |||
| 197 | Unfortunately, Oracle doesn't support identifiers over 30 chars in length, so | ||
| 198 | the L<DBIO::Relationship> name is shortened and appended with half of an | ||
| 199 | MD5 hash. | ||
| 200 | |||
| 201 | See L<DBIO::Storage::DBI/relname_to_table_alias>. | ||
| 202 | |||
| 203 | =head2 connect_by or connect_by_nocycle | ||
| 204 | |||
| 205 | =over 4 | ||
| 206 | |||
| 207 | =item Value: \%connect_by | ||
| 208 | |||
| 209 | =back | ||
| 210 | |||
| 211 | A hashref of conditions used to specify the relationship between parent rows | ||
| 212 | and child rows of the hierarchy. | ||
| 213 | |||
| 214 | connect_by => { parentid => 'prior personid' } | ||
| 215 | |||
| 216 | # adds a connect by statement to the query: | ||
| 217 | # SELECT | ||
| 218 | # me.persionid me.firstname, me.lastname, me.parentid | ||
| 219 | # FROM | ||
| 220 | # person me | ||
| 221 | # CONNECT BY | ||
| 222 | # parentid = prior persionid | ||
| 223 | |||
| 224 | |||
| 225 | connect_by_nocycle => { parentid => 'prior personid' } | ||
| 226 | |||
| 227 | # adds a connect by statement to the query: | ||
| 228 | # SELECT | ||
| 229 | # me.persionid me.firstname, me.lastname, me.parentid | ||
| 230 | # FROM | ||
| 231 | # person me | ||
| 232 | # CONNECT BY NOCYCLE | ||
| 233 | # parentid = prior persionid | ||
| 234 | |||
| 235 | =head2 start_with | ||
| 236 | |||
| 237 | =over 4 | ||
| 238 | |||
| 239 | =item Value: \%condition | ||
| 240 | |||
| 241 | =back | ||
| 242 | |||
| 243 | A hashref of conditions which specify the root row(s) of the hierarchy. | ||
| 244 | |||
| 245 | It uses the same syntax as L<DBIO::ResultSet/search> | ||
| 246 | |||
| 247 | start_with => { firstname => 'Foo', lastname => 'Bar' } | ||
| 248 | |||
| 249 | # SELECT | ||
| 250 | # me.persionid me.firstname, me.lastname, me.parentid | ||
| 251 | # FROM | ||
| 252 | # person me | ||
| 253 | # START WITH | ||
| 254 | # firstname = 'foo' and lastname = 'bar' | ||
| 255 | # CONNECT BY | ||
| 256 | # parentid = prior persionid | ||
| 257 | |||
| 258 | =head2 order_siblings_by | ||
| 259 | |||
| 260 | =over 4 | ||
| 261 | |||
| 262 | =item Value: ($order_siblings_by | \@order_siblings_by) | ||
| 263 | |||
| 264 | =back | ||
| 265 | |||
| 266 | Which column(s) to order the siblings by. | ||
| 267 | |||
| 268 | It uses the same syntax as L<DBIO::ResultSet/order_by> | ||
| 269 | |||
| 270 | 'order_siblings_by' => 'firstname ASC' | ||
| 271 | |||
| 272 | # SELECT | ||
| 273 | # me.persionid me.firstname, me.lastname, me.parentid | ||
| 274 | # FROM | ||
| 275 | # person me | ||
| 276 | # CONNECT BY | ||
| 277 | # parentid = prior persionid | ||
| 278 | # ORDER SIBLINGS BY | ||
| 279 | # firstname ASC | ||
| 280 | |||
| 281 | =head1 ATTRIBUTES | ||
| 282 | |||
| 283 | Following additional attributes can be used in resultsets. | ||
| 284 | |||
| 285 | =head1 SEE ALSO | ||
| 286 | |||
| 287 | =over | ||
| 288 | |||
| 289 | =item * L<DBIO::Oracle> - Oracle schema component | ||
| 290 | |||
| 291 | =item * L<DBIO::Oracle::SQLMaker> - Oracle SQL dialect | ||
| 292 | |||
| 293 | =item * L<DBIO::Oracle::Storage::WhereJoins> - WHERE-clause join support for Oracle E<lt> 9 | ||
| 294 | |||
| 295 | =item * L<DBIO::Storage::DBI> - Base DBI storage class | ||
| 296 | |||
| 297 | =back | ||
| 298 | |||
| 299 | =head1 AUTHOR | ||
| 300 | |||
| 301 | DBIO & DBIx::Class Authors | ||
| 302 | |||
| 303 | =head1 COPYRIGHT AND LICENSE | ||
| 304 | |||
| 305 | Copyright (C) 2026 DBIO Authors | ||
| 306 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||
| 307 | Based on DBIx::Class, heavily modified. | ||
| 308 | |||
| 309 | This is free software; you can redistribute it and/or modify it under | ||
| 310 | the same terms as the Perl 5 programming language system itself. | ||
| 311 | |||
| 312 | =cut | ||