Changeset 817 for trunk/pgdb.py


Ignore:
Timestamp:
Feb 4, 2016, 3:18:08 PM (4 years ago)
Author:
cito
Message:

Support the hstore data type

Added adaptation and typecasting of the hstore type as Python dictionaries.
For the typecasting, a fast parser has been added to the C extension.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pgdb.py

    r815 r817  
    387387        'text': str, 'varchar': str,
    388388        'bool': cast_bool, 'bytea': unescape_bytea,
    389         'int2': int, 'int4': int, 'serial': int,
    390         'int8': long, 'json': jsondecode, 'jsonb': jsondecode,
     389        'int2': int, 'int4': int, 'serial': int, 'int8': long,
     390        'hstore': cast_hstore, 'json': jsondecode, 'jsonb': jsondecode,
    391391        'oid': long, 'oid8': long,
    392392        'float4': float, 'float8': float,
     
    731731        if value is None:
    732732            return 'NULL'
    733         if isinstance(value, (datetime, date, time, timedelta, Json)):
     733        if isinstance(value, (datetime, date, time, timedelta, Hstore, Json)):
    734734            value = str(value)
    735735        if isinstance(value, basestring):
     
    15601560# Additional type helpers for PyGreSQL:
    15611561
     1562class Bytea(bytes):
     1563    """Construct an object capable of holding a bytea value."""
     1564
     1565
    15621566def Interval(days, hours=0, minutes=0, seconds=0, microseconds=0):
    15631567    """Construct an object holding a time inverval value."""
     
    15651569        microseconds=microseconds)
    15661570
    1567 class Bytea(bytes):
    1568     """Construct an object capable of holding a bytea value."""
     1571
     1572class Hstore(dict):
     1573    """Wrapper class for marking hstore values."""
     1574
     1575    _re_quote = regex('^[Nn][Uu][Ll][Ll]$|[ ,=>]')
     1576
     1577    @classmethod
     1578    def _quote(cls, s):
     1579        if s is None:
     1580            return 'NULL'
     1581        if not s:
     1582            return '""'
     1583        s = s.replace('"', '\\"')
     1584        if cls._re_quote.search(s):
     1585            s = '"%s"' % s
     1586        return s
     1587
     1588    def __str__(self):
     1589        q = self._quote
     1590        return ','.join('%s=>%s' % (q(k), q(v)) for k, v in self.items())
    15691591
    15701592
     
    15821604        return self.encode(obj)
    15831605
    1584     __pg_repr__ = __str__
    1585 
    15861606
    15871607class Literal:
     
    15951615
    15961616    __pg_repr__ = __str__
    1597 
    15981617
    15991618# If run as script, print some information:
Note: See TracChangeset for help on using the changeset viewer.