Skip to content

Commit

Permalink
Move SQLite functions to the relevant module (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet authored Feb 5, 2023
1 parent 965d8d5 commit 103e0cb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 51 deletions.
39 changes: 39 additions & 0 deletions geoalchemy2/dialects/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import os

from sqlalchemy import text
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import func
from sqlalchemy.sql import select

from geoalchemy2 import functions
from geoalchemy2.dialects.common import _check_spatial_type
from geoalchemy2.dialects.common import _format_select_args
from geoalchemy2.dialects.common import _spatial_idx_name
Expand Down Expand Up @@ -220,3 +222,40 @@ def before_drop(table, bind, **kw):
def after_drop(table, bind, **kw):
"""Handle spatial indexes during the after_drop event."""
table.columns = table.info.pop("_saved_columns")


# Define compiled versions for functions in SpatiaLite whose names don't have
# the ST_ prefix.
_SQLITE_FUNCTIONS = {
"ST_GeomFromEWKT": "GeomFromEWKT",
"ST_GeomFromEWKB": "GeomFromEWKB",
"ST_AsBinary": "AsBinary",
"ST_AsEWKB": "AsEWKB",
"ST_AsGeoJSON": "AsGeoJSON",
}


def _compiles_sqlite(cls, fn):
def _compile_sqlite(element, compiler, **kw):
return "{}({})".format(fn, compiler.process(element.clauses, **kw))

compiles(getattr(functions, cls), "sqlite")(_compile_sqlite)


def register_sqlite_mapping(mapping):
"""Register compilation mappings for the given functions.
Args:
mapping: Should have the following form::
{
"function_name_1": "sqlite_function_name_1",
"function_name_2": "sqlite_function_name_2",
...
}
"""
for cls, fn in mapping.items():
_compiles_sqlite(cls, fn)


register_sqlite_mapping(_SQLITE_FUNCTIONS)
51 changes: 0 additions & 51 deletions geoalchemy2/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,54 +277,3 @@ def __init__(self, *args, **kwargs):
attributes["__doc__"] = "\n\n".join(docs)

globals()[name] = type(name, (GenericFunction,), attributes)


#
# Define compiled versions for functions in SpatiaLite whose names don't have
# the ST_ prefix.
#


_SQLITE_FUNCTIONS = {
"ST_GeomFromEWKT": "GeomFromEWKT",
"ST_GeomFromEWKB": "GeomFromEWKB",
"ST_AsBinary": "AsBinary",
"ST_AsEWKB": "AsEWKB",
"ST_AsGeoJSON": "AsGeoJSON",
}


# Default handlers are required for SQLAlchemy < 1.1
# See more details in https://github.com/geoalchemy/geoalchemy2/issues/213
def _compiles_default(cls):
def _compile_default(element, compiler, **kw):
return "{}({})".format(cls, compiler.process(element.clauses, **kw))

compiles(globals()[cls])(_compile_default)


def _compiles_sqlite(cls, fn):
def _compile_sqlite(element, compiler, **kw):
return "{}({})".format(fn, compiler.process(element.clauses, **kw))

compiles(globals()[cls], "sqlite")(_compile_sqlite)


def register_sqlite_mapping(mapping):
"""Register compilation mappings for the given functions.
Args:
mapping: Should have the following form::
{
"function_name_1": "sqlite_function_name_1",
"function_name_2": "sqlite_function_name_2",
...
}
"""
for cls, fn in mapping.items():
_compiles_default(cls)
_compiles_sqlite(cls, fn)


register_sqlite_mapping(_SQLITE_FUNCTIONS)

0 comments on commit 103e0cb

Please sign in to comment.