Changeset 315


Ignore:
Timestamp:
Jan 6, 2007, 11:45:41 AM (13 years ago)
Author:
darcy
Message:

Allow keywords to be specified for insert, update and delete.
Document changes.
Add unit tests for changes.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/pg.txt

    r314 r315  
    841841Syntax::
    842842
    843   insert(table, a)
     843  insert(table, [d,] [key = val, ...])
    844844
    845845Parameters:
    846846  :table: name of table
    847   :a:     a dictionary of values
     847  :d:     optional dictionary of values
    848848
    849849Return type:
     
    855855  values from the database. This causes the dictionary to be updated
    856856  with values that are modified by rules, triggers, etc.
     857
     858  If the optional dictionary is not supplied then the required values
     859  must be included as keyword/value pairs.  If a dictionary is supplied
     860  then any keywords provided will be added to or replace the entry in
     861  the dictionary.
    857862
    858863  Due to the way that this function works you will find inserts taking
     
    866871Syntax::
    867872
    868   update(table, a)
     873  update(table, [d,] [key = val, ...])
    869874
    870875Parameters:
    871876  :table: name of table
    872   :a:     a dictionary of values
     877  :d:     optional dictionary of values
    873878
    874879Return type:
     
    881886  to triggers, rules, defaults, etc.
    882887
     888  Like insert, the dictionary is optional and updates will be performed
     889  on the fields in the keywords.  There must be an OID or primary key
     890  either in the dictionary where the OID must be munged, or in the keywords
     891  where it can be simply the string "oid".
     892
    883893clear - clears row values in memory
    884894-----------------------------------
     
    889899Parameters:
    890900  :table: name of table
    891   :a:     a dictionary of values
     901  :a:     optional dictionary of values
    892902
    893903Return type:
     
    901911  matching attribute names are cleared with everything else left unchanged.
    902912
     913  If the dictionary is not supplied a new one is created.
     914
    903915delete - delete a row from a database table
    904916-------------------------------------------
    905917Syntax::
    906918
    907   delete(table, [a])
     919  delete(table, [d,] [key = val, ...])
    908920
    909921Parameters:
    910922  :table: name of table
    911   :a:     a dictionary of values
     923  :d:     optional dictionary of values
    912924
    913925Returns:
     
    916928Description:
    917929  This method deletes the row from a table. It deletes based on the OID
    918   as munged as described above.
     930  as munged as described above.  Alternatively, the keyword "oid" can
     931  be used to specify the OID.
    919932
    920933escape_string - escape a string for use within SQL
  • trunk/module/TEST_PyGreSQL_classic.py

    r297 r315  
    3030
    3131        try: db.query("CREATE TABLE _test_schema "
    32                        "(_test int PRIMARY KEY, _i interval)")
     32                "(_test int PRIMARY KEY, _i interval, dvar int DEFAULT 999)")
    3333        except: pass
    3434
     
    4848        self.assertEqual(
    4949            db.get_attnames('_test_schema'),
    50             {'_test': 'int', 'oid': 'int', '_i': 'date'}
     50            {'_test': 'int', 'oid': 'int', '_i': 'date', 'dvar': 'int'}
    5151        )
    5252
    5353        self.assertEqual(
    5454            db.get_attnames('public._test_schema'),
    55             {'_test': 'int', 'oid': 'int', '_i': 'date'}
     55            {'_test': 'int', 'oid': 'int', '_i': 'date', 'dvar': 'int'}
    5656        )
    5757
     
    8585        self.failUnlessRaises(KeyError, db.get, '_test_vschema', 1234)
    8686        db.get('_test_vschema', 1234, keyname = '_test')
     87
     88    def test_insert(self):
     89        db.query("DELETE FROM _test_schema")
     90
     91        d = dict(_test = 1234)
     92        db.insert('_test_schema', d)
     93        self.assertEqual(d['dvar'], 999)
     94
     95        db.insert('_test_schema', _test = 1235)
     96        self.assertEqual(d['dvar'], 999)
     97
     98    def test_update(self):
     99        try: db.query("INSERT INTO _test_schema VALUES (1234)")
     100        except: pass # OK if it already exists
     101
     102        r = db.get('_test_schema', 1234)
     103        r['dvar'] = 123
     104        db.update('_test_schema', r)
     105        r = db.get('_test_schema', 1234)
     106        self.assertEqual(r['dvar'], 123)
     107
     108        r = db.get('_test_schema', 1234)
     109        db.update('_test_schema', _test = 1234, dvar = 456)
     110        r = db.get('_test_schema', 1234)
     111        self.assertEqual(r['dvar'], 456)
     112
     113        r = db.get('_test_schema', 1234)
     114        db.update('_test_schema', r, dvar = 456)
     115        r = db.get('_test_schema', 1234)
     116        self.assertEqual(r['dvar'], 456)
    87117
    88118    def test_quote(self):
  • trunk/module/pg.py

    r312 r315  
    66# Improved by Christoph Zwerschke
    77#
    8 # $Id: pg.py,v 1.50 2006-12-30 07:13:24 darcy Exp $
     8# $Id: pg.py,v 1.51 2007-01-06 16:45:41 darcy Exp $
    99#
    1010
     
    399399                return arg
    400400
    401         def insert(self, cl, a):
     401        def insert(self, cl, d = None, **kw):
    402402                """Insert a tuple into a database table.
    403403
     
    411411
    412412                """
     413                if d is None: a = {}
     414                else: a = d
     415                a.update(kw)
     416
    413417                qcl = _join_parts(self._split_schema(cl)) # build qualified name
    414418                foid = 'oid(%s)' % qcl # build mangled name
     
    432436                        return None
    433437
    434         def update(self, cl, a):
     438        def update(self, cl, d = None, **kw):
    435439                """Update an existing row in a database table.
    436440
     
    445449                qcl = _join_parts(self._split_schema(cl)) # build qualified name
    446450                foid = 'oid(%s)' % qcl # build mangled oid
     451
     452                # Note that we only accept oid key from named args for safety
     453                if kw.has_key('oid'):
     454                        kw[foid] = kw['oid']
     455                        del kw['oid']
     456
     457                if d is None: a = {}
     458                else: a = d
     459                a.update(kw)
    447460
    448461                # XXX this code is for backwards compatibility and will be
     
    504517                return a
    505518
    506         def delete(self, cl, a):
     519        def delete(self, cl, d = None, **kw):
    507520                """Delete an existing row in a database table.
    508521
    509522                This method deletes the row from a table.
    510                 It deletes based on the OID munged as described above.
    511 
    512                 """
     523                It deletes based on the OID munged as described above."""
     524
    513525                # Like update, delete works on the oid.
    514526                # One day we will be testing that the record to be deleted
     
    516528                qcl = _join_parts(self._split_schema(cl)) # build qualified name
    517529                foid = 'oid(%s)' % qcl # build mangled oid
     530
     531                # Note that we only accept oid key from named args for safety
     532                if kw.has_key('oid'):
     533                        kw[foid] = kw['oid']
     534                        del kw['oid']
     535
     536                if d is None: a = {}
     537                else: a = d
     538                a.update(kw)
    518539
    519540                # XXX this code is for backwards compatibility and will be
Note: See TracChangeset for help on using the changeset viewer.