take more care in mangling SELECT when applying subquery limits
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Oracle / Generic.pm
CommitLineData
18360aed 1package DBIx::Class::Storage::DBI::Oracle::Generic;
2
3use strict;
4use warnings;
5e782048 5use base qw/DBIx::Class::Storage::DBI/;
6use mro 'c3';
7use DBIx::Class::Carp;
b7b18f32 8use Scope::Guard ();
6298a324 9use Context::Preserve 'preserve_context';
ed7ab0f4 10use Try::Tiny;
00a28188 11use List::Util 'first';
fd323bf1 12use namespace::clean;
18360aed 13
6a247f33 14__PACKAGE__->sql_limit_dialect ('RowNum');
2b8cc2f2 15__PACKAGE__->sql_quote_char ('"');
5e782048 16__PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::Oracle');
17__PACKAGE__->datetime_parser_type('DateTime::Format::Oracle');
18
19sub __cache_queries_with_max_lob_parts { 2 }
6a247f33 20
7137528d 21=head1 NAME
22
7a84c41b 23DBIx::Class::Storage::DBI::Oracle::Generic - Oracle Support for DBIx::Class
7137528d 24
25=head1 SYNOPSIS
26
d88ecca6 27 # In your result (table) classes
28 use base 'DBIx::Class::Core';
2e46b6eb 29 __PACKAGE__->add_columns({ id => { sequence => 'mysequence', auto_nextval => 1 } });
7137528d 30 __PACKAGE__->set_primary_key('id');
7137528d 31
c0024355 32 # Somewhere in your Code
33 # add some data to a table with a hierarchical relationship
34 $schema->resultset('Person')->create ({
35 firstname => 'foo',
36 lastname => 'bar',
37 children => [
38 {
39 firstname => 'child1',
40 lastname => 'bar',
41 children => [
42 {
43 firstname => 'grandchild',
44 lastname => 'bar',
45 }
46 ],
47 },
48 {
49 firstname => 'child2',
50 lastname => 'bar',
51 },
52 ],
53 });
54
55 # select from the hierarchical relationship
56 my $rs = $schema->resultset('Person')->search({},
57 {
58 'start_with' => { 'firstname' => 'foo', 'lastname' => 'bar' },
e6600283 59 'connect_by' => { 'parentid' => { '-prior' => { -ident => 'personid' } },
25ca709b 60 'order_siblings_by' => { -asc => 'name' },
c0024355 61 };
62 );
63
64 # this will select the whole tree starting from person "foo bar", creating
65 # following query:
66 # SELECT
67 # me.persionid me.firstname, me.lastname, me.parentid
68 # FROM
69 # person me
70 # START WITH
71 # firstname = 'foo' and lastname = 'bar'
72 # CONNECT BY
e6600283 73 # parentid = prior personid
c0024355 74 # ORDER SIBLINGS BY
75 # firstname ASC
76
7137528d 77=head1 DESCRIPTION
78
6c0230de 79This class implements base Oracle support. The subclass
80L<DBIx::Class::Storage::DBI::Oracle::WhereJoins> is for C<(+)> joins in Oracle
86b23415 81versions before 9.0.
7137528d 82
83=head1 METHODS
84
85=cut
86
bf51641f 87sub _determine_supports_insert_returning {
88 my $self = shift;
89
90# TODO find out which version supports the RETURNING syntax
91# 8i has it and earlier docs are a 404 on oracle.com
92
93 return 1
94 if $self->_server_info->{normalized_dbms_version} >= 8.001;
95
96 return 0;
97}
98
99__PACKAGE__->_use_insert_returning_bound (1);
100
dd2600c6 101sub deployment_statements {
102 my $self = shift;;
103 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
104
105 $sqltargs ||= {};
032b2366 106 my $quote_char = $self->schema->storage->sql_maker->quote_char;
107 $sqltargs->{quote_table_names} = $quote_char ? 1 : 0;
108 $sqltargs->{quote_field_names} = $quote_char ? 1 : 0;
dd2600c6 109
96736321 110 if (
111 ! exists $sqltargs->{producer_args}{oracle_version}
112 and
113 my $dver = $self->_server_info->{dbms_version}
114 ) {
115 $sqltargs->{producer_args}{oracle_version} = $dver;
116 }
a4433d8e 117
38aead8e 118 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
dd2600c6 119}
120
18360aed 121sub _dbh_last_insert_id {
2e46b6eb 122 my ($self, $dbh, $source, @columns) = @_;
123 my @ids = ();
124 foreach my $col (@columns) {
125 my $seq = ($source->column_info($col)->{sequence} ||= $self->get_autoinc_seq($source,$col));
07cda1c5 126 my $id = $self->_sequence_fetch( 'CURRVAL', $seq );
2e46b6eb 127 push @ids, $id;
128 }
129 return @ids;
18360aed 130}
131
132sub _dbh_get_autoinc_seq {
133 my ($self, $dbh, $source, $col) = @_;
134
032b2366 135 my $sql_maker = $self->sql_maker;
07cda1c5 136 my ($ql, $qr) = map { $_ ? (quotemeta $_) : '' } $sql_maker->_quote_chars;
cb464582 137
e6dd7b42 138 my $source_name;
032b2366 139 if ( ref $source->name eq 'SCALAR' ) {
140 $source_name = ${$source->name};
07cda1c5 141
142 # the ALL_TRIGGERS match further on is case sensitive - thus uppercase
143 # stuff unless it is already quoted
144 $source_name = uc ($source_name) if $source_name !~ /\"/;
e6dd7b42 145 }
146 else {
032b2366 147 $source_name = $source->name;
07cda1c5 148 $source_name = uc($source_name) unless $ql;
e6dd7b42 149 }
38aead8e 150
032b2366 151 # trigger_body is a LONG
152 local $dbh->{LongReadLen} = 64 * 1024 if ($dbh->{LongReadLen} < 64 * 1024);
153
154 # disable default bindtype
155 local $sql_maker->{bindtype} = 'normal';
156
157 # look up the correct sequence automatically
07cda1c5 158 my ( $schema, $table ) = $source_name =~ /( (?:${ql})? \w+ (?:${qr})? ) \. ( (?:${ql})? \w+ (?:${qr})? )/x;
a6646e1b 159
160 # if no explicit schema was requested - use the default schema (which in the case of Oracle is the db user)
161 $schema ||= uc( ($self->_dbi_connect_info||[])->[1] || '');
162
032b2366 163 my ($sql, @bind) = $sql_maker->select (
164 'ALL_TRIGGERS',
07cda1c5 165 [qw/TRIGGER_BODY TABLE_OWNER TRIGGER_NAME/],
032b2366 166 {
07cda1c5 167 $schema ? (OWNER => $schema) : (),
168 TABLE_NAME => $table || $source_name,
169 TRIGGERING_EVENT => { -like => '%INSERT%' }, # this will also catch insert_or_update
170 TRIGGER_TYPE => { -like => '%BEFORE%' }, # we care only about 'before' triggers
171 STATUS => 'ENABLED',
032b2366 172 },
173 );
e6dd7b42 174
6f5f880d 175 # to find all the triggers that mention the column in question a simple
176 # regex grep since the trigger_body above is a LONG and hence not searchable
177 my @triggers = ( map
178 { my %inf; @inf{qw/body schema name/} = @$_; \%inf }
179 ( grep
07cda1c5 180 { $_->[0] =~ /\:new\.${ql}${col}${qr} | \:new\.$col/xi }
6f5f880d 181 @{ $dbh->selectall_arrayref( $sql, {}, @bind ) }
182 )
183 );
184
185 # extract all sequence names mentioned in each trigger
186 for (@triggers) {
187 $_->{sequences} = [ $_->{body} =~ / ( "? [\.\w\"\-]+ "? ) \. nextval /xig ];
188 }
189
190 my $chosen_trigger;
191
192 # if only one trigger matched things are easy
193 if (@triggers == 1) {
194
195 if ( @{$triggers[0]{sequences}} == 1 ) {
196 $chosen_trigger = $triggers[0];
197 }
198 else {
199 $self->throw_exception( sprintf (
200 "Unable to introspect trigger '%s' for column %s.%s (references multiple sequences). "
201 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
202 $triggers[0]{name},
203 $source_name,
204 $col,
205 $col,
206 ) );
207 }
208 }
209 # got more than one matching trigger - see if we can narrow it down
210 elsif (@triggers > 1) {
df6e3f5c 211
6f5f880d 212 my @candidates = grep
213 { $_->{body} =~ / into \s+ \:new\.$col /xi }
214 @triggers
215 ;
df6e3f5c 216
6f5f880d 217 if (@candidates == 1 && @{$candidates[0]{sequences}} == 1) {
218 $chosen_trigger = $candidates[0];
df6e3f5c 219 }
6f5f880d 220 else {
221 $self->throw_exception( sprintf (
222 "Unable to reliably select a BEFORE INSERT trigger for column %s.%s (possibilities: %s). "
223 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
224 $source_name,
225 $col,
226 ( join ', ', map { "'$_->{name}'" } @triggers ),
227 $col,
228 ) );
229 }
230 }
231
232 if ($chosen_trigger) {
233 my $seq_name = $chosen_trigger->{sequences}[0];
234
235 $seq_name = "$chosen_trigger->{schema}.$seq_name"
236 unless $seq_name =~ /\./;
df6e3f5c 237
07cda1c5 238 return \$seq_name if $seq_name =~ /\"/; # may already be quoted in-trigger
df6e3f5c 239 return $seq_name;
18360aed 240 }
6f5f880d 241
242 $self->throw_exception( sprintf (
243 "No suitable BEFORE INSERT triggers found for column %s.%s. "
244 . "You need to specify the correct 'sequence' explicitly in '%s's column_info.",
245 $source_name,
246 $col,
247 $col,
248 ));
18360aed 249}
250
2e46b6eb 251sub _sequence_fetch {
252 my ( $self, $type, $seq ) = @_;
07cda1c5 253
254 # use the maker to leverage quoting settings
255 my $sql_maker = $self->sql_maker;
256 my ($id) = $self->_get_dbh->selectrow_array ($sql_maker->select('DUAL', [ ref $seq ? \"$$seq.$type" : "$seq.$type" ] ) );
2e46b6eb 257 return $id;
258}
259
6dc4be0f 260sub _ping {
c2481821 261 my $self = shift;
7ba7a57d 262
6dc4be0f 263 my $dbh = $self->_dbh or return 0;
7ba7a57d 264
6dc4be0f 265 local $dbh->{RaiseError} = 1;
ecdf1ac8 266 local $dbh->{PrintError} = 0;
c2d7baef 267
52b420dd 268 return try {
ecdf1ac8 269 $dbh->do('select 1 from dual');
52b420dd 270 1;
ed7ab0f4 271 } catch {
52b420dd 272 0;
6dc4be0f 273 };
c2481821 274}
275
d789fa99 276sub _dbh_execute {
0e773352 277 my ($self, $dbh, $sql, @args) = @_;
d789fa99 278
87560ef9 279 my (@res, $tried);
cca282b6 280 my $want = wantarray;
4f661051 281 my $next = $self->next::can;
87560ef9 282 do {
52b420dd 283 try {
00a28188 284 my $exec = sub {
285 # Turn off sth caching for multi-part LOBs. See _prep_for_execute above.
286 local $self->{disable_sth_caching} = 1
287 if first {
288 ($_->[0]{_ora_lob_autosplit_part}||0)
289 > (__cache_queries_with_max_lob_parts-1)
290 } @{ $args[0] };
291
292 $self->$next($dbh, $sql, @args)
293 };
dd415de8 294
cca282b6 295 if (!defined $want) {
dd415de8 296 $exec->();
297 }
cca282b6 298 elsif (! $want) {
dd415de8 299 $res[0] = $exec->();
300 }
301 else {
302 @res = $exec->();
303 }
87560ef9 304
305 $tried++;
52b420dd 306 }
307 catch {
87560ef9 308 if (! $tried and $_ =~ /ORA-01003/) {
0f0abc97 309 # ORA-01003: no statement parsed (someone changed the table somehow,
310 # invalidating your cursor.)
0f0abc97 311 delete $dbh->{CachedKids}{$sql};
d789fa99 312 }
52b420dd 313 else {
314 $self->throw_exception($_);
315 }
316 };
87560ef9 317 } while (! $tried++);
d789fa99 318
cca282b6 319 return wantarray ? @res : $res[0];
d789fa99 320}
321
a5a27e7a 322sub _dbh_execute_array {
323 #my ($self, $sth, $tuple_status, @extra) = @_;
324
325 # DBD::Oracle warns loudly on partial execute_array failures
326 local $_[1]->{PrintWarn} = 0;
327
328 shift->next::method(@_);
329}
330
7137528d 331=head2 get_autoinc_seq
332
333Returns the sequence name for an autoincrement column
334
335=cut
336
18360aed 337sub get_autoinc_seq {
338 my ($self, $source, $col) = @_;
d4daee7b 339
373940e1 340 $self->dbh_do('_dbh_get_autoinc_seq', $source, $col);
18360aed 341}
342
8f7e044c 343=head2 datetime_parser_type
344
345This sets the proper DateTime::Format module for use with
346L<DBIx::Class::InflateColumn::DateTime>.
347
9900b569 348=head2 connect_call_datetime_setup
d2a3958e 349
350Used as:
351
9900b569 352 on_connect_call => 'datetime_setup'
d2a3958e 353
8384a713 354In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to set the session nls
355date, and timestamp values for use with L<DBIx::Class::InflateColumn::DateTime>
356and the necessary environment variables for L<DateTime::Format::Oracle>, which
357is used by it.
d2a3958e 358
82f6f45f 359Maximum allowable precision is used, unless the environment variables have
360already been set.
d2a3958e 361
9900b569 362These are the defaults used:
363
364 $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
365 $ENV{NLS_TIMESTAMP_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF';
366 $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
367
d9e53b85 368To get more than second precision with L<DBIx::Class::InflateColumn::DateTime>
369for your timestamps, use something like this:
370
371 use Time::HiRes 'time';
372 my $ts = DateTime->from_epoch(epoch => time);
373
d2a3958e 374=cut
375
9900b569 376sub connect_call_datetime_setup {
d2a3958e 377 my $self = shift;
d2a3958e 378
379 my $date_format = $ENV{NLS_DATE_FORMAT} ||= 'YYYY-MM-DD HH24:MI:SS';
380 my $timestamp_format = $ENV{NLS_TIMESTAMP_FORMAT} ||=
381 'YYYY-MM-DD HH24:MI:SS.FF';
382 my $timestamp_tz_format = $ENV{NLS_TIMESTAMP_TZ_FORMAT} ||=
383 'YYYY-MM-DD HH24:MI:SS.FF TZHTZM';
384
7a84c41b 385 $self->_do_query(
d7a58a29 386 "alter session set nls_date_format = '$date_format'"
387 );
7a84c41b 388 $self->_do_query(
d7a58a29 389 "alter session set nls_timestamp_format = '$timestamp_format'"
390 );
7a84c41b 391 $self->_do_query(
d7a58a29 392 "alter session set nls_timestamp_tz_format='$timestamp_tz_format'"
393 );
d2a3958e 394}
395
0e773352 396### Note originally by Ron "Quinn" Straight <quinnfazigu@gmail.org>
397### http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git;a=commitdiff;h=5db2758de644d53e07cd3e05f0e9037bf40116fc
398#
399# Handle LOB types in Oracle. Under a certain size (4k?), you can get away
400# with the driver assuming your input is the deprecated LONG type if you
401# encode it as a hex string. That ain't gonna fly at larger values, where
402# you'll discover you have to do what this does.
403#
404# This method had to be overridden because we need to set ora_field to the
405# actual column, and that isn't passed to the call (provided by Storage) to
406# bind_attribute_by_data_type.
407#
408# According to L<DBD::Oracle>, the ora_field isn't always necessary, but
409# adding it doesn't hurt, and will save your bacon if you're modifying a
410# table with more than one LOB column.
411#
412sub _dbi_attrs_for_bind {
413 my ($self, $ident, $bind) = @_;
00a28188 414
0e773352 415 my $attrs = $self->next::method($ident, $bind);
416
417 for my $i (0 .. $#$attrs) {
418 if (keys %{$attrs->[$i]||{}} and my $col = $bind->[$i][0]{dbic_colname}) {
419 $attrs->[$i]{ora_field} = $col;
420 }
421 }
5db2758d 422
0e773352 423 $attrs;
424}
5db2758d 425
0e773352 426my $dbd_loaded;
427sub bind_attribute_by_data_type {
428 my ($self, $dt) = @_;
429
430 $dbd_loaded ||= do {
431 require DBD::Oracle;
432 if ($DBD::Oracle::VERSION eq '1.23') {
433 $self->throw_exception(
434 "BLOB/CLOB support in DBD::Oracle == 1.23 is broken, use an earlier or later ".
435 "version.\n\nSee: https://rt.cpan.org/Public/Bug/Display.html?id=46016\n"
436 );
437 }
438 1;
439 };
5db2758d 440
0e773352 441 if ($self->_is_lob_type($dt)) {
442 return {
443 ora_type => $self->_is_text_lob_type($dt)
d7a58a29 444 ? DBD::Oracle::ORA_CLOB()
445 : DBD::Oracle::ORA_BLOB()
0e773352 446 };
d7a58a29 447 }
5db2758d 448}
449
00a28188 450# Handle blob columns in WHERE.
451#
452# For equality comparisons:
453#
454# We split data intended for comparing to a LOB into 2000 character chunks and
455# compare them using dbms_lob.substr on the LOB column.
456#
457# We turn off DBD::Oracle LOB binds for these partial LOB comparisons by passing
458# dbd_attrs => undef, because these are regular varchar2 comparisons and
459# otherwise the query will fail.
460#
461# Since the most common comparison size is likely to be under 4000 characters
462# (TEXT comparisons previously deployed to other RDBMSes) we disable
463# prepare_cached for queries with more than two part comparisons to a LOB
464# column. This is done in _dbh_execute (above) which was previously overridden
465# to gracefully recover from an Oracle error. This is to be careful to not
466# exhaust your application's open cursor limit.
467#
468# See:
469# http://itcareershift.com/blog1/2011/02/21/oracle-max-number-of-open-cursors-complete-reference-for-the-new-oracle-dba/
470# on the open_cursor limit.
471#
472# For everything else:
473#
474# We assume that everything that is not a LOB comparison, will most likely be a
475# LIKE query or some sort of function invocation. This may prove to be a naive
476# assumption in the future, but for now it should cover the two most likely
477# things users would want to do with a BLOB or CLOB, an equality test or a LIKE
478# query (on a CLOB.)
479#
480# For these expressions, the bind must NOT have the attributes of a LOB bind for
481# DBD::Oracle, otherwise the query will fail. This is done by passing
482# dbd_attrs => undef.
483
484sub _prep_for_execute {
485 my $self = shift;
486 my ($op) = @_;
487
488 my ($sql, $bind) = $self->next::method(@_);
489
5e782048 490 return ($sql, $bind) if $op eq 'insert';
00a28188 491
5e782048 492 my $blob_bind_index;
493 for (0 .. $#$bind) {
494 $blob_bind_index->{$_} = 1 if $self->_is_lob_type(
495 $bind->[$_][0]{sqlt_datatype}
496 );
497 }
00a28188 498
5e782048 499 return ($sql, $bind) unless $blob_bind_index;
00a28188 500
5e782048 501 my (@sql_parts, $new_sql, @new_binds);
502
503 if ($op eq 'select' || $op eq 'delete') {
504 @sql_parts = split /\?/, $sql;
505 }
506 elsif ($op eq 'update') {
507 $self->throw_exception('Update with complex WHERE clauses currently not supported')
508 if $sql =~ /\bWHERE\b .+ \bWHERE\b/xs;
509
510 my ($set_part, $where_part) = $sql =~ /^ (.+?) ( \bWHERE\b .+) /xs;
511
512 my $set_bind_count = $set_part =~ y/?//;
513 @new_binds = splice @$bind, 0, $set_bind_count;
514
515 @sql_parts = split /\?/, $where_part;
516 $new_sql = $set_part;
517 }
518 else {
519 $self->throw_exception("Unsupported \$op: $op");
520 }
521
522 my $col_equality_re = qr/ (?<=\s) ([\w."]+) (\s*=\s*) $/x;
523
524 for my $b_idx (0 .. $#$bind) {
525 my $bound = $bind->[$b_idx];
526
527 if ($blob_bind_index->{$b_idx}) {
528 if (my ($col, $eq) = $sql_parts[0] =~ $col_equality_re) {
00a28188 529 my $data = $bound->[1];
530
531 $data = "$data" if ref $data;
532
533 my @parts = unpack '(a2000)*', $data;
534
535 my @sql_frag;
536
537 for my $idx (0..$#parts) {
5e782048 538 push @sql_frag, sprintf (
539 'UTL_RAW.CAST_TO_VARCHAR2(RAWTOHEX(DBMS_LOB.SUBSTR(%s, 2000, %d))) = ?',
540 $col, ($idx*2000 + 1),
541 );
00a28188 542 }
543
544 my $sql_frag = '( ' . (join ' AND ', @sql_frag) . ' )';
545
5e782048 546 $sql_parts[0] =~ s/$col_equality_re/$sql_frag/;
00a28188 547
5e782048 548 $new_sql .= shift @sql_parts;
00a28188 549
550 for my $idx (0..$#parts) {
551 push @new_binds, [
552 {
553 %{ $bound->[0] },
554 _ora_lob_autosplit_part => $idx,
555 dbd_attrs => undef,
556 },
557 $parts[$idx]
558 ];
559 }
560 }
561 else {
5e782048 562 $new_sql .= shift(@sql_parts) . '?';
00a28188 563
564 push @new_binds, [
565 {
566 %{ $bound->[0] },
567 dbd_attrs => undef,
568 },
569 $bound->[1],
570 ];
571 }
572 }
573 else {
5e782048 574 $new_sql .= shift(@sql_parts) . '?';
00a28188 575 push @new_binds, $bound;
576 }
577 }
5e782048 578
579 if (@sql_parts > 1) {
580 carp "There are more placeholders than binds, this should not happen!";
581 @sql_parts = join ('?', @sql_parts);
582 }
583
584 $new_sql .= $sql_parts[0];
00a28188 585
586 return ($new_sql, \@new_binds);
587}
588
589# Savepoints stuff.
590
90d7422f 591sub _exec_svp_begin {
d7a58a29 592 my ($self, $name) = @_;
90d7422f 593 $self->_dbh->do("SAVEPOINT $name");
1816be4f 594}
595
281719d2 596# Oracle automatically releases a savepoint when you start another one with the
597# same name.
90d7422f 598sub _exec_svp_release { 1 }
281719d2 599
90d7422f 600sub _exec_svp_rollback {
d7a58a29 601 my ($self, $name) = @_;
90d7422f 602 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name")
281719d2 603}
604
6c0230de 605=head2 relname_to_table_alias
606
607L<DBIx::Class> uses L<DBIx::Class::Relationship> names as table aliases in
608queries.
609
610Unfortunately, Oracle doesn't support identifiers over 30 chars in length, so
af0edca1 611the L<DBIx::Class::Relationship> name is shortened and appended with half of an
612MD5 hash.
6c0230de 613
614See L<DBIx::Class::Storage/"relname_to_table_alias">.
615
616=cut
617
618sub relname_to_table_alias {
619 my $self = shift;
620 my ($relname, $join_count) = @_;
621
622 my $alias = $self->next::method(@_);
623
19c4cc62 624 # we need to shorten here in addition to the shortening in SQLA itself,
625 # since the final relnames are a crucial for the join optimizer
626 return $self->sql_maker->_shorten_identifier($alias);
6c0230de 627}
628
6c0bb6a7 629=head2 with_deferred_fk_checks
630
631Runs a coderef between:
632
633 alter session set constraints = deferred
634 ...
635 alter session set constraints = immediate
636
b7b18f32 637to defer foreign key checks.
638
639Constraints must be declared C<DEFERRABLE> for this to work.
6c0bb6a7 640
641=cut
642
643sub with_deferred_fk_checks {
644 my ($self, $sub) = @_;
b7b18f32 645
646 my $txn_scope_guard = $self->txn_scope_guard;
647
6c0bb6a7 648 $self->_do_query('alter session set constraints = deferred');
54161a15 649
b7b18f32 650 my $sg = Scope::Guard->new(sub {
651 $self->_do_query('alter session set constraints = immediate');
652 });
281719d2 653
6298a324 654 return
655 preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
281719d2 656}
657
c0024355 658=head1 ATTRIBUTES
659
660Following additional attributes can be used in resultsets.
661
6b2fbbf0 662=head2 connect_by or connect_by_nocycle
c0024355 663
664=over 4
665
666=item Value: \%connect_by
667
668=back
669
670A hashref of conditions used to specify the relationship between parent rows
671and child rows of the hierarchy.
672
6b2fbbf0 673
c0024355 674 connect_by => { parentid => 'prior personid' }
675
676 # adds a connect by statement to the query:
677 # SELECT
678 # me.persionid me.firstname, me.lastname, me.parentid
679 # FROM
680 # person me
681 # CONNECT BY
682 # parentid = prior persionid
6b2fbbf0 683
c0024355 684
6b2fbbf0 685 connect_by_nocycle => { parentid => 'prior personid' }
2ba03b16 686
6b2fbbf0 687 # adds a connect by statement to the query:
688 # SELECT
689 # me.persionid me.firstname, me.lastname, me.parentid
690 # FROM
691 # person me
692 # CONNECT BY NOCYCLE
693 # parentid = prior persionid
2ba03b16 694
695
c0024355 696=head2 start_with
697
698=over 4
699
700=item Value: \%condition
701
702=back
703
704A hashref of conditions which specify the root row(s) of the hierarchy.
705
706It uses the same syntax as L<DBIx::Class::ResultSet/search>
707
708 start_with => { firstname => 'Foo', lastname => 'Bar' }
709
710 # SELECT
711 # me.persionid me.firstname, me.lastname, me.parentid
712 # FROM
713 # person me
714 # START WITH
715 # firstname = 'foo' and lastname = 'bar'
716 # CONNECT BY
717 # parentid = prior persionid
718
719=head2 order_siblings_by
720
721=over 4
722
723=item Value: ($order_siblings_by | \@order_siblings_by)
724
725=back
726
727Which column(s) to order the siblings by.
728
729It uses the same syntax as L<DBIx::Class::ResultSet/order_by>
730
731 'order_siblings_by' => 'firstname ASC'
732
733 # SELECT
734 # me.persionid me.firstname, me.lastname, me.parentid
735 # FROM
736 # person me
737 # CONNECT BY
738 # parentid = prior persionid
739 # ORDER SIBLINGS BY
740 # firstname ASC
741
7a84c41b 742=head1 AUTHOR
18360aed 743
00a28188 744See L<DBIx::Class/AUTHOR> and L<DBIx::Class/CONTRIBUTORS>.
18360aed 745
746=head1 LICENSE
747
748You may distribute this code under the same terms as Perl itself.
749
750=cut
7137528d 751
7521;
00a28188 753# vim:sts=2 sw=2: