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

brick / geo / 13718641601

07 Mar 2025 10:22AM UTC coverage: 48.463% (+3.5%) from 44.937%
13718641601

push

github

BenMorel
TEMP

1703 of 3514 relevant lines covered (48.46%)

553.41 hits per line

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

90.91
/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;
609✔
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);
837✔
48
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
837✔
49

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

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

57
            $index = 1;
747✔
58

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

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

74
            $statement->execute();
747✔
75

76
            /** @var list<mixed>|false $result */
77
            $result = $statement->fetch(PDO::FETCH_NUM);
657✔
78
        } catch (PDOException $e) {
180✔
79
            $errorClass = substr((string) $e->getCode(), 0, 2);
180✔
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') {
180✔
84
                throw GeometryEngineException::operationNotSupportedByEngine($e);
180✔
85
            }
86

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

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

94
        return $result;
657✔
95
    }
96

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

104
        return parent::getGeomFromWKBSyntax();
258✔
105
    }
106

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

118
        return parent::getParameterPlaceholder($parameter);
212✔
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