Changeset 794

Show
Ignore:
Timestamp:
03/05/07 20:57:00 (1 year ago)
Author:
mfenniak
Message:

cont. work on pg8000 including type conversions for int and bool.

Files:

Legend:

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

    r792 r794  
    5757 
    5858    def execute(self, operation, *params): 
    59         row_desc = self.c.query(operation) 
     59        self.row_desc = self.c.query(operation) 
     60        #for f in row_desc.fields: 
     61        #    print repr(f) 
    6062 
    6163    def executemany(self, operation, seq): 
     
    6466    def fetchone(self): 
    6567        row = self.c.getrow() 
    66         return row 
     68        if row == None: 
     69            return None 
     70        return [Types.convert(row.fields[i], self.row_desc.fields[i]) for i in range(len(row.fields))] 
    6771 
    6872    def fetchmany(self, size=None): 
     
    9296            self.c = Protocol.Connection(host, port) 
    9397            self.c.connect() 
    94             if not self.c.authenticate(user, password=password, database=database): 
    95                 raise InterfaceError("authentication method failed or not supported") 
     98            self.c.authenticate(user, password=password, database=database) 
    9699        except socket.error, e: 
    97100            raise InterfaceError("communication error", e) 
     
    159162                return klass(data[4:]) 
    160163            else: 
    161                 raise InterfaceError("authentication method %r not supported" % (ident,)) 
     164                raise NotSupportedError("authentication method %r not supported" % (ident,)) 
    162165        createFromData = staticmethod(createFromData) 
    163166 
    164167        def ok(self, conn, user, **kwargs): 
    165             raise NotImplementedError("ok method should be overridden on AuthenticationRequest instance") 
     168            raise InternalError("ok method should be overridden on AuthenticationRequest instance") 
    166169 
    167170    class AuthenticationOk(AuthenticationRequest): 
     
    291294                else: 
    292295                    fields.append(data[:val_len]) 
    293                     data = data[val_len + 1:] 
     296                    data = data[val_len:] 
    294297            return Protocol.DataRow(fields) 
    295298        createFromData = staticmethod(createFromData) 
     
    304307        def verifyState(self, state): 
    305308            if self.state != state: 
    306                 raise InternalError, "connection state must be %s, is %s" % (state, self.state
     309                raise InternalError("connection state must be %s, is %s" % (state, self.state)
    307310 
    308311        def _send(self, msg): 
     
    349352                self.state = "in_query" 
    350353                return msg 
     354            elif isinstance(msg, Protocol.ErrorResponse): 
     355                raise ProgrammingError(msg.severity, msg.code, msg.msg) 
     356            else: 
     357                raise InternalError("RowDescription expected, other message recv'd") 
    351358 
    352359        def getrow(self): 
     
    371378        } 
    372379 
     380class Types(object): 
     381    def convert(data, description): 
     382        format = description['format'] 
     383        table = Types.t_formats.get(format) 
     384        if table == None: 
     385            raise NotSupportedError("data response format %r not supported" % format) 
     386        type_oid = description['type_oid'] 
     387        func = table.get(type_oid) 
     388        if func == None: 
     389            raise NotSupportedError("data response format %r, type %r not supported" % (format, type_oid)) 
     390        in_func, out_func = func 
     391        return in_func(data, description) 
     392    convert = staticmethod(convert) 
     393 
     394    def boolin(data, description): 
     395        return data == 't' 
     396 
     397    def boolout(v): 
     398        if v: 
     399            return 't' 
     400        else: 
     401            return 'f' 
     402 
     403    def int4in(data, description): 
     404        return int(data) 
     405 
     406    def int4out(v): 
     407        return str(v) 
     408 
     409    t_formats_text = { 
     410        16: (boolin, boolout), 
     411        23: (int4in, int4out), 
     412    } 
     413 
     414    t_formats = { 
     415        0: t_formats_text, 
     416    } 
     417 
     418 
  • pg8000/trunk/pg8000-test.py

    r792 r794  
    33import pg8000 
    44 
    5 db = pg8000.connect(host='localhost', user='laotzu') 
     5db = pg8000.connect(host='localhost', user='mfenniak') 
    66cur = db.cursor() 
    77 
    8 cur.execute("SELECT 1+1") 
     8cur.execute("SELECT 5000+1, true, false, '2001-01-01 01:01:01'::timestamp") 
    99res = cur.fetchall() 
    1010print repr(res)