Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

Changeset 219

Show
Ignore:
Timestamp:
11/08/07 17:59:50
Author:
fumanchu
Message:

postgres: better str to bytea support.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/geniusql/providers/postgres.py

    r218 r219  
    219219 
    220220 
    221 class PgBYTEA_Adapter(adapters.Pickler): 
    222      
    223     def push(self, value, dbtype): 
    224         if value is None: 
    225             return 'NULL' 
    226         # See http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html 
    227         value = pickle.dumps(value, 2) 
    228         def repl(char): 
    229             o = ord(char) 
    230             if o <= 31 or o == 39 or o == 92 or o >= 127: 
    231                 return r"\\%03d" % int(oct(o)) 
    232             return char 
    233         return "'%s'::bytea" % "".join(map(repl, value)) 
    234  
    235  
    236221class Pg_str_to_VARCHAR(adapters.str_to_SQL92VARCHAR): 
    237222     
     
    338323                        "%r and %r" % (op, op1.pytype, op2.pytype)) 
    339324 
     325 
     326# ---------------------------- BYTEA Adapters ---------------------------- # 
     327 
     328 
     329class Pg_str_to_BYTEA(Pg_str_to_VARCHAR): 
     330    """Python str to PostgreSQL bytea adapter. 
     331     
     332    For the most part, Postgres bytea works like Python's str: a sequence 
     333    of bytes. Certain bytes have to be octal-escaped for consumption by PG. 
     334     
     335    See http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html 
     336    """ 
     337     
     338    def push(self, value, dbtype): 
     339        if value is None: 
     340            return 'NULL' 
     341        def repl(char): 
     342            o = ord(char) 
     343            if o <= 31 or o == 39 or o == 92 or o >= 127: 
     344                return r"\\%03d" % int(oct(o)) 
     345            return char 
     346        return "'%s'::bytea" % "".join(map(repl, value)) 
     347 
     348 
     349class Pg_unicode_to_BYTEA(Pg_unicode_to_VARCHAR): 
     350    """Python unicode to PostgreSQL bytea adapter. 
     351     
     352    For the most part, Postgres bytea works like Python's str: a sequence 
     353    of bytes. Certain bytes have to be octal-escaped for consumption by PG. 
     354     
     355    See http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html 
     356    """ 
     357     
     358    def push(self, value, dbtype): 
     359        if value is None: 
     360            return 'NULL' 
     361        if not isinstance(value, str): 
     362            value = value.encode(dbtype.encoding) 
     363        def repl(char): 
     364            o = ord(char) 
     365            if o <= 31 or o == 39 or o == 92 or o >= 127: 
     366                return r"\\%03d" % int(oct(o)) 
     367            return char 
     368        return "'%s'::bytea" % "".join(map(repl, value)) 
     369 
     370 
     371class PgBYTEA_Pickler(PgPickler): 
     372    """Python object to PostgreSQL bytea adapter. 
     373     
     374    For the most part, Postgres bytea works like Python's str: a sequence 
     375    of bytes. Certain bytes have to be octal-escaped for consumption by PG. 
     376     
     377    See http://www.postgresql.org/docs/8.1/interactive/datatype-binary.html 
     378    """ 
     379     
     380    def push(self, value, dbtype): 
     381        if value is None: 
     382            return 'NULL' 
     383         
     384        value = pickle.dumps(value, 2) 
     385         
     386        def repl(char): 
     387            o = ord(char) 
     388            if o <= 31 or o == 39 or o == 92 or o >= 127: 
     389                return r"\\%03d" % int(oct(o)) 
     390            return char 
     391        return "'%s'::bytea" % "".join(map(repl, value)) 
    340392 
    341393 
     
    365417class BYTEA(dbtypes.FrozenByteType): 
    366418    """A type for binary data ("byte array").""" 
    367     default_adapters = {str: PgBYTEA_Adapter()} 
     419    default_adapters = {str: Pg_str_to_BYTEA(), 
     420                        unicode: Pg_unicode_to_BYTEA(), 
     421                        None: PgBYTEA_Pickler(), 
     422                        } 
    368423    default_pytype = str 
     424    encoding = 'utf8' 
    369425 
    370426class BIT(dbtypes.SQL92VARCHAR): 
  • trunk/geniusql/test/zoo_fixture.py

    r204 r219  
    6363        Zoo.add_index('ID') 
    6464        Zoo['Name'] = schema.column() 
     65         
     66        if db.__class__.__name__.endswith('PgDatabase'): 
     67            from geniusql.providers import postgres 
     68            Zoo['Name'].dbtype = postgres.BYTEA() 
     69            Zoo['Name'].adapter = postgres.Pg_unicode_to_BYTEA() 
     70         
    6571        Zoo['Founded'] = schema.column(datetime.date) 
    6672        Zoo['Opens'] = schema.column(datetime.time)