|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Illuminate\Database\Connectors; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Illuminate\Database\Connectors\Connector; |
|
8
|
|
|
use Illuminate\Database\Connectors\ConnectorInterface; |
|
9
|
|
|
use Hhxsv5\LaravelS\Illuminate\Database\CoroutineMySQL; |
|
10
|
|
|
use Hhxsv5\LaravelS\Illuminate\Database\StatementException; |
|
11
|
|
|
|
|
12
|
|
|
class CoroutineMySQLConnector extends Connector implements ConnectorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $dsn |
|
16
|
|
|
* @param array $config |
|
17
|
|
|
* @param array $options |
|
18
|
|
|
* @return CoroutineMySQL |
|
19
|
|
|
* @throws Exception |
|
20
|
|
|
*/ |
|
21
|
|
|
public function createConnection($dsn, array $config, array $options) |
|
22
|
|
|
{ |
|
23
|
|
|
$username = Arr::get($config, 'username'); |
|
24
|
|
|
$password = Arr::get($config, 'password'); |
|
25
|
|
|
|
|
26
|
|
|
try { |
|
27
|
|
|
$mysql = $this->connect($config); |
|
28
|
|
|
} catch (\Exception $e) { |
|
29
|
|
|
$mysql = $this->tryAgainIfCausedByLostConnection( |
|
30
|
|
|
$e, $dsn, $username, $password, $config |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $mysql; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function tryAgainIfCausedByLostConnection(Exception $e, $dsn, $username, $password, $options) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($this->causedByLostConnection($e)) { |
|
40
|
|
|
return $this->connect($options); |
|
41
|
|
|
} |
|
42
|
|
|
throw $e; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array $config |
|
47
|
|
|
* @return CoroutineMySQL |
|
48
|
|
|
* @throws StatementException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function connect(array $config) |
|
51
|
|
|
{ |
|
52
|
|
|
$connection = new CoroutineMySQL(); |
|
53
|
|
|
$connection->connect([ |
|
54
|
|
|
'host' => Arr::get($config, 'host', '127.0.0.1'), |
|
55
|
|
|
'port' => Arr::get($config, 'port', 3306), |
|
56
|
|
|
'user' => Arr::get($config, 'username', 'hhxsv5'), |
|
57
|
|
|
'password' => Arr::get($config, 'password', '52100'), |
|
58
|
|
|
'database' => Arr::get($config, 'database', 'test'), |
|
59
|
|
|
'timeout' => Arr::get($config, 'timeout', 5), |
|
60
|
|
|
'charset' => Arr::get($config, 'charset', 'utf8mb4'), |
|
61
|
|
|
'strict_type' => Arr::get($config, 'strict', false), |
|
62
|
|
|
]); |
|
63
|
|
|
if (isset($config['timezone'])) { |
|
64
|
|
|
$connection->prepare('set time_zone="' . $config['timezone'] . '"')->execute(); |
|
65
|
|
|
} |
|
66
|
|
|
if (isset($config['strict'])) { |
|
67
|
|
|
if ($config['strict']) { |
|
68
|
|
|
$connection->prepare("set session sql_mode='STRICT_ALL_TABLES,ANSI_QUOTES'")->execute(); |
|
69
|
|
|
} else { |
|
70
|
|
|
$connection->prepare("set session sql_mode='ANSI_QUOTES'")->execute(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return $connection; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: