Skip to content

Commit

Permalink
Refactor connections to use trait
Browse files Browse the repository at this point in the history
Prior design would require duplicating code to extend the Connection class
  • Loading branch information
octoberapp committed Aug 7, 2024
1 parent cbd822a commit 9d0d600
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 491 deletions.
2 changes: 1 addition & 1 deletion src/Database/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Connection as ConnectionBase;

/**
* Connection base class
* @deprecated see \October\Rain\Database\Connections\ExtendsConnection
*/
class Connection extends ConnectionBase
{
Expand Down
54 changes: 54 additions & 0 deletions src/Database/Connections/ExtendsConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php namespace October\Rain\Database\Connections;

use October\Rain\Database\QueryBuilder;

/**
* ExtendsConnection replaces the query builder in the connection,
* and modifies logging events. This trait must extend a connection
* class that extends the `Illuminate\Database\Connection` class.
*/
trait ExtendsConnection
{
/**
* query builder instance
* @return \October\Rain\Database\QueryBuilder
*/
public function query()
{
return new QueryBuilder(
$this,
$this->getQueryGrammar(),
$this->getPostProcessor()
);
}

/**
* logQuery in the connection's query log
* @param string $query
* @param array $bindings
* @param float|null $time
* @return void
*/
public function logQuery($query, $bindings, $time = null)
{
if (isset($this->events)) {
$this->events->dispatch('illuminate.query', [$query, $bindings, $time, $this->getName()]);
}

parent::logQuery($query, $bindings, $time);
}

/**
* fireConnectionEvent for this connection
* @param string $event
* @return void
*/
protected function fireConnectionEvent($event)
{
if (isset($this->events)) {
$this->events->dispatch('connection.'.$this->getName().'.'.$event, $this);
}

parent::fireConnectionEvent($event);
}
}
170 changes: 4 additions & 166 deletions src/Database/Connections/MySqlConnection.php
Original file line number Diff line number Diff line change
@@ -1,173 +1,11 @@
<?php namespace October\Rain\Database\Connections;

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;
use Illuminate\Database\Schema\MySqlBuilder;
use Illuminate\Database\Schema\MySqlSchemaState;
use Illuminate\Filesystem\Filesystem;
use Exception;
use PDO;
use Illuminate\Database\MySqlConnection as MySqlConnectionBase;

/**
* MySqlConnection
* MySqlConnection implements connection extension
*/
class MySqlConnection extends Connection
class MySqlConnection extends MySqlConnectionBase
{
/**
* The last inserted ID generated by the server
*
* @var string|int|null
*/
protected $lastInsertId;

/**
* Run an insert statement against the database.
*
* @param string $query
* @param array $bindings
* @param string|null $sequence
* @return bool
*/
public function insert($query, $bindings = [], $sequence = null)
{
return $this->run($query, $bindings, function ($query, $bindings) use ($sequence) {
if ($this->pretending()) {
return true;
}

$statement = $this->getPdo()->prepare($query);

$this->bindValues($statement, $this->prepareBindings($bindings));

$this->recordsHaveBeenModified();

$result = $statement->execute();

$this->lastInsertId = $this->getPdo()->lastInsertId($sequence);

return $result;
});
}

/**
* Get the last insert ID.
*
* @return int
*/
public function getLastInsertId()
{
return $this->lastInsertId;
}

/**
* Escape a binary value for safe SQL embedding.
*
* @param string $value
* @return string
*/
protected function escapeBinary($value)
{
$hex = bin2hex($value);

return "x'{$hex}'";
}

/**
* Determine if the given database exception was caused by a unique constraint violation.
*
* @param \Exception $exception
* @return bool
*/
protected function isUniqueConstraintError(Exception $exception)
{
return boolval(preg_match('#Integrity constraint violation: 1062#i', $exception->getMessage()));
}

/**
* Determine if the connected database is a MariaDB database.
*
* @return bool
*/
public function isMaria()
{
return str_contains($this->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), 'MariaDB');
}

/**
* Get the default query grammar instance.
*
* @return \Illuminate\Database\Query\Grammars\MySqlGrammar
*/
protected function getDefaultQueryGrammar()
{
$grammar = new QueryGrammar;
if (method_exists($grammar, 'setConnection')) {
$grammar->setConnection($this);
}

return $this->withTablePrefix($grammar);
}

/**
* Get a schema builder instance for the connection.
*
* @return \Illuminate\Database\Schema\MySqlBuilder
*/
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}

return new MySqlBuilder($this);
}

/**
* Get the default schema grammar instance.
*
* @return \Illuminate\Database\Schema\Grammars\MySqlGrammar
*/
protected function getDefaultSchemaGrammar()
{
$grammar = new SchemaGrammar;
if (method_exists($grammar, 'setConnection')) {
$grammar->setConnection($this);
}

return $this->withTablePrefix($grammar);
}

/**
* Get the schema state for the connection.
*
* @param \Illuminate\Filesystem\Filesystem|null $files
* @param callable|null $processFactory
* @return \Illuminate\Database\Schema\MySqlSchemaState
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
return new MySqlSchemaState($this, $files, $processFactory);
}

/**
* Get the default post processor instance.
*
* @return \Illuminate\Database\Query\Processors\MySqlProcessor
*/
protected function getDefaultPostProcessor()
{
return new MySqlProcessor;
}

/**
* Get the Doctrine DBAL driver.
*
* @return \Illuminate\Database\PDO\MySqlDriver
*/
protected function getDoctrineDriver()
{
return new MySqlDriver;
}
use \October\Rain\Database\Connections\ExtendsConnection;
}
106 changes: 6 additions & 100 deletions src/Database/Connections/PostgresConnection.php
Original file line number Diff line number Diff line change
@@ -1,105 +1,11 @@
<?php namespace October\Rain\Database\Connections;

use Illuminate\Database\PDO\PostgresDriver;
use Illuminate\Database\Query\Grammars\PostgresGrammar as QueryGrammar;
use Illuminate\Database\Query\Processors\PostgresProcessor;
use Illuminate\Database\Schema\Grammars\PostgresGrammar as SchemaGrammar;
use Illuminate\Database\Schema\PostgresBuilder;
use Illuminate\Database\Schema\PostgresSchemaState;
use Illuminate\Filesystem\Filesystem;
use PDO;
use Illuminate\Database\PostgresConnection as PostgresConnectionBase;

class PostgresConnection extends Connection
/**
* PostgresConnection implements connection extension
*/
class PostgresConnection extends PostgresConnectionBase
{
/**
* Bind values to their parameters in the given statement.
*
* @param \PDOStatement $statement
* @param array $bindings
* @return void
*/
public function bindValues($statement, $bindings)
{
foreach ($bindings as $key => $value) {
if (is_int($value)) {
$pdoParam = PDO::PARAM_INT;
} elseif (is_resource($value)) {
$pdoParam = PDO::PARAM_LOB;
} else {
$pdoParam = PDO::PARAM_STR;
}

$statement->bindValue(
is_string($key) ? $key : $key + 1,
$value,
$pdoParam
);
}
}

/**
* Get the default query grammar instance.
*
* @return \Illuminate\Database\Query\Grammars\PostgresGrammar
*/
protected function getDefaultQueryGrammar()
{
return $this->withTablePrefix(new QueryGrammar);
}

/**
* Get a schema builder instance for the connection.
*
* @return \Illuminate\Database\Schema\PostgresBuilder
*/
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}

return new PostgresBuilder($this);
}

/**
* Get the default schema grammar instance.
*
* @return \Illuminate\Database\Schema\Grammars\PostgresGrammar
*/
protected function getDefaultSchemaGrammar()
{
return $this->withTablePrefix(new SchemaGrammar);
}

/**
* Get the schema state for the connection.
*
* @param \Illuminate\Filesystem\Filesystem|null $files
* @param callable|null $processFactory
* @return \Illuminate\Database\Schema\PostgresSchemaState
*/
public function getSchemaState(Filesystem $files = null, callable $processFactory = null)
{
return new PostgresSchemaState($this, $files, $processFactory);
}

/**
* Get the default post processor instance.
*
* @return \Illuminate\Database\Query\Processors\PostgresProcessor
*/
protected function getDefaultPostProcessor()
{
return new PostgresProcessor;
}

/**
* Get the Doctrine DBAL driver.
*
* @return \Illuminate\Database\PDO\PostgresDriver
*/
protected function getDoctrineDriver()
{
return new PostgresDriver;
}
use \October\Rain\Database\Connections\ExtendsConnection;
}
Loading

0 comments on commit 9d0d600

Please sign in to comment.