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

brick / geo / 13418525576

19 Feb 2025 05:15PM UTC coverage: 47.546% (+0.2%) from 47.345%
13418525576

push

github

BenMorel
Add withers to geometries

11 of 11 new or added lines in 7 files covered. (100.0%)

60 existing lines in 4 files now uncovered.

1608 of 3382 relevant lines covered (47.55%)

971.56 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
    {
39
        return $this->pdo;
682✔
40
    }
41

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

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

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

55
            $index = 1;
1,015✔
56

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

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

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

74
            $result = $statement->fetch(PDO::FETCH_NUM);
850✔
UNCOV
75
        } catch (PDOException $e) {
222✔
UNCOV
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.
UNCOV
80
            if ($errorClass === '42' || $errorClass === '22') {
222✔
UNCOV
81
                throw GeometryEngineException::operationNotSupportedByEngine($e);
222✔
82
            }
83

84
            throw $e;
×
85
        }
86

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

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

91
        return $result;
850✔
92
    }
93

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

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

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

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