1 | <?php |
||
2 | /** |
||
3 | * Created by PhpStorm. |
||
4 | * User: hugh.li |
||
5 | * Date: 2021/6/8 |
||
6 | * Time: 5:47 下午. |
||
7 | */ |
||
8 | |||
9 | namespace HughCube\Laravel\OTS; |
||
10 | |||
11 | use Aliyun\OTS\OTSClient; |
||
12 | use DateTimeInterface; |
||
13 | use Exception; |
||
14 | use Illuminate\Database\Connection as IlluminateConnection; |
||
15 | use Illuminate\Support\Arr; |
||
16 | use Illuminate\Support\Carbon; |
||
17 | |||
18 | /** |
||
19 | * @mixin OTSClient |
||
20 | */ |
||
21 | class Connection extends IlluminateConnection |
||
22 | { |
||
23 | /** |
||
24 | * @var OTSClient |
||
25 | */ |
||
26 | private $ots; |
||
27 | |||
28 | /** |
||
29 | * Create a new database connection instance. |
||
30 | * |
||
31 | * @param array $config |
||
32 | */ |
||
33 | public function __construct(array $config) |
||
34 | { |
||
35 | $this->config = $config; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return OtsClient |
||
40 | */ |
||
41 | public function getOts(): OTSClient |
||
42 | { |
||
43 | if (!$this->ots instanceof OTSClient) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
44 | $this->ots = $this->createConnection($this->config); |
||
45 | } |
||
46 | |||
47 | return $this->ots; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function getDatabaseName() |
||
54 | { |
||
55 | return $this->getOts()->getClientConfig()->getInstanceName(); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Create a new OTSClient connection. |
||
60 | * |
||
61 | * @param array $config |
||
62 | * |
||
63 | * @return OTSClient |
||
64 | */ |
||
65 | protected function createConnection(array $config): OTSClient |
||
66 | { |
||
67 | $config['ErrorLogHandler'] = Arr::get($config, 'ErrorLogHandler', false); |
||
68 | $config['DebugLogHandler'] = Arr::get($config, 'DebugLogHandler', false); |
||
69 | |||
70 | return new OTSClient($config); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | public function disconnect() |
||
77 | { |
||
78 | $this->ots = null; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @inheritdoc |
||
83 | */ |
||
84 | public function getDriverName(): string |
||
85 | { |
||
86 | return 'ots'; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param mixed $row |
||
91 | * @param string $name |
||
92 | * |
||
93 | * @throws Exception |
||
94 | * |
||
95 | * @return null|int |
||
96 | * |
||
97 | * @deprecated 放在Ots实现 |
||
98 | */ |
||
99 | public function parseAutoIncId($row, string $name = 'id'): ?int |
||
100 | { |
||
101 | return Ots::parseRowAutoId($row, $name); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param mixed $row |
||
106 | * |
||
107 | * @return array |
||
108 | * |
||
109 | * @deprecated 放在Ots实现 |
||
110 | */ |
||
111 | public function parseRowColumns($row): array |
||
112 | { |
||
113 | return Ots::parseRow($row); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param int $delay |
||
118 | * |
||
119 | * @return string |
||
120 | * |
||
121 | * @deprecated 放在Knight依赖实现里面 |
||
122 | */ |
||
123 | public function availableDate(int $delay = 0): string |
||
124 | { |
||
125 | return Carbon::now()->addRealSeconds($delay)->format(DateTimeInterface::RFC3339_EXTENDED); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param mixed $date |
||
130 | * |
||
131 | * @return Carbon|null |
||
132 | * |
||
133 | * @deprecated 放在Knight依赖实现里面 |
||
134 | */ |
||
135 | public function availableDateToDateTime($date): ?Carbon |
||
136 | { |
||
137 | if (empty($date)) { |
||
138 | return null; |
||
139 | } |
||
140 | |||
141 | $dateTime = Carbon::createFromFormat(DateTimeInterface::RFC3339_EXTENDED, $date); |
||
142 | |||
143 | return $dateTime instanceof Carbon ? $dateTime : null; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @inheritDoc |
||
148 | */ |
||
149 | public function __call($method, $parameters = []) |
||
150 | { |
||
151 | if (method_exists($this->getOts(), $method)) { |
||
152 | return $this->getOts()->$method(...$parameters); |
||
153 | } |
||
154 | |||
155 | return parent::__call($method, $parameters); |
||
156 | } |
||
157 | } |
||
158 |