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

brick / geo / 13399473854

18 Feb 2025 08:26PM UTC coverage: 83.425%. Remained the same
13399473854

push

github

BenMorel
BoundingBox: readonly + promoted properties

23 of 28 new or added lines in 10 files covered. (82.14%)

8 existing lines in 3 files now uncovered.

1525 of 1828 relevant lines covered (83.42%)

1858.37 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;
682✔
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,062✔
48
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1,062✔
49

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

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

57
            $index = 1;
1,015✔
58

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

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

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

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

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

86
            throw $e;
×
87
        }
88

89
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, $errMode);
850✔
90

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

93
        return $result;
850✔
94
    }
95

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

103
        return parent::getGeomFromWKBSyntax();
208✔
104
    }
105

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

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