Skip to content

Commit

Permalink
[8.x] Support DBAL v3.0 (#35236)
Browse files Browse the repository at this point in the history
* Support doctrine 3

Co-Authored-By: Graham Campbell <[email protected]>

* Remove workflow steps

* Fix steps

* formatting

Co-authored-by: Graham Campbell <[email protected]>
Co-authored-by: Graham Campbell <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
4 people authored Nov 16, 2020
1 parent 9d9533f commit 0942a13
Show file tree
Hide file tree
Showing 15 changed files with 435 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ on:

jobs:
linux_tests:

runs-on: ubuntu-latest

services:
mysql:
image: mysql:5.7
Expand Down Expand Up @@ -64,8 +64,8 @@ jobs:
DB_USERNAME: root

windows_tests:

runs-on: windows-latest

strategy:
fail-fast: true
matrix:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
},
"require-dev": {
"aws/aws-sdk-php": "^3.0",
"doctrine/dbal": "^2.6",
"doctrine/dbal": "^2.6|^3.0",
"filp/whoops": "^2.8",
"guzzlehttp/guzzle": "^6.5.5|^7.0.1",
"league/flysystem-cached-adapter": "^1.0",
Expand Down Expand Up @@ -129,7 +129,7 @@
"ext-posix": "Required to use all features of the queue worker.",
"ext-redis": "Required to use the Redis cache and queue drivers (^4.0|^5.0).",
"aws/aws-sdk-php": "Required to use the SQS queue driver, DynamoDb failed job storage and SES mail driver (^3.0).",
"doctrine/dbal": "Required to rename columns and drop SQLite columns (^2.6).",
"doctrine/dbal": "Required to rename columns and drop SQLite columns (^2.6|^3.0).",
"filp/whoops": "Required for friendly error pages in development (^2.8).",
"fakerphp/faker": "Required to use the eloquent factory builder (^1.9.1).",
"guzzlehttp/guzzle": "Required to use the HTTP Client, Mailgun mail driver and the ping methods on schedules (^6.5.5|^7.0.1).",
Expand Down
10 changes: 8 additions & 2 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,13 @@ public function getDoctrineColumn($table, $column)
*/
public function getDoctrineSchemaManager()
{
return $this->getDoctrineDriver()->getSchemaManager($this->getDoctrineConnection());
$connection = $this->getDoctrineConnection();

// Doctrine v2 expects one parameter while v3 expects two. 2nd will be ignored on v2...
return $this->getDoctrineDriver()->getSchemaManager(
$connection,
$connection->getDatabasePlatform()
);
}

/**
Expand All @@ -907,7 +913,7 @@ public function getDoctrineConnection()
$this->doctrineConnection = new DoctrineConnection(array_filter([
'pdo' => $this->getPdo(),
'dbname' => $this->getDatabaseName(),
'driver' => $driver->getName(),
'driver' => method_exists($driver, 'getName') ? $driver->getName() : null,
'serverVersion' => $this->getConfig('server_version'),
]), $driver);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Database/MySqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Database;

use Doctrine\DBAL\Driver\PDOMySql\Driver as DoctrineDriver;
use Doctrine\DBAL\Version;
use Illuminate\Database\PDO\MySqlDriver;
use Illuminate\Database\Query\Grammars\MySqlGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Schema\Grammars\MySqlGrammar as SchemaGrammar;
Expand Down Expand Up @@ -82,10 +84,10 @@ protected function getDefaultPostProcessor()
/**
* Get the Doctrine DBAL driver.
*
* @return \Doctrine\DBAL\Driver\PDOMySql\Driver
* @return \Doctrine\DBAL\Driver\PDOMySql\Driver|\Illuminate\Database\PDO\MySqlDriver
*/
protected function getDoctrineDriver()
{
return new DoctrineDriver;
return class_exists(Version::class) ? new DoctrineDriver : new MySqlDriver;
}
}
24 changes: 24 additions & 0 deletions src/Illuminate/Database/PDO/Concerns/ConnectsToDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Database\PDO\Concerns;

use Illuminate\Database\PDO\Connection;
use PDO;

trait ConnectsToDatabase
{
/**
* Create a new database connection.
*
* @param array $params
* @return \Illuminate\Database\PDO\Connection
*/
public function connect(array $params)
{
if (! isset($params['pdo']) || ! $params['pdo'] instanceof PDO) {
throw new \InvalidArgumentException('Laravel requires the "pdo" property to be set and be a PDO instance.');
}

return new Connection($params['pdo']);
}
}
182 changes: 182 additions & 0 deletions src/Illuminate/Database/PDO/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

namespace Illuminate\Database\PDO;

use Doctrine\DBAL\Driver\PDO\Exception;
use Doctrine\DBAL\Driver\PDO\Result;
use Doctrine\DBAL\Driver\PDO\Statement;
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;
use PDO;
use PDOException;
use PDOStatement;

class Connection implements ServerInfoAwareConnection
{
/**
* The underlying PDO connection.
*
* @var \PDO
*/
protected $connection;

/**
* Create a new PDO connection instance.
*
* @param \PDO $connection
* @return void
*/
public function __construct(PDO $connection)
{
$this->connection = $connection;
}

/**
* Execute an SQL statement.
*
* @param string $statement
* @return int
*/
public function exec(string $statement): int
{
try {
$result = $this->connection->exec($statement);

\assert($result !== false);

return $result;
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}

/**
* Prepare a new SQL statement.
*
* @param string $sql
* @return \Doctrine\DBAL\Driver\Statement
*/
public function prepare(string $sql): StatementInterface
{
try {
return $this->createStatement(
$this->connection->prepare($sql)
);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}

/**
* Execute a new query against the connection.
*
* @param string $sql
* @return \Doctrine\DBAL\Driver\Result
*/
public function query(string $sql): ResultInterface
{
try {
$stmt = $this->connection->query($sql);

\assert($stmt instanceof PDOStatement);

return new Result($stmt);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}

/**
* Get the last insert ID.
*
* @param string|null $name
* @return mixed
*/
public function lastInsertId($name = null)
{
try {
if ($name === null) {
return $this->connection->lastInsertId();
}

return $this->connection->lastInsertId($name);
} catch (PDOException $exception) {
throw Exception::new($exception);
}
}

/**
* Create a new statement instance.
*
* @param \PDOStatement
* @return \Doctrine\DBAL\Driver\PDO\Statement
*/
protected function createStatement(PDOStatement $stmt): Statement
{
return new Statement($stmt);
}

/**
* Begin a new database transaction.
*
* @return void
*/
public function beginTransaction()
{
return $this->connection->beginTransaction();
}

/**
* Commit a database transaction.
*
* @return void
*/
public function commit()
{
return $this->connection->commit();
}

/**
* Roll back a database transaction.
*
* @return void
*/
public function rollBack()
{
return $this->connection->rollBack();
}

/**
* Wrap quotes around the given input.
*
* @param string $input
* @param string $type
* @return string
*/
public function quote($input, $type = ParameterType::STRING)
{
return $this->connection->quote($input, $type);
}

/**
* Get the server version for the connection.
*
* @return string
*/
public function getServerVersion()
{
return $this->connection->getAttribute(PDO::ATTR_SERVER_VERSION);
}

/**
* Get the wrapped PDO connection.
*
* @return \PDO
*/
public function getWrappedConnection(): PDO
{
return $this->connection;
}
}
11 changes: 11 additions & 0 deletions src/Illuminate/Database/PDO/MySqlDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Database\PDO;

use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Illuminate\Database\PDO\Concerns\ConnectsToDatabase;

class MySqlDriver extends AbstractMySQLDriver
{
use ConnectsToDatabase;
}
11 changes: 11 additions & 0 deletions src/Illuminate/Database/PDO/PostgresDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Database\PDO;

use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Illuminate\Database\PDO\Concerns\ConnectsToDatabase;

class PostgresDriver extends AbstractPostgreSQLDriver
{
use ConnectsToDatabase;
}
11 changes: 11 additions & 0 deletions src/Illuminate/Database/PDO/SQLiteDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Database\PDO;

use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
use Illuminate\Database\PDO\Concerns\ConnectsToDatabase;

class SQLiteDriver extends AbstractSQLiteDriver
{
use ConnectsToDatabase;
}
Loading

0 comments on commit 0942a13

Please sign in to comment.