Newer Older
792 lines | 17.056kb
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}) {
DBIx::Custom::Result::kv met...
Yuki Kimoto authored on 2013-03-04
259
      _deprecate('0.28', "DBIx::Custom::Result::kv method's "
260
        . 'multi option is DEPRECATED. use kvs method instead');
added EXPERIMETAL DBIx::Cust...
Yuki Kimoto authored on 2012-02-28
261
      $kv->{$key_value} ||= [];
262
      push @{$kv->{$key_value}}, $row;
263
    }
264
    else { $kv->{$key_value} = $row }
265
  }
266
  
267
  return $kv;
268
}
269

            
DBIx::Custom::Result::kv met...
Yuki Kimoto authored on 2013-03-04
270
sub kvs {
271
  my ($self, %opt) = @_;
272

            
273
  my $key_name = $self->{sth}{NAME}[0];
274
  my $kv = {};
275
  while (my $row = $self->fetch_hash) {
276
    my $key_value = delete $row->{$key_name};
277
    next unless defined $key_value;
278
    $kv->{$key_value} ||= [];
279
    push @{$kv->{$key_value}}, $row;
280
  }
281
  
282
  return $kv;
283
}
284

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

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

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
289
sub type_rule {
cleanup
Yuki Kimoto authored on 2012-01-20
290
  my $self = shift;
291
  
292
  if (@_) {
293
    my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
294

            
295
    # From
296
    for my $i (1 .. 2) {
297
      $type_rule->{"from$i"} = _array_to_hash($type_rule->{"from$i"});
298
      for my $data_type (keys %{$type_rule->{"from$i"} || {}}) {
299
        croak qq{data type of from$i section must be lower case or number}
300
          if $data_type =~ /[A-Z]/;
301
        my $fname = $type_rule->{"from$i"}{$data_type};
302
        if (defined $fname && ref $fname ne 'CODE') {
303
          croak qq{Filter "$fname" is not registered" } . _subname
304
            unless exists $self->dbi->filters->{$fname};
305
          
306
          $type_rule->{"from$i"}{$data_type} = $self->dbi->filters->{$fname};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
307
        }
cleanup
Yuki Kimoto authored on 2012-01-20
308
      }
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
309
    }
cleanup
Yuki Kimoto authored on 2012-01-20
310
    $self->{type_rule} = $type_rule;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
311
    
cleanup
Yuki Kimoto authored on 2012-01-20
312
    return $self;
313
  }
314
  
315
  return $self->{type_rule} || {};
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
316
}
317

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

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

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

            
336
sub type_rule1_on {
cleanup
Yuki Kimoto authored on 2012-01-20
337
  my $self = shift;
338
  $self->{type_rule1_off} = 0;
339
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
340
}
341

            
342
sub type_rule2_off {
cleanup
Yuki Kimoto authored on 2012-01-20
343
  my $self = shift;
344
  $self->{type_rule2_off} = 1;
345
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
346
}
347

            
348
sub type_rule2_on {
cleanup
Yuki Kimoto authored on 2012-01-20
349
  my $self = shift;
350
  $self->{type_rule2_off} = 0;
351
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
352
}
353

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
354
sub value {
cleanup
Yuki Kimoto authored on 2012-01-20
355
  my $self = shift;
356
  my $row = $self->fetch_one;
357
  my $value = $row ? $row->[0] : undef;
358
  return $value;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
359
}
360

            
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
361
sub values {
362
  my $self = shift;
363
  
364
  my $values = [];
365
  my $rows = $self->fetch_all;
366
  push @$values, $_->[0] for @$rows;
367
  return $values;
368
}
369

            
- fixed bug DBIx::Custom::Re...
Yuki Kimoto authored on 2011-11-08
370
sub _cache {
cleanup
Yuki Kimoto authored on 2012-01-20
371
  my $self = shift;
372
  $self->{_type_map} = {};
373
  $self->{_pos} = {};
374
  $self->{_columns} = {};
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
375
  for (my $i = 0; $i < @{$self->{sth}->{NAME} || []}; $i++) {
cleanup
Yuki Kimoto authored on 2012-01-20
376
    my $type = lc $self->{sth}{TYPE}[$i];
377
    my $name = $self->{sth}{NAME}[$i];
378
    $self->{_type_map}{$type} ||= [];
379
    push @{$self->{_type_map}{$type}}, $name;
380
    $self->{_pos}{$name} ||= [];
381
    push @{$self->{_pos}{$name}}, $i;
382
    $self->{_columns}{$name} = 1;
383
  }
384
  $self->{_cache} = 1;
- fixed bug DBIx::Custom::Re...
Yuki Kimoto authored on 2011-11-08
385
}
386

            
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
387
# DEPRECATED!
388
sub column {
389
  my $self = shift;
390
  
391
  _deprecate('0.25', "DBIx::Custom::Result::column method is DEPRECATED. "
392
    . "use values method instead");
393
  
394
  return $self->values(@_);
395
}
396

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
397
# DEPRECATED!
398
sub fetch_hash_first {
cleanup
Yuki Kimoto authored on 2012-01-20
399
  my $self = shift;
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
400
  _deprecate('0.24', "DBIx::Custom::Result::fetch_hash_first is DEPRECATED! "
401
    . "use fetch_hash_one instead");
cleanup
Yuki Kimoto authored on 2012-01-20
402
  return $self->fetch_hash_one(@_);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
403
}
404

            
405
# DEPRECATED!
406
sub fetch_first {
cleanup
Yuki Kimoto authored on 2012-01-20
407
  my $self = shift;
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
408
  _deprecate('0.24', "DBIx::Custom::Result::fetch_first is DEPRECATED! "
409
    . " use fetch_one instead");
cleanup
Yuki Kimoto authored on 2012-01-20
410
  return $self->fetch_one(@_);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
411
}
412

            
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
413
# DEPRECATED!
414
sub filter_off {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
415
  _deprecate('0.24', "filter_off method is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
416
  my $self = shift;
417
  $self->{filter_off} = 1;
418
  return $self;
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
419
}
420

            
421
# DEPRECATED!
422
sub filter_on {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
423
  _deprecated('0.24', "filter_on method is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
424
  my $self = shift;
425
  $self->{filter_off} = 0;
426
  return $self;
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
427
}
428

            
cleanup
Yuki Kimoto authored on 2011-06-13
429
# DEPRECATED!
430
sub end_filter {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
431
  _deprecate('0.24', "end_filter method is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
432
  my $self = shift;
433
  if (@_) {
434
    my $end_filter = {};
435
    if (ref $_[0] eq 'HASH') { $end_filter = $_[0] }
436
    else { 
437
      $end_filter = _array_to_hash(
438
          @_ > 1 ? [@_] : $_[0]
439
      );
440
    }
441
    for my $column (keys %$end_filter) {
442
      my $fname = $end_filter->{$column};
443
      if (exists $end_filter->{$column}
444
        && defined $fname
445
        && ref $fname ne 'CODE') 
446
      {
447
        croak qq{Filter "$fname" is not registered" } . _subname
448
          unless exists $self->dbi->filters->{$fname};
449
        $end_filter->{$column} = $self->dbi->filters->{$fname};
450
      }
cleanup
Yuki Kimoto authored on 2011-06-13
451
    }
cleanup
Yuki Kimoto authored on 2012-01-20
452
    $self->{end_filter} = {%{$self->end_filter}, %$end_filter};
453
    return $self;
454
  }
455
  return $self->{end_filter} ||= {};
cleanup
Yuki Kimoto authored on 2011-06-13
456
}
cleanup
Yuki Kimoto authored on 2011-06-13
457
# DEPRECATED!
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
458
sub remove_end_filter {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
459
  _deprecate('0.24', "remove_end_filter is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
460
  my $self = shift;
461
  $self->{end_filter} = {};
462
  return $self;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
463
}
cleanup
Yuki Kimoto authored on 2011-06-13
464
# DEPRECATED!
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
465
sub remove_filter {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
466
  _deprecate('0.24', "remove_filter is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
467
  my $self = shift;
468
  $self->{filter} = {};
469
  return $self;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-02-28
470
}
cleanup
Yuki Kimoto authored on 2011-06-13
471
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-01-12
472
sub default_filter {
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
473
  _deprecate('0.24', "default_filter is DEPRECATED!");
cleanup
Yuki Kimoto authored on 2012-01-20
474
  my $self = shift;
475
  if (@_) {
476
    my $fname = $_[0];
477
    if (@_ && !$fname) {
478
      $self->{default_filter} = undef;
cleanup
Yuki Kimoto authored on 2011-01-12
479
    }
cleanup
Yuki Kimoto authored on 2012-01-20
480
    else {
481
      croak qq{Filter "$fname" is not registered}
482
        unless exists $self->dbi->filters->{$fname};
483
      $self->{default_filter} = $self->dbi->filters->{$fname};
484
    }
485
    return $self;
486
  }
487
  return $self->{default_filter};
cleanup
Yuki Kimoto authored on 2011-01-12
488
}
cleanup
Yuki Kimoto authored on 2011-01-23
489
# DEPRECATED!
cleanup
Yuki Kimoto authored on 2011-06-13
490
has 'filter_check'; 
cleanup
Yuki Kimoto authored on 2011-01-23
491

            
update document
yuki-kimoto authored on 2010-01-30
492
1;
493

            
packaging one directory
yuki-kimoto authored on 2009-11-16
494
=head1 NAME
495

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

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

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

            
503
  # Fetch a row and put it into array reference
504
  while (my $row = $result->fetch) {
505
    my $author = $row->[0];
506
    my $title  = $row->[1];
507
  }
508
  
509
  # Fetch only a first row and put it into array reference
510
  my $row = $result->fetch_one;
511
  
512
  # Fetch all rows and put them into array of array reference
513
  my $rows = $result->fetch_all;
514

            
515
  # Fetch a row and put it into hash reference
516
  while (my $row = $result->fetch_hash) {
517
    my $title  = $row->{title};
518
    my $author = $row->{author};
519
  }
520
  
521
  # Fetch only a first row and put it into hash reference
522
  my $row = $result->fetch_hash_one;
523
  my $row = $result->one; # Alias for "fetch_hash_one"
524
  
525
  # Fetch all rows and put them into array of hash reference
526
  my $rows = $result->fetch_hash_all;
527
  my $rows = $result->all; # Alias for "fetch_hash_all"
packaging one directory
yuki-kimoto authored on 2009-11-16
528

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
531
=head2 dbi
cleanup
yuki-kimoto authored on 2010-10-17
532

            
cleanup
Yuki Kimoto authored on 2012-01-20
533
  my $dbi = $result->dbi;
534
  $result = $result->dbi($dbi);
cleanup
yuki-kimoto authored on 2010-10-17
535

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
538
=head2 sth
cleanup
yuki-kimoto authored on 2010-10-17
539

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

            
543
Statement handle of L<DBI>.
544

            
update document
yuki-kimoto authored on 2010-01-30
545
=head1 METHODS
546

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
550
=head2 all
updated pod
Yuki Kimoto authored on 2011-06-07
551

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
554
Same as fetch_hash_all.
updated pod
Yuki Kimoto authored on 2011-06-07
555

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
556
=head2 fetch
packaging one directory
yuki-kimoto authored on 2009-11-16
557

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

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
562
=head2 fetch_all
packaging one directory
yuki-kimoto authored on 2009-11-16
563

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

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
568
=head2 fetch_one
cleanup
yuki-kimoto authored on 2010-10-17
569

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

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
575
=head2 fetch_hash
packaging one directory
yuki-kimoto authored on 2009-11-16
576

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

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
581
=head2 fetch_hash_all
cleanup
yuki-kimoto authored on 2010-10-17
582

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

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
587
=head2 fetch_hash_one
cleanup
Yuki Kimoto authored on 2012-01-20
588
  
589
  my $row = $result->fetch_hash_one;
packaging one directory
yuki-kimoto authored on 2009-11-16
590

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
594
=head2 fetch_hash_multi
update document
yuki-kimoto authored on 2009-11-19
595

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
600
=head2 fetch_multi
packaging one directory
yuki-kimoto authored on 2009-11-16
601

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
606
=head2 filter
cleanup
Yuki Kimoto authored on 2010-12-21
607

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

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
615
=head2 flat
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2012-02-28
616

            
updated pod
Yuki Kimoto authored on 2012-04-02
617
  my @list = $result->flat;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2012-02-28
618

            
updated pod
Yuki Kimoto authored on 2012-04-02
619
All values is added to flatten list.
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2012-02-28
620
  
updated pod
Yuki Kimoto authored on 2012-04-02
621
  my @list = $dbi->select(['id', 'title'])->flat;
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2012-02-28
622

            
623
C<flat> method return the following data.
624

            
625
  (1, 'Perl', 2, 'Ruby')
626

            
627
You can create key-value pair easily.
628

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
631
=head2 kv
added EXPERIMETAL DBIx::Cust...
Yuki Kimoto authored on 2012-02-28
632

            
633
  my $key_value = $result->kv;
634

            
635
Get key-value pairs.
636

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

            
639
If C<all> method return the following data:
640

            
641
  [
642
    {id => 1, title => 'Perl', author => 'Ken'},
643
    {id => 2, title => 'Ruby', author => 'Taro'}
644
  ]
645

            
646
C<kv> method return the following data.
647

            
648
  {
649
    1 => {title => 'Perl', author => 'Ken'},
650
    2 => {title => 'Ruby', author => 'Taro'}
651
  }
652

            
653
First column value become key.
654

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
655
=head2 kvs
added EXPERIMETAL DBIx::Cust...
Yuki Kimoto authored on 2012-02-28
656

            
DBIx::Custom::Result::kv met...
Yuki Kimoto authored on 2013-03-04
657
  my $key_values = $result->kvs;
658

            
659
Get key-values pairs.
660

            
661
  my $books = $dbi->select(['author', 'title', 'price'])->kvs;
added EXPERIMETAL DBIx::Cust...
Yuki Kimoto authored on 2012-02-28
662

            
663
If C<all> method return the following data:
664

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

            
DBIx::Custom::Result::kv met...
Yuki Kimoto authored on 2013-03-04
672
C<kvs> method return the following data.
added EXPERIMETAL DBIx::Cust...
Yuki Kimoto authored on 2012-02-28
673

            
674
  {
675
    Ken => [
676
      {title => 'Perl', price => 1000},
677
      {title => 'Good', price => 2000}
678
    ],
679
    Taro => [
680
      {title => 'Ruby', price => 3000},
681
      {title => 'Sky', price => 4000}
682
    ]
683
  }
684

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
685
=head2 header
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-07-11
686

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

            
689
Get header column names.
690

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
691
=head2 one
updated pod
Yuki Kimoto authored on 2011-06-07
692

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

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
697
=head2 stash
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-25
698

            
cleanup
Yuki Kimoto authored on 2012-01-20
699
  my $stash = $result->stash;
700
  my $foo = $result->stash->{foo};
701
  $result->stash->{foo} = $foo;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-25
702

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

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
705
=head2 type_rule
cleanup
Yuki Kimoto authored on 2012-01-20
706
  
707
  # Merge type rule
708
  $result->type_rule(
709
    # DATE
710
    9 => sub { ... },
711
    # DATETIME or TIMESTAMP
712
    11 => sub { ... }
713
  );
714

            
715
  # Replace type rule(by reference)
716
  $result->type_rule([
717
    # DATE
718
    9 => sub { ... },
719
    # DATETIME or TIMESTAMP
720
    11 => sub { ... }
721
  ]);
EXPERIMENTAL type_rule_off i...
Yuki Kimoto authored on 2011-06-14
722

            
cleanup
Yuki Kimoto authored on 2011-06-15
723
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
724

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
725
=head2 type_rule_off
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
726

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

            
729
Turn C<from1> and C<from2> type rule off.
730
By default, type rule is on.
731

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
732
=head2 type_rule_on
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
733

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

            
736
Turn C<from1> and C<from2> type rule on.
737
By default, type rule is on.
738

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
739
=head2 type_rule1_off
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
740

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

            
743
Turn C<from1> type rule off.
744
By default, type rule is on.
745

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
746
=head2 type_rule1_on
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
747

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

            
750
Turn C<from1> type rule on.
751
By default, type rule is on.
752

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
753
=head2 type_rule2_off
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
754

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

            
757
Turn C<from2> type rule off.
758
By default, type rule is on.
759

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
760
=head2 type_rule2_on
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
761

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

            
764
Turn C<from2> type rule on.
765
By default, type rule is on.
766

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
767
=head2 value
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
768

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

            
771
Get first column's first value.
772

            
updated pod
Yuki Kimoto authored on 2012-04-02
773
  my $count = $dbi->select('count(*)', table => 'book')->value;
774

            
775
This is almost same as the following one.
776

            
777
  my $count = $dbi->select('count(*)')->fetch_one->[0];
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
778

            
cleanup and added deprecated...
Yuki Kimoto authored on 2013-03-04
779
=head2 values
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
780

            
781
  my $values = $result->values;
782

            
783
Get first column's values.
784

            
updated pod
Yuki Kimoto authored on 2012-04-02
785
  my $tables = $dbi->select('show tables')->values;
786

            
787
This is same as the following one.
788

            
789
  my $rows = $dbi->select('show tables')->fetch_all;
790
  my $tables = [map { $_->[0] } @$rows];
- added DBIX_CUSTOM_SUPPRESS...
Yuki Kimoto authored on 2012-03-19
791

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