Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| MasterSlaveSqlLogger | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| setLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| startQuery | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
12 | |||
| __destruct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| stopQuery | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\generis\persistence\sql\dbal\MasterSlaveConnection; |
| 24 | |
| 25 | use Doctrine\DBAL\Logging\SQLLogger; |
| 26 | |
| 27 | class MasterSlaveSqlLogger implements SQLLogger |
| 28 | { |
| 29 | private static $read = 0; |
| 30 | private static $write = 0; |
| 31 | |
| 32 | private $label; |
| 33 | |
| 34 | public function setLabel($label): void |
| 35 | { |
| 36 | $this->label = $label; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | */ |
| 42 | public function startQuery($sql, ?array $params = null, ?array $types = null): void |
| 43 | { |
| 44 | if (stripos($sql, 'SELECT') === 0) { |
| 45 | self::$read++; |
| 46 | $target = 'slave'; |
| 47 | } else { |
| 48 | self::$write++; |
| 49 | $target = 'master'; |
| 50 | } |
| 51 | |
| 52 | if ($target !== $this->label) { |
| 53 | \common_Logger::e( |
| 54 | sprintf( |
| 55 | '[ERROR] %s [%s] %s', |
| 56 | debug_backtrace()[1]['function'], |
| 57 | $this->label, |
| 58 | $sql |
| 59 | ) |
| 60 | ); |
| 61 | } else { |
| 62 | \common_Logger::i( |
| 63 | sprintf( |
| 64 | '[INFO] %s [%s] %s...', |
| 65 | debug_backtrace()[1]['function'], |
| 66 | $this->label, |
| 67 | substr($sql, 0, 10) |
| 68 | ) |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public function __destruct() |
| 74 | { |
| 75 | \common_Logger::d(sprintf('[READ] - %s', self::$read)); |
| 76 | \common_Logger::d(sprintf('[WRITE] - %s', self::$write)); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * {@inheritdoc} |
| 81 | */ |
| 82 | public function stopQuery() |
| 83 | { |
| 84 | } |
| 85 | } |