Changeset 872 for trunk


Ignore:
Timestamp:
Apr 9, 2016, 6:53:52 AM (3 years ago)
Author:
cito
Message:

Prefer OID over PK in update() and delete()

This restores backward compatibility with Python 4.x and allows to update
the primary key by identifying the row via the OID.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/contents/changelog.rst

    r866 r872  
    11ChangeLog
    22=========
     3
     4Version 5.0.1
     5-------------
     6- The update() and delete() methods of the DB wrapper now use the OID instead
     7  of the primary key if both are provided. This restores backward compatibility
     8  with PyGreSQL 4.x and allows updating the primary key itself if an OID exists.
     9- Made C extension compatible with MSVC 9 again (needed for Python 2 on Windws).
    310
    411Version 5.0 (2016-03-20)
  • trunk/docs/contents/pg/db_wrapper.rst

    r857 r872  
    364364    :raises KeyError: missing key value for the row
    365365
    366 Similar to insert but updates an existing row.  The update is based on
     366Similar to insert, but updates an existing row.  The update is based on
    367367the primary key of the table or the OID value as munged by :meth:`DB.get`
    368 or passed as keyword.
     368or passed as keyword.  The OID will take precedence if provided, so that it
     369is possible to update the primary key itself.
    369370
    370371The dictionary is then modified to reflect any changes caused by the
     
    541542This method deletes the row from a table.  It deletes based on the
    542543primary key of the table or the OID value as munged by :meth:`DB.get`
    543 or passed as keyword.
     544or passed as keyword.  The OID will take precedence if provided.
    544545
    545546The return value is the number of deleted rows (i.e. 0 if the row did not
  • trunk/pg.py

    r857 r872  
    18651865        corresponding to the passed keyname or primary key.  The fetched row
    18661866        from the table will be returned as a new dictionary or used to replace
    1867         the existing values when row was passed as aa dictionary.
     1867        the existing values when row was passed as a dictionary.
    18681868
    18691869        The OID is also put into the dictionary if the table has one, but
     
    19771977        """Update an existing row in a database table.
    19781978
    1979         Similar to insert but updates an existing row.  The update is based
    1980         on the primary key of the table or the OID value as munged by get
    1981         or passed as keyword.
     1979        Similar to insert, but updates an existing row.  The update is based
     1980        on the primary key of the table or the OID value as munged by get()
     1981        or passed as keyword.  The OID will take precedence if provided, so
     1982        that it is possible to update the primary key itself.
    19821983
    19831984        The dictionary is then modified to reflect any changes caused by the
     
    19951996        if qoid and qoid in row and 'oid' not in row:
    19961997            row['oid'] = row[qoid]
    1997         try:  # try using the primary key
    1998             keyname = self.pkey(table, True)
    1999         except KeyError:  # the table has no primary key
    2000             # try using the oid instead
    2001             if qoid and 'oid' in row:
    2002                 keyname = ('oid',)
    2003             else:
     1998        if qoid and 'oid' in row:  # try using the oid
     1999            keyname = ('oid',)
     2000        else:  # try using the primary key
     2001            try:
     2002                keyname = self.pkey(table, True)
     2003            except KeyError:  # the table has no primary key
    20042004                raise _prg_error('Table %s has no primary key' % table)
    2005         else:  # the table has a primary key
    20062005            # check whether all key columns have values
    20072006            if not set(keyname).issubset(row):
    2008                 # try using the oid instead
    2009                 if qoid and 'oid' in row:
    2010                     keyname = ('oid',)
    2011                 else:
    2012                     raise KeyError('Missing primary key in row')
     2007                raise KeyError('Missing value for primary key in row')
    20132008        params = self.adapter.parameter_list()
    20142009        adapt = params.add
     
    20662061
    20672062        So if in the case of a conflict you want to update every column that
    2068         has been passed in the dictionary row , you would call upsert(table, row).
     2063        has been passed in the dictionary row, you would call upsert(table, row).
    20692064        If you don't want to do anything in case of a conflict, i.e. leave
    20702065        the existing row as it is, call upsert(table, row, **dict.fromkeys(row)).
     
    21732168        This method deletes the row from a table.  It deletes based on the
    21742169        primary key of the table or the OID value as munged by get() or
    2175         passed as keyword.
     2170        passed as keyword.  The OID will take precedence if provided.
    21762171
    21772172        The return value is the number of deleted rows (i.e. 0 if the row
     
    21922187        if qoid and qoid in row and 'oid' not in row:
    21932188            row['oid'] = row[qoid]
    2194         try:  # try using the primary key
    2195             keyname = self.pkey(table, True)
    2196         except KeyError:  # the table has no primary key
    2197             # try using the oid instead
    2198             if qoid and 'oid' in row:
    2199                 keyname = ('oid',)
    2200             else:
     2189        if qoid and 'oid' in row:  # try using the oid
     2190            keyname = ('oid',)
     2191        else:  # try using the primary key
     2192            try:
     2193                keyname = self.pkey(table, True)
     2194            except KeyError:  # the table has no primary key
    22012195                raise _prg_error('Table %s has no primary key' % table)
    2202         else:  # the table has a primary key
    22032196            # check whether all key columns have values
    22042197            if not set(keyname).issubset(row):
    2205                 # try using the oid instead
    2206                 if qoid and 'oid' in row:
    2207                     keyname = ('oid',)
    2208                 else:
    2209                     raise KeyError('Missing primary key in row')
     2198                raise KeyError('Missing value for primary key in row')
    22102199        params = self.adapter.parameter_list()
    22112200        adapt = params.add
  • trunk/tests/test_classic_dbwrapper.py

    r857 r872  
    17661766        r = query(q).getresult()
    17671767        self.assertEqual(r, [(1, 3), (5, 9)])
     1768        s.update(n=4, m=7)
     1769        r = update('test_table', s, oid=oid)
     1770        self.assertIs(r, s)
     1771        self.assertEqual(r['n'], 4)
     1772        self.assertEqual(r['m'], 7)
     1773        r = query(q).getresult()
     1774        self.assertEqual(r, [(1, 3), (4, 7)])
    17681775
    17691776    def testUpdateWithoutOid(self):
     
    22452252        r = delete('test_table', s, n=5)
    22462253        self.assertEqual(r, 0)
    2247         self.assertEqual(query(q).getresult()[0], (6, 1))
     2254        s = get('test_table', 6, 'n')
     2255        self.assertEqual(s['n'], 6)
     2256        s['n'] = 7
     2257        r = delete('test_table', s)
     2258        self.assertEqual(r, 1)
     2259        self.assertEqual(query(q).getresult()[0], (None, 0))
    22482260
    22492261    def testDeleteWithCompositeKey(self):
Note: See TracChangeset for help on using the changeset viewer.