Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ExpressionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface ExpressionInterface
public function getType(): string;

/**
* Return PDO::PARAM_* type.s
* Return PDO::PARAM_* type.
*
* @return int
*/
Expand Down
18 changes: 2 additions & 16 deletions src/ManagesSystemVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
*/
trait ManagesSystemVariables
{
/**
* Set the reconnect instance on the connection.
*
* @param callable $reconnector
* @return $this
*/
abstract public function setReconnector(callable $reconnector);

/**
* Set MySQL system variable for both read and write PDOs.
* It is lazily executed for unresolved PDO instance.
Expand All @@ -41,14 +33,8 @@ public function setSystemVariable(string $key, $value, bool $memoizeForReconnect
*/
public function setSystemVariables(array $values, bool $memoizeForReconnect = true)
{
(new SystemVariableAssigner($this->readPdo, $this->pdo))->assign($values);

if (!$this->reconnector instanceof SystemVariableAwareReconnector) {
$this->setReconnector(new SystemVariableAwareReconnector($this->reconnector));
}
if ($memoizeForReconnect) {
$this->reconnector->memoizeSystemVariables($values);
}
(new SystemVariableMemoizedAssigner($this->reconnector, $this->readPdo, $this->pdo))
->assign($values, $memoizeForReconnect);

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function str(callable $callback): StringReplacerInterface
* Create new typed replacer for MySQL system variable.
*
* @param string $type
* @param bool|float|int|string $callback
* @param callable $callback
* @return \Mpyw\LaravelMySqlSystemVariableManager\ExpressionInterface
*/
public static function as(string $type, $callback): ExpressionInterface
Expand Down
7 changes: 3 additions & 4 deletions src/SystemVariableAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
use Mpyw\LaravelMySqlSystemVariableManager\Replacers\FloatReplacerInterface;
use Mpyw\LaravelMySqlSystemVariableManager\Replacers\IntegerReplacerInterface;
use Mpyw\LaravelMySqlSystemVariableManager\Replacers\StringReplacerInterface;
use Mpyw\LaravelMySqlSystemVariableManager\Value as BindingValue;
use Mpyw\LaravelPdoEmulationControl\EmulationController;
use Mpyw\Unclosure\Value;
use Mpyw\Unclosure\Value as ValueEffector;
use PDO;
use Mpyw\LaravelMySqlSystemVariableManager\SystemVariableGrammar as Grammar;
use PDOStatement;
Expand Down Expand Up @@ -55,7 +54,7 @@ public function assign(array $values)
protected function withEmulatedStatement(string $query, array $values = [])
{
foreach ($this->pdos as &$pdo) {
$pdo = Value::withCallback(
$pdo = ValueEffector::withCallback(
$pdo,
Closure::fromCallable([$this, 'withEmulatedStatementFor']),
$query,
Expand Down Expand Up @@ -91,7 +90,7 @@ protected static function withEmulatedStatementFor(PDO $pdo, string $query, arra
*/
protected static function withStatementFor(PDO $pdo, string $query, array $values): PDO
{
$expressions = array_map([BindingValue::class, 'wrap'], $values);
$expressions = array_map([Value::class, 'wrap'], $values);
$original = static::selectOriginalVariablesForReplacer($pdo, $expressions);
$statement = $pdo->prepare($query);

Expand Down
50 changes: 50 additions & 0 deletions src/SystemVariableMemoizedAssigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Mpyw\LaravelMySqlSystemVariableManager;

class SystemVariableMemoizedAssigner
{
/**
* @var \Mpyw\LaravelMySqlSystemVariableManager\SystemVariableAwareReconnector
*/
protected $reconnector;

/**
* @var \Closure[]|\PDO[]
*/
protected $pdos;

/**
* SystemVariableMemoizedAssigner constructor.
*
* @param null|callable|\Mpyw\LaravelMySqlSystemVariableManager\SystemVariableAwareReconnector &$reconnector
* @param null|\Closure|\PDO &...$pdos
*/
public function __construct(&$reconnector, &...$pdos)
{
$this->reconnector = $reconnector = !$reconnector instanceof SystemVariableAwareReconnector
? new SystemVariableAwareReconnector($reconnector)
: $reconnector;

$this->pdos = array_filter($pdos);
}

/**
* Set MySQL system variables for PDO.
*
* @param array $values
* @param bool $memoizeForReconnect
* @return $this
*/
public function assign(array $values, bool $memoizeForReconnect = true)
{
(new SystemVariableAssigner(...$this->pdos))
->assign($values);

if ($memoizeForReconnect) {
$this->reconnector->memoizeSystemVariables($values);
}

return $this;
}
}
3 changes: 1 addition & 2 deletions src/SystemVariableSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Mpyw\LaravelMySqlSystemVariableManager;

use Mpyw\LaravelMySqlSystemVariableManager\Value as BindingValue;
use PDO;

class SystemVariableSelector
Expand All @@ -25,7 +24,7 @@ public static function selectOriginalVariables(PDO $pdo, array $newValues): arra
->fetch(PDO::FETCH_ASSOC);

foreach ($original as $key => $value) {
$original[$key] = BindingValue::as(BindingValue::wrap($newValues[$key])->getType(), $value);
$original[$key] = Value::as(Value::wrap($newValues[$key])->getType(), $value);
}

return $original;
Expand Down
4 changes: 2 additions & 2 deletions src/SystemVariableTemporaryAssigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Mpyw\LaravelMySqlSystemVariableManager;

use Mpyw\Unclosure\Value;
use Mpyw\Unclosure\Value as ValueEffector;
use PDO;

class SystemVariableTemporaryAssigner
Expand Down Expand Up @@ -32,7 +32,7 @@ public function __construct(&...$pdos)
*/
public function using(array $using, callable $callback, ...$args)
{
return Value::withEffectForEach($this->pdos, function (PDO $pdo) use ($using) {
return ValueEffector::withEffectForEach($this->pdos, function (PDO $pdo) use ($using) {
$original = SystemVariableSelector::selectOriginalVariables($pdo, $using);
(new SystemVariableAssigner($pdo))->assign($using);

Expand Down