| ... | ... |
@@ -47,7 +47,7 @@ Default filter when parameter binding is executed. |
| 47 | 47 |
title => 'encode_utf8'}); |
| 48 | 48 |
|
| 49 | 49 |
Filters when parameter binding is executed. |
| 50 |
-This overrides C<default_filter>. |
|
| 50 |
+This overwrites C<default_filter>. |
|
| 51 | 51 |
|
| 52 | 52 |
=head2 C<sth> |
| 53 | 53 |
|
| ... | ... |
@@ -12,25 +12,27 @@ __PACKAGE__->attr([qw/sth filters default_filter filter/]); |
| 12 | 12 |
sub fetch {
|
| 13 | 13 |
my $self = shift; |
| 14 | 14 |
|
| 15 |
+ # Filters |
|
| 15 | 16 |
$self->{filters} ||= {};
|
| 16 | 17 |
$self->{filter} ||= {};
|
| 17 | 18 |
|
| 18 | 19 |
# Fetch |
| 19 | 20 |
my @row = $self->{sth}->fetchrow_array;
|
| 20 | 21 |
|
| 21 |
- # Cannot fetch |
|
| 22 |
+ # No row |
|
| 22 | 23 |
return unless @row; |
| 23 | 24 |
|
| 24 |
- # Filter |
|
| 25 |
- for (my $i = 0; $i < @{$self->{sth}->{NAME_lc}}; $i++) {
|
|
| 25 |
+ # Filtering |
|
| 26 |
+ my $columns = $self->{sth}->{NAME_lc};
|
|
| 27 |
+ for (my $i = 0; $i < @$columns; $i++) {
|
|
| 26 | 28 |
|
| 27 | 29 |
# Filter name |
| 28 |
- my $column = $self->{sth}->{NAME_lc}->[$i];
|
|
| 30 |
+ my $column = $columns->[$i]; |
|
| 29 | 31 |
my $fname = exists $self->{filter}->{$column}
|
| 30 | 32 |
? $self->{filter}->{$column}
|
| 31 | 33 |
: $self->{default_filter};
|
| 32 | 34 |
|
| 33 |
- # Filter |
|
| 35 |
+ # Filtering |
|
| 34 | 36 |
$row[$i] = $self->{filters}->{$fname}->($row[$i])
|
| 35 | 37 |
if $fname; |
| 36 | 38 |
} |
| ... | ... |
@@ -44,7 +46,7 @@ sub fetch_first {
|
| 44 | 46 |
# Fetch |
| 45 | 47 |
my $row = $self->fetch; |
| 46 | 48 |
|
| 47 |
- # Not exist |
|
| 49 |
+ # No row |
|
| 48 | 50 |
return unless $row; |
| 49 | 51 |
|
| 50 | 52 |
# Finish statement handle |
| ... | ... |
@@ -56,7 +58,7 @@ sub fetch_first {
|
| 56 | 58 |
sub fetch_multi {
|
| 57 | 59 |
my ($self, $count) = @_; |
| 58 | 60 |
|
| 59 |
- # Not specified Row count |
|
| 61 |
+ # Row count not specifed |
|
| 60 | 62 |
croak 'Row count must be specified' |
| 61 | 63 |
unless $count; |
| 62 | 64 |
|
| ... | ... |
@@ -64,9 +66,7 @@ sub fetch_multi {
|
| 64 | 66 |
my $rows = []; |
| 65 | 67 |
for (my $i = 0; $i < $count; $i++) {
|
| 66 | 68 |
my $row = $self->fetch; |
| 67 |
- |
|
| 68 | 69 |
last unless $row; |
| 69 |
- |
|
| 70 | 70 |
push @$rows, $row; |
| 71 | 71 |
} |
| 72 | 72 |
|
| ... | ... |
@@ -88,6 +88,7 @@ sub fetch_all {
|
| 88 | 88 |
sub fetch_hash {
|
| 89 | 89 |
my $self = shift; |
| 90 | 90 |
|
| 91 |
+ # Filters |
|
| 91 | 92 |
$self->{filters} ||= {};
|
| 92 | 93 |
$self->{filter} ||= {};
|
| 93 | 94 |
|
| ... | ... |
@@ -99,15 +100,16 @@ sub fetch_hash {
|
| 99 | 100 |
|
| 100 | 101 |
# Filter |
| 101 | 102 |
my $row_hash = {};
|
| 102 |
- for (my $i = 0; $i < @{$self->{sth}->{NAME_lc}}; $i++) {
|
|
| 103 |
+ my $columns = $self->{sth}->{NAME_lc};
|
|
| 104 |
+ for (my $i = 0; $i < @$columns; $i++) {
|
|
| 103 | 105 |
|
| 104 | 106 |
# Filter name |
| 105 |
- my $column = $self->{sth}->{NAME_lc}->[$i];
|
|
| 107 |
+ my $column = $columns->[$i]; |
|
| 106 | 108 |
my $fname = exists $self->{filter}->{$column}
|
| 107 | 109 |
? $self->{filter}->{$column}
|
| 108 | 110 |
: $self->{default_filter};
|
| 109 | 111 |
|
| 110 |
- # Filter |
|
| 112 |
+ # Filtering |
|
| 111 | 113 |
$row_hash->{$column}
|
| 112 | 114 |
= $fname ? $self->{filters}->{$fname}->($row->[$i])
|
| 113 | 115 |
: $row->[$i]; |
| ... | ... |
@@ -122,7 +124,7 @@ sub fetch_hash_first {
|
| 122 | 124 |
# Fetch hash |
| 123 | 125 |
my $row = $self->fetch_hash; |
| 124 | 126 |
|
| 125 |
- # Not exist |
|
| 127 |
+ # No row |
|
| 126 | 128 |
return unless $row; |
| 127 | 129 |
|
| 128 | 130 |
# Finish statement handle |
| ... | ... |
@@ -134,7 +136,7 @@ sub fetch_hash_first {
|
| 134 | 136 |
sub fetch_hash_multi {
|
| 135 | 137 |
my ($self, $count) = @_; |
| 136 | 138 |
|
| 137 |
- # Not specified Row count |
|
| 139 |
+ # Row count not specified |
|
| 138 | 140 |
croak 'Row count must be specified' |
| 139 | 141 |
unless $count; |
| 140 | 142 |
|
| ... | ... |
@@ -142,9 +144,7 @@ sub fetch_hash_multi {
|
| 142 | 144 |
my $rows = []; |
| 143 | 145 |
for (my $i = 0; $i < $count; $i++) {
|
| 144 | 146 |
my $row = $self->fetch_hash; |
| 145 |
- |
|
| 146 | 147 |
last unless $row; |
| 147 |
- |
|
| 148 | 148 |
push @$rows, $row; |
| 149 | 149 |
} |
| 150 | 150 |
|
| ... | ... |
@@ -168,46 +168,58 @@ sub fetch_hash_all {
|
| 168 | 168 |
|
| 169 | 169 |
=head1 NAME |
| 170 | 170 |
|
| 171 |
-DBIx::Custom::Result - Result of select |
|
| 171 |
+DBIx::Custom::Result - Result of select statement |
|
| 172 | 172 |
|
| 173 | 173 |
=head1 SYNOPSIS |
| 174 |
- |
|
| 174 |
+ |
|
| 175 |
+Get the result of select statement. |
|
| 176 |
+ |
|
| 175 | 177 |
# Result |
| 176 | 178 |
my $result = $dbi->select(table => 'books'); |
| 179 |
+ |
|
| 180 |
+Fetch row into array. |
|
| 177 | 181 |
|
| 178 | 182 |
# Fetch a row into array |
| 179 | 183 |
while (my $row = $result->fetch) {
|
| 180 |
- my $value1 = $row->[0]; |
|
| 181 |
- my $valuu2 = $row->[1]; |
|
| 184 |
+ my $author = $row->[0]; |
|
| 185 |
+ my $title = $row->[1]; |
|
| 182 | 186 |
|
| 183 |
- # do something |
|
| 184 | 187 |
} |
| 185 | 188 |
|
| 186 |
- # Fetch only first row into array |
|
| 189 |
+ # Fetch only a first row into array |
|
| 187 | 190 |
my $row = $result->fetch_first; |
| 188 | 191 |
|
| 189 | 192 |
# Fetch multiple rows into array of array |
| 190 | 193 |
while (my $rows = $result->fetch_multi(5)) {
|
| 191 |
- # do something |
|
| 194 |
+ my $first_author = $rows->[0][0]; |
|
| 195 |
+ my $first_title = $rows->[0][1]; |
|
| 196 |
+ my $second_author = $rows->[1][0]; |
|
| 197 |
+ my $second_value = $rows->[1][1]; |
|
| 198 |
+ |
|
| 192 | 199 |
} |
| 193 | 200 |
|
| 194 | 201 |
# Fetch all rows into array of array |
| 195 | 202 |
my $rows = $result->fetch_all; |
| 196 |
- |
|
| 197 |
- # Fetch hash into hash |
|
| 203 |
+ |
|
| 204 |
+Fetch row into hash. |
|
| 205 |
+ |
|
| 206 |
+ # Fetch a row into hash |
|
| 198 | 207 |
while (my $row = $result->fetch_hash) {
|
| 199 |
- my $value1 = $row->{title};
|
|
| 200 |
- my $value2 = $row->{author};
|
|
| 208 |
+ my $title = $row->{title};
|
|
| 209 |
+ my $author = $row->{author};
|
|
| 201 | 210 |
|
| 202 |
- # do something |
|
| 203 | 211 |
} |
| 204 | 212 |
|
| 205 |
- # Fetch only first row into hash |
|
| 213 |
+ # Fetch only a first row into hash |
|
| 206 | 214 |
my $row = $result->fetch_hash_first; |
| 207 | 215 |
|
| 208 | 216 |
# Fetch multiple rows into array of hash |
| 209 |
- while (my $rows = $result->fetch_hash_multi) {
|
|
| 210 |
- # do something |
|
| 217 |
+ while (my $rows = $result->fetch_hash_multi(5)) {
|
|
| 218 |
+ my $first_title = $rows->[0]{title};
|
|
| 219 |
+ my $first_author = $rows->[0]{author};
|
|
| 220 |
+ my $second_title = $rows->[1]{title};
|
|
| 221 |
+ my $second_author = $rows->[1]{author};
|
|
| 222 |
+ |
|
| 211 | 223 |
} |
| 212 | 224 |
|
| 213 | 225 |
# Fetch all rows into array of hash |
| ... | ... |
@@ -220,93 +232,76 @@ DBIx::Custom::Result - Result of select |
| 220 | 232 |
my $sth = $reuslt->sth |
| 221 | 233 |
$result = $result->sth($sth); |
| 222 | 234 |
|
| 223 |
-Statement handle. |
|
| 235 |
+Statement handle of L<DBI>. |
|
| 224 | 236 |
|
| 225 | 237 |
=head2 C<default_filter> |
| 226 | 238 |
|
| 227 | 239 |
my $default_filter = $result->default_filter; |
| 228 | 240 |
$result = $result->default_filter('decode_utf8');
|
| 229 | 241 |
|
| 230 |
-Default filter for fetching. |
|
| 242 |
+Default filter when a row is fetched. |
|
| 231 | 243 |
|
| 232 | 244 |
=head2 C<filter> |
| 233 | 245 |
|
| 234 | 246 |
my $filter = $result->filter; |
| 235 |
- $result = $result->filter({title => 'decode_utf8'});
|
|
| 247 |
+ $result = $result->filter({title => 'decode_utf8',
|
|
| 248 |
+ author => 'decode_utf8'}); |
|
| 236 | 249 |
|
| 237 |
-Filters for fetching. |
|
| 250 |
+Filters when a row is fetched. |
|
| 251 |
+This overwrites C<default_filter>. |
|
| 238 | 252 |
|
| 239 | 253 |
=head1 METHODS |
| 240 | 254 |
|
| 241 |
-This class is L<Object::Simple> subclass. |
|
| 242 |
-You can use all methods of L<Object::Simple> |
|
| 255 |
+L<DBIx::Custom::Resutl> inherits all methods from L<Object::Simple> |
|
| 256 |
+and implements the following new ones. |
|
| 243 | 257 |
|
| 244 | 258 |
=head2 C<fetch> |
| 245 | 259 |
|
| 246 |
- $row = $result->fetch; |
|
| 260 |
+ my $row = $result->fetch; |
|
| 247 | 261 |
|
| 248 |
-Fetch a row into array |
|
| 249 |
- |
|
| 250 |
- while (my $row = $result->fetch) {
|
|
| 251 |
- # do something |
|
| 252 |
- my $value1 = $row->[0]; |
|
| 253 |
- my $value2 = $row->[1]; |
|
| 254 |
- } |
|
| 262 |
+Fetch a row into array. |
|
| 255 | 263 |
|
| 256 | 264 |
=head2 C<fetch_first> |
| 257 | 265 |
|
| 258 |
- $row = $result->fetch_first; |
|
| 266 |
+ my $row = $result->fetch_first; |
|
| 259 | 267 |
|
| 260 |
-Fetch only first row into array and finish statment handle. |
|
| 268 |
+Fetch only a first row into array and finish statment handle. |
|
| 261 | 269 |
|
| 262 | 270 |
=head2 C<fetch_multi> |
| 263 | 271 |
|
| 264 |
- $rows = $result->fetch_multi($count); |
|
| 272 |
+ my $rows = $result->fetch_multi(5); |
|
| 265 | 273 |
|
| 266 | 274 |
Fetch multiple rows into array of array. |
| 267 |
- |
|
| 268 |
- while(my $rows = $result->fetch_multi(10)) {
|
|
| 269 |
- # do someting |
|
| 270 |
- } |
|
| 275 |
+Row count must be specified. |
|
| 271 | 276 |
|
| 272 | 277 |
=head2 C<fetch_all> |
| 273 | 278 |
|
| 274 |
- $rows = $result->fetch_all; |
|
| 279 |
+ my $rows = $result->fetch_all; |
|
| 275 | 280 |
|
| 276 | 281 |
Fetch all rows into array of array. |
| 277 | 282 |
|
| 278 | 283 |
=head2 C<fetch_hash> |
| 279 | 284 |
|
| 280 |
- $row = $result->fetch_hash; |
|
| 285 |
+ my $row = $result->fetch_hash; |
|
| 281 | 286 |
|
| 282 | 287 |
Fetch a row into hash |
| 283 | 288 |
|
| 284 |
- while (my $row = $result->fetch_hash) {
|
|
| 285 |
- my $val1 = $row->{title};
|
|
| 286 |
- my $val2 = $row->{author};
|
|
| 287 |
- |
|
| 288 |
- # do something |
|
| 289 |
- } |
|
| 290 |
- |
|
| 291 | 289 |
=head2 C<fetch_hash_first> |
| 292 | 290 |
|
| 293 |
- $row = $result->fetch_hash_first; |
|
| 291 |
+ my $row = $result->fetch_hash_first; |
|
| 294 | 292 |
|
| 295 | 293 |
Fetch only first row into hash and finish statment handle. |
| 296 | 294 |
|
| 297 | 295 |
=head2 C<fetch_hash_multi> |
| 298 | 296 |
|
| 299 |
- $rows = $result->fetch_hash_multi($count); |
|
| 297 |
+ my $rows = $result->fetch_hash_multi(5); |
|
| 300 | 298 |
|
| 301 | 299 |
Fetch multiple rows into array of hash |
| 302 |
- |
|
| 303 |
- while(my $rows = $result->fetch_hash_multi(10)) {
|
|
| 304 |
- # do someting |
|
| 305 |
- } |
|
| 300 |
+Row count must be specified. |
|
| 306 | 301 |
|
| 307 | 302 |
=head2 C<fetch_hash_all> |
| 308 | 303 |
|
| 309 |
- $rows = $result->fetch_hash_all; |
|
| 304 |
+ my $rows = $result->fetch_hash_all; |
|
| 310 | 305 |
|
| 311 | 306 |
Fetch all rows into array of hash. |
| 312 | 307 |
|