Newer Older
311 lines | 6.143kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
package DBIx::Custom::Result;
update document
yuki-kimoto authored on 2009-11-17
2

            
packaging one directory
yuki-kimoto authored on 2009-11-16
3
use strict;
4
use warnings;
update document
yuki-kimoto authored on 2010-01-30
5

            
6
use base 'Object::Simple';
cleanup
yuki-kimoto authored on 2010-02-11
7

            
packaging one directory
yuki-kimoto authored on 2009-11-16
8
use Carp 'croak';
9

            
many many changes
yuki-kimoto authored on 2010-04-30
10
__PACKAGE__->attr([qw/sth filters default_filter filter/]);
cleanup
yuki-kimoto authored on 2010-01-21
11

            
packaging one directory
yuki-kimoto authored on 2009-11-16
12
sub fetch {
update document
yuki-kimoto authored on 2010-05-27
13

            
14
    $_[0]->{filters} ||= {};
15
    $_[0]->{filter}  ||= {};
packaging one directory
yuki-kimoto authored on 2009-11-16
16
    
17
    # Fetch
update document
yuki-kimoto authored on 2010-05-27
18
    my @row = $_[0]->{sth}->fetchrow_array;
packaging one directory
yuki-kimoto authored on 2009-11-16
19
    
20
    # Cannot fetch
update document
yuki-kimoto authored on 2010-05-27
21
    return unless @row;
many many changes
yuki-kimoto authored on 2010-04-30
22

            
packaging one directory
yuki-kimoto authored on 2009-11-16
23
    # Filter
update document
yuki-kimoto authored on 2010-05-27
24
    for (my $i = 0; $i < @{$_[0]->{sth}->{NAME_lc}}; $i++) {
25
        my $fname  = $_[0]->{filter}->{$_[0]->{sth}->{NAME_lc}->[$i]} 
26
                  || $_[0]->{default_filter};
27
        
28
        croak "Filter \"$fname\" is not registered."
29
          if $fname && ! exists $_[0]->{filters}->{$fname};
some changed
yuki-kimoto authored on 2010-05-02
30
        
update document
yuki-kimoto authored on 2010-05-27
31
        next unless $fname;
32
        
33
        $row[$i] = ref $fname
34
                   ? $fname->($row[$i]) 
35
                   : $_[0]->{filters}->{$fname}->($row[$i]);
packaging one directory
yuki-kimoto authored on 2009-11-16
36
    }
many many changes
yuki-kimoto authored on 2010-04-30
37

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
38
    return \@row;
39
}
40

            
41
sub fetch_first {
42
    my $self = shift;
43
    
44
    # Fetch
45
    my $row = $self->fetch;
46
    
47
    # Not exist
48
    return unless $row;
49
    
50
    # Finish statement handle
51
    $self->sth->finish;
52
    
53
    return $row;
54
}
55

            
56
sub fetch_multi {
57
    my ($self, $count) = @_;
58
    
59
    # Not specified Row count
60
    croak("Row count must be specified")
61
      unless $count;
62
    
63
    # Fetch multi rows
64
    my $rows = [];
65
    for (my $i = 0; $i < $count; $i++) {
66
        my $row = $self->fetch;
67
        
68
        last unless $row;
69
        
70
        push @$rows, $row;
71
    }
72
    
73
    return unless @$rows;
74
    return $rows;
75
}
76

            
77
sub fetch_all {
78
    my $self = shift;
79
    
80
    # Fetch all rows
81
    my $rows = [];
82
    while(my $row = $self->fetch) {
83
        push @$rows, $row;
84
    }
85
    return $rows;
packaging one directory
yuki-kimoto authored on 2009-11-16
86
}
87

            
88
sub fetch_hash {
many many changes
yuki-kimoto authored on 2010-04-30
89

            
update document
yuki-kimoto authored on 2010-05-27
90
    $_[0]->{filters} ||= {};
91
    $_[0]->{filter}  ||= {};
packaging one directory
yuki-kimoto authored on 2009-11-16
92
    
93
    # Fetch
update document
yuki-kimoto authored on 2010-05-27
94
    my $row = $_[0]->{sth}->fetchrow_arrayref;
packaging one directory
yuki-kimoto authored on 2009-11-16
95
    
96
    # Cannot fetch
97
    return unless $row;
98
    
99
    # Filter
100
    my $row_hash = {};
update document
yuki-kimoto authored on 2010-05-27
101
    for (my $i = 0; $i < @{$_[0]->{sth}->{NAME_lc}}; $i++) {
102
        
103
        my $fname  = $_[0]->{filter}->{$_[0]->{sth}->{NAME_lc}->[$i]}
104
                  || $_[0]->{default_filter};
add query filter error check
yuki-kimoto authored on 2010-05-14
105
        
update document
yuki-kimoto authored on 2010-05-27
106
        croak "Filter \"$fname\" is not registered."
107
          if $fname && ! exists $_[0]->{filters}->{$fname};
some changed
yuki-kimoto authored on 2010-05-02
108
        
update document
yuki-kimoto authored on 2010-05-27
109
        $row_hash->{$_[0]->{sth}->{NAME_lc}->[$i]}
110
          = !$fname    ? $row->[$i] :
111
            ref $fname ? $fname->($row->[$i]) :
112
            $_[0]->{filters}->{$fname}->($row->[$i]);
packaging one directory
yuki-kimoto authored on 2009-11-16
113
    }
114
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
115
    return $row_hash;
packaging one directory
yuki-kimoto authored on 2009-11-16
116
}
117

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
118
sub fetch_hash_first {
packaging one directory
yuki-kimoto authored on 2009-11-16
119
    my $self = shift;
120
    
121
    # Fetch hash
122
    my $row = $self->fetch_hash;
123
    
124
    # Not exist
125
    return unless $row;
126
    
127
    # Finish statement handle
some changed
yuki-kimoto authored on 2010-05-02
128
    $self->sth->finish;
packaging one directory
yuki-kimoto authored on 2009-11-16
129
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
130
    return $row;
packaging one directory
yuki-kimoto authored on 2009-11-16
131
}
132

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
133
sub fetch_hash_multi {
packaging one directory
yuki-kimoto authored on 2009-11-16
134
    my ($self, $count) = @_;
135
    
136
    # Not specified Row count
137
    croak("Row count must be specified")
138
      unless $count;
139
    
140
    # Fetch multi rows
141
    my $rows = [];
142
    for (my $i = 0; $i < $count; $i++) {
removed reconnect method
yuki-kimoto authored on 2010-05-28
143
        my $row = $self->fetch_hash;
packaging one directory
yuki-kimoto authored on 2009-11-16
144
        
removed reconnect method
yuki-kimoto authored on 2010-05-28
145
        last unless $row;
packaging one directory
yuki-kimoto authored on 2009-11-16
146
        
removed reconnect method
yuki-kimoto authored on 2010-05-28
147
        push @$rows, $row;
packaging one directory
yuki-kimoto authored on 2009-11-16
148
    }
149
    
150
    return unless @$rows;
removed reconnect method
yuki-kimoto authored on 2010-05-28
151
    return $rows;
packaging one directory
yuki-kimoto authored on 2009-11-16
152
}
153

            
154
sub fetch_hash_all {
155
    my $self = shift;
156
    
update document
yuki-kimoto authored on 2010-01-30
157
    # Fetch all rows as hash
packaging one directory
yuki-kimoto authored on 2009-11-16
158
    my $rows = [];
removed reconnect method
yuki-kimoto authored on 2010-05-28
159
    while(my $row = $self->fetch_hash) {
160
        push @$rows, $row;
packaging one directory
yuki-kimoto authored on 2009-11-16
161
    }
removed reconnect method
yuki-kimoto authored on 2010-05-28
162
    return $rows;
packaging one directory
yuki-kimoto authored on 2009-11-16
163
}
164

            
update document
yuki-kimoto authored on 2010-01-30
165
1;
166

            
packaging one directory
yuki-kimoto authored on 2009-11-16
167
=head1 NAME
168

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
169
DBIx::Custom::Result - Manipulate the result of select statement
packaging one directory
yuki-kimoto authored on 2009-11-16
170

            
update document
yuki-kimoto authored on 2010-01-30
171
=head1 SYNOPSIS
packaging one directory
yuki-kimoto authored on 2009-11-16
172
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
173
    # Result
174
    my $result = $dbi->select(table => 'books');
175
    
176
    # Fetch a row into array
177
    while (my $row = $result->fetch) {
178
        my $value1 = $row->[0];
179
        my $valuu2 = $row->[1];
180
        
181
        # do something
version 0.0901
yuki-kimoto authored on 2009-12-17
182
    }
183
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
184
    # Fetch only first row into array
185
    my $row = $result->fetch_first;
186
    
187
    # Fetch multiple rows into array of array
188
    while (my $rows = $result->fetch_multi(5)) {
189
        # do something
190
    }
191
    
192
    # Fetch all rows into array of array
193
    my $rows = $result->fetch_all;
194
    
195
    # Fetch hash into hash
196
    while (my $row = $result->fetch_hash) {
197
        my $value1 = $row->{title};
198
        my $value2 = $row->{author};
199
        
200
        # do something
packaging one directory
yuki-kimoto authored on 2009-11-16
201
    }
removed reconnect method
yuki-kimoto authored on 2010-05-28
202
    
203
    # Fetch only first row into hash
204
    my $row = $result->fetch_hash_first;
205
    
206
    # Fetch multiple rows into array of hash
207
    while (my $rows = $result->fetch_hash_multi) {
208
        # do something
209
    }
210
    
211
    # Fetch all rows into array of hash
212
    my $rows = $result->fetch_hash_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
213

            
update document
yuki-kimoto authored on 2010-01-30
214
=head1 ATTRIBUTES
packaging one directory
yuki-kimoto authored on 2009-11-16
215

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
216
=head2 C<sth>
packaging one directory
yuki-kimoto authored on 2009-11-16
217

            
version 0.0901
yuki-kimoto authored on 2009-12-17
218
    $result = $result->sth($sth);
219
    $sth    = $reuslt->sth
many many changes
yuki-kimoto authored on 2010-04-30
220

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
221
Statement handle.
222

            
223
=head2 C<default_filter>
many many changes
yuki-kimoto authored on 2010-04-30
224

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
225
    $result         = $result->default_filter('decode_utf8');
many many changes
yuki-kimoto authored on 2010-04-30
226
    $default_filter = $result->default_filter;
227

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
228
Default filter for fetching.
packaging one directory
yuki-kimoto authored on 2009-11-16
229

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
230
=head2 C<filter>
update document
yuki-kimoto authored on 2009-11-19
231

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
232
    $result = $result->filter({title => 'decode_utf8'});
233
    $filter = $result->filter;
packaging one directory
yuki-kimoto authored on 2009-11-16
234

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
235
Filters for fetching.
236

            
update document
yuki-kimoto authored on 2010-01-30
237
=head1 METHODS
238

            
239
This class is L<Object::Simple> subclass.
240
You can use all methods of L<Object::Simple>
packaging one directory
yuki-kimoto authored on 2009-11-16
241

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
242
=head2 C<fetch>
packaging one directory
yuki-kimoto authored on 2009-11-16
243

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
244
    $row = $result->fetch;
version 0.0901
yuki-kimoto authored on 2009-12-17
245

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
246
Fetch a row into array
packaging one directory
yuki-kimoto authored on 2009-11-16
247

            
248
    while (my $row = $result->fetch) {
249
        # do something
removed reconnect method
yuki-kimoto authored on 2010-05-28
250
        my $value1 = $row->[0];
251
        my $value2 = $row->[1];
packaging one directory
yuki-kimoto authored on 2009-11-16
252
    }
253

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
254
=head2 C<fetch_first>
update document
yuki-kimoto authored on 2009-11-19
255

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
256
    $row = $result->fetch_first;
packaging one directory
yuki-kimoto authored on 2009-11-16
257

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
258
Fetch only first row into array and finish statment handle.
packaging one directory
yuki-kimoto authored on 2009-11-16
259

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
260
=head2 C<fetch_multi>
update document
yuki-kimoto authored on 2009-11-19
261

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
262
    $rows = $result->fetch_multi($count);
packaging one directory
yuki-kimoto authored on 2009-11-16
263
    
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
264
Fetch multiple rows into array of array.
version 0.0901
yuki-kimoto authored on 2009-12-17
265

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
266
    while(my $rows = $result->fetch_multi(10)) {
update document
yuki-kimoto authored on 2009-11-19
267
        # do someting
268
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
269

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
270
=head2 C<fetch_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
271

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
272
    $rows = $result->fetch_all;
version 0.0901
yuki-kimoto authored on 2009-12-17
273

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
274
Fetch all rows into array of array.
packaging one directory
yuki-kimoto authored on 2009-11-16
275

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
276
=head2 C<fetch_hash>
packaging one directory
yuki-kimoto authored on 2009-11-16
277

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
278
    $row = $result->fetch_hash;
packaging one directory
yuki-kimoto authored on 2009-11-16
279

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
280
Fetch a row into hash
update document
yuki-kimoto authored on 2009-11-19
281

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
282
    while (my $row = $result->fetch_hash) {
283
        my $val1 = $row->{title};
284
        my $val2 = $row->{author};
285
        
286
        # do something
287
    }
version 0.0901
yuki-kimoto authored on 2009-12-17
288

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
289
=head2 C<fetch_hash_first>
removed reconnect method
yuki-kimoto authored on 2010-05-28
290
    
291
    $row = $result->fetch_hash_first;
packaging one directory
yuki-kimoto authored on 2009-11-16
292

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
293
Fetch only first row into hash and finish statment handle.
packaging one directory
yuki-kimoto authored on 2009-11-16
294

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
295
=head2 C<fetch_hash_multi>
update document
yuki-kimoto authored on 2009-11-19
296

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
297
    $rows = $result->fetch_hash_multi($count);
update document
yuki-kimoto authored on 2009-11-19
298
    
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
299
Fetch multiple rows into array of hash
packaging one directory
yuki-kimoto authored on 2009-11-16
300

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
301
    while(my $rows = $result->fetch_hash_multi(10)) {
302
        # do someting
303
    }
update document
yuki-kimoto authored on 2009-11-19
304

            
removed DBIx::Custom commit ...
yuki-kimoto authored on 2010-07-14
305
=head2 C<fetch_hash_all>
packaging one directory
yuki-kimoto authored on 2009-11-16
306

            
removed reconnect method
yuki-kimoto authored on 2010-05-28
307
    $rows = $result->fetch_hash_all;
packaging one directory
yuki-kimoto authored on 2009-11-16
308

            
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
309
Fetch all rows into array of hash.
310

            
packaging one directory
yuki-kimoto authored on 2009-11-16
311
=cut