Changeset 817 for trunk/pg.py


Ignore:
Timestamp:
Feb 4, 2016, 3:18:08 PM (3 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/pg.py

    r815 r817  
    180180        'float': 'float4 float8',
    181181        'int': 'cid int2 int4 int8 oid xid',
    182         'json': 'json jsonb',
     182        'hstore': 'hstore', 'json': 'json jsonb',
    183183        'num': 'numeric',
    184184        'money': 'money',
     
    227227
    228228
     229class Bytea(bytes):
     230    """Wrapper class for marking Bytea values."""
     231
     232
     233class Hstore(dict):
     234    """Wrapper class for marking hstore values."""
     235
     236    _re_quote = regex('^[Nn][Uu][Ll][Ll]$|[ ,=>]')
     237
     238    @classmethod
     239    def _quote(cls, s):
     240        if s is None:
     241            return 'NULL'
     242        if not s:
     243            return '""'
     244        s = s.replace('"', '\\"')
     245        if cls._re_quote.search(s):
     246            s = '"%s"' % s
     247        return s
     248
     249    def __str__(self):
     250        q = self._quote
     251        return ','.join('%s=>%s' % (q(k), q(v)) for k, v in self.items())
     252
     253
     254class Json:
     255    """Wrapper class for marking Json values."""
     256
     257    def __init__(self, obj):
     258        self.obj = obj
     259
     260
    229261class Literal(str):
    230262    """Wrapper class for marking literal SQL values."""
    231 
    232 
    233 class Json:
    234     """Wrapper class for marking Json values."""
    235 
    236     def __init__(self, obj):
    237         self.obj = obj
    238 
    239 
    240 class Bytea(bytes):
    241     """Wrapper class for marking Bytea values."""
    242263
    243264
     
    836857        'text': str, 'varchar': str,
    837858        'bool': cast_bool, 'bytea': unescape_bytea,
    838         'int2': int, 'int4': int, 'serial': int,
    839         'int8': long, 'json': cast_json, 'jsonb': cast_json,
     859        'int2': int, 'int4': int, 'serial': int, 'int8': long,
     860        'hstore': cast_hstore, 'json': cast_json, 'jsonb': cast_json,
    840861        'oid': long, 'oid8': long,
    841862        'float4': float, 'float8': float,
Note: See TracChangeset for help on using the changeset viewer.