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

brick / geo / 13687430472

05 Mar 2025 11:13PM UTC coverage: 47.89% (+0.03%) from 47.859%
13687430472

push

github

BenMorel
Restore the PDO error mode in case of an exception

0 of 1 new or added line in 1 file covered. (0.0%)

33 existing lines in 3 files now uncovered.

1634 of 3412 relevant lines covered (47.89%)

970.39 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 PDO;
10
use PDOException;
11
use PDOStatement;
12

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

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

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

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

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

42
    protected function executeQuery(string $query, array $parameters) : array
43
    {
44
        /** @var int $errMode */
UNCOV
45
        $errMode = $this->pdo->getAttribute(PDO::ATTR_ERRMODE);
1,062✔
UNCOV
46
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1,062✔
47

48
        try {
UNCOV
49
            if (! isset($this->statements[$query])) {
1,062✔
UNCOV
50
                $this->statements[$query] = $this->pdo->prepare($query);
222✔
51
            }
52

UNCOV
53
            $statement = $this->statements[$query];
1,015✔
54

UNCOV
55
            $index = 1;
1,015✔
56

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

UNCOV
68
                    $statement->bindValue($index++, $parameter, $type);
67✔
69
                }
70
            }
71

UNCOV
72
            $statement->execute();
1,015✔
73

UNCOV
74
            $result = $statement->fetch(PDO::FETCH_NUM);
850✔
75
        } catch (PDOException $e) {
222✔
76
            $errorClass = substr((string) $e->getCode(), 0, 2);
222✔
77

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

84
            throw $e;
×
85
        } finally {
NEW
86
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, $errMode);
1,062✔
87
        }
88

UNCOV
89
        assert($result !== false);
90

UNCOV
91
        return $result;
850✔
92
    }
93

94
    protected function getGeomFromWKBSyntax(): string
95
    {
UNCOV
96
        if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql') {
952✔
97
            return 'ST_GeomFromWKB(BINARY ?, ?)';
744✔
98
        }
99

UNCOV
100
        return parent::getGeomFromWKBSyntax();
208✔
101
    }
102

103
    protected function getParameterPlaceholder(string|float|int $parameter): string
104
    {
UNCOV
105
        if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'pgsql') {
89✔
UNCOV
106
            if (is_int($parameter)) {
21✔
107
                // https://stackoverflow.com/q/66625661/759866
108
                // https://externals.io/message/113521
UNCOV
109
                return 'CAST (? AS INTEGER)';
4✔
110
            }
111
        }
112

UNCOV
113
        return parent::getParameterPlaceholder($parameter);
85✔
114
    }
115
}
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