Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Icon > Re: Generator "...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 6 Topic 132 of 189
Post > Topic >>

Re: Generator "Cleanup" Question

by Steve Wampler <swampler@[EMAIL PROTECTED] > Nov 27, 2006 at 04:22 PM

Cp200205@[EMAIL PROTECTED]
 wrote:
>> The fetching of the result sequence is not really a logical
>> part of the generation of events from the result sequence,
>> hence combining that (and the free of the result sequence)
>> into a single procedure may not be the way to go.
> 
> I thought it work kind of well:
> 
> every person := dbi_conn_find(conn, "SELECT * FROM people") do
>     displayPerson(person)
> 
> That prevented me from having to deal with having a var for storing the
> result, doing the actual query, and freeing the query when I was done
> with it.

Oh, I know, that is attractive.  And you can still write dbi_conn_find()
that way for the situation where you want all results displayed.  Or,
write
a procedure that applies displayPerson to every result:

    applyToRes(conn, "SELECT * FROM people", displayPerson)

    procedure applyToRes(conn, sql, func)
        res := dbi_conn_query(conn, sql)
        every func(dbi_result_fetch_next_table(res))
        dbi_result_free(res)
    end

which has the same drawback as the original [only works 'right' when
you want all of them displayed].  *But* consider the following version:

    procedure applyToRes(conn, sql, func, limit)
       res := dbi_conn_query(conn, sql)
       if \limit then
           every func(dbi_result_fetch_next_table(res))\limit
       else
           every func(dbi_result_fetch_next_table(res))
       dbi_result_free(res)
    end

so, if you want only the first result:

    applyToRes(conn, "SELECT * FROM people", displayPerson, 1)

Of course, once you allow this, then you could more efficiently write:

    procedure applyToRes(conn, sql, func, limit)
        if \limit then sql ||:= " LIMIT "||limit
        res := dbi_conn_query(conn, sql)
        every func(dbi_result_fetch_next_table(res))
        dbi_result_free(res)
    end

> However, I see now that's not possible, so I'll just omit the function
> and use:
> 
> res := dbi_conn_query(conn, "SELECT * FROM people")
> every person := dbi_result_fetch_next_table(res) do
>   displayPerson(person)
> dbi_result_free(res)
> 
> Add's two more lines and a declared var, no huge deal, just though the
> other was rather nice.

You can also write:

  res := dbi_conn_query(conn, "SELECT * FROM people")
  every displayPerson(dbi_result_fetch_next_table(res))
  dbi_result_free(res)

which gets rid of one variable (just a different one than before).

And, if you're feeling perverse, consider:

   res := dbi_conn_query(conn, "SELECT * FROM people")
   every displayPerson(
           (dbi_result_fetch_next_table|dbi_result_free)(res))

which works if dbi_result_free() always fails.  Not
that I'd wish that on any sane human, mind you...

-- 
Steve Wampler -- swampler@[EMAIL PROTECTED]
 gods that smiled on your birth are now laughing out loud.
 




 6 Posts in Topic:
Generator "Cleanup" Question
Cp200205@[EMAIL PROTECTED  2006-11-26 08:22:20 
Re: Generator "Cleanup" Question
gmt@[EMAIL PROTECTED] (G  2006-11-27 19:40:23 
Re: Generator "Cleanup" Question
espie@[EMAIL PROTECTED]   2006-11-27 20:45:30 
Re: Generator "Cleanup" Question
Steve Wampler <swample  2006-11-27 14:12:14 
Re: Generator "Cleanup" Question
Cp200205@[EMAIL PROTECTED  2006-11-27 14:15:29 
Re: Generator "Cleanup" Question
Steve Wampler <swample  2006-11-27 16:22:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 17:15:03 CDT 2008.