Newer Older
313 lines | 6.038kb
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 {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
13
    my $self = shift;
14
    
15
    $self->{filters} ||= {};
16
    $self->{filter}  ||= {};
packaging one directory
yuki-kimoto authored on 2009-11-16
17
    
18
    # Fetch
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
19
    my @row = $self->{sth}->fetchrow_array;
packaging one directory
yuki-kimoto authored on 2009-11-16
20
    
21
    # Cannot fetch
update document
yuki-kimoto authored on 2010-05-27
22
    return unless @row;
many many changes
yuki-kimoto authored on 2010-04-30
23

            
packaging one directory
yuki-kimoto authored on 2009-11-16
24
    # Filter
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
25
    for (my $i = 0; $i < @{$self->{sth}->{NAME_lc}}; $i++) {
update document
yuki-kimoto authored on 2010-05-27
26
        
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
27
        # Filter name
28
        my $column = $self->{sth}->{NAME_lc}->[$i];
29
        my $fname  = exists $self->{filter}->{$column}
30
                   ? $self->{filter}->{$column}
31
                   : $self->{default_filter};
some changed
yuki-kimoto authored on 2010-05-02
32
        
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
33
        # Filter
34
        $row[$i] = $self->{filters}->{$fname}->($row[$i])
35
          if $fname;
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
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
60
    croak 'Row count must be specified'
removed reconnect method
yuki-kimoto authored on 2010-05-28
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 {
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
89
    my $self = shift;
90
    
91
    $self->{filters} ||= {};
92
    $self->{filter}  ||= {};
packaging one directory
yuki-kimoto authored on 2009-11-16
93
    
94
    # Fetch
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
95
    my $row = $self->{sth}->fetchrow_arrayref;
packaging one directory
yuki-kimoto authored on 2009-11-16
96
    
97
    # Cannot fetch
98
    return unless $row;
99
    
100
    # Filter
101
    my $row_hash = {};
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
102
    for (my $i = 0; $i < @{$self->{sth}->{NAME_lc}}; $i++) {
update document
yuki-kimoto authored on 2010-05-27
103
        
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
104
        # Filter name
105
        my $column = $self->{sth}->{NAME_lc}->[$i];
106
        my $fname  = exists $self->{filter}->{$column}
107
                   ? $self->{filter}->{$column}
108
                   : $self->{default_filter};
add query filter error check
yuki-kimoto authored on 2010-05-14
109
        
changed argument of tag proc...
yuki-kimoto authored on 2010-08-03
110
        # Filter
111
        $row_hash->{$column}
112
          = $fname ? $self->{filters}->{$fname}->($row->[$i]) 
113
                   : $row->[$i];
packaging one directory
yuki-kimoto authored on 2009-11-16
114
    }
115
    
removed reconnect method
yuki-kimoto authored on 2010-05-28
116
    return $row_hash;
packaging one directory
yuki-kimoto authored on 2009-11-16
117
}
118

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

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

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

            
update document
yuki-kimoto authored on 2010-01-30
167
1;
168

            
packaging one directory
yuki-kimoto authored on 2009-11-16
169
=head1 NAME
170

            
cleanup
yuki-kimoto authored on 2010-08-03
171
DBIx::Custom::Result - Result of select
packaging one directory
yuki-kimoto authored on 2009-11-16
172

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

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

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

            
cleanup
yuki-kimoto authored on 2010-08-03
220
    my $sth = $reuslt->sth
version 0.0901
yuki-kimoto authored on 2009-12-17
221
    $result = $result->sth($sth);
many many changes
yuki-kimoto authored on 2010-04-30
222

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

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

            
cleanup
yuki-kimoto authored on 2010-08-03
227
    my $default_filter = $result->default_filter;
228
    $result            = $result->default_filter('decode_utf8');
many many changes
yuki-kimoto authored on 2010-04-30
229

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

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

            
cleanup
yuki-kimoto authored on 2010-08-03
234
    my $filter = $result->filter;
removed reconnect method
yuki-kimoto authored on 2010-05-28
235
    $result = $result->filter({title => 'decode_utf8'});
packaging one directory
yuki-kimoto authored on 2009-11-16
236

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

            
update document
yuki-kimoto authored on 2010-01-30
239
=head1 METHODS
240

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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