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

brick / geo / 13718604625

07 Mar 2025 10:20AM UTC coverage: 44.937% (-4.3%) from 49.232%
13718604625

push

github

BenMorel
TEMP

1580 of 3516 relevant lines covered (44.94%)

141.39 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\Engine\Internal\TypeChecker;
8
use Brick\Geo\Exception\GeometryEngineException;
9
use Brick\Geo\Exception\SQLite3Exception;
10
use Brick\Geo\Geometry;
11
use Brick\Geo\LineString;
12
use Brick\Geo\Point;
13
use Override;
14
use SQLite3;
15
use SQLite3Stmt;
16

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

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

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

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

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

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

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

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

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

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

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

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

84
        $index = 1;
×
85

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

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

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

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

109
    #[Override]
110
    public function lineInterpolatePoint(LineString $lineString, float $fraction) : Point
111
    {
112
        $result = $this->queryGeometry('ST_Line_Interpolate_Point', $lineString, $fraction);
×
113
        TypeChecker::check($result, Point::class);
×
114

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