Changeset 664 for trunk


Ignore:
Timestamp:
Dec 10, 2015, 7:59:20 AM (4 years ago)
Author:
cito
Message:

get() should convert bytea to bytes

insert() and update() should also do this with their return values.

Location:
trunk/module
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/module/pg.py

    r663 r664  
    759759            except KeyError:
    760760                raise _prg_error('Class %s has no primary key' % qcl)
     761        attnames = self.get_attnames(qcl)
    761762        # We want the oid for later updates if that isn't the key
    762763        if keyname == 'oid':
     
    766767            else:
    767768                arg = {qoid: arg}
     769            what = '*'
    768770            where = 'oid = %s' % arg[qoid]
    769             attnames = '*'
    770         else:
    771             attnames = self.get_attnames(qcl)
     771        else:
    772772            if isinstance(keyname, basestring):
    773773                keyname = (keyname,)
     
    776776                    raise _prg_error('Composite key needs dict as arg')
    777777                arg = dict([(k, arg) for k in keyname])
     778            what = ', '.join(attnames)
    778779            where = ' AND '.join(['%s = %s'
    779780                % (k, self._quote(arg[k], attnames[k])) for k in keyname])
    780             attnames = ', '.join(attnames)
    781         q = 'SELECT %s FROM %s WHERE %s LIMIT 1' % (attnames, qcl, where)
     781        q = 'SELECT %s FROM %s WHERE %s LIMIT 1' % (what, qcl, where)
    782782        self._do_debug(q)
    783783        res = self.db.query(q).dictresult()
    784784        if not res:
    785785            raise _db_error('No such record in %s where %s' % (qcl, where))
    786         for att, value in res[0].items():
    787             arg[qoid if att == 'oid' else att] = value
     786        for n, value in res[0].items():
     787            if n == 'oid':
     788                n = qoid
     789            elif attnames.get(n) == 'bytea':
     790                value = self.unescape_bytea(value)
     791            arg[n] = value
    788792        return arg
    789793
     
    823827        res = self.db.query(q)
    824828        if ret:
    825             res = res.dictresult()
    826             for att, value in res[0].items():
    827                 d[qoid if att == 'oid' else att] = value
     829            res = res.dictresult()[0]
     830            for n, value in res.items():
     831                if n == 'oid':
     832                    n = qoid
     833                elif attnames.get(n) == 'bytea':
     834                    value = self.unescape_bytea(value)
     835                d[n] = value
    828836        elif isinstance(res, int):
    829837            d[qoid] = res
     
    894902        if ret:
    895903            res = res.dictresult()[0]
    896             for att, value in res.items():
    897                 d[qoid if att == 'oid' else att] = value
     904            for n, value in res.items():
     905                if n == 'oid':
     906                    n = qoid
     907                elif attnames.get(n) == 'bytea':
     908                    value = self.unescape_bytea(value)
     909                d[n] = value
    898910        else:
    899911            if selectable:
  • trunk/module/tests/test_classic_dbwrapper.py

    r662 r664  
    10881088        query('drop table bytea_test')
    10891089
    1090     def testInsertUpdateBytea(self):
     1090    def testInsertUpdateGetBytea(self):
    10911091        query = self.db.query
    10921092        query('drop table if exists bytea_test')
     
    11001100        self.assertIn('data', r)
    11011101        r = r['data']
    1102         # the following two lines should be removed once insert()
    1103         # will be enhanced to adapt the types of return values
    1104         self.assertIsInstance(r, str)
    1105         r = self.db.unescape_bytea(r)
    11061102        self.assertIsInstance(r, bytes)
    11071103        self.assertEqual(r, s)
     
    11141110        self.assertIn('data', r)
    11151111        r = r['data']
    1116         # the following two lines should be removed once update()
    1117         # will be enhanced to adapt the types of return values
    1118         self.assertIsInstance(r, str)
    1119         r = self.db.unescape_bytea(r)
    11201112        self.assertIsInstance(r, bytes)
    11211113        self.assertEqual(r, s)
     
    11281120        self.assertIsInstance(r, str)
    11291121        r = self.db.unescape_bytea(r)
     1122        self.assertIsInstance(r, bytes)
     1123        self.assertEqual(r, s)
     1124        r = self.db.get('bytea_test', dict(n=5))
     1125        self.assertIsInstance(r, dict)
     1126        self.assertIn('n', r)
     1127        self.assertEqual(r['n'], 5)
     1128        self.assertIn('data', r)
     1129        r = r['data']
    11301130        self.assertIsInstance(r, bytes)
    11311131        self.assertEqual(r, s)
Note: See TracChangeset for help on using the changeset viewer.