Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
27pchrisl committed Mar 21, 2024
1 parent 53c8c3f commit e8c6908
Show file tree
Hide file tree
Showing 15 changed files with 1,495 additions and 803 deletions.
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
"require": {
"doctrine/dbal": "^3",
"php": "^7.3|^8.0",
"illuminate/bus": "^8.0|^9.0|^10.0",
"illuminate/database": "^8.40.0|^9.0|^10.0",
"illuminate/events": "^8.0|^9.0|^10.0",
"illuminate/http": "^8.0|^9.0|^10.0",
"illuminate/queue": "^8.0|^9.0|^10.0",
"illuminate/routing": "^8.0|^9.0|^10.0",
"illuminate/bus": "^8.0|^9.0|^10.0|^11.0",
"illuminate/database": "^8.40.0|^9.0|^10.0|^11.0",
"illuminate/events": "^8.0|^9.0|^10.0|^11.0",
"illuminate/http": "^8.0|^9.0|^10.0|^11.0",
"illuminate/queue": "^8.0|^9.0|^10.0|^11.0",
"illuminate/routing": "^8.0|^9.0|^10.0|^11.0",
"ext-simplexml": "*",
"ext-json": "*",
"ext-dom": "*",
Expand All @@ -49,10 +49,10 @@
},
"require-dev": {
"phpunit/phpunit": "^8.4|^9.0|^10.0",
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0",
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0|^9.0",
"spatie/phpunit-snapshot-assertions": "^4.2|main-dev",
"phpstan/phpstan": "^1.0.0",
"nunomaduro/collision": "^5.3|^6.0|^7.0",
"nunomaduro/collision": "^5.3|^6.0|^7.0|^8.0",
"brianium/paratest": "^6.2|^7.0",
"eclipxe/xmlschemavalidator": "^3.0",
"league/csv": "^9.7",
Expand Down
1,737 changes: 950 additions & 787 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/ComputedProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function getExpression(): string

/**
* Convert the provided value to a property value based on this property
* @param mixed $value Value
* @param mixed $value Value
* @return PropertyValue Property Value
*/
public function toPropertyValue($value): PropertyValue
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(IlluminateRequest $request)
$this->query = $request->query;
$this->content = $request->content;
$this->server = $request->server;
$this->request = $this;
$this->request = $request->request;
$this->attributes = $request->attributes;
$this->files = $request->files;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Controller/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Flat3\Lodata\Helper\JSON;
use Flat3\Lodata\Interfaces\ResourceInterface;
use Flat3\Lodata\Transaction\MediaType;
use Illuminate\Http\ResponseTrait;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Throwable;

Expand All @@ -36,7 +35,7 @@ abstract class Response extends StreamedResponse

/**
* The exception that triggered the error response (if applicable).
* @var \Throwable|null
* @var Throwable|null
*/
public $exception;

Expand Down
30 changes: 30 additions & 0 deletions src/Drivers/SQL/PDO/Concerns/ConnectsToDatabase.php
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']);
}
}
187 changes: 187 additions & 0 deletions src/Drivers/SQL/PDO/Connection.php
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;
}
}
67 changes: 67 additions & 0 deletions src/Drivers/SQL/PDO/Doctrine.php
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');
}
}
19 changes: 19 additions & 0 deletions src/Drivers/SQL/PDO/MySqlDriver.php
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';
}
}
19 changes: 19 additions & 0 deletions src/Drivers/SQL/PDO/PostgresDriver.php
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';
}
}
Loading

0 comments on commit e8c6908

Please sign in to comment.