[Zope-Perl] ZDBI_DA product
Gisle Aas
gisle@ActiveState.com
23 Jun 2000 23:51:04 +0200
Today I set up a Zope database adapter for perl DBI. It was in fact
very simple. Instant access to all DBI supported databases
<http://www.symbolstone.org/technology/perl/DBI/>.
The guts of the whole thing is basically this:
-------------------------------------------------------------------
from string import find, split
def make_item(n,t,p):
if 1 <= t <= 12:
t = "tnniinnndttt"[t-1]
else:
t = "t"
return { 'name': n, 'type': t, 'width': p }
class DB:
def __init__(self,connection):
user = ""
auth = ""
if find(connection, "@") >= 0:
user, connection = split(connection, "@", 1)
if find(user, ":") >= 0:
user, auth = split(user, ":", 1)
import dbi
print "DBI Connect", [connection, user, auth]
self.db = dbi.connect(connection, user, auth,
RaiseError = 1,
PrintError = 0,
AutoCommit = 1,
)
def query(self,query_string, max_rows=9999999):
dbh=self.db
sth = dbh.prepare(query_string)
rows = sth.execute()
return map(make_item, sth["NAME"], sth["TYPE"], sth["PRECISION"]), \
sth.fetchall_arrayref()
-------------------------------------------------------------------
The cool thing is that sth.fetchall_arrayref() is a direct DBI call
that returns the whole result set as a perl array of perl arrays.
This stuff can then be returned as it is directly into the Zope
RDB-machinery without any conversion.
Regards,
Gisle