Changeset 799 for trunk/pgdb.py


Ignore:
Timestamp:
Jan 31, 2016, 1:45:58 PM (3 years ago)
Author:
cito
Message:

Improve adaptation and add query_formatted() method

Also added more tests and documentation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pgdb.py

    r798 r799  
    210210            for t in typ:
    211211                self[t] = cast
    212                 self.pop('_%s % t', None)
     212                self.pop('_%s' % t, None)
    213213
    214214    def reset(self, typ=None):
     
    225225                typ = [typ]
    226226            for t in typ:
    227                 self.set(t, defaults.get(t))
     227                cast = defaults.get(t)
     228                if cast:
     229                    self[t] = cast
     230                    t = '_%s' % t
     231                    cast = defaults.get(t)
     232                    if cast:
     233                        self[t] = cast
     234                    else:
     235                        self.pop(t, None)
     236                else:
     237                    self.pop(t, None)
     238                    self.pop('_%s' % t, None)
    228239
    229240    def create_array_cast(self, cast):
     
    483494                return "'NaN'"
    484495            return value
    485         if isinstance(value, (int, long, Decimal)):
     496        if isinstance(value, (int, long, Decimal, Literal)):
    486497            return value
    487498        if isinstance(value, list):
     
    497508            # literal because it carries the information that this is a record
    498509            # and not a string.  We don't use the keyword ROW in order to make
    499             # this usable with the IN synntax as well.  It is only necessary
     510            # this usable with the IN syntax as well.  It is only necessary
    500511            # when the records has a single column which is not really useful.
    501512            q = self._quote
     
    503514        try:
    504515            value = value.__pg_repr__()
    505             if isinstance(value, (tuple, list)):
    506                 value = self._quote(value)
    507             return value
    508516        except AttributeError:
    509517            raise InterfaceError(
    510518                'Do not know how to adapt type %s' % type(value))
     519        if isinstance(value, (tuple, list)):
     520            value = self._quote(value)
     521        return value
    511522
    512523    def _quoteparams(self, string, parameters):
     
    12961307# Additional type helpers for PyGreSQL:
    12971308
     1309class Bytea(bytes):
     1310    """Construct an object capable of holding a bytea value."""
     1311
     1312
    12981313class Json:
    12991314    """Construct a wrapper for holding an object serializable to JSON."""
     
    13121327
    13131328
     1329class Literal:
     1330    """Construct a wrapper for holding a literal SQL string."""
     1331
     1332    def __init__(self, sql):
     1333        self.sql = sql
     1334
     1335    def __str__(self):
     1336        return self.sql
     1337
     1338    __pg_repr__ = __str__
     1339
     1340
    13141341# If run as script, print some information:
    13151342
Note: See TracChangeset for help on using the changeset viewer.