Newer Older
386 lines | 8.063kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
package DBIx::Custom::Result;
2
use Object::Simple;
update document
yuki-kimoto authored on 2009-11-17
3

            
packaging one directory
yuki-kimoto authored on 2009-11-16
4
use strict;
5
use warnings;
6
use Carp 'croak';
7

            
8
# Attributes
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
9
sub _dbi             : Attr {}
packaging one directory
yuki-kimoto authored on 2009-11-16
10
sub sth              : Attr {}
11
sub fetch_filter     : Attr {}
update document
yuki-kimoto authored on 2009-11-19
12

            
13
sub no_fetch_filters : Attr { type => 'array', trigger => sub {
packaging one directory
yuki-kimoto authored on 2009-11-16
14
    my $self = shift;
15
    my $no_fetch_filters = $self->no_fetch_filters || [];
16
    my %no_fetch_filters_map = map {$_ => 1} @{$no_fetch_filters};
17
    $self->_no_fetch_filters_map(\%no_fetch_filters_map);
18
}}
update document
yuki-kimoto authored on 2009-11-19
19

            
packaging one directory
yuki-kimoto authored on 2009-11-16
20
sub _no_fetch_filters_map : Attr {default => sub { {} }}
21

            
22
# Fetch (array)
23
sub fetch {
24
    my ($self, $type) = @_;
25
    my $sth = $self->sth;
26
    my $fetch_filter = $self->fetch_filter;
27
    
28
    # Fetch
29
    my $row = $sth->fetchrow_arrayref;
30
    
31
    # Cannot fetch
32
    return unless $row;
33
    
34
    # Filter
35
    if ($fetch_filter) {
36
        my $keys  = $sth->{NAME_lc};
37
        my $types = $sth->{TYPE};
38
        for (my $i = 0; $i < @$keys; $i++) {
39
            next if $self->_no_fetch_filters_map->{$keys->[$i]};
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
40
            $row->[$i]= $fetch_filter->($row->[$i], $keys->[$i], $self->_dbi,
41
                                        {type => $types->[$i], sth => $sth, index => $i});
packaging one directory
yuki-kimoto authored on 2009-11-16
42
        }
43
    }
44
    return wantarray ? @$row : $row;
45
}
46

            
47
# Fetch (hash)
48
sub fetch_hash {
49
    my $self = shift;
50
    my $sth = $self->sth;
51
    my $fetch_filter = $self->fetch_filter;
52
    
53
    # Fetch
54
    my $row = $sth->fetchrow_arrayref;
55
    
56
    # Cannot fetch
57
    return unless $row;
58
    
59
    # Keys
60
    my $keys  = $sth->{NAME_lc};
61
    
62
    # Filter
63
    my $row_hash = {};
64
    if ($fetch_filter) {
65
        my $types = $sth->{TYPE};
66
        for (my $i = 0; $i < @$keys; $i++) {
67
            if ($self->_no_fetch_filters_map->{$keys->[$i]}) {
68
                $row_hash->{$keys->[$i]} = $row->[$i];
69
            }
70
            else {
71
                $row_hash->{$keys->[$i]}
bind_filter argument is chan...
yuki-kimoto authored on 2009-11-19
72
                  = $fetch_filter->($row->[$i], $keys->[$i], $self->_dbi,
73
                                    {type => $types->[$i], sth => $sth, index => $i});
packaging one directory
yuki-kimoto authored on 2009-11-16
74
            }
75
        }
76
    }
77
    
78
    # No filter
79
    else {
80
        for (my $i = 0; $i < @$keys; $i++) {
81
            $row_hash->{$keys->[$i]} = $row->[$i];
82
        }
83
    }
84
    return wantarray ? %$row_hash : $row_hash;
85
}
86

            
87
# Fetch only first (array)
88
sub fetch_first {
89
    my $self = shift;
90
    
91
    # Fetch
92
    my $row = $self->fetch;
93
    
94
    # Not exist
95
    return unless $row;
96
    
97
    # Finish statement handle
98
    $self->finish;
99
    
100
    return wantarray ? @$row : $row;
101
}
102

            
103
# Fetch only first (hash)
104
sub fetch_hash_first {
105
    my $self = shift;
106
    
107
    # Fetch hash
108
    my $row = $self->fetch_hash;
109
    
110
    # Not exist
111
    return unless $row;
112
    
113
    # Finish statement handle
114
    $self->finish;
115
    
116
    return wantarray ? %$row : $row;
117
}
118

            
119
# Fetch multi rows (array)
120
sub fetch_rows {
121
    my ($self, $count) = @_;
122
    
123
    # Not specified Row count
124
    croak("Row count must be specified")
125
      unless $count;
126
    
127
    # Fetch multi rows
128
    my $rows = [];
129
    for (my $i = 0; $i < $count; $i++) {
130
        my @row = $self->fetch;
131
        
132
        last unless @row;
133
        
134
        push @$rows, \@row;
135
    }
136
    
137
    return unless @$rows;
138
    return wantarray ? @$rows : $rows;
139
}
140

            
141
# Fetch multi rows (hash)
142
sub fetch_hash_rows {
143
    my ($self, $count) = @_;
144
    
145
    # Not specified Row count
146
    croak("Row count must be specified")
147
      unless $count;
148
    
149
    # Fetch multi rows
150
    my $rows = [];
151
    for (my $i = 0; $i < $count; $i++) {
152
        my %row = $self->fetch_hash;
153
        
154
        last unless %row;
155
        
156
        push @$rows, \%row;
157
    }
158
    
159
    return unless @$rows;
160
    return wantarray ? @$rows : $rows;
161
}
162

            
163

            
164
# Fetch all (array)
165
sub fetch_all {
166
    my $self = shift;
167
    
168
    my $rows = [];
169
    while(my @row = $self->fetch) {
170
        push @$rows, [@row];
171
    }
172
    return wantarray ? @$rows : $rows;
173
}
174

            
175
# Fetch all (hash)
176
sub fetch_hash_all {
177
    my $self = shift;
178
    
179
    my $rows = [];
180
    while(my %row = $self->fetch_hash) {
181
        push @$rows, {%row};
182
    }
183
    return wantarray ? @$rows : $rows;
184
}
185

            
186
# Finish
187
sub finish { shift->sth->finish }
188

            
189
# Error
190
sub error { 
191
    my $self = shift;
192
    my $sth  = $self->sth;
193
    return wantarray ? ($sth->errstr, $sth->err, $sth->state) : $sth->errstr;
194
}
195

            
196
Object::Simple->build_class;
197

            
198
=head1 NAME
199

            
update document
yuki-kimoto authored on 2009-11-17
200
DBIx::Custom::Result - DBIx::Custom Resultset
packaging one directory
yuki-kimoto authored on 2009-11-16
201

            
update document
yuki-kimoto authored on 2009-11-19
202
=head1 Synopsis
packaging one directory
yuki-kimoto authored on 2009-11-16
203

            
version 0.0901
yuki-kimoto authored on 2009-12-17
204
    my $result = $dbi->query($query);
packaging one directory
yuki-kimoto authored on 2009-11-16
205
    
version 0.0901
yuki-kimoto authored on 2009-12-17
206
    # Fetch
207
    while (my @row = $result->fetch) {
208
        # Do something
209
    }
210
    
211
    # Fetch hash
212
    while (my %row = $result->fetch_hash) {
213
        # Do something
packaging one directory
yuki-kimoto authored on 2009-11-16
214
    }
215

            
update document
yuki-kimoto authored on 2009-11-19
216
=head1 Accessors
packaging one directory
yuki-kimoto authored on 2009-11-16
217

            
218
=head2 sth
219

            
update document
yuki-kimoto authored on 2009-11-19
220
Set and Get statement handle
221

            
version 0.0901
yuki-kimoto authored on 2009-12-17
222
    $result = $result->sth($sth);
223
    $sth    = $reuslt->sth
update document
yuki-kimoto authored on 2009-11-19
224
    
packaging one directory
yuki-kimoto authored on 2009-11-16
225
=head2 fetch_filter
226

            
update document
yuki-kimoto authored on 2009-11-19
227
Set and Get fetch filter
228

            
version 0.0901
yuki-kimoto authored on 2009-12-17
229
    $result         = $result->fetch_filter($sth);
230
    $fetch_filter   = $result->fech_filter;
packaging one directory
yuki-kimoto authored on 2009-11-16
231

            
232
=head2 no_fetch_filters
233

            
update document
yuki-kimoto authored on 2009-11-19
234
Set and Get no filter keys when fetching
235

            
version 0.0901
yuki-kimoto authored on 2009-12-17
236
    $result           = $result->no_fetch_filters($no_fetch_filters);
packaging one directory
yuki-kimoto authored on 2009-11-16
237
    $no_fetch_filters = $result->no_fetch_filters;
238

            
update document
yuki-kimoto authored on 2009-11-19
239
=head1 Methods
packaging one directory
yuki-kimoto authored on 2009-11-16
240

            
241
=head2 fetch
242

            
update document
yuki-kimoto authored on 2009-11-19
243
Fetch a row
244

            
version 0.0901
yuki-kimoto authored on 2009-12-17
245
    $row = $result->fetch; # array reference
246
    @row = $result->fecth; # array
247

            
248
The following is fetch sample
packaging one directory
yuki-kimoto authored on 2009-11-16
249

            
250
    while (my $row = $result->fetch) {
251
        # do something
252
        my $val1 = $row->[0];
253
        my $val2 = $row->[1];
254
    }
255

            
256
=head2 fetch_hash
257

            
update document
yuki-kimoto authored on 2009-11-19
258
Fetch row as hash
259

            
version 0.0901
yuki-kimoto authored on 2009-12-17
260
    $row = $result->fetch_hash; # hash reference
261
    %row = $result->fetch_hash; # hash
262

            
263
The following is fetch_hash sample
packaging one directory
yuki-kimoto authored on 2009-11-16
264

            
265
    while (my $row = $result->fetch_hash) {
266
        # do something
267
        my $val1 = $row->{key1};
268
        my $val2 = $row->{key2};
269
    }
270

            
271
=head2 fetch_first
272

            
update document
yuki-kimoto authored on 2009-11-19
273
Fetch only first row(Scalar context)
274

            
version 0.0901
yuki-kimoto authored on 2009-12-17
275
    $row = $result->fetch_first; # array reference
276
    @row = $result->fetch_first; # array
packaging one directory
yuki-kimoto authored on 2009-11-16
277
    
version 0.0901
yuki-kimoto authored on 2009-12-17
278
The following is fetch_first sample
279

            
update document
yuki-kimoto authored on 2009-11-19
280
    $row = $result->fetch_first;
packaging one directory
yuki-kimoto authored on 2009-11-16
281
    
update document
yuki-kimoto authored on 2009-11-19
282
This method fetch only first row and finish statement handle
packaging one directory
yuki-kimoto authored on 2009-11-16
283

            
284
=head2 fetch_hash_first
285
    
update document
yuki-kimoto authored on 2009-11-19
286
Fetch only first row as hash
287

            
version 0.0901
yuki-kimoto authored on 2009-12-17
288
    $row = $result->fetch_hash_first; # hash reference
289
    %row = $result->fetch_hash_first; # hash
packaging one directory
yuki-kimoto authored on 2009-11-16
290
    
version 0.0901
yuki-kimoto authored on 2009-12-17
291
The following is fetch_hash_first sample
292

            
update document
yuki-kimoto authored on 2009-11-19
293
    $row = $result->fetch_hash_first;
packaging one directory
yuki-kimoto authored on 2009-11-16
294
    
update document
yuki-kimoto authored on 2009-11-19
295
This method fetch only first row and finish statement handle
packaging one directory
yuki-kimoto authored on 2009-11-16
296

            
297
=head2 fetch_rows
298

            
update document
yuki-kimoto authored on 2009-11-19
299
Fetch rows
300

            
version 0.0901
yuki-kimoto authored on 2009-12-17
301
    $rows = $result->fetch_rows($row_count); # array ref of array ref
302
    @rows = $result->fetch_rows($row_count); # array of array ref
packaging one directory
yuki-kimoto authored on 2009-11-16
303
    
version 0.0901
yuki-kimoto authored on 2009-12-17
304
The following is fetch_rows sample
305

            
update document
yuki-kimoto authored on 2009-11-19
306
    while(my $rows = $result->fetch_rows(10)) {
307
        # do someting
308
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
309

            
310
=head2 fetch_hash_rows
311

            
update document
yuki-kimoto authored on 2009-11-19
312
Fetch rows as hash
313

            
version 0.0901
yuki-kimoto authored on 2009-12-17
314
    $rows = $result->fetch_hash_rows($row_count); # array ref of hash ref
315
    @rows = $result->fetch_hash_rows($row_count); # array of hash ref
packaging one directory
yuki-kimoto authored on 2009-11-16
316
    
version 0.0901
yuki-kimoto authored on 2009-12-17
317
The following is fetch_hash_rows sample
318

            
update document
yuki-kimoto authored on 2009-11-19
319
    while(my $rows = $result->fetch_hash_rows(10)) {
320
        # do someting
321
    }
packaging one directory
yuki-kimoto authored on 2009-11-16
322

            
323
=head2 fetch_all
324

            
update document
yuki-kimoto authored on 2009-11-19
325
Fetch all rows
326

            
version 0.0901
yuki-kimoto authored on 2009-12-17
327
    $rows = $result->fetch_all; # array ref of array ref
328
    @rows = $result->fecth_all; # array of array ref
329

            
330
The following is fetch_all sample
packaging one directory
yuki-kimoto authored on 2009-11-16
331

            
332
    my $rows = $result->fetch_all;
333

            
334
=head2 fetch_hash_all
335

            
update document
yuki-kimoto authored on 2009-11-19
336
Fetch all row as array ref of hash ref (Scalar context)
337

            
version 0.0901
yuki-kimoto authored on 2009-12-17
338
    $rows = $result->fetch_hash_all; # array ref of hash ref
339
    @rows = $result->fecth_all_hash; # array of hash ref
340

            
341
The following is fetch_hash_all sample
packaging one directory
yuki-kimoto authored on 2009-11-16
342

            
343
    my $rows = $result->fetch_hash_all;
344

            
345
=head2 error
346

            
update document
yuki-kimoto authored on 2009-11-19
347
Get error infomation
348

            
version 0.0901
yuki-kimoto authored on 2009-12-17
349
    $error_messege = $result->error;
350
    ($error_message, $error_number, $error_state) = $result->error;
update document
yuki-kimoto authored on 2009-11-19
351
    
packaging one directory
yuki-kimoto authored on 2009-11-16
352

            
update document
yuki-kimoto authored on 2009-11-19
353
You can get get information. This is same as the following.
packaging one directory
yuki-kimoto authored on 2009-11-16
354

            
355
    $error_message : $result->sth->errstr
356
    $error_number  : $result->sth->err
357
    $error_state   : $result->sth->state
358

            
359
=head2 finish
360

            
update document
yuki-kimoto authored on 2009-11-19
361
Finish statement handle
362

            
packaging one directory
yuki-kimoto authored on 2009-11-16
363
    $result->finish
364

            
update document
yuki-kimoto authored on 2009-11-19
365
This is equel to
packaging one directory
yuki-kimoto authored on 2009-11-16
366

            
367
    $result->sth->finish;
368

            
update document
yuki-kimoto authored on 2009-11-19
369
=head1 See also
370

            
371
L<DBIx::Custom>
372

            
373
=head1 Author
packaging one directory
yuki-kimoto authored on 2009-11-16
374

            
375
Yuki Kimoto, C<< <kimoto.yuki at gmail.com> >>
376

            
377
Github L<http://github.com/yuki-kimoto>
378

            
update document
yuki-kimoto authored on 2009-11-19
379
=head1 Copyright & licence
packaging one directory
yuki-kimoto authored on 2009-11-16
380

            
381
Copyright 2009 Yuki Kimoto, all rights reserved.
382

            
383
This program is free software; you can redistribute it and/or modify it
384
under the same terms as Perl itself.
385

            
386
=cut