-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5737 from derrabus/feature/sqlite3
New driver: SQLite3
- Loading branch information
Showing
17 changed files
with
565 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="../../../vendor/phpunit/phpunit/phpunit.xsd" | ||
colors="true" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTodoAnnotatedTests="true" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
convertDeprecationsToExceptions="true" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1" /> | ||
|
||
<var name="db_driver" value="pdo_sqlite"/> | ||
<var name="db_memory" value="true"/> | ||
<var name="db_event_subscribers" value="Doctrine\DBAL\Event\Listeners\SQLiteSessionInit"/> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="Doctrine DBAL Test Suite"> | ||
<directory>../../../tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage> | ||
<include> | ||
<directory suffix=".php">../../../src</directory> | ||
</include> | ||
</coverage> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Driver\SQLite3; | ||
|
||
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; | ||
use Doctrine\DBAL\ParameterType; | ||
use SQLite3; | ||
|
||
use function assert; | ||
use function sprintf; | ||
|
||
final class Connection implements ServerInfoAwareConnection | ||
{ | ||
private SQLite3 $connection; | ||
|
||
/** @internal The connection can be only instantiated by its driver. */ | ||
public function __construct(SQLite3 $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
public function prepare(string $sql): Statement | ||
{ | ||
try { | ||
$statement = $this->connection->prepare($sql); | ||
} catch (\Exception $e) { | ||
throw Exception::new($e); | ||
} | ||
|
||
assert($statement !== false); | ||
|
||
return new Statement($this->connection, $statement); | ||
} | ||
|
||
public function query(string $sql): Result | ||
{ | ||
try { | ||
$result = $this->connection->query($sql); | ||
} catch (\Exception $e) { | ||
throw Exception::new($e); | ||
} | ||
|
||
assert($result !== false); | ||
|
||
return new Result($result, $this->connection->changes()); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function quote($value, $type = ParameterType::STRING): string | ||
{ | ||
return sprintf('\'%s\'', SQLite3::escapeString($value)); | ||
} | ||
|
||
public function exec(string $sql): int | ||
{ | ||
try { | ||
$this->connection->exec($sql); | ||
} catch (\Exception $e) { | ||
throw Exception::new($e); | ||
} | ||
|
||
return $this->connection->changes(); | ||
} | ||
|
||
/** @inheritdoc */ | ||
public function lastInsertId($name = null): int | ||
{ | ||
return $this->connection->lastInsertRowID(); | ||
} | ||
|
||
public function beginTransaction(): bool | ||
{ | ||
try { | ||
return $this->connection->exec('BEGIN'); | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function commit(): bool | ||
{ | ||
try { | ||
return $this->connection->exec('COMMIT'); | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function rollBack(): bool | ||
{ | ||
try { | ||
return $this->connection->exec('ROLLBACK'); | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function getNativeConnection(): SQLite3 | ||
{ | ||
return $this->connection; | ||
} | ||
|
||
public function getServerVersion(): string | ||
{ | ||
return SQLite3::version()['versionString']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Driver\SQLite3; | ||
|
||
use Doctrine\DBAL\Driver\AbstractSQLiteDriver; | ||
use Doctrine\DBAL\Driver\API\SQLite\UserDefinedFunctions; | ||
use SQLite3; | ||
|
||
final class Driver extends AbstractSQLiteDriver | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function connect(array $params): Connection | ||
{ | ||
$isMemory = (bool) ($params['memory'] ?? false); | ||
|
||
if (isset($params['path'])) { | ||
if ($isMemory) { | ||
throw new Exception( | ||
'Invalid connection settings: specifying both parameters "path" and "memory" ambiguous.', | ||
); | ||
} | ||
|
||
$filename = $params['path']; | ||
} elseif ($isMemory) { | ||
$filename = ':memory:'; | ||
} else { | ||
throw new Exception( | ||
'Invalid connection settings: specify either the "path" or the "memory" parameter for SQLite3.', | ||
); | ||
} | ||
|
||
try { | ||
$connection = new SQLite3($filename); | ||
} catch (\Exception $e) { | ||
throw Exception::new($e); | ||
} | ||
|
||
$connection->enableExceptions(true); | ||
|
||
UserDefinedFunctions::register([$connection, 'createFunction']); | ||
|
||
return new Connection($connection); | ||
} | ||
} |
Oops, something went wrong.