| 1 |
from functools import lru_cache |
| 2 |
|
| 3 |
from django.db.backends.base.base import NO_DB_ALIAS |
| 4 |
from django.db.backends.postgresql.base import DatabaseWrapper as PsycopgDatabaseWrapper |
| 5 |
from django.db.backends.postgresql.features import ( |
| 6 |
DatabaseFeatures as PsycopgDatabaseFeatures, |
| 7 |
) |
| 8 |
from django.db.backends.postgresql.introspection import ( |
| 9 |
DatabaseIntrospection as PsycopgDatabaseIntrospection, |
| 10 |
) |
| 11 |
from django.db.backends.postgresql.operations import ( |
| 12 |
DatabaseOperations as PsycopgDatabaseOperations, |
| 13 |
) |
| 14 |
from django.db.backends.postgresql.psycopg_any import is_psycopg3 |
| 15 |
|
| 16 |
from .adapter import PostGISAdapter |
| 17 |
from .features import DatabaseFeatures |
| 18 |
from .introspection import PostGISIntrospection |
| 19 |
from .operations import PostGISOperations |
| 20 |
from .schema import PostGISSchemaEditor |
| 21 |
|
| 22 |
if is_psycopg3: |
| 23 |
from psycopg.adapt import Dumper |
| 24 |
from psycopg.pq import Format |
| 25 |
from psycopg.types import TypeInfo |
| 26 |
from psycopg.types.string import TextBinaryLoader, TextLoader |
| 27 |
|
| 28 |
class GeometryType: |
| 29 |
pass |
| 30 |
|
| 31 |
class GeographyType: |
| 32 |
pass |
| 33 |
|
| 34 |
class RasterType: |
| 35 |
pass |
| 36 |
|
| 37 |
class BaseTextDumper(Dumper): |
| 38 |
def dump(self, obj): |
| 39 |
# Return bytes as hex for text formatting |
| 40 |
return obj.ewkb.hex().encode() |
| 41 |
|
| 42 |
class BaseBinaryDumper(Dumper): |
| 43 |
format = Format.BINARY |
| 44 |
|
| 45 |
def dump(self, obj): |
| 46 |
return obj.ewkb |
| 47 |
|
| 48 |
@lru_cache |
| 49 |
def postgis_adapters(geo_oid, geog_oid, raster_oid): |
| 50 |
class BaseDumper(Dumper): |
| 51 |
def __init_subclass__(cls, base_dumper): |
| 52 |
super().__init_subclass__() |
| 53 |
|
| 54 |
cls.GeometryDumper = type( |
| 55 |
"GeometryDumper", (base_dumper,), {"oid": geo_oid} |
| 56 |
) |
| 57 |
cls.GeographyDumper = type( |
| 58 |
"GeographyDumper", (base_dumper,), {"oid": geog_oid} |
| 59 |
) |
| 60 |
cls.RasterDumper = type( |
| 61 |
"RasterDumper", (BaseTextDumper,), {"oid": raster_oid} |
| 62 |
) |
| 63 |
|
| 64 |
def get_key(self, obj, format): |
| 65 |
if obj.is_geometry: |
| 66 |
return GeographyType if obj.geography else GeometryType |
| 67 |
else: |
| 68 |
return RasterType |
| 69 |
|
| 70 |
def upgrade(self, obj, format): |
| 71 |
if obj.is_geometry: |
| 72 |
if obj.geography: |
| 73 |
return self.GeographyDumper(GeographyType) |
| 74 |
else: |
| 75 |
return self.GeometryDumper(GeometryType) |
| 76 |
else: |
| 77 |
return self.RasterDumper(RasterType) |
| 78 |
|
| 79 |
def dump(self, obj): |
| 80 |
raise NotImplementedError |
| 81 |
|
| 82 |
class PostGISTextDumper(BaseDumper, base_dumper=BaseTextDumper): |
| 83 |
pass |
| 84 |
|
| 85 |
class PostGISBinaryDumper(BaseDumper, base_dumper=BaseBinaryDumper): |
| 86 |
format = Format.BINARY |
| 87 |
|
| 88 |
return PostGISTextDumper, PostGISBinaryDumper |
| 89 |
|
| 90 |
|
| 91 |
class DatabaseWrapper(PsycopgDatabaseWrapper): |
| 92 |
SchemaEditorClass = PostGISSchemaEditor |
| 93 |
features_class = DatabaseFeatures |
| 94 |
ops_class = PostGISOperations |
| 95 |
introspection_class = PostGISIntrospection |
| 96 |
|
| 97 |
_type_infos = { |
| 98 |
"geometry": {}, |
| 99 |
"geography": {}, |
| 100 |
"raster": {}, |
| 101 |
} |
| 102 |
|
| 103 |
def __init__(self, *args, **kwargs): |
| 104 |
if kwargs.get("alias", "") == NO_DB_ALIAS: |
| 105 |
# Don't initialize PostGIS-specific stuff for non-db connections. |
| 106 |
self.features_class = PsycopgDatabaseFeatures |
| 107 |
self.ops_class = PsycopgDatabaseOperations |
| 108 |
self.introspection_class = PsycopgDatabaseIntrospection |
| 109 |
|
| 110 |
super().__init__(*args, **kwargs) |
| 111 |
|
| 112 |
def prepare_database(self): |
| 113 |
super().prepare_database() |
| 114 |
# Check that postgis extension is installed. |
| 115 |
with self.cursor() as cursor: |
| 116 |
cursor.execute("SELECT 1 FROM pg_extension WHERE extname = %s", ["postgis"]) |
| 117 |
if bool(cursor.fetchone()): |
| 118 |
return |
| 119 |
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis") |
| 120 |
if is_psycopg3: |
| 121 |
# Ensure adapters are registers if PostGIS is used within this |
| 122 |
# connection. |
| 123 |
self.register_geometry_adapters(self.connection, True) |
| 124 |
|
| 125 |
def get_new_connection(self, conn_params): |
| 126 |
connection = super().get_new_connection(conn_params) |
| 127 |
if is_psycopg3: |
| 128 |
self.register_geometry_adapters(connection) |
| 129 |
return connection |
| 130 |
|
| 131 |
if is_psycopg3: |
| 132 |
|
| 133 |
def _register_type(self, pg_connection, typename): |
| 134 |
registry = self._type_infos[typename] |
| 135 |
try: |
| 136 |
info = registry[self.alias] |
| 137 |
except KeyError: |
| 138 |
info = TypeInfo.fetch(pg_connection, typename) |
| 139 |
registry[self.alias] = info |
| 140 |
|
| 141 |
if info: # Can be None if the type does not exist (yet). |
| 142 |
info.register(pg_connection) |
| 143 |
pg_connection.adapters.register_loader(info.oid, TextLoader) |
| 144 |
pg_connection.adapters.register_loader(info.oid, TextBinaryLoader) |
| 145 |
|
| 146 |
return info.oid if info else None |
| 147 |
|
| 148 |
def register_geometry_adapters(self, pg_connection, clear_caches=False): |
| 149 |
if clear_caches: |
| 150 |
for typename in self._type_infos: |
| 151 |
self._type_infos[typename].pop(self.alias, None) |
| 152 |
|
| 153 |
geo_oid = self._register_type(pg_connection, "geometry") |
| 154 |
geog_oid = self._register_type(pg_connection, "geography") |
| 155 |
raster_oid = self._register_type(pg_connection, "raster") |
| 156 |
|
| 157 |
PostGISTextDumper, PostGISBinaryDumper = postgis_adapters( |
| 158 |
geo_oid, geog_oid, raster_oid |
| 159 |
) |
| 160 |
pg_connection.adapters.register_dumper(PostGISAdapter, PostGISTextDumper) |
| 161 |
pg_connection.adapters.register_dumper(PostGISAdapter, PostGISBinaryDumper) |