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

brick / geo / 13971277707

20 Mar 2025 02:05PM UTC coverage: 65.568% (+0.1%) from 65.472%
13971277707

push

github

BenMorel
Accept $prettyPrint in AbstractWktWriter constructor

1889 of 2881 relevant lines covered (65.57%)

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

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

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

57
            $index = 1;
1,345✔
58

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

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

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

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

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

86
        return $result;
1,078✔
87
    }
88

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

96
        return parent::getGeomFromWkbSyntax();
277✔
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') {
400✔
103
            if (is_int($parameter)) {
80✔
104
                // https://stackoverflow.com/q/66625661/759866
105
                // https://externals.io/message/113521
106
                return 'CAST (? AS INTEGER)';
7✔
107
            }
108
        }
109

110
        return parent::getParameterPlaceholder($parameter);
393✔
111
    }
112
}
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