-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,495 additions
and
803 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,30 @@ | ||
<?php | ||
|
||
namespace Flat3\Lodata\Drivers\SQL\PDO\Concerns; | ||
|
||
use Flat3\Lodata\Drivers\SQL\PDO\Connection; | ||
use InvalidArgumentException; | ||
use PDO; | ||
|
||
trait ConnectsToDatabase | ||
{ | ||
/** | ||
* Create a new database connection. | ||
* | ||
* @param mixed[] $params | ||
* @param string|null $username | ||
* @param string|null $password | ||
* @param mixed[] $driverOptions | ||
* @return Connection | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) | ||
{ | ||
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']); | ||
} | ||
} |
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,187 @@ | ||
<?php | ||
|
||
namespace Flat3\Lodata\Drivers\SQL\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; | ||
use function assert; | ||
|
||
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 StatementInterface | ||
* | ||
* @throws Exception | ||
*/ | ||
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 ResultInterface | ||
*/ | ||
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 | ||
* | ||
* @throws Exception | ||
*/ | ||
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 $stmt | ||
* @return Statement | ||
*/ | ||
protected function createStatement(PDOStatement $stmt): Statement | ||
{ | ||
return new Statement($stmt); | ||
} | ||
|
||
/** | ||
* Begin a new database transaction. | ||
* | ||
* @return bool | ||
*/ | ||
public function beginTransaction() | ||
{ | ||
return $this->connection->beginTransaction(); | ||
} | ||
|
||
/** | ||
* Commit a database transaction. | ||
* | ||
* @return bool | ||
*/ | ||
public function commit() | ||
{ | ||
return $this->connection->commit(); | ||
} | ||
|
||
/** | ||
* Rollback a database transaction. | ||
* | ||
* @return bool | ||
*/ | ||
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; | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flat3\Lodata\Drivers\SQL\PDO; | ||
|
||
use Doctrine\DBAL\Connection as DoctrineConnection; | ||
use Doctrine\DBAL\Driver; | ||
use Exception; | ||
use Illuminate\Database\MySqlConnection; | ||
use Illuminate\Database\PostgresConnection; | ||
use Illuminate\Database\SQLiteConnection; | ||
use Illuminate\Database\SqlServerConnection; | ||
|
||
trait Doctrine | ||
{ | ||
protected $doctrineConnection = null; | ||
|
||
protected function getDoctrineSchemaManager() | ||
{ | ||
$connection = $this->getDoctrineConnection(); | ||
|
||
return $connection->createSchemaManager(); | ||
} | ||
|
||
/** | ||
* Get the Doctrine DBAL database connection instance. | ||
* | ||
* @return DoctrineConnection | ||
*/ | ||
protected function getDoctrineConnection() | ||
{ | ||
if (is_null($this->doctrineConnection)) { | ||
$driver = $this->getDoctrineDriver(); | ||
|
||
$connection = $this->getConnection(); | ||
|
||
$this->doctrineConnection = new DoctrineConnection(array_filter([ | ||
'pdo' => $connection->getPdo(), | ||
'dbname' => $connection->getDatabaseName(), | ||
'driver' => $driver->getName(), | ||
'serverVersion' => $connection->getConfig('server_version'), | ||
]), $driver); | ||
} | ||
|
||
return $this->doctrineConnection; | ||
} | ||
|
||
protected function getDoctrineDriver(): Driver | ||
{ | ||
switch (true) { | ||
case $this->getConnection() instanceof PostgresConnection: | ||
return new PostgresDriver; | ||
|
||
case $this->getConnection() instanceof MySqlConnection: | ||
return new MySqlDriver; | ||
|
||
case $this->getConnection() instanceof SQLiteConnection: | ||
return new SQLiteDriver; | ||
|
||
case $this->getConnection() instanceof SqlServerConnection: | ||
return new SqlServerDriver; | ||
} | ||
|
||
throw new Exception('Connection not known'); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Flat3\Lodata\Drivers\SQL\PDO; | ||
|
||
use Doctrine\DBAL\Driver\AbstractMySQLDriver; | ||
use Flat3\Lodata\Drivers\SQL\PDO\Concerns\ConnectsToDatabase; | ||
|
||
class MySqlDriver extends AbstractMySQLDriver | ||
{ | ||
use ConnectsToDatabase; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'pdo_mysql'; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Flat3\Lodata\Drivers\SQL\PDO; | ||
|
||
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; | ||
use Flat3\Lodata\Drivers\SQL\PDO\Concerns\ConnectsToDatabase; | ||
|
||
class PostgresDriver extends AbstractPostgreSQLDriver | ||
{ | ||
use ConnectsToDatabase; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'pdo_pgsql'; | ||
} | ||
} |
Oops, something went wrong.