Newer Older
759 lines | 16.335kb
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';
cleanup
Yuki Kimoto authored on 2011-04-25
5
use DBIx::Custom::Util qw/_array_to_hash _subname/;
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

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
12
sub column {
cleanup
Yuki Kimoto authored on 2012-01-20
13
  my $self = shift;
14
  
15
  my $column = [];
16
  my $rows = $self->fetch_all;
17
  push @$column, $_->[0] for @$rows;
18
  return $column;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
19
}
20

            
micro optimization
Yuki Kimoto authored on 2011-11-07
21
sub fetch {
cleanup
Yuki Kimoto authored on 2012-01-20
22
  my $self = shift;
23
  
24
  # Info
25
  $self->_cache unless $self->{_cache};
26
  
27
  # Fetch
28
  my @row = $self->{sth}->fetchrow_array;
29
  return unless @row;
30
  
31
  # Type rule
32
  if ($self->{type_rule}->{from1} && !$self->{type_rule_off} && !$self->{type_rule1_off}) {
33
    my $from = $self->{type_rule}->{from1};
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
  if ($self->{type_rule}->{from2} && !$self->{type_rule_off} && !$self->{type_rule2_off}) {
42
    my $from = $self->{type_rule}->{from2};
43
    for my $type (keys %$from) {
44
      for my $column (@{$self->{_type_map}->{$type}}) {
45
        $row[$_] = $from->{$type}->($row[$_])
46
          for @{$self->{_pos}{$column} || []};
47
      }
micro optimization
Yuki Kimoto authored on 2011-11-07
48
    }
cleanup
Yuki Kimoto authored on 2012-01-20
49
  }
50
  
51
  # Filter
52
  if (($self->{filter} || $self->{default_filter}) && !$self->{filter_off}) {
53
     my @columns = $self->{default_filter} ? keys %{$self->{_columns}}
54
       : keys %{$self->{filter}};
55
     
56
     for my $column (@columns) {
57
       my $filter = exists $self->{filter}->{$column} ? $self->{filter}->{$column}
58
         : $self->{default_filter};
59
       next unless $filter;
60
       $row[$_] = $filter->($row[$_])
61
         for @{$self->{_pos}{$column} || []};
62
     }
63
  }
64
  if ($self->{end_filter} && !$self->{filter_off}) {
65
     for my $column (keys %{$self->{end_filter}}) {
66
       next unless $self->{end_filter}->{$column};
67
       $row[$_] = $self->{end_filter}->{$column}->($row[$_])
68
         for @{$self->{_pos}{$column} || []};
69
     }
70
  }
71

            
72
  return \@row;
micro optimization
Yuki Kimoto authored on 2011-11-07
73
}
74

            
75
sub fetch_hash {
cleanup
Yuki Kimoto authored on 2012-01-20
76
  my $self = shift;
77
  
78
  # Info
79
  $self->_cache unless $self->{_cache};
80
  
81
  # Fetch
82
  return unless my $row = $self->{sth}->fetchrow_hashref;
83
  
84
  # Type rule
85
  if ($self->{type_rule}->{from1} &&
86
    !$self->{type_rule_off} && !$self->{type_rule1_off})
87
  {
88
    my $from = $self->{type_rule}->{from1};
89
    for my $type (keys %$from) {
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
  if ($self->{type_rule}->{from2} &&
95
    !$self->{type_rule_off} && !$self->{type_rule2_off})
96
  {
97
    my $from = $self->{type_rule}->{from2};
98
    for my $type (keys %{$self->{type_rule}->{from2}}) {
99
      $from->{$type} and $row->{$_} = $from->{$type}->($row->{$_})
100
        for @{$self->{_type_map}->{$type}};
micro optimization
Yuki Kimoto authored on 2011-11-07
101
    }
cleanup
Yuki Kimoto authored on 2012-01-20
102
  }        
103
  # Filter
104
  if (($self->{filter} || $self->{default_filter}) &&
105
    !$self->{filter_off})
106
  {
107
     my @columns = $self->{default_filter} ? keys %{$self->{_columns}}
108
       : keys %{$self->{filter}};
109
     
110
     for my $column (@columns) {
111
       next unless exists $row->{$column};
112
       my $filter = exists $self->{filter}->{$column} ? $self->{filter}->{$column}
113
         : $self->{default_filter};
114
       $row->{$column} = $filter->($row->{$column}) if $filter;
115
     }
116
  }
117
  if ($self->{end_filter} && !$self->{filter_off}) {
118
     exists $self->{_columns}{$_} && $self->{end_filter}->{$_} and
119
         $row->{$_} = $self->{end_filter}->{$_}->($row->{$_})
120
       for keys %{$self->{end_filter}};
121
  }
122
  $row;
micro optimization
Yuki Kimoto authored on 2011-11-07
123
}
124

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

            
135
sub fetch_hash_all {
cleanup
Yuki Kimoto authored on 2012-01-20
136
  my $self = shift;
137
  
138
  # Fetch all rows as hash
139
  my $rows = [];
140
  while(my $row = $self->fetch_hash) { push @$rows, $row }
141
  
142
  return $rows;
cleanup
yuki-kimoto authored on 2010-10-17
143
}
144

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
145
sub fetch_hash_one {
cleanup
Yuki Kimoto authored on 2012-01-20
146
  my $self = shift;
147
  
148
  # Fetch hash
149
  my $row = $self->fetch_hash;
150
  return unless $row;
151
  
152
  # Finish statement handle
153
  $self->sth->finish;
154
  
155
  return $row;
packaging one directory
yuki-kimoto authored on 2009-11-16
156
}
157

            
renamed fetch_rows to fetch_...
yuki-kimoto authored on 2010-05-01
158
sub fetch_hash_multi {
cleanup
Yuki Kimoto authored on 2012-01-20
159
  my ($self, $count) = @_;
160
  
161
  # Fetch multiple rows
162
  croak 'Row count must be specified ' . _subname
163
    unless $count;
164
  
165
  return if $self->{_finished};
166

            
167
  my $rows = [];
168
  for (my $i = 0; $i < $count; $i++) {
169
    my $row = $self->fetch_hash;
170
    unless ($row) {
171
      $self->{_finished} = 1;
172
      last;
packaging one directory
yuki-kimoto authored on 2009-11-16
173
    }
cleanup
Yuki Kimoto authored on 2012-01-20
174
    push @$rows, $row;
175
  }
176
  
177
  return unless @$rows;
178
  return $rows;
packaging one directory
yuki-kimoto authored on 2009-11-16
179
}
180

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

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

            
206
sub fetch_one {
cleanup
Yuki Kimoto authored on 2012-01-20
207
  my $self = shift;
208
  
209
  # Fetch
210
  my $row = $self->fetch;
211
  return unless $row;
212
  
213
  # Finish statement handle
214
  $self->sth->finish;
215
  
216
  return $row;
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
217
}
218

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

            
249
sub flat {
250
  my $self = shift;
251
  
252
  my @flat;
253
  while (my $row = $self->fetch) {
254
    push @flat, @$row;
255
  }
256
  return @flat;
257
}
258

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

            
262
  my $key_name = $self->{sth}{NAME}[0];
263
  my $kv = {};
264
  while (my $row = $self->fetch_hash) {
265
    my $key_value = delete $row->{$key_name};
266
    next unless defined $key_value;
267
    if ($opt{multi}) {
268
      $kv->{$key_value} ||= [];
269
      push @{$kv->{$key_value}}, $row;
270
    }
271
    else { $kv->{$key_value} = $row }
272
  }
273
  
274
  return $kv;
275
}
276

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

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

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2011-06-14
281
sub type_rule {
cleanup
Yuki Kimoto authored on 2012-01-20
282
  my $self = shift;
283
  
284
  if (@_) {
285
    my $type_rule = ref $_[0] eq 'HASH' ? $_[0] : {@_};
286

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

            
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
310
sub type_rule_off {
cleanup
Yuki Kimoto authored on 2012-01-20
311
  my $self = shift;
312
  $self->{type_rule_off} = 1;
313
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
314
}
315

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

            
322
sub type_rule1_off {
cleanup
Yuki Kimoto authored on 2012-01-20
323
  my $self = shift;
324
  $self->{type_rule1_off} = 1;
325
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
326
}
327

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

            
334
sub type_rule2_off {
cleanup
Yuki Kimoto authored on 2012-01-20
335
  my $self = shift;
336
  $self->{type_rule2_off} = 1;
337
  return $self;
- changed EXPERIMENTAL DBIx:...
Yuki Kimoto authored on 2011-06-20
338
}
339

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

            
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
346
sub value {
cleanup
Yuki Kimoto authored on 2012-01-20
347
  my $self = shift;
348
  my $row = $self->fetch_one;
349
  my $value = $row ? $row->[0] : undef;
350
  return $value;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
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

            
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
370
# DEPRECATED!
371
sub fetch_hash_first {
cleanup
Yuki Kimoto authored on 2012-01-20
372
  my $self = shift;
373
  warn "DBIx::Custom::Result::fetch_hash_first is DEPRECATED! use fetch_hash_one instead";
374
  return $self->fetch_hash_one(@_);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
375
}
376

            
377
# DEPRECATED!
378
sub fetch_first {
cleanup
Yuki Kimoto authored on 2012-01-20
379
  my $self = shift;
380
  warn "DBIx::Custom::Result::fetch_first is DEPRECATED! use fetch_one instead";
381
  return $self->fetch_one(@_);
- renamed DBIx::Custom::Resu...
Yuki Kimoto authored on 2012-01-20
382
}
383

            
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
384
# DEPRECATED!
385
sub filter_off {
cleanup
Yuki Kimoto authored on 2012-01-20
386
  warn "filter_off method is DEPRECATED!";
387
  my $self = shift;
388
  $self->{filter_off} = 1;
389
  return $self;
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
390
}
391

            
392
# DEPRECATED!
393
sub filter_on {
cleanup
Yuki Kimoto authored on 2012-01-20
394
  warn "filter_on method is DEPRECATED!";
395
  my $self = shift;
396
  $self->{filter_off} = 0;
397
  return $self;
- DBIx::Custom::Result filte...
Yuki Kimoto authored on 2011-11-03
398
}
399

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

            
update document
yuki-kimoto authored on 2010-01-30
463
1;
464

            
packaging one directory
yuki-kimoto authored on 2009-11-16
465
=head1 NAME
466

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

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

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

            
474
  # Fetch a row and put it into array reference
475
  while (my $row = $result->fetch) {
476
    my $author = $row->[0];
477
    my $title  = $row->[1];
478
  }
479
  
480
  # Fetch only a first row and put it into array reference
481
  my $row = $result->fetch_one;
482
  
483
  # Fetch all rows and put them into array of array reference
484
  my $rows = $result->fetch_all;
485

            
486
  # Fetch a row and put it into hash reference
487
  while (my $row = $result->fetch_hash) {
488
    my $title  = $row->{title};
489
    my $author = $row->{author};
490
  }
491
  
492
  # Fetch only a first row and put it into hash reference
493
  my $row = $result->fetch_hash_one;
494
  my $row = $result->one; # Alias for "fetch_hash_one"
495
  
496
  # Fetch all rows and put them into array of hash reference
497
  my $rows = $result->fetch_hash_all;
498
  my $rows = $result->all; # Alias for "fetch_hash_all"
packaging one directory
yuki-kimoto authored on 2009-11-16
499

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
504
  my $dbi = $result->dbi;
505
  $result = $result->dbi($dbi);
cleanup
yuki-kimoto authored on 2010-10-17
506

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

            
509
=head2 C<sth>
510

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

            
514
Statement handle of L<DBI>.
515

            
update document
yuki-kimoto authored on 2010-01-30
516
=head1 METHODS
517

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

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

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

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

            
- removed EXPERIMENTAL statu...
Yuki Kimoto authored on 2012-01-24
527
=head2 C<column>
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
528

            
cleanup
Yuki Kimoto authored on 2012-01-20
529
  my $column = $result->column;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
530

            
531
Get first column's all values.
532

            
cleanup
Yuki Kimoto authored on 2012-01-20
533
  my $names = $dbi->select('name', table => 'book')->column;
- added EXPERIMENTAL DBIx::C...
Yuki Kimoto authored on 2012-01-14
534

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

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

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

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

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

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

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

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

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

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

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

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

            
cleanup
yuki-kimoto authored on 2010-10-17
560
=head2 C<fetch_hash_all>
561

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

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

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

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

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2010-12-21
585
=head2 C<filter>
586

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

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

            
added EXPERIMENTAL DBIx::Cus...
Yuki Kimoto authored on 2012-02-28
594
=head2 C<flat> EXPERIMENTAL
595

            
596
  my $flat = $result->flat;
597

            
598
All value is flatten and added to one array reference.
599
  
600
  my @flat = $dbi->select(['id', 'title'])->flat;
601

            
602
If C<fetch_all> method return the following data
603

            
604
  [
605
    [1, 'Perl'],
606
    [2, 'Ruby']
607
  ]
608

            
609
C<flat> method return the following data.
610

            
611
  (1, 'Perl', 2, 'Ruby')
612

            
613
You can create key-value pair easily.
614

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

            
added EXPERIMETAL DBIx::Cust...
Yuki Kimoto authored on 2012-02-28
617
=head2 C<kv> EXPERIMENTAL
618

            
619
  my $key_value = $result->kv;
620
  my $key_values = $result->kv(multi => 1);
621

            
622
Get key-value pairs.
623

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

            
626
If C<all> method return the following data:
627

            
628
  [
629
    {id => 1, title => 'Perl', author => 'Ken'},
630
    {id => 2, title => 'Ruby', author => 'Taro'}
631
  ]
632

            
633
C<kv> method return the following data.
634

            
635
  {
636
    1 => {title => 'Perl', author => 'Ken'},
637
    2 => {title => 'Ruby', author => 'Taro'}
638
  }
639

            
640
First column value become key.
641

            
642
If value contains multipule data, you can push it to
643
array refernce by C<multi> option.
644

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

            
647
If C<all> method return the following data:
648

            
649
  [
650
    {author => 'Ken', title => 'Perl', price => 1000},
651
    {author => 'Ken', title => 'Good', price => 2000},
652
    {author => 'Taro', title => 'Ruby', price => 3000}
653
    {author => 'Taro', title => 'Sky', price => 4000}
654
  ]
655

            
656
C<kv> method return the following data.
657

            
658
  {
659
    Ken => [
660
      {title => 'Perl', price => 1000},
661
      {title => 'Good', price => 2000}
662
    ],
663
    Taro => [
664
      {title => 'Ruby', price => 3000},
665
      {title => 'Sky', price => 4000}
666
    ]
667
  }
668

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

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

            
673
Get header column names.
674

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

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

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

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

            
cleanup
Yuki Kimoto authored on 2012-01-20
683
  my $stash = $result->stash;
684
  my $foo = $result->stash->{foo};
685
  $result->stash->{foo} = $foo;
added experimental DBIx::Cus...
Yuki Kimoto authored on 2011-01-25
686

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

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

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

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

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

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

            
713
Turn C<from1> and C<from2> type rule off.
714
By default, type rule is on.
715

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

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

            
720
Turn C<from1> and C<from2> type rule on.
721
By default, type rule is on.
722

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

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

            
727
Turn C<from1> type rule off.
728
By default, type rule is on.
729

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

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

            
734
Turn C<from1> type rule on.
735
By default, type rule is on.
736

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

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

            
741
Turn C<from2> type rule off.
742
By default, type rule is on.
743

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

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

            
748
Turn C<from2> type rule on.
749
By default, type rule is on.
750

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

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

            
755
Get first column's first value.
756

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

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