Re: [RFC] ideas for a new Python DBAPI driver (was Re: libpq test suite)

From: Manlio Perillo <manlio(dot)perillo(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: [RFC] ideas for a new Python DBAPI driver (was Re: libpq test suite)
Date: 2013-02-14 17:40:21
Message-ID: 511D2185.90606@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 14/02/2013 18:18, Jonathan Rogers ha scritto:
> A number of the described features sound quite useful. Is it not
> practical to extend an existing library such as psycopg2?

I suspect there are compatibility issues.

> What method
> will you use to call libpq functions? As you are no doubt aware,
> psycopg2 uses the traditional CPython API but there is a fork which uses
> ctypes instead, enabling it to work with CPython and Pypy.
>

I'm implementing a prototype version, in pure Python with ctypes.
When the prototype is ready, I will implement a CPython extension module
(since the purpose of writing yet another driver is to make it more
efficient than the current best driver).

I will also discuss about porting some of the features to psycopg2 (but
first I need to have a working driver).

> Manlio Perillo wrote:
>
>> For my Python DBAPI2 PostgreSQL driver I plan the following optimizations:
>
>> 2) when the driver detects a Python string is being sent to the
>> database, use binary format.
>
> What exactly do you mean by "Python string"?
>

A Python string object.

The libpq interface will implement the functions
pg_str_encode(string object) -> bytes
pg_str_decode(bytes, result object) -> string object
as described in
http://wiki.postgresql.org/wiki/Driver_development

but with some differences.

The pg_str_encode function will return a tuple with the raw bytes and
the "suggested" parameter format.

As an example, pg_str_encode(<Python 3.x byte string>) will return the
byte string and 1 (since it is a binary string, and I want to avoid to
use PQescapeBytea function).

For the DBAPI interface, I plan to implement a generic API to map
PostgreSQL types to Python types (different from the one implemented in
psycopg2); something like:

pgtype = connection.create_type_map(
<python type object>, <postgresql type name>,
<input text function>, <output text function>,
<input binary function - optional>,
<input binary function - optional>)

but I have yet to decide how to detect the param format to use.

Maybe there will be only one output function, that will decide the best
format to be used.

Regards Manlio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlEdIYQACgkQscQJ24LbaUSfBACfWR7eD6pdyipC3/fljUiuelx6
GV4An3agVt4tx0E/JDUvO0iM8/BiZT1o
=xckI
-----END PGP SIGNATURE-----

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Merlin Moncure 2013-02-14 17:44:33 Re: proposal or just idea for psql - show first N rows from relation backslash statement
Previous Message Fujii Masao 2013-02-14 17:27:22 Re: 9.2.3 crashes during archive recovery