Changeset 825

Show
Ignore:
Timestamp:
03/09/07 22:35:35 (2 years ago)
Author:
mfenniak
Message:

preliminary db-api support

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pg8000/trunk/pg8000.py

    r824 r825  
    9292    ProgrammingError = ProgrammingError 
    9393    NotSupportedError = NotSupportedError 
     94     
     95    apilevel = "2.0" 
     96    threadsafety = 3 
     97    paramstyle = 'none-of-the-above' 
     98 
     99    class CursorWrapper(object): 
     100        def __init__(self, conn): 
     101            self.cursor = Cursor(conn) 
     102            self.arraysize = 1 
     103 
     104        def execute(self, operation, args=()): 
     105            self.cursor.execute(operation, *args) 
     106 
     107        def executemany(self, operation, parameter_sets): 
     108            for parameters in parameter_sets: 
     109                self.execute(operation, parameters) 
     110 
     111        def fetchone(self): 
     112            return self.cursor.read_tuple() 
     113 
     114        def fetchmany(self, size=None): 
     115            if size == None: 
     116                size = self.arraysize 
     117            rows = [] 
     118            for i in range(size): 
     119                rows.append(self.fetchone()) 
     120            return rows 
     121 
     122        def fetchall(self): 
     123            return tuple(cursor.iterate_tuple()) 
     124 
     125        def close(self): 
     126            self.cursor = None 
     127 
     128        def setinputsizes(self, sizes): 
     129            pass 
     130 
     131        def setoutputsize(self, size, column=None): 
     132            pass 
    94133 
    95134    class ConnectionWrapper(object): 
    96         pass 
    97  
    98     def connect(): 
    99         return ConnectionWrapper() 
     135        def __init__(self, **kwargs): 
     136            self.conn = Connection(**kwargs) 
     137            self.conn.begin() 
     138 
     139        def cursor(self): 
     140            return CursorWrapper(self.conn) 
     141 
     142        def commit(self): 
     143            # There's a threading bug here.  If a query is sent after the 
     144            # commit, but before the begin, it will be executed immediately 
     145            # without a surrounding transaction.  Like all threading bugs -- it 
     146            # sounds unlikely, until it happens every time in one 
     147            # application...  however, to fix this, we need to lock the 
     148            # database connection entirely, so that no cursors can execute 
     149            # statements on other threads.  Support for that type of lock will 
     150            # be done later. 
     151            self.conn.commit() 
     152            self.conn.begin() 
     153 
     154        def rollback(self): 
     155            # see bug description in commit. 
     156            self.conn.rollback() 
     157            self.conn.begin() 
     158 
     159        def close(self): 
     160            self.conn = None 
     161 
     162    def connect(user, host=None, unix_sock=None, port=5432, database=None, password=None, socket_timeout=60): 
     163        return ConnectionWrapper(user=user, host=host, unix_sock=unix_sock, 
     164                port=port, database=database, password=password, 
     165                socket_timeout=socket_timeout) 
    100166 
    101167