Changeset 872 for trunk/pg.py


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.