fork tests/code improved, ithreads tests/code added, version bumped
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Cursor.pm
1 package # hide from PAUSE 
2     DBIx::Class::Storage::DBI::Cursor;
3
4 use base qw/DBIx::Class::Cursor/;
5
6 use strict;
7 use warnings;
8
9 sub new {
10   my ($class, $storage, $args, $attrs) = @_;
11   #use Data::Dumper; warn Dumper(@_);
12   $class = ref $class if ref $class;
13   my $new = {
14     storage => $storage,
15     args => $args,
16     pos => 0,
17     attrs => $attrs,
18     pid => $$,
19   };
20
21   $new->{tid} = threads->tid if $INC{'threads.pm'};
22   
23   return bless ($new, $class);
24 }
25
26 sub next {
27   my ($self) = @_;
28
29   $self->_check_forks_threads;
30   if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
31     $self->{sth}->finish if $self->{sth}->{Active};
32     delete $self->{sth};
33     $self->{done} = 1;
34   }
35   return if $self->{done};
36   unless ($self->{sth}) {
37     $self->{sth} = ($self->{storage}->_select(@{$self->{args}}))[1];
38     if ($self->{attrs}{software_limit}) {
39       if (my $offset = $self->{attrs}{offset}) {
40         $self->{sth}->fetch for 1 .. $offset;
41       }
42     }
43   }
44   my @row = $self->{sth}->fetchrow_array;
45   if (@row) {
46     $self->{pos}++;
47   } else {
48     delete $self->{sth};
49     $self->{done} = 1;
50   }
51   return @row;
52 }
53
54 sub all {
55   my ($self) = @_;
56
57   $self->_check_forks_threads;
58   return $self->SUPER::all if $self->{attrs}{rows};
59   $self->{sth}->finish if $self->{sth}->{Active};
60   delete $self->{sth};
61   my ($rv, $sth) = $self->{storage}->_select(@{$self->{args}});
62   return @{$sth->fetchall_arrayref};
63 }
64
65 sub reset {
66   my ($self) = @_;
67
68   $self->_check_forks_threads;
69   $self->{sth}->finish if $self->{sth}->{Active};
70   $self->_soft_reset;
71 }
72
73 sub _soft_reset {
74   my ($self) = @_;
75
76   delete $self->{sth};
77   $self->{pos} = 0;
78   delete $self->{done};
79   return $self;
80 }
81
82 sub _check_forks_threads {
83   my ($self) = @_;
84
85   if($INC{'threads.pm'} && $self->{tid} != threads->tid) {
86       $self->_soft_reset;
87       $self->{tid} = threads->tid;
88   }
89
90   if($self->{pid} != $$) {
91       $self->_soft_reset;
92       $self->{pid} = $$;
93   }
94 }
95
96 sub DESTROY {
97   my ($self) = @_;
98
99   $self->_check_forks_threads;
100   $self->{sth}->finish if $self->{sth}->{Active};
101 }
102
103 1;