Changeset 800

Show
Ignore:
Timestamp:
03/07/07 20:55:19 (2 years ago)
Author:
mfenniak
Message:

Make execute functions work with non-queries (insert, create, so on)

Files:

Legend:

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

    r799 r800  
    8787    # A configuration variable that determines whether iterating over the 
    8888    # connection will return tuples of queried rows (False), or dictionaries 
    89     # indexed by column name/alias (True).  By default, this variable is set to 
    90     # False. 
     89    # indexed by column name/alias (True).  By default, this variable value is 
     90    # copied from the connection's iterate_dicts value. 
    9191    # <p> 
    9292    # Stability: Added in v1.00, stability guaranteed for v1.xx. 
     
    9696 
    9797    def __init__(self, connection, name = None): 
     98        self.iterate_dicts = connection.iterate_dicts 
    9899        self.c = connection.c 
    99100        if name == None: 
     
    108109        self._command_complete = False 
    109110        self._row_desc = self.c.extended_query(self.name, '', query, args) 
     111        if self._row_desc == None: 
     112            self._command_complete = True 
    110113 
    111114    def _fetch(self): 
     
    117120            if end_of_data: 
    118121                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 
    119126        row = self._cached_rows[0] 
    120127        del self._cached_rows[0] 
     
    188195# authentication, then the password will not be used. 
    189196class 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 
    190207    def __init__(self, host, user, port=5432, database=None, password=None): 
    191208        self._row_desc = None 
     
    416433        createFromData = staticmethod(createFromData) 
    417434 
     435    class NoData(object): 
     436        def createFromData(data): 
     437            return Protocol.NoData() 
     438        createFromData = staticmethod(createFromData) 
     439 
    418440    class ParseComplete(object): 
    419441        def createFromData(data): 
     
    582604                    # good news everybody! 
    583605                    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") 
    584622                elif isinstance(msg, Protocol.RowDescription): 
    585623                    return msg 
     
    649687        "3": CloseComplete, 
    650688        "s": PortalSuspended, 
     689        "n": NoData, 
    651690        } 
    652691 
  • pg8000/trunk/pg8000-test.py

    r799 r800  
    44 
    55db = pg8000.Connection(host='localhost', user='mfenniak') 
    6 cursor = pg8000.Cursor(db) 
    7 # db.iterate_dicts = True 
     6db.iterate_dicts = True 
     7 
     8cur1 = pg8000.Cursor(db) 
     9cur2 = pg8000.Cursor(db) 
     10 
     11cur1.execute("DELETE FROM t1") 
     12cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 1, 1) 
     13cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 2, 10) 
     14cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 3, 100) 
     15cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 4, 1000) 
     16cur1.execute("INSERT INTO t1 (f1, f2) VALUES ($1, $2)", 5, 10000) 
    817 
    918print "begin query..." 
    10 cursor.execute("SELECT township, range, meridian FROM ats LIMIT 176") 
     19cur1.execute("SELECT * FROM t1") 
    1120i = 0 
    12 for row in cursor
     21for row1 in cur1
    1322    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) 
    1527print "end query..." 
    1628 
    1729print "begin query..." 
    18 cursor.execute("SELECT 5000 + 1, True as pg_stuff, False, '2000-01-02 03:04:05.67'::timestamp, $1", 55
    19 for row in cursor
     30cur1.execute("SELECT 5000 + 1 as int_test, True as bool_test, '2000-01-02 03:04:05.67'::timestamp as timestamp_test"
     31for row in cur1
    2032    print repr(row) 
    2133print "end query..."