Skip to content

Commit

Permalink
Merge branch 'main' into fix/allow-empty-update-columns
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Lyskov committed Aug 29, 2023
2 parents 31ad09e + 40f6a1f commit 5c18982
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
25 changes: 22 additions & 3 deletions src/Database/Type/GeoPointType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Voronoy\PgUtils\Database\Type;

use Cake\Core\StaticConfigTrait;
use Cake\Database\DriverInterface;
use Cake\Database\Expression\FunctionExpression;
use Cake\Database\ExpressionInterface;
Expand All @@ -12,6 +13,8 @@

class GeoPointType extends BaseType implements ExpressionTypeInterface
{
use StaticConfigTrait;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -70,9 +73,25 @@ public function toExpression($value): ExpressionInterface
$lat = null;
}

return new FunctionExpression('ST_SetSRID', [
new FunctionExpression('ST_Point', [$lng, $lat]),
4326,
return new FunctionExpression($this->_schemaPrefix('ST_SetSRID'), [
new FunctionExpression($this->_schemaPrefix('ST_Point'), [$lng, $lat]),
static::getConfig('srid') ?? 4326,
]);
}

/**
* Returns function name with schema prefix.
*
* @param string $function Function name.
* @return string
*/
protected function _schemaPrefix(string $function): string
{
$schema = static::getConfig('schema');
if (!$schema) {
return $function;
}

return $schema . '.' . $function;
}
}
22 changes: 18 additions & 4 deletions tests/TestCase/Database/Type/GeoTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Cake\TestSuite\TestCase;
use Voronoy\PgUtils\Database\GeoPoint;
use Voronoy\PgUtils\Database\Type\GeoPointType;

class GeoTypeTest extends TestCase
{
Expand All @@ -31,11 +32,11 @@ public function testPoint()
{
$first = $this->Geo->get(1);
$second = $this->Geo->get(2);
$this->assertTrue($first->pt instanceof GeoPoint);
$this->assertEquals(-118.146251, $first->pt->lng());
$this->assertEquals(33.82928, $first->pt->lat());
$this->assertTrue($first->get('pt') instanceof GeoPoint);
$this->assertEquals(-118.146251, $first->get('pt')->lng());
$this->assertEquals(33.82928, $first->get('pt')->lat());
$this->assertEquals('{"id":1,"pt":{"lng":-118.146251,"lat":33.82928}}', json_encode($first));
$this->assertNull($second->pt);
$this->assertNull($second->get('pt'));

$lng = -118.017946;
$lat = 33.954716;
Expand All @@ -60,4 +61,17 @@ public function testPoint()
}
}
}

public function testNonDefaultSchema()
{
GeoPointType::setConfig('schema', 'public');
$lng = -118.017946;
$lat = 33.954716;
$entity = $this->Geo->newEntity(['id' => 6, 'pt' => "$lng,$lat"]);
$this->Geo->save($entity);
$record = $this->Geo->find()->where(['id' => 6])->first();
$this->assertTrue($record->get('pt') instanceof GeoPoint);
$this->assertEquals($lng, $record->get('pt')->lng());
$this->assertEquals($lat, $record->get('pt')->lat());
}
}
2 changes: 0 additions & 2 deletions tests/TestCase/ORM/CachedMaterializedViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Voronoy\PgUtils\Test\TestCase\ORM;

use Cake\Core\Configure;
use Cake\Database\Schema\CachedCollection;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -32,7 +31,6 @@ public function setUp(): void

public function testMatViewCollection()
{
pr(Configure::read());
$class = new \ReflectionClass($this->MatView);
$method = $class->getMethod('getSchemaCollection');
$method->setAccessible(true);
Expand Down

0 comments on commit 5c18982

Please sign in to comment.