|
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 $response |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function isBatchWriteSuccess($response): bool |
|
70
|
|
|
{ |
|
71
|
|
|
if (empty($response['tables']) || !is_array($response['tables'])) { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
foreach ($response['tables'] as $table) { |
|
76
|
|
|
foreach (($table['rows'] ?? []) as $row) { |
|
77
|
|
|
if (empty($row['is_ok'])) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param mixed $response |
|
88
|
|
|
* @return void |
|
89
|
|
|
* @throws Exception |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function throwBatchWriteException($response) |
|
92
|
|
|
{ |
|
93
|
|
|
if (empty($response['tables']) || !is_array($response['tables'])) { |
|
94
|
|
|
throw new Exception('Abnormal operation.'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
foreach ($response['tables'] as $table) { |
|
98
|
|
|
foreach ($table['rows'] as $row) { |
|
99
|
|
|
if (empty($row['is_ok'])) { |
|
100
|
|
|
throw new Exception(sprintf('Failed to write the "%s" table.', $table['table_name'])); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|