Newer Older
771 lines | 16.651kb
packaging one directory
yuki-kimoto authored on 2009-11-16
1
package DBIx::Custom::Result;
updatedd pod
Yuki Kimoto authored on 2011-06-12
2
use Object::Simple -base;
cleanup
yuki-kimoto authored on 2010-02-11
3

            
packaging one directory
yuki-kimoto authored on 2009-11-16
4
use Carp 'croak';
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
5
use DBIx::Custom::Util qw/_array_to_hash _subname _deprecate/;
packaging one directory
yuki-kimoto authored on 2009-11-16
6

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
7
has [qw/dbi sth/],
cleanup
Yuki Kimoto authored on 2012-01-20
8
  stash => sub { {} };
cleanup
Yuki Kimoto authored on 2010-12-21
9

            
- added DBIx::Custom::Result...
Yuki Kimoto authored on 2011-06-07
10
*all = \&fetch_hash_all;
11

            
micro optimization
Yuki Kimoto authored on 2011-11-07
12
sub fetch {
cleanup
Yuki Kimoto authored on 2012-01-20
13
  my $self = shift;
14
  
15
  # Info
16
  $self->_cache unless $self->{_cache};
17
  
18
  # Fetch
19
  my @row = $self->{sth}->fetchrow_array;
20
  return unless @row;
21
  
22
  # Type rule
23
  if ($self->{type_rule}->{from1} && !$self->{type_rule_off} && !$self->{type_rule1_off}) {
24
    my $from = $self->{type_rule}->{from1};
25
    for my $type (keys %$from) {
26
      for my $column (@{$self->{_type_map}->{$type}}) {
27
        $row[$_] = $from->{$type}->($row[$_])
28
          for @{$self->{_pos}{$column} || []};
29
      }
micro optimization
Yuki Kimoto authored on 2011-11-07
30
    }
cleanup
Yuki Kimoto authored on 2012-01-20
31
  }
32
  if ($self->{type_rule}->{from2} && !$self->{type_rule_off} && !$self->{type_rule2_off}) {
33
    my $from = $self->{type_rule}->{from2};
34
    for my $type (keys %$from) {
35
      for my $column (@{$self->{_type_map}->{$type}}) {
36
        $row[$_] = $from->{$type}->($row[$_])
37
          for @{$self->{_pos}{$column} || []};
38
      }
micro optimization
Yuki Kimoto authored on 2011-11-07
39
    }
cleanup
Yuki Kimoto authored on 2012-01-20
40
  }
41
  
42
  # Filter
43
  if (($self->{filter} || $self->{default_filter}) && !$self->{filter_off}) {
44
     my @columns = $self->{default_filter} ? keys %{$self->{_columns}}
45
       : keys %{$self->{filter}};
46
     
47
     for my $column (@columns) {
48
       my $filter = exists $self->{filter}->{$column} ? $self->{filter}->{$column}
49
         : $self->{default_filter};
50
       next unless $filter;
51
       $row[$_] = $filter->($row[$_])
52
         for @{$self->{_pos}{$column} || []};
53
     }
54
  }
55
  if ($self->{end_filter} && !$self->{filter_off}) {
56
     for my $column (keys %{$self->{end_filter}}) {
57
       next unless $self->{end_filter}->{$column};
58
       $row[$_] = $self->{end_filter}->{$column}->($row[$_])
59
         for @{$self->{_pos}{$column} || []};
60
     }
61
  }
62

            
63
  return \@row;
micro optimization
Yuki Kimoto authored on 2011-11-07
64
}
65

            
66
sub fetch_hash {
cleanup
Yuki Kimoto authored on 2012-01-20
67
  my $self = shift;
68
  
69
  # Info
70
  $self->_cache unless $self->{_cache};
71
  
72
  # Fetch
73
  return unless my $row = $self->{sth}->fetchrow_hashref;
74
  
75
  # Type rule
76
  if ($self->{type_rule}->{from1} &&
77
    !$self->{type_rule_off} && !$self->{type_rule1_off})
78
  {
79
    my $from = $self->{type_rule}->{from1};
80
    for my $type (keys %$from) {
81
      $from->{$type} and $row->{$_} = $from->{$type}->($row->{$_})
82
        for @{$self->{_type_map}->{$type}};
micro optimization
Yuki Kimoto authored on 2011-11-07
83
    }
cleanup
Yuki Kimoto authored on 2012-01-20
84
  }
85
  if ($self->{type_rule}->{from2} &&
86
    !$self->{type_rule_off} && !$self->{type_rule2_off})
87
  {
88
    my $from = $self->{type_rule}->{from2};
89
    for my $type (keys %{$self->{type_rule}->{from2}}) {
90
      $from->{$type} and $row->{$_} = $from->{$type}->($row->{$_})
91
        for @{$self->{_type_map}->{$type}};
micro optimization
Yuki Kimoto authored on 2011-11-07
92
    }
cleanup
Yuki Kimoto authored on 2012-01-20
93
  }        
94
  # Filter
95
  if (($self->{filter} || $self->{default_filter}) &&
96
    !$self->{filter_off})
97
  {
98
     my @columns = $self->{default_filter} ? keys %{$self->{_columns}}
99
       : keys %{$self->{filter}};
100
     
101
     for my $column (@columns) {
102
       next unless exists $row->{$column};
103
       my $filter = exists $self->{filter}->{$column} ? $self->{filter}->{$column}
104
         : $self->{default_filter};
105
       $row->{$column} = $filter->($row->{$column}) if $filter;
106
     }
107
  }
108
  if ($self->{end_filter} && !$self->{filter_off}) {
109
     exists $self->{_columns}{$_} && $self->{end_filter}->{$_} and
110
         $row->{$_} = $self->{end_filter}->{$_}->($row->{$_})
111
       for keys %{$self->{end_filter}};
112
  }
113
  $row;
micro optimization
Yuki Kimoto authored on 2011-11-07
114
}
115

            
cleanup
yuki-kimoto authored on 2010-10-17
116
sub fetch_all {
cleanup
Yuki Kimoto authored on 2012-01-20
117
  my $self = shift;
118
  
119
  # Fetch all rows
120
  my $rows = [];
121
  while(my $row = $self->fetch) { push @$rows, $row}
122
  
123
  return $rows;
cleanup
yuki-kimoto authored on 2010-10-17
124
}
125

            
126
sub fetch_hash_all {
cleanup
Yuki Kimoto authored on 2012-01-20
127
  my $self = shift;
128
  
129
  # Fetch all rows as hash
130
  my $rows = [];
131
  while(my $row = $self->fetch_hash) { push @$rows, $row }
132
  
133
  return $rows;
cleanup
yuki-kimoto authored on 2010-10-17
134
}
135

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
136
sub fetch_hash_one {
cleanup
Yuki Kimoto authored on 2012-01-20
137
  my $self = shift;
138
  
139
  # Fetch hash
140
  my $row = $self->fetch_hash;
141
  return unless $row;
142
  
143
  # Finish statement handle
144
  $self->sth->finish;
145
  
146
  return $row;
packaging one directory
yuki-kimoto authored on 2009-11-16
147
}
148

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
149
sub fetch_hash_multi {
cleanup
Yuki Kimoto authored on 2012-01-20
150
  my ($self, $count) = @_;
151
  
152
  # Fetch multiple rows
153
  croak 'Row count must be specified ' . _subname
154
    unless $count;
155
  
156
  return if $self->{_finished};
157

            
158
  my $rows = [];
159
  for (my $i = 0; $i < $count; $i++) {
160
    my $row = $self->fetch_hash;
161
    unless ($row) {
162
      $self->{_finished} = 1;
163
      last;
packaging one directory
yuki-kimoto authored on 2009-11-16
164
    }
cleanup
Yuki Kimoto authored on 2012-01-20
165
    push @$rows, $row;
166
  }
167
  
168
  return unless @$rows;
169
  return $rows;
packaging one directory
yuki-kimoto authored on 2009-11-16
170
}
171

            
cleanup
yuki-kimoto authored on 2010-10-17
172
sub fetch_multi {
cleanup
Yuki Kimoto authored on 2012-01-20
173
  my ($self, $count) = @_;
174
  
175
  # Row count not specifed
176
  croak 'Row count must be specified ' . _subname
177
    unless $count;
178
  
179
  return if $self->{_finished};
180
  
181
  # Fetch multi rows
182
  my $rows = [];
183
  for (my $i = 0; $i < $count; $i++) {
184
    my $row = $self->fetch;
185
    unless ($row) {
186
      $self->{_finished} = 1;
187
      last;
packaging one directory
yuki-kimoto authored on 2009-11-16
188
    }
cleanup
Yuki Kimoto authored on 2012-01-20
189
    push @$rows, $row;
190
  }
191
  
192
  return unless @$rows;
193
  return $rows;
packaging one directory
yuki-kimoto authored on 2009-11-16
194
}
195

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
196

            
197
sub fetch_one {
cleanup
Yuki Kimoto authored on 2012-01-20
198
  my $self = shift;
199
  
200
  # Fetch
201
  my $row = $self->fetch;
202
  return unless $row;
203
  
204
  # Finish statement handle
205
  $self->sth->finish;
206
  
207
  return $row;
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
208
}
209

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2012-02-28
210
sub filter {
211
  my $self = shift;
212
  
213
  # Set
214
  if (@_) {
215
    
216
    # Convert filter name to subroutine
217
    my $filter = @_ == 1 ? $_[0] : [@_];
218
    $filter = _array_to_hash($filter);
219
    for my $column (keys %$filter) {
220
      my $fname = $filter->{$column};
221
      if  (exists $filter->{$column}
222
        && defined $fname
223
        && ref $fname ne 'CODE') 
224
      {
225
        croak qq{Filter "$fname" is not registered" } . _subname
226
          unless exists $self->dbi->filters->{$fname};
227
        $filter->{$column} = $self->dbi->filters->{$fname};
228
      }
229
    }
230
    
231
    # Merge
232
    $self->{filter} = {%{$self->filter}, %$filter};
233
    
234
    return $self;
235
  }
236
  
237
  return $self->{filter} ||= {};
238
}
239

            
240
sub flat {
241
  my $self = shift;
242
  
243
  my @flat;
244
  while (my $row = $self->fetch) {
245
    push @flat, @$row;
246
  }
247
  return @flat;
248
}
249

            
added EXPERIMETAL DBIx::Cust...
Yuki Kimoto authored on 2012-02-28
250
sub kv {
251
  my ($self, %opt) = @_;
252

            
253
  my $key_name = $self->{sth}{NAME}[0];
254
  my $kv = {};
255
  while (my $row = $self->fetch_hash) {
256
    my $key_value = delete $row->{$key_name};
257
    next unless defined $key_value;
258
    if ($opt{multi}) {
259
      $kv->{$key_value} ||= [];
260
      push @{$kv->{$key_value}}, $row;
261
    }
262
    else { $kv->{$key_value} = $row }
263
  }
264
  
265
  return $kv;
266
}
267

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-07-11
268
sub header { shift->sth->{NAME} }
269

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
270
*one = \&fetch_hash_one;
- added DBIx::Custom::Result...
Yuki Kimoto authored on 2011-06-07
271

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
272
sub type_rule {
cleanup
Yuki Kimoto authored on 2012-01-20
273
  my $self = shift;
274
  
275
  if (@_) {
276
    my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
277

            
278
    # From
279
    for my $i (1 .. 2) {
280
      $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
281
      for my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
282
        croak qq{data type of from$i section must be lower case or number}
283
          if $data_type =~ /[A-Z]/;
284
        my $fname = $type_rule->{"from$i"}{$data_type};
285
        if (defined $fname && ref $fname ne 'CODE') {
286
          croak qq{Filter "$fname" is not registered" } . _subname
287
            unless exists $self->dbi->filters->{$fname};
288
          
289
          $type_rule->{"from$i"}{$data_type} = $self->dbi->filters->{$fname};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
290
        }
cleanup
Yuki Kimoto authored on 2012-01-20
291
      }
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
292
    }
cleanup
Yuki Kimoto authored on 2012-01-20
293
    $self->{type_rule} = $type_rule;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
294
    
cleanup
Yuki Kimoto authored on 2012-01-20
295
    return $self;
296
  }
297
  
298
  return $self->{type_rule} || {};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
299
}
300

            
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
301
sub type_rule_off {
cleanup
Yuki Kimoto authored on 2012-01-20
302
  my $self = shift;
303
  $self->{type_rule_off} = 1;
304
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
305
}
306

            
307
sub type_rule_on {
cleanup
Yuki Kimoto authored on 2012-01-20
308
  my $self = shift;
309
  $self->{type_rule_off} = 0;
310
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
311
}
312

            
313
sub type_rule1_off {
cleanup
Yuki Kimoto authored on 2012-01-20
314
  my $self = shift;
315
  $self->{type_rule1_off} = 1;
316
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
317
}
318

            
319
sub type_rule1_on {
cleanup
Yuki Kimoto authored on 2012-01-20
320
  my $self = shift;
321
  $self->{type_rule1_off} = 0;
322
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
323
}
324

            
325
sub type_rule2_off {
cleanup
Yuki Kimoto authored on 2012-01-20
326
  my $self = shift;
327
  $self->{type_rule2_off} = 1;
328
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
329
}
330

            
331
sub type_rule2_on {
cleanup
Yuki Kimoto authored on 2012-01-20
332
  my $self = shift;
333
  $self->{type_rule2_off} = 0;
334
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
335
}
336

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
337
sub value {
cleanup
Yuki Kimoto authored on 2012-01-20
338
  my $self = shift;
339
  my $row = $self->fetch_one;
340
  my $value = $row ? $row->[0] : undef;
341
  return $value;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
342
}
343

            
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
344
sub values {
345
  my $self = shift;
346
  
347
  my $values = [];
348
  my $rows = $self->fetch_all;
349
  push @$values, $_->[0] for @$rows;
350
  return $values;
351
}
352

            
- fixed bug DBIx::Custom::Re...
Yuki Kimoto authored on 2011-11-08
353
sub _cache {
cleanup
Yuki Kimoto authored on 2012-01-20
354
  my $self = shift;
355
  $self->{_type_map} = {};
356
  $self->{_pos} = {};
357
  $self->{_columns} = {};
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
358
  for (my $i = 0; $i < @{$self->{sth}->{NAME} || []}; $i++) {
cleanup
Yuki Kimoto authored on 2012-01-20
359
    my $type = lc $self->{sth}{TYPE}[$i];
360
    my $name = $self->{sth}{NAME}[$i];
361
    $self->{_type_map}{$type} ||= [];
362
    push @{$self->{_type_map}{$type}}, $name;
363
    $self->{_pos}{$name} ||= [];
364
    push @{$self->{_pos}{$name}}, $i;
365
    $self->{_columns}{$name} = 1;
366
  }
367
  $self->{_cache} = 1;
- fixed bug DBIx::Custom::Re...
Yuki Kimoto authored on 2011-11-08
368
}
369

            
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
370
# DEPRECATED!
371
sub column {
372
  my $self = shift;
373
  
374
  _deprecate('0.25', "DBIx::Custom::Result::column method is DEPRECATED. "
375
    . "use values method instead");
376
  
377
  return $self->values(@_);
378
}
379

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
380
# DEPRECATED!
381
sub fetch_hash_first {
cleanup
Yuki Kimoto authored on 2012-01-20
382
  my $self = shift;
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
383
  _deprecate('0.24', "DBIx::Custom::Result::fetch_hash_first is DEPRECATED! "
384
    . "use fetch_hash_one instead");
cleanup
Yuki Kimoto authored on 2012-01-20
385
  return $self->fetch_hash_one(@_);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
386
}
387

            
388
# DEPRECATED!
389
sub fetch_first {
cleanup
Yuki Kimoto authored on 2012-01-20
390
  my $self = shift;
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
391
  _deprecate('0.24', "DBIx::Custom::Result::fetch_first is DEPRECATED! "
392
    . " use fetch_one instead");
cleanup
Yuki Kimoto authored on 2012-01-20
393
  return $self->fetch_one(@_);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
394
}
395

            
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
396
# DEPRECATED!
397
sub filter_off {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
398
  _deprecate('0.24', "filter_off method is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
399
  my $self = shift;
400
  $self->{filter_off} = 1;
401
  return $self;
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
402
}
403

            
404
# DEPRECATED!
405
sub filter_on {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
406
  _deprecated('0.24', "filter_on method is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
407
  my $self = shift;
408
  $self->{filter_off} = 0;
409
  return $self;
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
410
}
411

            
cleanup
Yuki Kimoto authored on 2011-06-13
412
# DEPRECATED!
413
sub end_filter {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
414
  _deprecate('0.24', "end_filter method is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
415
  my $self = shift;
416
  if (@_) {
417
    my $end_filter = {};
418
    if (ref $_[0] eq 'HASH') { $end_filter = $_[0] }
419
    else { 
420
      $end_filter = _array_to_hash(
421
          @_ > 1 ? [@_] : $_[0]
422
      );
423
    }
424
    for my $column (keys %$end_filter) {
425
      my $fname = $end_filter->{$column};
426
      if (exists $end_filter->{$column}
427
        && defined $fname
428
        && ref $fname ne 'CODE') 
429
      {
430
        croak qq{Filter "$fname" is not registered" } . _subname
431
          unless exists $self->dbi->filters->{$fname};
432
        $end_filter->{$column} = $self->dbi->filters->{$fname};
433
      }
cleanup
Yuki Kimoto authored on 2011-06-13
434
    }
cleanup
Yuki Kimoto authored on 2012-01-20
435
    $self->{end_filter} = {%{$self->end_filter}, %$end_filter};
436
    return $self;
437
  }
438
  return $self->{end_filter} ||= {};
cleanup
Yuki Kimoto authored on 2011-06-13
439
}
cleanup
Yuki Kimoto authored on 2011-06-13
440
# DEPRECATED!
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
441
sub remove_end_filter {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
442
  _deprecate('0.24', "remove_end_filter is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
443
  my $self = shift;
444
  $self->{end_filter} = {};
445
  return $self;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
446
}
cleanup
Yuki Kimoto authored on 2011-06-13
447
# DEPRECATED!
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
448
sub remove_filter {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
449
  _deprecate('0.24', "remove_filter is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
450
  my $self = shift;
451
  $self->{filter} = {};
452
  return $self;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
453
}
cleanup
Yuki Kimoto authored on 2011-06-13
454
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
455
sub default_filter {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
456
  _deprecate('0.24', "default_filter is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
457
  my $self = shift;
458
  if (@_) {
459
    my $fname = $_[0];
460
    if (@_ && !$fname) {
461
      $self->{default_filter} = undef;
cleanup
Yuki Kimoto authored on 2011-01-12
462
    }
cleanup
Yuki Kimoto authored on 2012-01-20
463
    else {
464
      croak qq{Filter "$fname" is not registered}
465
        unless exists $self->dbi->filters->{$fname};
466
      $self->{default_filter} = $self->dbi->filters->{$fname};
467
    }
468
    return $self;
469
  }
470
  return $self->{default_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
471
}
cleanup
Yuki Kimoto authored on 2011-01-23
472
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-06-13
473
has 'filter_check'; 
cleanup
Yuki Kimoto authored on 2011-01-23
474

            
update document
yuki-kimoto authored on 2010-01-30
475
1;
476

            
packaging one directory
yuki-kimoto authored on 2009-11-16
477
=head1 NAME
478

            
cleanup
yuki-kimoto authored on 2010-08-05
479
DBIx::Custom::Result - Result of select statement
packaging one directory
yuki-kimoto authored on 2009-11-16
480

            
update document
yuki-kimoto authored on 2010-01-30
481
=head1 SYNOPSIS
cleanup
yuki-kimoto authored on 2010-08-05
482

            
cleanup
Yuki Kimoto authored on 2012-01-20
483
  # Result
484
  my $result = $dbi->select(table => 'book');
485

            
486
  # Fetch a row and put it into array reference
487
  while (my $row = $result->fetch) {
488
    my $author = $row->[0];
489
    my $title  = $row->[1];
490
  }
491
  
492
  # Fetch only a first row and put it into array reference
493
  my $row = $result->fetch_one;
494
  
495
  # Fetch all rows and put them into array of array reference
496
  my $rows = $result->fetch_all;
497

            
498
  # Fetch a row and put it into hash reference
499
  while (my $row = $result->fetch_hash) {
500
    my $title  = $row->{title};
501
    my $author = $row->{author};
502
  }
503
  
504
  # Fetch only a first row and put it into hash reference
505
  my $row = $result->fetch_hash_one;
506
  my $row = $result->one; # Alias for "fetch_hash_one"
507
  
508
  # Fetch all rows and put them into array of hash reference
509
  my $rows = $result->fetch_hash_all;
510
  my $rows = $result->all; # Alias for "fetch_hash_all"
packaging one directory
yuki-kimoto authored on 2009-11-16
511

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

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
514
=head2 C<dbi>
cleanup
yuki-kimoto authored on 2010-10-17
515

            
cleanup
Yuki Kimoto authored on 2012-01-20
516
  my $dbi = $result->dbi;
517
  $result = $result->dbi($dbi);
cleanup
yuki-kimoto authored on 2010-10-17
518

            
sub module use DBIx::Custom ...
Yuki Kimoto authored on 2011-08-02
519
L<DBIx::Custom> object.
cleanup
yuki-kimoto authored on 2010-10-17
520

            
521
=head2 C<sth>
522

            
cleanup
Yuki Kimoto authored on 2012-01-20
523
  my $sth = $reuslt->sth
524
  $result = $result->sth($sth);
cleanup
yuki-kimoto authored on 2010-10-17
525

            
526
Statement handle of L<DBI>.
527

            
update document
yuki-kimoto authored on 2010-01-30
528
=head1 METHODS
529

            
renamed build_query to creat...
yuki-kimoto authored on 2010-08-06
530
L<DBIx::Custom::Result> inherits all methods from L<Object::Simple>
cleanup
yuki-kimoto authored on 2010-08-05
531
and implements the following new ones.
packaging one directory
yuki-kimoto authored on 2009-11-16
532

            
updated pod
Yuki Kimoto authored on 2011-06-07
533
=head2 C<all>
534

            
cleanup
Yuki Kimoto authored on 2012-01-20
535
  my $rows = $result->all;
updated pod
Yuki Kimoto authored on 2011-06-07
536

            
cleanup
Yuki Kimoto authored on 2011-06-15
537
Same as C<fetch_hash_all>.
updated pod
Yuki Kimoto authored on 2011-06-07
538

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
541
  my $row = $result->fetch;
version 0.0901
yuki-kimoto authored on 2009-12-17
542

            
cleanup
Yuki Kimoto authored on 2011-06-15
543
Fetch a row and put it into array reference.
packaging one directory
yuki-kimoto authored on 2009-11-16
544

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
547
  my $rows = $result->fetch_all;
version 0.0901
yuki-kimoto authored on 2009-12-17
548

            
cleanup
Yuki Kimoto authored on 2011-06-15
549
Fetch all rows and put them into array of array reference.
packaging one directory
yuki-kimoto authored on 2009-11-16
550

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
551
=head2 C<fetch_one>
cleanup
yuki-kimoto authored on 2010-10-17
552

            
cleanup
Yuki Kimoto authored on 2012-01-20
553
  my $row = $result->fetch_one;
cleanup
yuki-kimoto authored on 2010-10-17
554

            
cleanup
Yuki Kimoto authored on 2011-06-15
555
Fetch only a first row and put it into array reference,
556
and finish statment handle.
cleanup
yuki-kimoto authored on 2010-10-17
557

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
560
  my $row = $result->fetch_hash;
packaging one directory
yuki-kimoto authored on 2009-11-16
561

            
cleanup
Yuki Kimoto authored on 2011-06-15
562
Fetch a row and put it into hash reference.
update document
yuki-kimoto authored on 2009-11-19
563

            
cleanup
yuki-kimoto authored on 2010-10-17
564
=head2 C<fetch_hash_all>
565

            
cleanup
Yuki Kimoto authored on 2012-01-20
566
  my $rows = $result->fetch_hash_all;
cleanup
yuki-kimoto authored on 2010-10-17
567

            
cleanup
Yuki Kimoto authored on 2011-06-15
568
Fetch all rows and put them into array of hash reference.
cleanup
yuki-kimoto authored on 2010-10-17
569

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
570
=head2 C<fetch_hash_one>
cleanup
Yuki Kimoto authored on 2012-01-20
571
  
572
  my $row = $result->fetch_hash_one;
packaging one directory
yuki-kimoto authored on 2009-11-16
573

            
cleanup
Yuki Kimoto authored on 2011-06-15
574
Fetch only a first row and put it into hash reference,
575
and finish statment handle.
packaging one directory
yuki-kimoto authored on 2009-11-16
576

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
579
  my $rows = $result->fetch_hash_multi(5);
580
  
cleanup
Yuki Kimoto authored on 2011-06-15
581
Fetch multiple rows and put them into array of hash reference.
update document
yuki-kimoto authored on 2009-11-19
582

            
cleanup
yuki-kimoto authored on 2010-10-17
583
=head2 C<fetch_multi>
packaging one directory
yuki-kimoto authored on 2009-11-16
584

            
cleanup
Yuki Kimoto authored on 2012-01-20
585
  my $rows = $result->fetch_multi(5);
586
  
cleanup
Yuki Kimoto authored on 2011-06-15
587
Fetch multiple rows and put them into array of array reference.
removed DESTROY method(not b...
yuki-kimoto authored on 2010-07-18
588

            
cleanup
Yuki Kimoto authored on 2010-12-21
589
=head2 C<filter>
590

            
cleanup
Yuki Kimoto authored on 2012-01-20
591
  $result->filter(title  => sub { uc $_[0] }, author => 'to_upper');
592
  $result->filter([qw/title author/] => 'to_upper');
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-17
593

            
cleanup
Yuki Kimoto authored on 2011-06-15
594
Set filter for column.
595
You can use subroutine or filter name as filter.
- DBIx::Custom Model filter ...
Yuki Kimoto authored on 2011-06-15
596
This filter is executed after C<type_rule> filter.
cleanup
Yuki Kimoto authored on 2010-12-21
597

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-03-19
598
=head2 C<flat>
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2012-02-28
599

            
600
  my $flat = $result->flat;
601

            
602
All value is flatten and added to one array reference.
603
  
604
  my @flat = $dbi->select(['id', 'title'])->flat;
605

            
606
If C<fetch_all> method return the following data
607

            
608
  [
609
    [1, 'Perl'],
610
    [2, 'Ruby']
611
  ]
612

            
613
C<flat> method return the following data.
614

            
615
  (1, 'Perl', 2, 'Ruby')
616

            
617
You can create key-value pair easily.
618

            
619
  my %titles = $dbi->select(['id', 'title'])->flat;
620

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-03-19
621
=head2 C<kv>
added EXPERIMETAL DBIx::Cust...
Yuki Kimoto authored on 2012-02-28
622

            
623
  my $key_value = $result->kv;
624
  my $key_values = $result->kv(multi => 1);
625

            
626
Get key-value pairs.
627

            
628
  my $books = $dbi->select(['id', 'title', 'author'])->kv;
629

            
630
If C<all> method return the following data:
631

            
632
  [
633
    {id => 1, title => 'Perl', author => 'Ken'},
634
    {id => 2, title => 'Ruby', author => 'Taro'}
635
  ]
636

            
637
C<kv> method return the following data.
638

            
639
  {
640
    1 => {title => 'Perl', author => 'Ken'},
641
    2 => {title => 'Ruby', author => 'Taro'}
642
  }
643

            
644
First column value become key.
645

            
646
If value contains multipule data, you can push it to
647
array refernce by C<multi> option.
648

            
649
  my $books = $dbi->select(['author', 'title', 'price'])->kv(multi => 1);
650

            
651
If C<all> method return the following data:
652

            
653
  [
654
    {author => 'Ken', title => 'Perl', price => 1000},
655
    {author => 'Ken', title => 'Good', price => 2000},
656
    {author => 'Taro', title => 'Ruby', price => 3000}
657
    {author => 'Taro', title => 'Sky', price => 4000}
658
  ]
659

            
660
C<kv> method return the following data.
661

            
662
  {
663
    Ken => [
664
      {title => 'Perl', price => 1000},
665
      {title => 'Good', price => 2000}
666
    ],
667
    Taro => [
668
      {title => 'Ruby', price => 3000},
669
      {title => 'Sky', price => 4000}
670
    ]
671
  }
672

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2011-07-26
673
=head2 C<header>
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-07-11
674

            
cleanup
Yuki Kimoto authored on 2012-01-20
675
  my $header = $result->header;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-07-11
676

            
677
Get header column names.
678

            
updated pod
Yuki Kimoto authored on 2011-06-07
679
=head2 C<one>
680

            
cleanup
Yuki Kimoto authored on 2012-01-20
681
  my $row = $result->one;
updated pod
Yuki Kimoto authored on 2011-06-07
682

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
683
Alias for C<fetch_hash_one>.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
684

            
- removed DEPRECATED DBIx::C...
Yuki Kimoto authored on 2011-04-11
685
=head2 C<stash>
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-25
686

            
cleanup
Yuki Kimoto authored on 2012-01-20
687
  my $stash = $result->stash;
688
  my $foo = $result->stash->{foo};
689
  $result->stash->{foo} = $foo;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-25
690

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
691
Stash is hash reference to save some data.
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-25
692

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
693
=head2 C<type_rule>
cleanup
Yuki Kimoto authored on 2012-01-20
694
  
695
  # Merge type rule
696
  $result->type_rule(
697
    # DATE
698
    9 => sub { ... },
699
    # DATETIME or TIMESTAMP
700
    11 => sub { ... }
701
  );
702

            
703
  # Replace type rule(by reference)
704
  $result->type_rule([
705
    # DATE
706
    9 => sub { ... },
707
    # DATETIME or TIMESTAMP
708
    11 => sub { ... }
709
  ]);
EXPERIMENTAL type_rule_off i...
Yuki Kimoto authored on 2011-06-14
710

            
cleanup
Yuki Kimoto authored on 2011-06-15
711
This is same as L<DBIx::Custom>'s C<type_rule>'s <from>.
EXPERIMENTAL type_rule_off i...
Yuki Kimoto authored on 2011-06-14
712

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
713
=head2 C<type_rule_off>
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
714

            
cleanup
Yuki Kimoto authored on 2012-01-20
715
  $result = $result->type_rule_off;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
716

            
717
Turn C<from1> and C<from2> type rule off.
718
By default, type rule is on.
719

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
720
=head2 C<type_rule_on>
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
721

            
cleanup
Yuki Kimoto authored on 2012-01-20
722
  $result = $result->type_rule_on;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
723

            
724
Turn C<from1> and C<from2> type rule on.
725
By default, type rule is on.
726

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
727
=head2 C<type_rule1_off>
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
728

            
cleanup
Yuki Kimoto authored on 2012-01-20
729
  $result = $result->type_rule1_off;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
730

            
731
Turn C<from1> type rule off.
732
By default, type rule is on.
733

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
734
=head2 C<type_rule1_on>
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
735

            
cleanup
Yuki Kimoto authored on 2012-01-20
736
  $result = $result->type_rule1_on;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
737

            
738
Turn C<from1> type rule on.
739
By default, type rule is on.
740

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
741
=head2 C<type_rule2_off>
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
742

            
cleanup
Yuki Kimoto authored on 2012-01-20
743
  $result = $result->type_rule2_off;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
744

            
745
Turn C<from2> type rule off.
746
By default, type rule is on.
747

            
- removed EXPERIMENTAL flag ...
Yuki Kimoto authored on 2011-09-12
748
=head2 C<type_rule2_on>
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
749

            
cleanup
Yuki Kimoto authored on 2012-01-20
750
  $result = $result->type_rule2_on;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
751

            
752
Turn C<from2> type rule on.
753
By default, type rule is on.
754

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
755
=head2 C<value>
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
756

            
cleanup
Yuki Kimoto authored on 2012-01-20
757
  my $value = $result->value;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
758

            
759
Get first column's first value.
760

            
cleanup
Yuki Kimoto authored on 2012-01-20
761
  my $count = $dbi->select('count(*)')->value;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
762

            
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
763
=head2 C<values>
764

            
765
  my $values = $result->values;
766

            
767
Get first column's values.
768

            
769
  my $names = $dbi->select('name', table => 'book')->values;
770

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