Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Tie'ing a dbh result set?

by jfroebe (Vicar)
on Jul 25, 2011 at 19:37 UTC ( #916597=note: print w/ replies, xml ) Need Help??


in reply to Re: Tie'ing a dbh result set?
in thread Tie'ing a dbh result set?

Actually, it isn't that difficult to implement. Ultimately, I will be using a 'sliding window' for each result set to retrieve all the data transparently. As I really don't care about what I've already retrieved, I can pull back say 5,000 rows, on the request for 5001st row, purge the first 5000 rows and pull back another 5,000 from the database. The only thing I have to worry about is to to make sure the FETCH() and FETCHSIZE() handle the window.

Jason L. Froebe

Blog, Tech Blog


Comment on Re^2: Tie'ing a dbh result set?
Re^3: Tie'ing a dbh result set?
by thargas (Chaplain) on Jul 26, 2011 at 15:03 UTC

    If you can guarantee that the array will only be accessed:

    • in order
    • read only
    • only once per element
    • you know how many elements there are
    then you can do it by simply writing a FETCH which returns the next resultset and a FETCHSIZE which returns the correct size. It would work correctly for usage like:
    foreach my $result_ref (@array) { process($result_ref); }

    Otherwise, please explain how you make it function as an iterator.

Re^3: Tie'ing a dbh result set? (unknown size)
by tye (Cardinal) on Jul 26, 2011 at 17:12 UTC

    Actually, as part of playing with a new toy I wrote, I noticed that for( @tiedarray ) actually deals with the size of the array changing each iteration so you don't need to know the number of records up-front like I initially worried. You can just always fetch at least one record ahead and return the size of what you have fetched so far -- for for( @tiedarray ) to work fine as an iterator.

    - tye        

      One teeny tiny problem with that.. if the size of the Tie'd array is say a maximum of 5000 entries, the 5001st will not be returned correctly to your for loop as the size of the array is 5000 not 5001. (assuming you grabbed the next 5,000 rows)

      One way around this is by Tie'ing the array to a hash where the key is the index value. The next grab could either replace the key/value pairs with 5001/row data or purge the hash data structure and assign a new one depending on which is better for the application in question. The FETCHSIZE() will have to report this pseudo size so the for loop thinks there is another entry to retrieve.

      The module Tie::File implements something similar.

      Jason L. Froebe

      Blog, Tech Blog

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://916597]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-09-16 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls