Skip to content

Commit

Permalink
fix for #837
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Aug 8, 2022
1 parent be39f33 commit 95f4802
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 115 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ These are all the configuration options and their default value between brackets
- "command": Extra SQL to initialize the database connection (none)
- "tables": Comma separated list of tables to publish (defaults to 'all')
- "mapping": Comma separated list of table/column mappings (no mappping)
- "geometrySrid": SRID assumed when converting from WKT to geometry (`4326`)
- "middlewares": List of middlewares to load (`cors`)
- "controllers": List of controllers to load (`records,geojson,openapi,status`)
- "customControllers": List of user custom controllers to load (no default)
Expand Down Expand Up @@ -605,6 +606,7 @@ For spatial support there is an extra set of filters that can be applied on geom
- "siv": spatial is valid (geometry is valid)

These filters are based on OGC standards and so is the WKT specification in which the geometry columns are represented.
Note that the SRID that is assumed when converting from WKT to geometry is specified by the config variable `geometrySrid` and defaults to 4326 (WGS 84).

#### GeoJSON

Expand Down
80 changes: 53 additions & 27 deletions api.include.php
Original file line number Diff line number Diff line change
Expand Up @@ -5037,10 +5037,12 @@ public function ping(ServerRequestInterface $request): ResponseInterface
class ColumnConverter
{
private $driver;
private $geometrySrid;

public function __construct(string $driver)
public function __construct(string $driver, int $geometrySrid)
{
$this->driver = $driver;
$this->geometrySrid = $geometrySrid;
}

public function convertColumnValue(ReflectedColumn $column): string
Expand All @@ -5066,12 +5068,13 @@ public function convertColumnValue(ReflectedColumn $column): string
}
}
if ($column->isGeometry()) {
$srid = $this->geometrySrid;
switch ($this->driver) {
case 'mysql':
case 'pgsql':
return "ST_GeomFromText(?)";
return "ST_GeomFromText(?,$srid)";
case 'sqlsrv':
return "geometry::STGeomFromText(?,0)";
return "geometry::STGeomFromText(?,$srid)";
}
}
return '?';
Expand Down Expand Up @@ -5114,10 +5117,10 @@ class ColumnsBuilder
private $driver;
private $converter;

public function __construct(string $driver)
public function __construct(string $driver, int $geometrySrid)
{
$this->driver = $driver;
$this->converter = new ColumnConverter($driver);
$this->converter = new ColumnConverter($driver, $geometrySrid);
}

public function getOffsetLimit(int $offset, int $limit): string
Expand Down Expand Up @@ -5238,10 +5241,12 @@ public function getIncrement(ReflectedTable $table, array $columnValues): string
class ConditionsBuilder
{
private $driver;
private $geometrySrid;

public function __construct(string $driver)
public function __construct(string $driver, int $geometrySrid)
{
$this->driver = $driver;
$this->geometrySrid = $geometrySrid;
}

private function getConditionSql(Condition $condition, array &$arguments): string
Expand Down Expand Up @@ -5402,14 +5407,15 @@ private function hasSpatialArgument(string $operator): bool

private function getSpatialFunctionCall(string $functionName, string $column, bool $hasArgument): string
{
$srid = $this->geometrySrid;
switch ($this->driver) {
case 'mysql':
case 'pgsql':
$argument = $hasArgument ? 'ST_GeomFromText(?)' : '';
$argument = $hasArgument ? "ST_GeomFromText(?,$srid)" : '';
return "$functionName($column, $argument)=TRUE";
case 'sqlsrv':
$functionName = str_replace('ST_', 'ST', $functionName);
$argument = $hasArgument ? 'geometry::STGeomFromText(?,0)' : '';
$argument = $hasArgument ? "geometry::STGeomFromText(?,$srid)" : '';
return "$column.$functionName($argument)=1";
case 'sqlite':
$argument = $hasArgument ? '?' : '0';
Expand Down Expand Up @@ -5572,6 +5578,7 @@ class GenericDB
private $conditions;
private $columns;
private $converter;
private $geometrySrid;

private function getDsn(): string
{
Expand Down Expand Up @@ -5655,13 +5662,13 @@ private function initPdo(): bool
$this->mapper = new RealNameMapper($this->mapping);
$this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper);
$this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper);
$this->conditions = new ConditionsBuilder($this->driver);
$this->columns = new ColumnsBuilder($this->driver);
$this->conditions = new ConditionsBuilder($this->driver, $this->geometrySrid);
$this->columns = new ColumnsBuilder($this->driver, $this->geometrySrid);
$this->converter = new DataConverter($this->driver);
return $result;
}

public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password)
public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid)
{
$this->driver = $driver;
$this->address = $address;
Expand All @@ -5672,10 +5679,11 @@ public function __construct(string $driver, string $address, int $port, string $
$this->mapping = $mapping;
$this->username = $username;
$this->password = $password;
$this->geometrySrid = $geometrySrid;
$this->initPdo();
}

public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password): bool
public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid): bool
{
if ($driver) {
$this->driver = $driver;
Expand Down Expand Up @@ -5704,6 +5712,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
if ($password) {
$this->password = $password;
}
if ($geometrySrid) {
$this->geometrySrid = $geometrySrid;
}
return $this->initPdo();
}

Expand Down Expand Up @@ -8817,13 +8828,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

class ReconnectMiddleware extends Middleware
{
private $reflection;
private $config;
private $db;

public function __construct(Router $router, Responder $responder, Config $config, string $middleware, ReflectionService $reflection, GenericDB $db)
{
parent::__construct($router, $responder, $config, $middleware);
$this->reflection = $reflection;
$this->config = $config;
$this->db = $db;
}

Expand All @@ -8833,7 +8844,7 @@ private function getDriver(): string
if ($driverHandler) {
return call_user_func($driverHandler);
}
return '';
return $this->config->getDriver();
}

private function getAddress(): string
Expand All @@ -8842,7 +8853,7 @@ private function getAddress(): string
if ($addressHandler) {
return call_user_func($addressHandler);
}
return '';
return $this->config->getAddress();
}

private function getPort(): int
Expand All @@ -8851,7 +8862,7 @@ private function getPort(): int
if ($portHandler) {
return call_user_func($portHandler);
}
return 0;
return $this->config->getPort();
}

private function getDatabase(): string
Expand All @@ -8860,7 +8871,7 @@ private function getDatabase(): string
if ($databaseHandler) {
return call_user_func($databaseHandler);
}
return '';
return $this->config->getDatabase();
}

private function getCommand(): string
Expand All @@ -8869,7 +8880,7 @@ private function getCommand(): string
if ($commandHandler) {
return call_user_func($commandHandler);
}
return '';
return $this->config->getCommand();
}

private function getTables(): array
Expand All @@ -8878,7 +8889,7 @@ private function getTables(): array
if ($tablesHandler) {
return call_user_func($tablesHandler);
}
return [];
return $this->config->getTables();
}

private function getMapping(): array
Expand All @@ -8887,7 +8898,7 @@ private function getMapping(): array
if ($mappingHandler) {
return call_user_func($mappingHandler);
}
return [];
return $this->config->getMapping();
}

private function getUsername(): string
Expand All @@ -8896,7 +8907,7 @@ private function getUsername(): string
if ($usernameHandler) {
return call_user_func($usernameHandler);
}
return '';
return $this->config->getUsername();
}

private function getPassword(): string
Expand All @@ -8905,7 +8916,16 @@ private function getPassword(): string
if ($passwordHandler) {
return call_user_func($passwordHandler);
}
return '';
return $this->config->getPassword();
}

private function getGeometrySrid(): int
{
$geometrySridHandler = $this->getProperty('geometrySridHandler', '');
if ($geometrySridHandler) {
return call_user_func($geometrySridHandler);
}
return $this->config->getGeometrySrid();
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
Expand All @@ -8919,9 +8939,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$mapping = $this->getMapping();
$username = $this->getUsername();
$password = $this->getPassword();
if ($driver || $address || $port || $database || $command || $tables || $mapping || $username || $password) {
$this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password);
}
$geometrySrid = $this->getGeometrySrid();
$this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password, $geometrySrid);
return $next->handle($request);
}
}
Expand Down Expand Up @@ -11634,7 +11653,8 @@ public function __construct(Config $config)
$config->getTables(),
$config->getMapping(),
$config->getUsername(),
$config->getPassword()
$config->getPassword(),
$config->getGeometrySrid()
);
$prefix = sprintf('phpcrudapi-%s-', substr(md5(__FILE__), 0, 8));
$cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath());
Expand Down Expand Up @@ -11833,6 +11853,7 @@ class Config
'debug' => false,
'basePath' => '',
'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}',
'geometrySrid' => 4326,
];

private function getDefaultDriver(array $values): string
Expand Down Expand Up @@ -12022,6 +12043,11 @@ public function getOpenApiBase(): array
{
return json_decode($this->values['openApiBase'], true);
}

public function getGeometrySrid(): int
{
return $this->values['geometrySrid'];
}
}
}

Expand Down
Loading

0 comments on commit 95f4802

Please sign in to comment.