Changeset 852 for pg8000/trunk
- Timestamp:
- 03/14/07 11:04:51 (2 years ago)
- Files:
-
- pg8000/trunk/pg8000.py (modified) (12 diffs)
- pg8000/trunk/pg8000_test.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pg8000/trunk/pg8000.py
r851 r852 38 38 import time 39 39 40 debug_log = file("/Users/mfenniak/SQLAlchemy-0.3.5/pg8000_debug.log", "w") 41 40 42 class Warning(StandardError): 41 43 pass … … 106 108 # 2 -- inside quoted identifier "..." 107 109 # 3 -- inside escaped single-quote string, E'...' 110 debug_log.write("convert_paramstyle(%r, %r, %r)\n" % (src_style, query, args)) 108 111 state = 0 109 112 output_query = "" … … 289 292 290 293 def execute(self, operation, args=()): 294 debug_log.write("execute(%r, %r)\n" % (operation, args)) 291 295 if self.cursor == None: 292 296 raise InterfaceError("cursor is closed") … … 1475 1479 return data == "\x01" 1476 1480 1481 def boolout(v, **kwargs): 1482 if v: 1483 return 't' 1484 else: 1485 return 'f' 1486 1477 1487 def int2recv(data, **kwargs): 1478 1488 return struct.unpack("!h", data)[0] … … 1529 1539 return v.isoformat(' ') 1530 1540 1541 def date_in(data, **kwargs): 1542 year = int(data[0:4]) 1543 month = int(data[5:7]) 1544 day = int(data[8:10]) 1545 return datetime.date(year, month, day) 1546 1547 def date_out(v, **kwargs): 1548 return v.isoformat() 1549 1550 def time_in(data, **kwargs): 1551 hour = int(data[0:2]) 1552 minute = int(data[3:5]) 1553 sec = decimal.Decimal(data[6:]) 1554 return datetime.time(hour, minute, int(sec), int((sec - int(sec)) * 1000000)) 1555 1556 def time_out(v, **kwargs): 1557 return v.isoformat() 1558 1531 1559 def numeric_in(data, **kwargs): 1532 1560 if data.find(".") == -1: … … 1544 1572 return v.encode(client_encoding) 1545 1573 1546 def timestamptz_in(data, description):1574 def timestamptz_in(data, **kwargs): 1547 1575 year = int(data[0:4]) 1548 1576 month = int(data[5:7]) … … 1553 1581 sec = decimal.Decimal(data[17:tz_sep]) 1554 1582 tz = data[tz_sep:] 1555 print repr(data), repr(description)1556 print repr(tz)1557 1583 return datetime.datetime(year, month, day, hour, minute, int(sec), int((sec - int(sec)) * 1000000), Types.FixedOffsetTz(tz)) 1558 1584 … … 1571 1597 return datetime.timedelta(0) 1572 1598 1599 def __eq__(self, other): 1600 if not isinstance(other, FixedOffsetTz): 1601 return False 1602 return self.hrs == other.hrs 1603 1573 1604 def byteasend(v, **kwargs): 1574 1605 return str(v) … … 1582 1613 1583 1614 py_types = { 1615 bool: {"tid": 16, "txt_out": boolout}, 1584 1616 int: {"tid": 1700, "txt_out": numeric_out}, 1585 1617 long: {"tid": 1700, "txt_out": numeric_out}, … … 1590 1622 Bytea: {"tid": 17, "bin_out": byteasend}, 1591 1623 datetime.datetime: {"tid": 1114, "txt_out": timestamp_out}, 1624 datetime.date: {"tid": 1082, "txt_out": date_out}, 1625 datetime.time: {"tid": 1083, "txt_out": time_out}, 1592 1626 type(None): {"tid": -1}, 1593 1627 } … … 1596 1630 16: {"txt_in": boolin, "bin_in": boolrecv, "prefer": "bin"}, 1597 1631 17: {"bin_in": bytearecv}, 1632 19: {"txt_in": varcharin}, # name type 1598 1633 20: {"txt_in": int8in, "bin_in": int8recv, "prefer": "bin"}, 1599 1634 21: {"txt_in": int2in, "bin_in": int2recv, "prefer": "bin"}, … … 1605 1640 1042: {"txt_in": varcharin}, # CHAR type 1606 1641 1043: {"txt_in": varcharin}, # VARCHAR type 1607 1114: {"txt_in": timestamp_in}, #, "bin_in": timestamp_recv, "prefer": "bin"}, 1642 1082: {"txt_in": date_in}, 1643 1083: {"txt_in": time_in}, 1644 1114: {"txt_in": timestamp_in}, 1645 1184: {"txt_in": timestamptz_in}, # timestamp w/ tz 1608 1646 1186: {"txt_in": interval_in}, 1609 1647 1700: {"txt_in": numeric_in}, 1610 1648 } 1611 #1184: (timestamptz_in, None), # timestamp w/ tz 1612 1613 1614 1649 1650 1651 pg8000/trunk/pg8000_test.py
r846 r852 8 8 import struct 9 9 10 db_ connect = {10 db_joy_connect = { 11 11 "host": "joy", 12 12 "user": "pg8000-test", … … 16 16 "ssl": False, 17 17 } 18 db_local_connect = { 19 "unix_sock": "/tmp/.s.PGSQL.5432", 20 "user": "mfenniak" 21 } 22 db_connect = db_local_connect 18 23 db = pg8000.Connection(**db_connect) 19 24 dbapi = pg8000.DBAPI … … 264 269 # Tests relating to type conversion. 265 270 class TypeTests(unittest.TestCase): 271 def TestTimeRoundtrip(self): 272 db.execute("SELECT $1 as f1", datetime.time(4, 5, 6)) 273 retval = tuple(db.iterate_dict()) 274 self.assert_(retval == ({"f1": datetime.time(4, 5, 6)},), 275 "retrieved value match failed") 276 277 def TestDateRoundtrip(self): 278 db.execute("SELECT $1 as f1", datetime.date(2001, 2, 3)) 279 retval = tuple(db.iterate_dict()) 280 self.assert_(retval == ({"f1": datetime.date(2001, 2, 3)},), 281 "retrieved value match failed") 282 283 def TestBoolRoundtrip(self): 284 db.execute("SELECT $1 as f1", True) 285 retval = tuple(db.iterate_dict()) 286 self.assert_(retval == ({"f1": True},), 287 "retrieved value match failed") 288 266 289 def TestNullRoundtrip(self): 267 290 # We can't just "SELECT $1" and set None as the parameter, since it has … … 338 361 "retrieved value match failed") 339 362 340 def TestOidIn(self): 363 def TestTimestampTzOut(self): 364 db.execute("SELECT '2001-02-03 04:05:06.17 Canada/Mountain'::timestamp with time zone") 365 retval = tuple(db.iterate_dict()) 366 dt = retval[0]['timestamptz'] 367 self.assert_(dt.tzinfo.hrs == -7, 368 "timezone hrs != -7") 369 self.assert_( 370 datetime.datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond) == 371 datetime.datetime(2001, 2, 3, 4, 5, 6, 170000), 372 "retrieved value match failed") 373 374 def TestNameOut(self): 375 # select a field that is of "name" type: 376 db.execute("SELECT usename FROM pg_user") 377 retval = tuple(db.iterate_dict()) 378 # It is sufficient that no errors were encountered. 379 380 def TestOidOut(self): 341 381 db.execute("SELECT oid FROM pg_type") 342 382 retval = tuple(db.iterate_dict())
