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

brick / geo / 13715715561

06 Mar 2025 10:47PM UTC coverage: 44.086% (-40.4%) from 84.507%
13715715561

push

github

BenMorel
Remove Psalm-specific annotations

1543 of 3500 relevant lines covered (44.09%)

270.91 hits per line

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

0.0
/src/Engine/SQLite3Engine.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\Exception\SQLite3Exception;
9
use Brick\Geo\Geometry;
10
use Brick\Geo\LineString;
11
use Brick\Geo\Point;
12
use Override;
13
use SQLite3;
14
use SQLite3Stmt;
15

16
/**
17
 * Database engine based on a SQLite3 driver.
18
 *
19
 * The spatialite extension must be loaded in this driver.
20
 */
21
final class SQLite3Engine extends DatabaseEngine
22
{
23
    /**
24
     * The database connection.
25
     */
26
    private readonly SQLite3 $sqlite3;
27

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

35
    public function __construct(SQLite3 $sqlite3, bool $useProxy = true)
36
    {
37
        parent::__construct($useProxy);
×
38

39
        $this->sqlite3 = $sqlite3;
×
40
    }
41

42
    public function getSQLite3() : SQLite3
43
    {
44
        return $this->sqlite3;
×
45
    }
46

47
    #[Override]
48
    protected function executeQuery(string $query, array $parameters) : array
49
    {
50
        if (isset($this->statements[$query])) {
×
51
            $statement = $this->statements[$query];
×
52
            $statement->reset();
×
53
        } else {
54
            // Temporary set the error reporting level to 0 to avoid any warning.
55
            $errorReportingLevel = error_reporting(0);
×
56

57
            // Don't use exceptions, they're just \Exception instances that don't even contain the SQLite error code!
58
            $enableExceptions = $this->sqlite3->enableExceptions(false);
×
59

60
            $statement = $this->sqlite3->prepare($query);
×
61

62
            // Restore the original settings.
63
            $this->sqlite3->enableExceptions($enableExceptions);
×
64
            error_reporting($errorReportingLevel);
×
65

66
            $errorCode = $this->sqlite3->lastErrorCode();
×
67

68
            if ($errorCode !== 0) {
×
69
                $exception = new SQLite3Exception($this->sqlite3->lastErrorMsg(), $errorCode);
×
70

71
                if ($errorCode === 1) {
×
72
                    // SQL error cause by a missing function, this must be reported with a GeometryEngineException.
73
                    throw GeometryEngineException::operationNotSupportedByEngine($exception);
×
74
                } else {
75
                    // Other SQLite3 error; we cannot trigger the original E_WARNING, so we throw this exception instead.
76
                    throw $exception;
×
77
                }
78
            } else {
79
                $this->statements[$query] = $statement;
×
80
            }
81
        }
82

83
        $index = 1;
×
84

85
        foreach ($parameters as $parameter) {
×
86
            if ($parameter instanceof GeometryParameter) {
×
87
                $statement->bindValue($index++, $parameter->data, $parameter->isBinary ? SQLITE3_BLOB : SQLITE3_TEXT);
×
88
                $statement->bindValue($index++, $parameter->srid, SQLITE3_INTEGER);
×
89
            } else {
90
                if (is_int($parameter)) {
×
91
                    $type = SQLITE3_INTEGER;
×
92
                } elseif (is_float($parameter)) {
×
93
                    $type = SQLITE3_FLOAT;
×
94
                } else {
95
                    $type = SQLITE3_TEXT;
×
96
                }
97

98
                $statement->bindValue($index++, $parameter, $type);
×
99
            }
100
        }
101

102
        $result = $statement->execute();
×
103

104
        /** @var list<mixed> */
105
        return $result->fetchArray(SQLITE3_NUM);
×
106
    }
107

108
    #[Override]
109
    public function lineInterpolatePoint(LineString $linestring, float $fraction) : Point
110
    {
111
        $result = $this->queryGeometry('ST_Line_Interpolate_Point', $linestring, $fraction);
×
112
        if (! $result instanceof Point) {
×
113
            throw new GeometryEngineException('This operation yielded the wrong geometry type: ' . $result::class);
×
114
        }
115

116
        return $result;
×
117
    }
118
}
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