Construct the DB string that GDAL will use to inspect the database. GDAL will create its own connection to the database, so we re-use the connection settings from the Django test.
()
| 201 | |
| 202 | |
| 203 | def get_ogr_db_string(): |
| 204 | """ |
| 205 | Construct the DB string that GDAL will use to inspect the database. |
| 206 | GDAL will create its own connection to the database, so we re-use the |
| 207 | connection settings from the Django test. |
| 208 | """ |
| 209 | db = connections.settings["default"] |
| 210 | |
| 211 | # Map from the django backend into the OGR driver name and database |
| 212 | # identifier https://gdal.org/drivers/vector/ |
| 213 | # |
| 214 | # TODO: Support Oracle (OCI). |
| 215 | drivers = { |
| 216 | "django.contrib.gis.db.backends.postgis": ( |
| 217 | "PostgreSQL", |
| 218 | "PG:dbname='%(db_name)s'", |
| 219 | " ", |
| 220 | ), |
| 221 | "django.contrib.gis.db.backends.mysql": ("MySQL", 'MYSQL:"%(db_name)s"', ","), |
| 222 | "django.contrib.gis.db.backends.spatialite": ("SQLite", "%(db_name)s", ""), |
| 223 | } |
| 224 | |
| 225 | db_engine = db["ENGINE"] |
| 226 | if db_engine not in drivers: |
| 227 | return None |
| 228 | |
| 229 | drv_name, db_str, param_sep = drivers[db_engine] |
| 230 | |
| 231 | # Ensure that GDAL library has driver support for the database. |
| 232 | try: |
| 233 | Driver(drv_name) |
| 234 | except GDALException: |
| 235 | return None |
| 236 | |
| 237 | # SQLite/SpatiaLite in-memory databases |
| 238 | if DatabaseCreation.is_in_memory_db(db["NAME"]): |
| 239 | return None |
| 240 | |
| 241 | # Build the params of the OGR database connection string |
| 242 | params = [db_str % {"db_name": db["NAME"]}] |
| 243 | |
| 244 | def add(key, template): |
| 245 | value = db.get(key, None) |
| 246 | # Don't add the parameter if it is not in django's settings |
| 247 | if value: |
| 248 | params.append(template % value) |
| 249 | |
| 250 | add("HOST", "host='%s'") |
| 251 | add("PORT", "port='%s'") |
| 252 | add("USER", "user='%s'") |
| 253 | add("PASSWORD", "password='%s'") |
| 254 | |
| 255 | return param_sep.join(params) |
no test coverage detected