Skip to content

Commit 658db55

Browse files
author
Karel Wintersky
committed
1.0.0
PHP8 Release
1 parent 4acffa9 commit 658db55

File tree

5 files changed

+97
-38
lines changed

5 files changed

+97
-38
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ $sphinx_connection = new \Arris\Database\DBWrapper([
2525
'charset_collate' => NULL
2626
]);
2727

28+
// или через Arris\Database\Connector()
29+
2830
$toolkit = new PDOWrapper($mysql_connection, $sphinx_connection);
2931
$toolkit->setRebuildIndexOptions([
3032
'log_rows_inside_chunk' => false,
@@ -53,4 +55,48 @@ if ($rt_index) {
5355
CLIConsole::say("[SEARCH.RT_INDEX.ARTICLES] <font color='red'>disabled</font>");
5456
}
5557

58+
```
59+
60+
# ToDo
61+
62+
Добавить
63+
```php
64+
/**
65+
* @param $connection
66+
* @param $index
67+
* @return false|\PDOStatement
68+
*/
69+
public static function RTIndexOptimize($connection, $index)
70+
{
71+
$query = "OPTIMIZE INDEX {$index}";
72+
return $connection->query($query);
73+
}
74+
75+
/**
76+
* @param $connection
77+
* @param $index
78+
* @param bool $reconfigure
79+
* @return bool
80+
*/
81+
public static function RTIndexTruncate($connection, $index, bool $reconfigure = true): bool
82+
{
83+
$with = $reconfigure ? 'WITH RECONFIGURE' : '';
84+
return (bool)$connection->query("TRUNCATE RTINDEX {$index} {$with}");
85+
}
86+
87+
/**
88+
* @param $connection
89+
* @param $index
90+
* @return bool
91+
*/
92+
public static function RTIndexCheckExist($connection, $index)
93+
{
94+
$index_definition = $connection->query("SHOW TABLES LIKE '{$index}' ")->fetchAll();
95+
96+
return \count($index_definition) > 0;
97+
}
98+
99+
// +show meta
100+
// +show version
101+
56102
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": "^7.4 | 8.*",
14+
"php": "^8.2",
1515
"psr/log": "*",
1616
"ext-pdo": "*"
1717
},

src/PDOWrapper.php

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,33 @@
66
use PDO;
77
use Psr\Log\LoggerInterface;
88
use Psr\Log\NullLogger;
9+
use function array_filter;
10+
use function array_key_exists;
11+
use function array_keys;
12+
use function array_map;
13+
use function call_user_func_array;
14+
use function ceil;
15+
use function count;
16+
use function implode;
17+
use function in_array;
18+
use function is_array;
19+
use function is_null;
920

1021
class PDOWrapper implements PDOWrapperInterface
1122
{
1223
/**
1324
* MySQL-коннектор.
1425
*
15-
* NB: Мы не объявляем тип PDO, поскольку может передаваться как PDO, так и Arris\DBWrapper
16-
*
1726
* @var PDO
1827
*/
19-
private $mysql_connection;
28+
private PDO $mysql_connection;
2029

2130
/**
2231
* PDO-коннектор к SearchD
2332
*
24-
* NB: Мы не объявляем тип PDO, поскольку может передаваться как PDO, так и Arris\DBWrapper
25-
*
2633
* @var PDO
2734
*/
28-
private $searchd_connection;
35+
private PDO $searchd_connection;
2936

3037
/**
3138
* Логгер
@@ -107,7 +114,7 @@ public function setOptions(array $options = []):array
107114
* @param callable $messenger
108115
* @return void
109116
*/
110-
public function setConsoleMessenger(callable $messenger)
117+
public function setConsoleMessenger(callable $messenger): void
111118
{
112119
$this->messenger = $messenger;
113120
}
@@ -131,22 +138,22 @@ public function rebuildAbstractIndex(string $mysql_table, string $searchd_index,
131138
$sphinx_connection = $this->searchd_connection;
132139

133140
if (empty($searchd_index)) {
134-
throw new PDOWrapperException("Requested update of undefined index", 1);
141+
throw PDOWrapperException::create("Requested update of undefined index",[], 1);
135142
}
136143

137144
if (empty($mysql_table)) {
138-
throw new PDOWrapperException('Not defined source SQL table', 1);
145+
throw PDOWrapperException::create('Not defined source SQL table', [], 1);
139146
}
140147

141148
// проверяем, существует ли индекс
142149
if (! self::RTIndexCheckExist($this->searchd_connection, $searchd_index)) {
143-
throw new PDOWrapperException("Index [{$searchd_index}] not present", 1);
150+
throw PDOWrapperException::create("Index [{$searchd_index}] not present", [],1);
144151
}
145152

146153

147154
$chunk_size = $this->options['chunk_length'];
148-
if (0 == $chunk_size) {
149-
throw new PDOWrapperException("Chunk size is ZERO");
155+
if (0 === $chunk_size) {
156+
throw PDOWrapperException::create("Chunk size is ZERO");
150157
}
151158

152159
// truncate
@@ -157,23 +164,23 @@ public function rebuildAbstractIndex(string $mysql_table, string $searchd_index,
157164
$total_updated = 0;
158165

159166
if ($this->options['log_before_index']) {
160-
\call_user_func_array($this->messenger, [ "<font color='yellow'>[{$searchd_index}]</font> index : ", false ]);
167+
call_user_func_array($this->messenger, [ "<font color='yellow'>[{$searchd_index}]</font> index : ", false ]);
161168
}
162169

163170
if ($this->options['log_total_rows_found']) {
164-
\call_user_func_array($this->messenger, [
171+
call_user_func_array($this->messenger, [
165172
"<font color='green'>{$total_count}</font> elements found for rebuild."
166173
]);
167174
}
168175

169176
// iterate chunks (ASC)
170177
// для обратной итерации (DESC) надо писать do-while цикл?
171178
172-
for ($i = 0; $i < \ceil($total_count / $chunk_size); $i++) {
179+
for ($i = 0; $i < ceil($total_count / $chunk_size); $i++) {
173180
$offset = $i * $chunk_size;
174181

175182
if ($this->options['log_before_chunk']) {
176-
\call_user_func_array($this->messenger, [
183+
call_user_func_array($this->messenger, [
177184
"Rebuilding elements from <font color='green'>{$offset}</font>, <font color='yellow'>{$chunk_size}</font> count... ",
178185
false
179186
]);
@@ -188,7 +195,7 @@ public function rebuildAbstractIndex(string $mysql_table, string $searchd_index,
188195
// iterate inside chunk
189196
while ($item = $sth->fetch()) {
190197
if ($this->options['log_rows_inside_chunk']) {
191-
\call_user_func_array($this->messenger, [
198+
call_user_func_array($this->messenger, [
192199
"{$mysql_table}: {$item['id']}"
193200
]);
194201
}
@@ -209,30 +216,30 @@ public function rebuildAbstractIndex(string $mysql_table, string $searchd_index,
209216
$breakline_after_chunk = !$this->options['sleep_after_chunk'];
210217

211218
if ($this->options['log_after_chunk']) {
212-
\call_user_func_array($this->messenger, [
219+
call_user_func_array($this->messenger, [
213220
"Updated RT-index <font color='yellow'>{$searchd_index}</font>.",
214221
$breakline_after_chunk
215222
]);
216223
} else {
217-
\call_user_func_array($this->messenger, [
224+
call_user_func_array($this->messenger, [
218225
"<strong>Ok</strong>",
219226
$breakline_after_chunk
220227
]);
221228
}
222229

223230
if ($this->options['sleep_after_chunk']) {
224-
\call_user_func_array($this->messenger, [
231+
call_user_func_array($this->messenger, [
225232
"ZZZZzzz for {$this->options['sleep_time']} second(s)... ",
226233
false
227234
]);
228235
sleep($this->options['sleep_time']);
229-
\call_user_func_array($this->messenger, [
236+
call_user_func_array($this->messenger, [
230237
"I woke up!"
231238
]);
232239
}
233240
} // for
234241
if ($this->options['log_after_index']) {
235-
\call_user_func_array($this->messenger, [
242+
call_user_func_array($this->messenger, [
236243
"Total updated <strong>{$total_updated}</strong> elements for <font color='yellow'>{$searchd_index}</font> RT-index. <br>"
237244
]);
238245
}
@@ -273,22 +280,22 @@ private static function buildReplaceQueryMVA(string $table, array $dataset, arra
273280
{
274281
$query = "REPLACE INTO {$table} (";
275282

276-
$dataset_keys = \array_keys($dataset);
283+
$dataset_keys = array_keys($dataset);
277284

278-
$query .= \implode(', ', \array_map(function ($i){
285+
$query .= implode(', ', array_map(function ($i){
279286
return "{$i}";
280287
}, $dataset_keys));
281288

282289
$query .= " ) VALUES ( ";
283290

284-
$query .= \implode(', ', \array_map(function ($i) use ($mva_attributes, $dataset){
285-
return \in_array($i, $mva_attributes) ? "({$dataset[$i]})" : ":{$i}";
291+
$query .= implode(', ', array_map(function ($i) use ($mva_attributes, $dataset){
292+
return in_array($i, $mva_attributes) ? "({$dataset[$i]})" : ":{$i}";
286293
}, $dataset_keys));
287294

288295
$query .= " ) ";
289296

290-
$new_dataset = \array_filter($dataset, function ($value, $key) use ($mva_attributes) {
291-
return !\in_array($key, $mva_attributes);
297+
$new_dataset = array_filter($dataset, function ($value, $key) use ($mva_attributes) {
298+
return !in_array($key, $mva_attributes);
292299
}, ARRAY_FILTER_USE_BOTH);
293300

294301
return [
@@ -303,17 +310,17 @@ private static function buildReplaceQueryMVA(string $table, array $dataset, arra
303310
*/
304311
private static function buildReplaceQuery(string $table, array $dataset):string
305312
{
306-
$dataset_keys = \array_keys($dataset);
313+
$dataset_keys = array_keys($dataset);
307314

308315
$query = "REPLACE INTO {$table} (";
309316

310-
$query.= \implode(', ', \array_map(function ($i){
317+
$query.= implode(', ', array_map(function ($i){
311318
return "{$i}";
312319
}, $dataset_keys));
313320

314321
$query.= " ) VALUES ( ";
315322

316-
$query.= \implode(', ', \array_map(function ($i){
323+
$query.= implode(', ', array_map(function ($i){
317324
return ":{$i}";
318325
}, $dataset_keys));
319326

@@ -331,15 +338,15 @@ private static function buildReplaceQuery(string $table, array $dataset):string
331338
*/
332339
private static function setOption(array $options = [], $key = null, $default_value = null)
333340
{
334-
if (!\is_array($options)) {
341+
if (!is_array($options)) {
335342
return $default_value;
336343
}
337344

338-
if (\is_null($key)) {
345+
if (is_null($key)) {
339346
return $default_value;
340347
}
341348

342-
return \array_key_exists($key, $options) ? $options[ $key ] : $default_value;
349+
return array_key_exists($key, $options) ? $options[ $key ] : $default_value;
343350
}
344351

345352
private static function RTIndexCheckExist($connection, string $index)
@@ -350,7 +357,7 @@ private static function RTIndexCheckExist($connection, string $index)
350357

351358
$index_definition = $connection->query("SHOW TABLES LIKE '{$index}' ")->fetchAll();
352359

353-
return \count($index_definition) > 0;
360+
return count($index_definition) > 0;
354361
}
355362

356363
/**
@@ -386,6 +393,6 @@ private static function sqlGetRowsCount($pdo, string $table = '', string $condit
386393

387394
return $pdo->query($query)->fetchColumn() ?? 0;
388395
}
396+
}
389397

390-
391-
}
398+
# -eof- #

src/PDOWrapperException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44

55
class PDOWrapperException extends \RuntimeException
66
{
7+
public static function create($message, $args = [], $code = 0): PDOWrapperException
8+
{
9+
return new static(vsprintf($message, $args), $code);
10+
}
711

812
}

src/PDOWrapperInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ public function setOptions(array $options = []):array;
1212
public function setConsoleMessenger(callable $messenger);
1313
public function rebuildAbstractIndex(string $mysql_table, string $searchd_index, Closure $make_updated_set_method, string $condition = '', bool $mva_columns_used = false, array $mva_indexes_list = []):int;
1414

15+
public function checkIndexExist(string $index):bool;
16+
1517
}

0 commit comments

Comments
 (0)