Changeset 794
- Timestamp:
- 03/05/07 20:57:00 (1 year ago)
- Files:
-
- pg8000/trunk/pg8000.py (modified) (8 diffs)
- pg8000/trunk/pg8000-test.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pg8000/trunk/pg8000.py
r792 r794 57 57 58 58 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) 60 62 61 63 def executemany(self, operation, seq): … … 64 66 def fetchone(self): 65 67 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))] 67 71 68 72 def fetchmany(self, size=None): … … 92 96 self.c = Protocol.Connection(host, port) 93 97 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) 96 99 except socket.error, e: 97 100 raise InterfaceError("communication error", e) … … 159 162 return klass(data[4:]) 160 163 else: 161 raise InterfaceError("authentication method %r not supported" % (ident,))164 raise NotSupportedError("authentication method %r not supported" % (ident,)) 162 165 createFromData = staticmethod(createFromData) 163 166 164 167 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") 166 169 167 170 class AuthenticationOk(AuthenticationRequest): … … 291 294 else: 292 295 fields.append(data[:val_len]) 293 data = data[val_len + 1:]296 data = data[val_len:] 294 297 return Protocol.DataRow(fields) 295 298 createFromData = staticmethod(createFromData) … … 304 307 def verifyState(self, state): 305 308 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)) 307 310 308 311 def _send(self, msg): … … 349 352 self.state = "in_query" 350 353 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") 351 358 352 359 def getrow(self): … … 371 378 } 372 379 380 class 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 3 3 import pg8000 4 4 5 db = pg8000.connect(host='localhost', user=' laotzu')5 db = pg8000.connect(host='localhost', user='mfenniak') 6 6 cur = db.cursor() 7 7 8 cur.execute("SELECT 1+1")8 cur.execute("SELECT 5000+1, true, false, '2001-01-01 01:01:01'::timestamp") 9 9 res = cur.fetchall() 10 10 print repr(res)
