<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7651.59">
<TITLE>RE: [DB-SIG] Controlling return types for DB APIs</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>Anthony Tuininga wrote:<BR>
&gt; cursor.setdefaulttype(databaseType, type)<BR>
&gt; connection.setdefaulttype(databaseType, type)<BR>
&gt;<BR>
&gt; What this method would do is specify that whenever<BR>
&gt; an item that is represented on the database by the<BR>
&gt; given database type is to be retrieved, the specified<BR>
&gt; [Python] type should be used instead of the default.<BR>
&gt; This would allow for a global or local specification<BR>
&gt; that numbers are to be returned as strings or<BR>
&gt; decimal.Decimal objects or that strings are to be<BR>
&gt; returned as unicode objects, for example.<BR>
&gt;<BR>
&gt; cursor.settype(position, type)<BR>
&gt;<BR>
&gt; This would allow specification of the type to use<BR>
&gt; for a particular column being fetched.<BR>
<BR>
As soon as you provide cursor.settype(dbtype, pytype),<BR>
you should expect someone to ask for<BR>
&quot;pytype = cursor.gettype(dbtype)&quot;, and once you've written<BR>
that, you'll realize they're both spelled better as<BR>
&quot;cursor.types[dbtype] = pytype&quot; (for set) and<BR>
&quot;pytype = cursor.types[dbtype]&quot; (for get). Same thing<BR>
goes for adapters: once you allow people to set them,<BR>
you should expect people will want to inspect them.<BR>
So a cursor.adapters object (copied from a similar<BR>
connection.adapters object) should be used. Namespaces<BR>
are one honking great idea. The container classes don't<BR>
*have* to use slicing (you could go all Java-ish and<BR>
use get() and set()), but slicing is the most natural<BR>
choice in this case.<BR>
<BR>
You also should be very explicit about the direction<BR>
of each operation when building type- or language-<BR>
translation layers. That is, include the direction<BR>
in the names of every method and object, because at<BR>
some point, people will want to do the reverse.<BR>
A Python method named &quot;coerce&quot; is not as good as a<BR>
method named &quot;coerce_in&quot; or &quot;coerce_from_database&quot;;<BR>
the namespaced spelling, &quot;adapter_in.coerce&quot; is even<BR>
better.<BR>
<BR>
&gt; registeradapter(type, databaseType, fromPythonMethod,<BR>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; toPythonMethod)<BR>
&gt;<BR>
&gt; This would specify that whenver an object of the given<BR>
&gt; [Python] type is bound to a cursor, that the<BR>
&gt; fromPythonMethod method would be invoked with the<BR>
&gt; value and would expect a return value that can be<BR>
&gt; directly bound to the databaseType. The toPythonMethod<BR>
&gt; method would be invoked when columns are retrieved and<BR>
&gt; would accept the databaseType value and expect back a<BR>
&gt; value of the given type.<BR>
<BR>
That sounds good if by &quot;databaseType&quot; you mean<BR>
&quot;class VARCHAR&quot;; that is, a Python type which models<BR>
a database type. Because there's no such thing as a<BR>
&quot;database value&quot; you can pass to a toPythonMethod;<BR>
it *must* have already been converted into some Python<BR>
object of a Python type (unless you're writing your<BR>
adapters in C). The best you can do is have a Python<BR>
type (designed/selected for minimum information loss)<BR>
to which the incoming value gets coerced before<BR>
passing it to your toPythonMethod for further<BR>
adaptation or casting.<BR>
<BR>
<BR>
Robert Brewer<BR>
System Architect<BR>
Amor Ministries<BR>
fumanchu@amor.org</FONT>
</P>

</BODY>
</HTML>