| 1 | ############################################## | ||
| 2 | ## Copyright (c) 2002-2004 - Brendan Fagan | ||
| 3 | ############################################## | ||
| 4 | package DBIx::FetchLoop; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use vars qw($VERSION @ISA); | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | use DBI; | ||
| 11 | |||
| 12 | $VERSION = '0.6'; | ||
| 13 | |||
| 14 | ############################################## | ||
| 15 | ## public methods | ||
| 16 | ############################################## | ||
| 17 | sub new { | ||
| 18 | my ($class,$sth,$dbi_method) = @_; | ||
| 19 | unless($sth) { | ||
| 20 | my ($p,$f,$l) = caller(); | ||
| 21 | croak("DBIx::FetchLoop statement handle empty from $p, $f at line $l\n"); | ||
| 22 | } | ||
| 23 | unless($sth->execute()) { | ||
| 24 | my ($p,$f,$l) = caller(); | ||
| 25 | croak("DBIx::FetchLoop failed execute from $p, $f at line $l\n"); | ||
| 26 | } | ||
| 27 | my $obj = bless { | ||
| 28 | _sth => $sth, | ||
| 29 | _dbi_method => $dbi_method || 'fetchrow_hashref', | ||
| 30 | _data => {}, | ||
| 31 | _aggregates => {}, | ||
| 32 | _agg_list => [], | ||
| 33 | _concatenates => {}, | ||
| 34 | _cat_list => [], | ||
| 35 | _flags => {}, | ||
| 36 | _count => 0 | ||
| 37 | }, $class; | ||
| 38 | push(@ISA,"DBIx::FetchLoop::$obj->{_dbi_method}"); | ||
| 39 | return $obj; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub fetch_current_data { | ||
| 43 | my $obj = shift; | ||
| 44 | |||
| 45 | unless ($obj->{_flags}->{done} == 1) { | ||
| 46 | |||
| 47 | my $current = $obj->{_data}->{current} || undef; | ||
| 48 | my $next = $obj->{_data}->{next} || undef; | ||
| 49 | |||
| 50 | undef $obj->{_data}->{previous}; | ||
| 51 | undef $obj->{_data}->{current}; | ||
| 52 | undef $obj->{_data}->{next}; | ||
| 53 | |||
| 54 | if ($current) { | ||
| 55 | $obj->{_data}->{previous} = $current; | ||
| 56 | } else { | ||
| 57 | $obj->{_data}->{current} = $obj->_fetch || undef; | ||
| 58 | if ($obj->{_data}->{current}) { | ||
| 59 | $obj->{_count}++; | ||
| 60 | } else { | ||
| 61 | $obj->{_flags}->{done} = 1; | ||
| 62 | $obj->{_sth}->finish(); | ||
| 63 | undef $obj->{_sth}; | ||
| 64 | return undef; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | undef $current; | ||
| 69 | |||
| 70 | if ($next) { | ||
| 71 | $obj->{_data}->{current} = $next; | ||
| 72 | $obj->{_count}++; | ||
| 73 | } | ||
| 74 | |||
| 75 | undef $next; | ||
| 76 | |||
| 77 | unless ($obj->{_data}->{next} = $obj->_fetch) { | ||
| 78 | $obj->{_flags}->{done} = 1; | ||
| 79 | $obj->{_sth}->finish(); | ||
| 80 | undef $obj->{_sth}; | ||
| 81 | } | ||
| 82 | |||
| 83 | $obj->_process_aggregates; | ||
| 84 | $obj->_process_concatenates; | ||
| 85 | |||
| 86 | if ($obj->{_data}->{current}) { | ||
| 87 | return $obj->{_data}; | ||
| 88 | } | ||
| 89 | } return undef; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub fetch_current_row { | ||
| 93 | my $obj = shift; | ||
| 94 | |||
| 95 | my $data = $obj->fetch_current_data; | ||
| 96 | if ($data) { | ||
| 97 | return $data->{current}; | ||
| 98 | } else { | ||
| 99 | return undef; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub previous { | ||
| 104 | my $obj = shift; | ||
| 105 | |||
| 106 | CASE: { | ||
| 107 | unless ($obj->{_data}) { | ||
| 108 | return undef; | ||
| 109 | last CASE; | ||
| 110 | } | ||
| 111 | |||
| 112 | unless ($obj->{_data}->{previous}) { | ||
| 113 | return undef; | ||
| 114 | last CASE; | ||
| 115 | } | ||
| 116 | |||
| 117 | return $obj->{_data}->{previous}; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | sub current { | ||
| 122 | my $obj = shift; | ||
| 123 | |||
| 124 | CASE: { | ||
| 125 | unless ($obj->{_data}) { | ||
| 126 | return undef; | ||
| 127 | last CASE; | ||
| 128 | } | ||
| 129 | |||
| 130 | unless ($obj->{_data}->{current}) { | ||
| 131 | return undef; | ||
| 132 | last CASE; | ||
| 133 | } | ||
| 134 | |||
| 135 | return $obj->{_data}->{current}; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | sub next { | ||
| 140 | my $obj = shift; | ||
| 141 | |||
| 142 | CASE: { | ||
| 143 | unless ($obj->{_data}) { | ||
| 144 | return undef; | ||
| 145 | last CASE; | ||
| 146 | } | ||
| 147 | |||
| 148 | unless ($obj->{_data}->{next}) { | ||
| 149 | return undef; | ||
| 150 | last CASE; | ||
| 151 | } | ||
| 152 | |||
| 153 | return $obj->{_data}->{next}; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub is_first { | ||
| 158 | my $obj = shift; | ||
| 159 | if ($obj->{_count} == 1) { return 1; } | ||
| 160 | else { return 0; } | ||
| 161 | } | ||
| 162 | |||
| 163 | sub is_last { | ||
| 164 | my $obj = shift; | ||
| 165 | if ($obj->{_flags}->{done} == 1) { return 1; } | ||
| 166 | else { return 0; } | ||
| 167 | } | ||
| 168 | |||
| 169 | sub count { | ||
| 170 | my $obj = shift; | ||
| 171 | $obj->{_count}; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub set_aggregate { | ||
| 175 | my ($obj,$aggregate,$field) = (@_); | ||
| 176 | |||
| 177 | unless ($obj->{_flags}->{kinetic} == 1) { | ||
| 178 | $obj->{_aggregates}->{$aggregate} = $field; | ||
| 179 | my $array = $obj->{_agg_list}; | ||
| 180 | push(@$array,$aggregate); | ||
| 181 | } else { | ||
| 182 | warn('Error: set_aggregate must be called before fetch_current_data or fetch_current_row'); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | sub set_concatenate { | ||
| 186 | my ($obj,$concatenate,$field) = (@_); | ||
| 187 | |||
| 188 | unless ($obj->{_flags}->{kinetic} == 1) { | ||
| 189 | $obj->{_concatenates}->{$concatenate} = $field; | ||
| 190 | my $array = $obj->{_cat_list}; | ||
| 191 | push(@$array,$concatenate); | ||
| 192 | } else { | ||
| 193 | warn('Error: set_concatenate must be called before fetch_current_data or fetch_current_row'); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | sub reset_aggregate { | ||
| 198 | my ($obj,$aggregate) = (@_); | ||
| 199 | $obj->_reset_field($aggregate); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub reset_concatenate { | ||
| 203 | my ($obj,$concatenate) = (@_); | ||
| 204 | $obj->_reset_field($concatenate); | ||
| 205 | } | ||
| 206 | |||
| 207 | ############################################## | ||
| 208 | ## Module cleanup | ||
| 209 | ############################################## | ||
| 210 | sub DESTROY { | ||
| 211 | my $obj = shift; | ||
| 212 | |||
| 213 | if (($obj->{_flags}->{kinetic} == 1) && ($obj->{_flags}->{done} != 1)) { | ||
| 214 | if ($obj->{_sth}) { | ||
| 215 | $obj->{_sth}->finish; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | ############################################## | ||
| 221 | ## private methods: fetchrow_hashref | ||
| 222 | ############################################## | ||
| 223 | package DBIx::FetchLoop::fetchrow_hashref; | ||
| 224 | |||
| 225 | sub _fetch { | ||
| 226 | my $obj = shift; | ||
| 227 | return $obj->{_sth}->fetchrow_hashref; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub _process_aggregates { | ||
| 231 | my $obj = shift; | ||
| 232 | my $agg_list = $obj->{_agg_list}; | ||
| 233 | foreach my $aggregate (@$agg_list) { | ||
| 234 | my $field = $obj->{_aggregates}->{$aggregate}; | ||
| 235 | $obj->{_data}->{current}->{$aggregate} = | ||
| 236 | $obj->{_data}->{previous}->{$aggregate} + $obj->{_data}->{current}->{$field}; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | sub _process_concatenates { | ||
| 241 | my $obj = shift; | ||
| 242 | my $cat_list = $obj->{_cat_list}; | ||
| 243 | foreach my $concatenate (@$cat_list) { | ||
| 244 | my $field = $obj->{_concatenates}->{$concatenate}; | ||
| 245 | $obj->{_data}->{current}->{$concatenate} = | ||
| 246 | $obj->{_data}->{previous}->{$concatenate} . $obj->{_data}->{current}->{$field}; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | sub pre_loop { | ||
| 250 | my ($obj,$field) = @_; | ||
| 251 | if ($field) { | ||
| 252 | if ($obj->{_data}->{previous}->{$field} ne $obj->{_data}->{current}->{$field}) { | ||
| 253 | return 1; | ||
| 254 | } else { | ||
| 255 | return 0; | ||
| 256 | } | ||
| 257 | } else { | ||
| 258 | warn('Error: $field required when calling pre_loop'); | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | sub pre_loop_substr { | ||
| 263 | my ($obj,$field,$offset,$length) = @_; | ||
| 264 | if ($field && $length) { | ||
| 265 | if (uc(substr($obj->{_data}->{previous}->{$field},$offset,$length)) ne uc(substr($obj->{_data}->{current}->{$field},$offset,$length))) { | ||
| 266 | return 1; | ||
| 267 | } else { | ||
| 268 | return 0; | ||
| 269 | } | ||
| 270 | } else { | ||
| 271 | warn('Error: $field, $offset, $length required when calling pre_loop_substr'); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | sub post_loop { | ||
| 276 | my ($obj,$field) = (@_); | ||
| 277 | if ($field) { | ||
| 278 | if ($obj->{_data}->{current}->{$field} ne $obj->{_data}->{next}->{$field}) { | ||
| 279 | return 1; | ||
| 280 | } else { | ||
| 281 | return 0; | ||
| 282 | } | ||
| 283 | } else { | ||
| 284 | warn('Error: $field required when calling post_loop'); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | sub post_loop_substr { | ||
| 289 | my ($obj,$field,$offset,$length) = @_; | ||
| 290 | if ($field && $length) { | ||
| 291 | if (substr($obj->{_data}->{current}->{$field},$offset,$length) ne substr($obj->{_data}->{next}->{$field},$offset,$length)) { | ||
| 292 | return 1; | ||
| 293 | } else { | ||
| 294 | return 0; | ||
| 295 | } | ||
| 296 | } else { | ||
| 297 | warn('Error: $field, $offset, $length required when calling post_loop_substr'); | ||
| 298 | } | ||
| 299 | } | ||
| 300 | |||
| 301 | sub _reset_field { | ||
| 302 | my ($obj,$field) = @_; | ||
| 303 | $obj->{_data}->{current}->{$field} = undef; | ||
| 304 | } | ||
| 305 | |||
| 306 | ############################################## | ||
| 307 | ## private methods: fetchrow_arrayref | ||
| 308 | ############################################## | ||
| 309 | package DBIx::FetchLoop::fetchrow_arrayref; | ||
| 310 | |||
| 311 | sub _fetch { | ||
| 312 | my $obj = shift; | ||
| 313 | return $obj->{_sth}->fetchrow_arrayref; | ||
| 314 | } | ||
| 315 | |||
| 316 | sub _process_aggregates { | ||
| 317 | my $obj = shift; | ||
| 318 | my $agg_list = $obj->{_agg_list}; | ||
| 319 | foreach my $aggregate (@$agg_list) { | ||
| 320 | my $field = $obj->{_aggregates}->{$aggregate}; | ||
| 321 | $obj->{_data}->{current}->[$aggregate] = | ||
| 322 | $obj->{_data}->{previous}->[$aggregate] + $obj->{_data}->{current}->[$field]; | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | sub _process_concatenates { | ||
| 327 | my $obj = shift; | ||
| 328 | my $cat_list = $obj->{_cat_list}; | ||
| 329 | foreach my $concatenate (@$cat_list) { | ||
| 330 | my $field = $obj->{_concatenates}->{$concatenate}; | ||
| 331 | $obj->{_data}->{current}->[$concatenate] = | ||
| 332 | $obj->{_data}->{previous}->[$concatenate] . $obj->{_data}->{current}->[$field]; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | sub pre_loop { | ||
| 337 | my ($obj,$field) = (@_); | ||
| 338 | if ($field) { | ||
| 339 | if ($obj->{_data}->{previous}->[$field] ne $obj->{_data}->{current}->[$field]) { | ||
| 340 | return 1; | ||
| 341 | } else { | ||
| 342 | return 0; | ||
| 343 | } | ||
| 344 | } else { | ||
| 345 | warn('Error: $field required when calling pre_loop'); | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | sub pre_loop_substr { | ||
| 350 | my ($obj,$field,$offset,$length) = @_; | ||
| 351 | if ($field && $length) { | ||
| 352 | if (substr($obj->{_data}->{previous}->[$field],$offset,$length) ne substr($obj->{_data}->{current}->[$field],$offset,$length)) { | ||
| 353 | return 1; | ||
| 354 | } else { | ||
| 355 | return 0; | ||
| 356 | } | ||
| 357 | } else { | ||
| 358 | warn('Error: $field, $offset, $length required when calling pre_loop_substr'); | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | sub post_loop { | ||
| 363 | my ($obj,$field) = (@_); | ||
| 364 | if ($field) { | ||
| 365 | if ($obj->{_data}->{current}->[$field] ne $obj->{_data}->[next]->{$field}) { | ||
| 366 | return 1; | ||
| 367 | } else { | ||
| 368 | return 0; | ||
| 369 | } | ||
| 370 | } else { | ||
| 371 | warn('Error: $field required when calling post_loop'); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | |||
| 375 | sub post_loop_substr { | ||
| 376 | my ($obj,$field,$offset,$length) = @_; | ||
| 377 | if ($field && $length) { | ||
| 378 | if (substr($obj->{_data}->{current}->[$field],$offset,$length) ne substr($obj->{_data}->{next}->[$field],$offset,$length)) { | ||
| 379 | return 1; | ||
| 380 | } else { | ||
| 381 | return 0; | ||
| 382 | } | ||
| 383 | } else { | ||
| 384 | warn('Error: $field, $offset, $length required when calling post_loop_substr'); | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | sub _reset_field { | ||
| 389 | my ($obj,$field) = @_; | ||
| 390 | $obj->{_data}->{current}->[$field] = undef; | ||
| 391 | } | ||
| 392 | |||
| 393 | 1; | ||
| 394 | __END__ |