Skip to content

Commit

Permalink
Fix cache warnings (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet authored Dec 24, 2021
1 parent 54531cf commit b107df9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions geoalchemy2/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ class CompositeElement(FunctionElement):
Instances of this class wrap a Postgres composite type.
"""

inherit_cache = False

def __init__(self, base, field, type_):
self.name = field
self.type = to_instance(type_)
Expand Down
9 changes: 8 additions & 1 deletion geoalchemy2/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ def __init__(cls, clsname, bases, clsdict):


class TableRowElement(ColumnElement):

inherit_cache = False

def __init__(self, selectable):
self.selectable = selectable

Expand All @@ -88,6 +91,7 @@ class ST_AsGeoJSON(with_metaclass(_GenericMeta, functions.GenericFunction)):
feature version introduced in PostGIS 3."""

name = "ST_AsGeoJSON"
inherit_cache = True

def __init__(self, *args, **kwargs):
expr = kwargs.pop('expr', None)
Expand Down Expand Up @@ -197,7 +201,10 @@ def __init__(self, *args, **kwargs):

# Iterate through _FUNCTIONS and create GenericFunction classes dynamically
for name, type_, doc in _FUNCTIONS:
attributes = {'name': name}
attributes = {
'name': name,
'inherit_cache': True,
}
docs = []

if isinstance(doc, tuple):
Expand Down
18 changes: 18 additions & 0 deletions geoalchemy2/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class _GISType(UserDefinedType):
""" This is the way by which spatial operators are defined for
geometry/geography columns. """

cache_ok = False
""" Disable cache for this type. """

def __init__(self, geometry_type='GEOMETRY', srid=-1, dimension=2,
spatial_index=True, use_N_D_index=False, management=False, use_typmod=None,
from_text=None, name=None, nullable=True):
Expand Down Expand Up @@ -273,6 +276,9 @@ class Geometry(_GISType):
""" The element class to use. Used by the parent class'
``result_processor`` method. """

cache_ok = False
""" Disable cache for this type. """


class Geography(_GISType):
"""
Expand Down Expand Up @@ -302,6 +308,9 @@ class Geography(_GISType):
""" The element class to use. Used by the parent class'
``result_processor`` method. """

cache_ok = False
""" Disable cache for this type. """


class Raster(_GISType):
"""
Expand Down Expand Up @@ -344,6 +353,9 @@ class Raster(_GISType):
""" The element class to use. Used by the parent class'
``result_processor`` method. """

cache_ok = False
""" Disable cache for this type. """

def __init__(self, *args, **kwargs):
# Enforce default values
kwargs['geometry_type'] = None
Expand Down Expand Up @@ -385,6 +397,9 @@ class GeometryDump(CompositeType):
typemap = {'path': postgresql.ARRAY(Integer), 'geom': Geometry}
""" Dictionary defining the contents of a ``geometry_dump``. """

cache_ok = True
""" Enable cache for this type. """


# Register Geometry, Geography and Raster to SQLAlchemy's Postgres reflection
# subsystem.
Expand All @@ -403,3 +418,6 @@ class SummaryStats(CompositeType):
'min': Float,
'max': Float,
}

cache_ok = True
""" Enable cache for this type. """
4 changes: 4 additions & 0 deletions tests/gallery/test_summarystatsagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ class SummaryStatsCustomType(CompositeType):
'max': Float,
}

cache_ok = True


class ST_SummaryStatsAgg(GenericFunction):
type = SummaryStatsCustomType
# Set a specific identifier to not override the actual ST_SummaryStatsAgg function
identifier = "ST_SummaryStatsAgg_custom"

inherit_cache = True


engine = create_engine('postgresql://gis:gis@localhost/gis', echo=True)
metadata = MetaData(engine)
Expand Down

0 comments on commit b107df9

Please sign in to comment.