Commit | Line | Data |
c0e7b4e5 |
1 | package # hide from PAUSE |
2 | DBIx::Class::Storage::DBI::Cursor; |
28927b50 |
3 | |
4 | use base qw/DBIx::Class::Cursor/; |
5 | |
6 | use strict; |
7 | use warnings; |
8 | |
9 | sub new { |
cb5f2eea |
10 | my ($class, $storage, $args, $attrs) = @_; |
28927b50 |
11 | #use Data::Dumper; warn Dumper(@_); |
cb5f2eea |
12 | $class = ref $class if ref $class; |
28927b50 |
13 | my $new = { |
cb5f2eea |
14 | storage => $storage, |
28927b50 |
15 | args => $args, |
16 | pos => 0, |
1346e22d |
17 | attrs => $attrs, |
18 | pid => $$, |
19 | }; |
20 | |
21 | $new->{tid} = threads->tid if $INC{'threads.pm'}; |
22 | |
cb5f2eea |
23 | return bless ($new, $class); |
28927b50 |
24 | } |
25 | |
26 | sub next { |
27 | my ($self) = @_; |
1346e22d |
28 | |
29 | $self->_check_forks_threads; |
cb5f2eea |
30 | if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) { |
71e65b39 |
31 | $self->{sth}->finish if $self->{sth}->{Active}; |
cb5f2eea |
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]; |
5c91499f |
38 | if ($self->{attrs}{software_limit}) { |
39 | if (my $offset = $self->{attrs}{offset}) { |
cb5f2eea |
40 | $self->{sth}->fetch for 1 .. $offset; |
5c91499f |
41 | } |
42 | } |
28927b50 |
43 | } |
cb5f2eea |
44 | my @row = $self->{sth}->fetchrow_array; |
45 | if (@row) { |
46 | $self->{pos}++; |
47 | } else { |
48 | delete $self->{sth}; |
49 | $self->{done} = 1; |
50 | } |
28927b50 |
51 | return @row; |
52 | } |
53 | |
1a14aa3f |
54 | sub all { |
55 | my ($self) = @_; |
1346e22d |
56 | |
57 | $self->_check_forks_threads; |
1a14aa3f |
58 | return $self->SUPER::all if $self->{attrs}{rows}; |
cb5f2eea |
59 | $self->{sth}->finish if $self->{sth}->{Active}; |
60 | delete $self->{sth}; |
61 | my ($rv, $sth) = $self->{storage}->_select(@{$self->{args}}); |
1a14aa3f |
62 | return @{$sth->fetchall_arrayref}; |
63 | } |
64 | |
28927b50 |
65 | sub reset { |
66 | my ($self) = @_; |
1346e22d |
67 | |
68 | $self->_check_forks_threads; |
28927b50 |
69 | $self->{sth}->finish if $self->{sth}->{Active}; |
1346e22d |
70 | $self->_soft_reset; |
71 | } |
72 | |
73 | sub _soft_reset { |
74 | my ($self) = @_; |
75 | |
cb5f2eea |
76 | delete $self->{sth}; |
28927b50 |
77 | $self->{pos} = 0; |
cb5f2eea |
78 | delete $self->{done}; |
28927b50 |
79 | return $self; |
80 | } |
81 | |
1346e22d |
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 | |
28927b50 |
96 | sub DESTROY { |
97 | my ($self) = @_; |
1346e22d |
98 | |
99 | $self->_check_forks_threads; |
28927b50 |
100 | $self->{sth}->finish if $self->{sth}->{Active}; |
101 | } |
102 | |
103 | 1; |