[Zope-DB] ZFirebirdDA

W. Robert Kellock sales@creditscore.co.nz
Wed, 1 May 2002 13:56:19 +1200


This is a multi-part message in MIME format.

------=_NextPart_000_0008_01C1F117.F88FCCC0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Joao,

Fix to db.py attached.
----------------------------------------------------------------------------
---------------------------------
W.Robert Kellock                                    Ph:
+64 3 326 6115
Sales Manager                                        Fax:
+64 3 326 6115
Credit Systems Control Ltd                      Web:
www.creditscore.co.nz
                                              "making decisions easy"
----------------------------------------------------------------------------
---------------------------------

----- Original Message -----
From: Joao Granado
To: zope-db@zope.org
Sent: Tuesday, 30 April 2002 22:15
Subject: [Zope-DB] ZFirebirdDA


I want to insert records into a table using zfirebirdda. No error occurs but
it seems that the transaction is not commited and so no records are inserted
into the database.

Thanks for any help.

Joao Granado





------=_NextPart_000_0008_01C1F117.F88FCCC0
Content-Type: text/plain;
	name="db.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="db.py"

#####
# ZFireBirdDA
#=20
# (C) Copyright 2000,2001 Phil Harris
# ACN: 082 081 472  ABN: 83 082 081 472
# All Rights Reserved
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR =
PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE =
LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR =
CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE =
GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, =
STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY =
WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE
#
# Author Phil Harris
#####
'''ZFireBird'''
__version__=3D'0.0.2'

import kinterbasdb, DateTime
# W Robert Kellock 11/04/02 this is a single threaded adaptor so
# to be safe serialize threads
#
# from Shared.DC.ZRDB.TM import TM
import Shared.DC.ZRDB.THUNK

import string, sys
strsplit=3Dstring.split
from string import strip, find
from time import time

failures=3D0
calls=3D0
last_call_time=3Dtime()

# W Robert Kellock 11/04/02 this is a single threaded adaptor so
# to be safe serialize threads
#
# class DB(TM):
class DB(Shared.DC.ZRDB.THUNK.THUNKED_TM):

    _p_oid=3D_p_changed=3D_registered=3DNone

    Database_Connection=3Dkinterbasdb.connect
    Database_Error=3Dkinterbasdb.Error

    def __init__(self,connection):
        self.connection=3Dconnection
	#---
	# parse the connection string
	#---
	self.setup()

    def setup(self):
	try:
	    self.cursor.close()
	except:
	    pass
	try:
	    self.db.close()
	except:
	    pass
	connbits=3Dstrsplit(self.connection, ' ')
	=
db=3Dkinterbasdb.connect(dsn=3Dconnbits[0],user=3Dconnbits[1],password=3D=
connbits[2])
	self.db=3Ddb
	self.cursor=3Dself.db.cursor()
       =20
    def str(self,v, StringType=3Dtype('')):
        if v is None: return ''
        r=3Dstr(v)
        if r[-1:]=3D=3D'L' and type(v) is not StringType: r=3Dr[:-1]
        return r

    def _finish(self, *ignored):
        # W Robert Kellock 12/04/02 changed to explicitly commit =
transaction
	# pass
        self.db.commit()

    def _abort(self, *ignored):
        # W Robert Kellock 12/04/02 changed to explicitly abort =
transaction
	# pass
        self.db.rollback()

    def tables(self, rdb=3D0,
               _care=3D('TABLE', 'VIEW')):
        r=3D[]
        a=3Dr.append
	c=3Dself.db.cursor()
	c.execute('''SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE ( NOT =
(RDB$RELATION_NAME STARTING WITH 'RDB$') ) ORDER BY 1''')

	tabs=3Dc.fetchall()
	for tab in tabs:
	    a({'TABLE_NAME': strip(str(tab[0])), 'TABLE_TYPE': 'TABLE'})
	return r

    def columns(self, table_name):
        c=3Dself.cursor
	try: r=3Dc.execute('''select  r.rdb$field_name field_name, =
t.rdb$type_name type_name,=20
	 f.rdb$field_length field_length, f.rdb$field_sub_type field_sub_type,=20
	 f.rdb$null_flag null_flag, rdb$field_precision field_precision,
	 f.rdb$field_scale field_scale, f.rdb$default_source default_value
 from    rdb$relation_fields r, rdb$types t, rdb$fields f
 where   r.rdb$relation_name=3D'%s' and
	 f.rdb$field_name=3Dr.rdb$field_source and
	 t.rdb$field_name=3D'RDB$FIELD_TYPE' and
	 f.rdb$field_type=3Dt.rdb$type;''' % table_name)
	except:=20
	    self.setup()
	    return ()
	=09
	rows=3Dc.fetchallmap()
	r=3D[]
	a=3Dr.append
	for row in rows:
	    name=3Drow['field_name']
	    typ=3Dstrip(row['type_name'])
	    p=3Drow['field_length']
	    scale=3Drow['field_scale']
	    null_ok=3Drow['null_flag'] is not None and 'Nullable' or ''
	    default=3Drow['default_value']
	    print "'%s'" % typ
	    if strip(typ)=3D=3D'VARYING':=20
		typ=3D'VARCHAR'
	    subtyp=3D''
	    if typ=3D=3D'BLOB':
	        subtyp=3Drow['field_sub_type']
		c.execute('select rdb$type_name from rdb$types where rdb$type=3D%s' % =
subtyp)
		subtyp=3Dc.fetchall()
		print subtyp
		subtyp=3Dstrip(subtyp[0][0])
	    a({ 'Name': strip(name),
		'Type': strip(typ),
		'Precision': strip(str(p)),
		'Scale': strip(str(scale)),
		'Nullable': strip(str(null_ok)),
		'Default': strip(str(default)),
		'SubType': strip(str(subtyp)),
		})

        return r

    def query(self,query_string, max_rows=3D9999999):
        global failures, calls, last_call_time
        calls=3Dcalls+1
        desc=3DNone
        result=3D()
        self._register()
        c=3Dself.cursor
	try:
	    for qs in filter(None, map(strip,strsplit(query_string, '\0'))):
		r=3Dc.execute(qs)
		if r is None:
		    if desc is not None:
			if c.description !=3D desc:
			    raise 'Query Error', ( 'Multiple select schema are not allowed')
			if type(result) is not ListType: result=3Dlist(result)
			if max_rows:
			    for row in c.fetchmany(max_rows-len(result)):
				result.append(row)
		    else:
			desc=3Dc.description
                        # W Robert Kellock 12/04/02 changed max_rows to =
desc
    		        if desc:
			    if max_rows=3D=3D1: result=3D(c.fetchone(),)
			    else: result=3Dc.fetchmany(max_rows)
			=09
	    failures=3D0
	    last_call_time=3Dtime()
	except kinterbasdb.ProgrammingError, mess:
	    print mess
	    if mess[0]!=3D0:
		    raise self.Database_Error(mess)
	    self.setup()
	except self.Database_Error,mess:
	    raise sys.exc_type, sys.exc_value, sys.exc_traceback

        if desc is None: return (),()

        items=3D[]
        for name, type, width, ds, p, scale, null_ok in desc:
            if type=3D=3D'NUMBER':
                if scale=3D=3D0: type=3D'i'
                else: type=3D'n'
            elif type=3D=3D'DATE':
                type=3D'd'
            else: type=3D's'
            items.append({
                'name': name,
                'type': type,
                'width': width,
                'null': null_ok,
                })
        return items, result



------=_NextPart_000_0008_01C1F117.F88FCCC0--