Re: Resdhift's lack of cursors and PQsetSingleRowMode - Mailing list psycopg

From Marko Kreen
Subject Re: Resdhift's lack of cursors and PQsetSingleRowMode
Date
Msg-id 20131227143846.GA14697@gmail.com
Whole thread Raw
In response to Re: Resdhift's lack of cursors and PQsetSingleRowMode  (Daniele Varrazzo <daniele.varrazzo@gmail.com>)
List psycopg
On Fri, Dec 27, 2013 at 12:59:52PM +0000, Daniele Varrazzo wrote:
> On Thu, Dec 26, 2013 at 1:37 PM, Marko Kreen <markokr@gmail.com> wrote:
> > The single row mode is designed for following high-level API:
> >
> >     curs = db.single-row-mode-cursor()
> >     curs.execute(sql)
> >     for row in curs.fetchall():
> >         process(row)
>
> Because it's neither easy nor necessary to have a full-fledged cursor
> object we could just have a new method on the cursor, returning an
> iterable object responsible of all the state during the iteration:
> something like:
>
>     for r in cur.execute_iter(query [, args]):    # better name?
>         process(row)

Because DB-API 2.0 has already standardized iterator usage, I would
prefer DB-API compatible iterator API.  But I also understand that
adding it has hack to current C code will be messy.

In the long-term psycopg would need C code that handles iterating as
main case, but perhaps short-term we can implement _execute_iter()
in C and then have IterConnection/IterCursor classes that emulate
proper cursor on top of that?

> Because in the DBAPI querying and retrieving is done with different
> set of methods, I would have preferred something like:
>
>     cur.execute(query)
>     for row in cur.iter_single():
>         process(row)
>
> but usually psycopg calls both PQexec and PQgetResult during execute()
> so this interface wouldn't be straightforward to implement.

In any case the .execute() needs to know about iterating, so it can
do PQsend() + PQsetSingleRowMode() + PQgetResult().  PQgetResult to
get the column headers from first row.  So it must be either cursor
default or flag to execute().

--
marko



psycopg by date:

Previous
From: Daniele Varrazzo
Date:
Subject: Re: Resdhift's lack of cursors and PQsetSingleRowMode
Next
From: Krystian Samp
Date:
Subject: results via pgAdmin but not via psycopg2