Changeset 800
- Timestamp:
- 03/07/07 20:55:19 (2 years 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
r799 r800 87 87 # A configuration variable that determines whether iterating over the 88 88 # connection will return tuples of queried rows (False), or dictionaries 89 # indexed by column name/alias (True). By default, this variable is set to90 # False.89 # indexed by column name/alias (True). By default, this variable value is 90 # copied from the connection's iterate_dicts value. 91 91 # <p> 92 92 # Stability: Added in v1.00, stability guaranteed for v1.xx. … … 96 96 97 97 def __init__(self, connection, name = None): 98 self.iterate_dicts = connection.iterate_dicts 98 99 self.c = connection.c 99 100 if name == None: … … 108 109 self._command_complete = False 109 110 self._row_desc = self.c.extended_query(self.name, '', query, args) 111 if self._row_desc == None: 112 self._command_complete = True 110 113 111 114 def _fetch(self): … … 117 120 if end_of_data: 118 121 self._command_complete = True 122 if not rows: 123 # special case - an empty query, hit end_of_data and no 124 # rows at the same time 125 return None 119 126 row = self._cached_rows[0] 120 127 del self._cached_rows[0] … … 188 195 # authentication, then the password will not be used. 189 196 class Connection(object): 197 198 ## 199 # A configuration variable that determines whether iterating over the 200 # connection will return tuples of queried rows (False), or dictionaries 201 # indexed by column name/alias (True). By default, this variable is set to 202 # False. 203 # <p> 204 # Stability: Added in v1.00, stability guaranteed for v1.xx. 205 iterate_dicts = False 206 190 207 def __init__(self, host, user, port=5432, database=None, password=None): 191 208 self._row_desc = None … … 416 433 createFromData = staticmethod(createFromData) 417 434 435 class NoData(object): 436 def createFromData(data): 437 return Protocol.NoData() 438 createFromData = staticmethod(createFromData) 439 418 440 class ParseComplete(object): 419 441 def createFromData(data): … … 582 604 # good news everybody! 583 605 pass 606 elif isinstance(msg, Protocol.NoData): 607 # no data means we should execute this command right away 608 self._send(Protocol.Execute(portal, 0)) 609 self._send(Protocol.Sync()) 610 while 1: 611 msg = self._read_message() 612 if isinstance(msg, Protocol.CommandComplete): 613 # more good news! 614 pass 615 elif isinstance(msg, Protocol.ReadyForQuery): 616 # ready to move on with life... 617 return None 618 elif isinstance(msg, Protocol.ErrorResponse): 619 raise msg.createException() 620 else: 621 raise InternalError("unexpected response") 584 622 elif isinstance(msg, Protocol.RowDescription): 585 623 return msg … … 649 687 "3": CloseComplete, 650 688 "s": PortalSuspended, 689 "n": NoData, 651 690 } 652 691 pg8000/trunk/pg8000-test.py
r799 r800 4 4 5 5 db = pg8000.Connection(host='localhost', user='mfenniak') 6 cursor = pg8000.Cursor(db) 7 # db.iterate_dicts = True 6 db.iterate_dicts = True 7 8 cur1 = pg8000.Cursor(db) 9 cur2 = pg8000.Cursor(db) 10 11 cur1.execute("DELETE FROM t1") 12 cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 1, 1) 13 cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 2, 10) 14 cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 3, 100) 15 cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 4, 1000) 16 cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 5, 10000) 8 17 9 18 print "begin query..." 10 cur sor.execute("SELECT township, range, meridian FROM ats LIMIT 176")19 cur1.execute("SELECT * FROM t1") 11 20 i = 0 12 for row in cursor:21 for row1 in cur1: 13 22 i = i + 1 14 print i, repr(row) 23 print i, repr(row1) 24 cur2.execute("SELECT * FROM t1 WHERE f1 > $1", row1['f1']) 25 for row2 in cur2: 26 print "\t", repr(row2) 15 27 print "end query..." 16 28 17 29 print "begin query..." 18 cur sor.execute("SELECT 5000 + 1, True as pg_stuff, False, '2000-01-02 03:04:05.67'::timestamp, $1", 55)19 for row in cur sor:30 cur1.execute("SELECT 5000 + 1 as int_test, True as bool_test, '2000-01-02 03:04:05.67'::timestamp as timestamp_test") 31 for row in cur1: 20 32 print repr(row) 21 33 print "end query..."
