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 Exception; |
12
|
|
|
use Illuminate\Support\Facades\DB; |
13
|
|
|
|
14
|
|
|
class Ots |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string $name |
18
|
|
|
* @return Connection |
19
|
|
|
* @throws |
20
|
|
|
* @phpstan-ignore-next-line |
21
|
|
|
*/ |
22
|
|
|
public static function connection(string $name = 'ots'): Connection |
23
|
|
|
{ |
24
|
|
|
$connection = DB::connection($name); |
25
|
|
|
|
26
|
|
|
if (!$connection instanceof Connection) { |
27
|
|
|
throw new Exception('Only ots connections can be obtained'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $connection; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param mixed $row |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public static function parseRow($row): array |
38
|
|
|
{ |
39
|
|
|
$row = array_merge(($row['primary_key'] ?? []), ($row['attribute_columns'] ?? [])); |
40
|
|
|
|
41
|
|
|
$columns = []; |
42
|
|
|
foreach ($row as $item) { |
43
|
|
|
if (isset($item[0], $item[1])) { |
44
|
|
|
$columns[$item[0]] = $item[1]; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
return $columns; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param mixed $row |
52
|
|
|
* @param string $name |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
public static function parseRowAutoId($row, string $name = 'id'): ?int |
56
|
|
|
{ |
57
|
|
|
foreach (($row['primary_key'] ?? []) as $key) { |
58
|
|
|
if (isset($key[0], $key[1]) && $name === $key[0] && is_int($key[1])) { |
59
|
|
|
return $key[1]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $row |
67
|
|
|
* @param string $name |
68
|
|
|
* @return int |
69
|
|
|
* @throws |
70
|
|
|
* @phpstan-ignore-next-line |
71
|
|
|
*/ |
72
|
|
|
public static function mustParseRowAutoId($row, string $name = 'id'): int |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$id = Ots::parseRowAutoId($row); |
75
|
|
|
|
76
|
|
|
if (!is_int($id)) { |
77
|
|
|
throw new Exception('Failed to obtain id.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $id; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $response |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public static function isBatchWriteSuccess($response): bool |
88
|
|
|
{ |
89
|
|
|
if (empty($response['tables']) || !is_array($response['tables'])) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($response['tables'] as $table) { |
94
|
|
|
foreach (($table['rows'] ?? []) as $row) { |
95
|
|
|
if (empty($row['is_ok'])) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param mixed $response |
106
|
|
|
* @return void |
107
|
|
|
* @throws Exception |
108
|
|
|
*/ |
109
|
|
|
public static function throwBatchWriteException($response) |
110
|
|
|
{ |
111
|
|
|
if (empty($response['tables']) || !is_array($response['tables'])) { |
112
|
|
|
throw new Exception('Abnormal operation.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
foreach ($response['tables'] as $table) { |
116
|
|
|
foreach ($table['rows'] as $row) { |
117
|
|
|
if (empty($row['is_ok'])) { |
118
|
|
|
throw new Exception(sprintf('Failed to write the "%s" table.', $table['table_name'])); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.