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

brick / geo / 13717813827

07 Mar 2025 09:27AM UTC coverage: 49.004% (+0.6%) from 48.448%
13717813827

push

github

BenMorel
GEOSEngine: rename props + readonly

4 of 12 new or added lines in 1 file covered. (33.33%)

54 existing lines in 4 files now uncovered.

1722 of 3514 relevant lines covered (49.0%)

817.62 hits per line

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

78.79
/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
    {
UNCOV
40
        return $this->pdo;
634✔
41
    }
42

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

50
        try {
UNCOV
51
            if (! isset($this->statements[$query])) {
938✔
UNCOV
52
                $this->statements[$query] = $this->pdo->prepare($query);
187✔
53
            }
54

UNCOV
55
            $statement = $this->statements[$query];
891✔
56

UNCOV
57
            $index = 1;
891✔
58

UNCOV
59
            foreach ($parameters as $parameter) {
891✔
UNCOV
60
                if ($parameter instanceof GeometryParameter) {
891✔
UNCOV
61
                    $statement->bindValue($index++, $parameter->data, $parameter->isBinary ? PDO::PARAM_LOB : PDO::PARAM_STR);
891✔
UNCOV
62
                    $statement->bindValue($index++, $parameter->srid, PDO::PARAM_INT);
891✔
63
                } else {
UNCOV
64
                    if (is_int($parameter)) {
152✔
UNCOV
65
                        $type = PDO::PARAM_INT;
×
66
                    } else {
UNCOV
67
                        $type = PDO::PARAM_STR;
152✔
68
                    }
69

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

UNCOV
74
            $statement->execute();
891✔
75

76
            /** @var list<mixed>|false $result */
UNCOV
77
            $result = $statement->fetch(PDO::FETCH_NUM);
724✔
78
        } catch (PDOException $e) {
224✔
79
            $errorClass = substr((string) $e->getCode(), 0, 2);
224✔
80

81
            // 42XXX = syntax error or access rule violation; reported on undefined function.
82
            // 22XXX = data exception; reported by MySQL 5.7 on unsupported geometry.
83
            if ($errorClass === '42' || $errorClass === '22') {
224✔
84
                throw GeometryEngineException::operationNotSupportedByEngine($e);
224✔
85
            }
86

87
            throw $e;
×
88
        } finally {
UNCOV
89
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, $errMode);
938✔
90
        }
91

UNCOV
92
        assert($result !== false);
93

UNCOV
94
        return $result;
724✔
95
    }
96

97
    #[Override]
98
    protected function getGeomFromWKBSyntax(): string
99
    {
UNCOV
100
        if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql') {
848✔
101
            return 'ST_GeomFromWKB(BINARY ?, ?)';
848✔
102
        }
103

UNCOV
104
        return parent::getGeomFromWKBSyntax();
×
105
    }
106

107
    #[Override]
108
    protected function getParameterPlaceholder(string|float|int $parameter): string
109
    {
UNCOV
110
        if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'pgsql') {
174✔
UNCOV
111
            if (is_int($parameter)) {
×
112
                // https://stackoverflow.com/q/66625661/759866
113
                // https://externals.io/message/113521
UNCOV
114
                return 'CAST (? AS INTEGER)';
×
115
            }
116
        }
117

UNCOV
118
        return parent::getParameterPlaceholder($parameter);
174✔
119
    }
120
}
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