Changeset 878 for trunk


Ignore:
Timestamp:
Jul 21, 2016, 12:57:25 PM (3 years ago)
Author:
cito
Message:

Allow extra keys in params to query_formatted()

Location:
trunk
Files:
3 edited

Legend:

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

    r877 r878  
    1414- Fixed an issue with Postgres types that have an OID >= 0x80000000 (reported
    1515  on the mailing list by Justin Pryzby).
     16- Allow extra values that are not used in the command in the parameter dict
     17  passed to the query_formatted() method (as suggested by Justin Pryzby).
    1618- Made C extension compatible with MSVC 9 again (this was needed to compile for
    1719  Python 2 on Windows).
  • trunk/pg.py

    r876 r878  
    620620                    for value in values:
    621621                        append(add(value))
    622             command = command % tuple(literals)
     622            command %= tuple(literals)
    623623        elif isinstance(values, dict):
     624            # we want to allow extra keys in the dictionary,
     625            # so we first must find the values actually used in the command
     626            used_values = {}
     627            literals = dict.fromkeys(values, '')
     628            for key in literals:
     629                del literals[key]
     630                try:
     631                    command % literals
     632                except KeyError:
     633                    used_values[key] = values[key]
     634                literals[key] = ''
     635            values = used_values
    624636            if inline:
    625637                adapt = self.adapt_inline
     
    630642                literals = {}
    631643                if types:
    632                     if (not isinstance(types, dict) or
    633                             len(types) < len(values)):
     644                    if not isinstance(types, dict):
    634645                        raise TypeError('The values and types do not match')
    635646                    for key in sorted(values):
    636                         literals[key] = add(values[key], types[key])
     647                        literals[key] = add(values[key], types.get(key))
    637648                else:
    638649                    for key in sorted(values):
    639650                        literals[key] = add(values[key])
    640             command = command % literals
     651            command %= literals
    641652        else:
    642653            raise TypeError('The values must be passed as tuple, list or dict')
  • trunk/tests/test_classic_dbwrapper.py

    r876 r878  
    894894        f = self.db.query_formatted
    895895        t = True if pg.get_bool() else 't'
     896        # test with tuple
    896897        q = f("select %s::int, %s::real, %s::text, %s::bool",
    897898              (3, 2.5, 'hello', True))
    898899        r = q.getresult()[0]
    899900        self.assertEqual(r, (3, 2.5, 'hello', t))
     901        # test with tuple, inline
    900902        q = f("select %s, %s, %s, %s", (3, 2.5, 'hello', True), inline=True)
    901903        r = q.getresult()[0]
     
    906908            r = tuple(r)
    907909        self.assertEqual(r, (3, 2.5, 'hello', t))
     910        # test with dict
     911        q = f("select %(a)s::int, %(b)s::real, %(c)s::text, %(d)s::bool",
     912              dict(a=3, b=2.5, c='hello', d=True))
     913        r = q.getresult()[0]
     914        self.assertEqual(r, (3, 2.5, 'hello', t))
     915        # test with dict, inline
     916        q = f("select %(a)s, %(b)s, %(c)s, %(d)s",
     917              dict(a=3, b=2.5, c='hello', d=True), inline=True)
     918        r = q.getresult()[0]
     919        if isinstance(r[1], Decimal):
     920            # Python 2.6 cannot compare float and Decimal
     921            r = list(r)
     922            r[1] = float(r[1])
     923            r = tuple(r)
     924        self.assertEqual(r, (3, 2.5, 'hello', t))
     925        # test with dict and extra values
     926        q = f("select %(a)s||%(b)s||%(c)s||%(d)s||'epsilon'",
     927              dict(a='alpha', b='beta', c='gamma', d='delta', e='extra'))
     928        r = q.getresult()[0][0]
     929        self.assertEqual(r, 'alphabetagammadeltaepsilon')
     930
     931
    908932
    909933    def testPkey(self):
Note: See TracChangeset for help on using the changeset viewer.