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

brick / geo / 13865662283

14 Mar 2025 09:28PM UTC coverage: 50.726% (-0.06%) from 50.781%
13865662283

push

github

BenMorel
WIP

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

215 existing lines in 3 files now uncovered.

1852 of 3651 relevant lines covered (50.73%)

1000.92 hits per line

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

75.0
/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;
844✔
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,192✔
48
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
1,192✔
49

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

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

57
            $index = 1;
1,045✔
58

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

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

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

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

84
        assert($result !== false);
85

86
        return $result;
778✔
87
    }
88

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

96
        return parent::getGeomFromWKBSyntax();
×
97
    }
98

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

NEW
109
            if (is_float($parameter)) {
×
NEW
110
                return 'CAST (? AS DOUBLE PRECISION)';
×
111
            }
112

NEW
113
            if (is_bool($parameter)) {
×
NEW
114
                return 'CAST (? AS BOOLEAN)';
×
115
            }
116
        }
117

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