Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Tie'ing a dbh result set?

by thargas (Deacon)
on Jul 25, 2011 at 18:02 UTC ( [id://916573]=note: print w/replies, xml ) Need Help??


in reply to Tie'ing a dbh result set?

It depends on how you access the info. The docs for tieing an array don't include any hook for iteration; they've only got: FETCH, STORE, FETCHSIZE, STORESIZE, EXTEND, EXISTS, DELETE, CLEAR, PUSH, POP, UNSHIFT, SHIFT, SPLICE, UNTIE, DESTROY.

If you're only pulling the info out via unshift or pop, you might be able to make something work, but you'd have to booby-trap FETCH to ensure that the data was only accessed via unshift and pop. Sounds ugly. You may have to bite the bullet.

Or maybe someone cleverer than I will have a brilliant method.

Replies are listed 'Best First'.
Re^2: Tie'ing a dbh result set?
by jfroebe (Parson) on Jul 25, 2011 at 19:37 UTC

    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

      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

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-18 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found