Skip to content

Commit f3a0331

Browse files
authored
Refactor using other packages (#3)
- Abandon Laravel 5.6 and 5.7 - Add dependencies - [mpyw/unclosure: Closure unwrapper especially suited for Laravel PDO.](https://github.com/mpyw/unclosure) - [mpyw/laravel-pdo-emulation-control: Temporarily enable/disable PDO prepared statement emulation](https://github.com/mpyw/laravel-pdo-emulation-control) - Remove `PdoDecorator` and introduce some new classes
1 parent 7a1678c commit f3a0331

File tree

7 files changed

+171
-150
lines changed

7 files changed

+171
-150
lines changed

.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ cache:
2222
env:
2323
- LARAVEL_VERSION=^6.0 TESTBENCH_VERSION=^4.0
2424
- LARAVEL_VERSION=5.8.* TESTBENCH_VERSION=3.8.*
25-
- LARAVEL_VERSION=5.7.* TESTBENCH_VERSION=3.7.*
26-
- LARAVEL_VERSION=5.6.* TESTBENCH_VERSION=3.6.*
2725

2826
before_install:
2927
- sudo rm /usr/local/bin/docker-compose

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A tiny extension of `MySqlConnection` that manages **session** system variables
55
## Requirements
66

77
- PHP: ^7.1
8-
- Laravel: ^5.6 || ^6.0
8+
- Laravel: ^5.8 || ^6.0
99

1010
## Installing
1111

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
"require": {
2424
"php": "^7.1",
2525
"ext-pdo": "*",
26-
"illuminate/support": "^5.6 || ^6.0 || ^7.0 || ^8.0",
27-
"illuminate/database": "^5.6 || ^6.0 || ^7.0 || ^8.0"
26+
"illuminate/support": "^5.8 || ^6.0 || ^7.0 || ^8.0",
27+
"illuminate/database": "^5.8 || ^6.0 || ^7.0 || ^8.0",
28+
"mpyw/unclosure": "^0.1.0",
29+
"mpyw/laravel-pdo-emulation-control": "^0.1.0"
2830
},
2931
"require-dev": {
3032
"orchestra/testbench": "^4.0",

src/ManagesSystemVariables.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public function setSystemVariable(string $key, $value, bool $memoizeForReconnect
4141
*/
4242
public function setSystemVariables(array $values, bool $memoizeForReconnect = true)
4343
{
44-
foreach (array_filter([&$this->pdo, &$this->readPdo]) as &$pdo) {
45-
$pdo = PdoDecorator::withSystemVariables($pdo, $values);
46-
}
44+
(new SystemVariableAssigner($this->readPdo, $this->pdo))->assign($values);
4745

4846
if (!$this->reconnector instanceof SystemVariableAwareReconnector) {
4947
$this->setReconnector(new SystemVariableAwareReconnector($this->reconnector));

src/PdoDecorator.php

Lines changed: 0 additions & 142 deletions
This file was deleted.

src/SystemVariableAssigner.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Mpyw\LaravelMySqlSystemVariableManager;
4+
5+
use Closure;
6+
use Mpyw\LaravelPdoEmulationControl\EmulationController;
7+
use Mpyw\Unclosure\Value;
8+
use PDO;
9+
use Mpyw\LaravelMySqlSystemVariableManager\SystemVariableGrammar as Grammar;
10+
11+
class SystemVariableAssigner
12+
{
13+
/**
14+
* @var \Closure[]|\PDO[]
15+
*/
16+
protected $pdos;
17+
18+
/**
19+
* SystemVariableAssigner constructor.
20+
*
21+
* @param null|\Closure|\PDO &...$pdos
22+
*/
23+
public function __construct(&...$pdos)
24+
{
25+
$this->pdos = array_filter($pdos);
26+
}
27+
28+
/**
29+
* Set MySQL system variables for PDO.
30+
*
31+
* @param array $values
32+
* @return $this
33+
*/
34+
public function assign(array $values)
35+
{
36+
return $values
37+
? $this->withEmulatedStatement(Grammar::assignmentStatement($values), $values)
38+
: $this;
39+
}
40+
41+
/**
42+
* Configure PDO using query and parameters temporarily enabling PDO::ATTR_EMULATE_PREPARES.
43+
*
44+
* @param string $query
45+
* @param array $values
46+
* @return $this
47+
*/
48+
protected function withEmulatedStatement(string $query, array $values = [])
49+
{
50+
foreach ($this->pdos as &$pdo) {
51+
$pdo = Value::withCallback(
52+
$pdo,
53+
Closure::fromCallable([$this, 'withEmulatedStatementFor']),
54+
$query,
55+
$values
56+
);
57+
}
58+
unset($pdo);
59+
60+
return $this;
61+
}
62+
63+
/**
64+
* @param PDO $pdo
65+
* @param string $query
66+
* @param array $values
67+
* @return PDO
68+
*/
69+
protected static function withEmulatedStatementFor(PDO $pdo, string $query, array $values): PDO
70+
{
71+
return (new EmulationController($pdo))->emulated(
72+
Closure::fromCallable([static::class, 'withStatementFor']),
73+
$pdo,
74+
$query,
75+
$values
76+
);
77+
}
78+
79+
/**
80+
* @param PDO $pdo
81+
* @param string $query
82+
* @param array $values
83+
* @return PDO
84+
*/
85+
protected static function withStatementFor(PDO $pdo, string $query, array $values): PDO
86+
{
87+
$statement = $pdo->prepare($query);
88+
foreach (array_values($values) as $i => $value) {
89+
$statement->bindValue($i + 1, $value, Grammar::paramTypeFor($value));
90+
}
91+
$statement->execute();
92+
93+
return $pdo;
94+
}
95+
}

src/SystemVariableGrammar.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Mpyw\LaravelMySqlSystemVariableManager;
4+
5+
use PDO;
6+
7+
class SystemVariableGrammar
8+
{
9+
/**
10+
* @param array $values
11+
* @return string
12+
*/
13+
public static function assignmentStatement(array $values): string
14+
{
15+
return 'set session ' . implode(', ', static::assignmentExpressions($values));
16+
}
17+
18+
/**
19+
* @param array $values
20+
* @return string[]
21+
*/
22+
public static function assignmentExpressions(array $values): array
23+
{
24+
$expressions = [];
25+
foreach ($values as $name => $value) {
26+
$expressions[] = static::escapeIdentifier($name) . '=' . static::placeholderFor($value);
27+
}
28+
return $expressions;
29+
}
30+
31+
/**
32+
* @param mixed $value
33+
* @return int
34+
*/
35+
public static function paramTypeFor($value): int
36+
{
37+
switch (gettype($value)) {
38+
case 'integer':
39+
return PDO::PARAM_INT;
40+
case 'boolean':
41+
return PDO::PARAM_BOOL;
42+
case 'NULL':
43+
default:
44+
return PDO::PARAM_STR;
45+
}
46+
}
47+
48+
/**
49+
* @param mixed $value
50+
* @return string
51+
*/
52+
public static function placeholderFor($value): string
53+
{
54+
switch (gettype($value)) {
55+
case 'double':
56+
return 'cast(? as decimal(65, 30))';
57+
default:
58+
return '?';
59+
}
60+
}
61+
62+
/**
63+
* @param string $identifier
64+
* @return string
65+
*/
66+
public static function escapeIdentifier(string $identifier): string
67+
{
68+
return '`' . str_replace('`', '``', $identifier) . '`';
69+
}
70+
}

0 commit comments

Comments
 (0)