Skip site navigation (1) Skip section navigation (2)

Re: Change in datetime type casting

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: Federico Di Gregorio <fog(at)dndg(dot)it>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: Change in datetime type casting
Date: 2012-07-03 01:25:49
Message-ID: (view raw or whole thread)
Lists: psycopg
On Mon, Jul 2, 2012 at 9:24 AM, Federico Di Gregorio <fog(at)dndg(dot)it> wrote:

> 2) Throw an exception if keys/values aren't strings and force the user
>    to convert the data before trying to send it to the hstore.

We can definitely add a check to verify every key is a basestring and
every value is a basestring or null (this is exactly the hstore
domain). It is an extra iteration to be performed on the entire
content of the dict though.

Something on a slightly different note we could do: adapt takes a
protocol as input (undocumented I believe, defaulting to ISQLQuote,
currently the only protocol used): if we added a parameter to the
protocol, defaulting to the current extensions.adapters we could have:

- adaptation of hstore keys is performed on a limited set of
datatypes: only str and unicode. Trying to pass another type in a dict
would raise an adaptation error;
- adaptation of hstore values is performed on the same types plus None;
- if s.b. wanted to map other types into an hstore they can add other
adapters just on the HstoreAdapter object (the object returned by
- we can have adapters map per connection/cursor, other argument
touched in this thread.

So we stay strict in the default you have independently verified are
the only sane default choice, allow for extension by users who know
what they want to do and get a nice symmetry between adaptation and
type casting.

As an interface, register_adapter() could take an extra context as
register_type() does: it could be a connection, a cursor or an
HstoreAdapter (e.g. it could be whatever object exporting a method
add_adapter() or something along this line).

This is an idea of 3am after the PyBirra: if nothing makes sense here,
please ignore.

-- Daniele

In response to

psycopg by date

Next:From: Matthieu RigalDate: 2012-07-03 09:59:02
Subject: insert into with a dictionary, or generally with a variable number of columns
Previous:From: Federico Di GregorioDate: 2012-07-02 08:24:44
Subject: Re: Change in datetime type casting

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group