Changeset 893 for trunk


Ignore:
Timestamp:
Sep 21, 2016, 10:44:59 AM (3 years ago)
Author:
cito
Message:

Add a way to switch off array casting in pgdb

Location:
trunk
Files:
5 edited

Legend:

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

    r887 r893  
    22=========
    33
    4 Version 5.0.2 (2016-09-08)
     4Version 5.0.3 (2016-09-21)
     5--------------------------
     6- It is now possible to use a custom array cast method for the pgdb module,
     7  e.g. pgdb.set_typecast('anyarray', lambda v, basecast: v) will cause arrays
     8  to be always returned as strings instead of lists.
     9
     10Version 5.0.2 (2016-09-13)
    511--------------------------
    612- Fixed an infinite recursion problem in the DB wrapper class of the classic
  • trunk/docs/contents/pgdb/module.rst

    r882 r893  
    8585
    8686.. versionadded:: 5.0
     87
     88As of version 5.0.3 you can also use this method to change the typecasting
     89of PostgreSQL array types. You must run ``set_typecast('anyarray', cast)``
     90in order to do this. The ``cast`` method must take a string value and a cast
     91function for the base type and return the array converted to a Python object.
     92For instance, run ``set_typecast('anyarray', lambda v, c: v)`` to switch off
     93the casting of arrays completely, and always return them encoded as strings.
    8794
    8895.. method:: reset_typecast([typ])
  • trunk/docs/index.rst

    r889 r893  
    1 .. PyGreSQL index page without toc (for use with cloud theme)
     1.. PyGreSQL index page with toc (for use without cloud theme)
    22
    33Welcome to PyGreSQL
     
    55
    66.. toctree::
    7     :hidden:
     7    :maxdepth: 2
    88
     9    about
    910    copyright
    1011    announce
     
    1213    contents/index
    1314    community/index
    14 
    15 .. include:: about.txt
  • trunk/pgdb.py

    r881 r893  
    518518    def create_array_cast(self, basecast):
    519519        """Create an array typecast for the given base cast."""
     520        cast_array = self['anyarray']
    520521        def cast(v):
    521522            return cast_array(v, basecast)
     
    524525    def create_record_cast(self, name, fields, casts):
    525526        """Create a named record typecast for the given fields and casts."""
     527        cast_record = self['record']
    526528        record = namedtuple(name, fields)
    527529        def cast(v):
  • trunk/tests/test_dbapi20.py

    r881 r893  
    931931        self.assertEqual(i8, 8)
    932932
     933    def test_set_typecast_for_arrays(self):
     934        query = 'select ARRAY[1,2,3]'
     935        try:
     936            con = self._connect()
     937            try:
     938                r = con.cursor().execute(query).fetchone()[0]
     939            finally:
     940                con.close()
     941            self.assertIsInstance(r, list)
     942            self.assertEqual(r, [1, 2, 3])
     943            pgdb.set_typecast('anyarray', lambda v, basecast: v)
     944            con = self._connect()
     945            try:
     946                r = con.cursor().execute(query).fetchone()[0]
     947            finally:
     948                con.close()
     949            self.assertIsInstance(r, str)
     950            self.assertEqual(r, '{1,2,3}')
     951        finally:
     952            pgdb.reset_typecast()
     953        con = self._connect()
     954        try:
     955            r = con.cursor().execute(query).fetchone()[0]
     956        finally:
     957            con.close()
     958        self.assertIsInstance(r, list)
     959        self.assertEqual(r, [1, 2, 3])
     960
    933961    def test_unicode_with_utf8(self):
    934962        table = self.table_prefix + 'booze'
Note: See TracChangeset for help on using the changeset viewer.