• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / geo / 13861125046

14 Mar 2025 04:33PM UTC coverage: 48.122% (-2.9%) from 51.017%
13861125046

push

github

BenMorel
Add more tests for spatial equality

This engine method is the foundation for other tests, so we need to ensure that it works fine in more complex cases.

1755 of 3647 relevant lines covered (48.12%)

863.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.38
/src/Engine/PDOEngine.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\Engine;
6

7
use Brick\Geo\Exception\GeometryEngineException;
8
use Brick\Geo\Geometry;
9
use Override;
10
use PDO;
11
use PDOException;
12
use PDOStatement;
13

14
/**
15
 * Database engine based on a PDO driver.
16
 */
17
final class PDOEngine extends DatabaseEngine
18
{
19
    /**
20
     * The database connection.
21
     */
22
    private readonly PDO $pdo;
23

24
    /**
25
     * A cache of the prepared statements, indexed by query.
26
     *
27
     * @var array<string, PDOStatement>
28
     */
29
    private array $statements = [];
30

31
    public function __construct(PDO $pdo, bool $useProxy = true)
32
    {
33
        parent::__construct($useProxy);
×
34

35
        $this->pdo = $pdo;
×
36
    }
37

38
    public function getPDO() : PDO
39
    {
40
        return $this->pdo;
844✔
41
    }
42

43
    #[Override]
44
    protected function executeQuery(string $query, array $parameters) : array
45
    {
46
        /** @var int $errMode */
47
        $errMode = $this->pdo->getAttribute(PDO::ATTR_ERRMODE);
1,192✔
48
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1,192✔
49

50
        try {
51
            if (! isset($this->statements[$query])) {
1,192✔
52
                $this->statements[$query] = $this->pdo->prepare($query);
301✔
53
            }
54

55
            $statement = $this->statements[$query];
1,045✔
56

57
            $index = 1;
1,045✔
58

59
            foreach ($parameters as $parameter) {
1,045✔
60
                if ($parameter instanceof GeometryParameter) {
1,045✔
61
                    $statement->bindValue($index++, $parameter->data, $parameter->isBinary ? PDO::PARAM_LOB : PDO::PARAM_STR);
1,045✔
62
                    $statement->bindValue($index++, $parameter->srid, PDO::PARAM_INT);
1,045✔
63
                } else {
64
                    $type = match (true) {
230✔
65
                        is_int($parameter) => PDO::PARAM_INT,
230✔
66
                        is_bool($parameter) => PDO::PARAM_BOOL,
209✔
67
                        default => PDO::PARAM_STR,
209✔
68
                    };
230✔
69

70
                    $statement->bindValue($index++, $parameter, $type);
230✔
71
                }
72
            }
73

74
            $statement->execute();
1,045✔
75

76
            /** @var list<mixed>|false $result */
77
            $result = $statement->fetch(PDO::FETCH_NUM);
778✔
78
        } catch (PDOException $e) {
424✔
79
            throw GeometryEngineException::wrap($e);
424✔
80
        } finally {
81
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, $errMode);
1,192✔
82
        }
83

84
        assert($result !== false);
85

86
        return $result;
778✔
87
    }
88

89
    #[Override]
90
    protected function getGeomFromWKBSyntax(): string
91
    {
92
        if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql') {
1,100✔
93
            return 'ST_GeomFromWKB(BINARY ?, ?)';
1,100✔
94
        }
95

96
        return parent::getGeomFromWKBSyntax();
×
97
    }
98

99
    #[Override]
100
    protected function getParameterPlaceholder(string|float|int|bool $parameter): string
101
    {
102
        if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'pgsql') {
320✔
103
            if (is_int($parameter)) {
×
104
                // https://stackoverflow.com/q/66625661/759866
105
                // https://externals.io/message/113521
106
                return 'CAST (? AS INTEGER)';
×
107
            }
108
        }
109

110
        return parent::getParameterPlaceholder($parameter);
320✔
111
    }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc