[Zope-Perl] ZOPE DBI adapter - feedback and patch (please review)

Gisle Aas gisle@ActiveState.com
31 Aug 2000 15:52:50 +0200


Joseph Wayne Norton <norton@alum.mit.edu> writes:

> > 
> > If we set 'AutoCommit = 0' then MySQL croaks at this point with a:
> > 
> >   perl.PerlError: Transactions not supported by database
> > 
> > so some other approach is needed to deal with transaction-less
> > databases.
> > 
> 
> How about being able to override these defaults in the connection
> string?

I don't think I want to put even more syntax into it.

My class DB now looks like this.  It appears to work nicely with MySQL
and I think it should work with other more capable databases too.

The current 'svrv_object.c' fails to invoke magic for assignment to
elements of tied hashes, so you can not actually try this until alpha
5 is out.  With alpha 4, the assignment:

   self.db["AutoCommit"] = 0

does not do anything.

Regards,
Gisle


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,
                             )
	try: self.db["AutoCommit"] = 0
	except:	pass

    def query(self,query_string, max_rows=9999999):
        dbh=self.db
	queries=filter(None, map(strip,split(query_string, '\0')))
	if not queries:
	    raise 'Query Error', 'empty query'
	result=None
	try:
	    for qs in queries:
		sth = dbh.prepare(qs)
		rows = sth.execute()
		try:
		    r = (map(make_item,
			     sth["NAME"], sth["TYPE"], sth["PRECISION"]),
			 sth.fetchall_arrayref())
		except:
		    r = None
		del(sth)
		if not r:
		    continue
		if result:
		    raise 'Query Error', 'Multiple select statements'
		result = r
	except:
	    if not dbh["AutoCommit"]:
		dbh.rollback()
	    raise
	else:
	    if not dbh["AutoCommit"]:
		dbh.commit()
	return result or ((), ())