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

brick / geo / 17456208570

04 Sep 2025 07:10AM UTC coverage: 50.432%. Remained the same
17456208570

push

github

BenMorel
Use @extends and @implements instead of @template-* variants

For consistency with the rest of the project.

1867 of 3702 relevant lines covered (50.43%)

1140.21 hits per line

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

93.75
/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 Override;
9
use PDO;
10
use PDOException;
11
use PDOStatement;
12

13
use function assert;
14
use function is_bool;
15
use function is_int;
16

17
/**
18
 * Database engine based on a PDO driver.
19
 */
20
final class PdoEngine extends DatabaseEngine
21
{
22
    /**
23
     * The database connection.
24
     */
25
    private readonly PDO $pdo;
26

27
    /**
28
     * A cache of the prepared statements, indexed by query.
29
     *
30
     * @var array<string, PDOStatement>
31
     */
32
    private array $statements = [];
33

34
    public function __construct(PDO $pdo, bool $useProxy = true)
35
    {
36
        parent::__construct($useProxy);
×
37

38
        $this->pdo = $pdo;
×
39
    }
40

41
    public function getPDO(): PDO
42
    {
43
        return $this->pdo;
1,057✔
44
    }
45

46
    #[Override]
47
    protected function executeQuery(string $query, array $parameters): array
48
    {
49
        /** @var int $errMode */
50
        $errMode = $this->pdo->getAttribute(PDO::ATTR_ERRMODE);
1,492✔
51
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1,492✔
52

53
        try {
54
            if (! isset($this->statements[$query])) {
1,492✔
55
                $this->statements[$query] = $this->pdo->prepare($query);
346✔
56
            }
57

58
            $statement = $this->statements[$query];
1,345✔
59

60
            $index = 1;
1,345✔
61

62
            foreach ($parameters as $parameter) {
1,345✔
63
                if ($parameter instanceof GeometryParameter) {
1,345✔
64
                    $statement->bindValue($index++, $parameter->data, $parameter->isBinary ? PDO::PARAM_LOB : PDO::PARAM_STR);
1,345✔
65
                    $statement->bindValue($index++, $parameter->srid, PDO::PARAM_INT);
1,345✔
66
                } else {
67
                    $type = match (true) {
310✔
68
                        is_int($parameter) => PDO::PARAM_INT,
310✔
69
                        is_bool($parameter) => PDO::PARAM_BOOL,
282✔
70
                        default => PDO::PARAM_STR,
282✔
71
                    };
310✔
72

73
                    $statement->bindValue($index++, $parameter, $type);
310✔
74
                }
75
            }
76

77
            $statement->execute();
1,345✔
78

79
            /** @var list<mixed>|false $result */
80
            $result = $statement->fetch(PDO::FETCH_NUM);
1,078✔
81
        } catch (PDOException $e) {
424✔
82
            throw GeometryEngineException::wrap($e);
424✔
83
        } finally {
84
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, $errMode);
1,492✔
85
        }
86

87
        assert($result !== false);
88

89
        return $result;
1,078✔
90
    }
91

92
    #[Override]
93
    protected function getGeomFromWkbSyntax(): string
94
    {
95
        if ($this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql') {
1,377✔
96
            return 'ST_GeomFromWKB(BINARY ?, ?)';
1,100✔
97
        }
98

99
        return parent::getGeomFromWkbSyntax();
277✔
100
    }
101

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

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