[DB-SIG] need some database API design advice

Peter Eisentraut peter_e at gmx.net
Wed Apr 11 20:35:54 CEST 2012


I'm looking for some API design advice.

The PostgreSQL developers are currently considering an extension of the
PL/Python database access API [0].  This is, for better or worse, a
lower-level custom Python database access API, but you can build DB-API
on top of that.

We have added a few functions that extract metadata such as column names
and types from a result set object (rv.colnames(), rv.coltypes(); think
cursor.description).  The question was what to do when there is no
resulting row set, because the command was a utility command such as
CREATE TABLE.  Option 1 was that the functions should throw an
exception, because the request is invalid.  Option 2 was that the
functions should return None.  (This is what cursor.description is
specified to do.)  This was objected to because it would require extra
checking for None.  In addition to that, the question relative to option
1 in particular was how to detect whether a result row set exists, to
avoid the exception-throwing calls.  With option 2 you could check for
is None, of course.  There is an ongoing discussion [1] about which ones
of these would be better style.

So, in terms of code, which one of these is "better"?

1a.

rv = plpy.execute("some SQL command")
try:
    output(rv.colnames())
except SomeException:
    output("it's a utility command")

1b.

rv = plpy.execute("some SQL command")
if rv.has_rows():  # some currently nonexisting function to be added
    output(rv.colnames())
else:
    output("it's a utility command")

2.

rv = plpy.execute("some SQL command")
if rv.colnames() is not None:
    output(rv.colnames())
else:
    output("it's a utility command")


[0] http://www.postgresql.org/docs/devel/static/plpython-database.html
[1] http://archives.postgresql.org/message-id/CAK6bCay4yrFJD3po_bCke4ukjjsPLkbf+ad07jZiAU3N6cwUiA@mail.gmail.com



More information about the DB-SIG mailing list