Re: Resdhift's lack of cursors and PQsetSingleRowMode

From: Marko Kreen <markokr(at)gmail(dot)com>
To: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
Cc: Ryan Kelly <rpkelly22(at)gmail(dot)com>, "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Re: Resdhift's lack of cursors and PQsetSingleRowMode
Date: 2013-12-27 14:38:46
Message-ID: 20131227143846.GA14697@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)gmail(dot)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

In response to

Browse psycopg by date

  From Date Subject
Next Message Krystian Samp 2013-12-31 11:44:46 results via pgAdmin but not via psycopg2
Previous Message Daniele Varrazzo 2013-12-27 12:59:52 Re: Resdhift's lack of cursors and PQsetSingleRowMode