AddColumn.php
5 years ago
AddColumns.php
5 years ago
AddIndex.php
5 years ago
AddPrimaryKey.php
5 years ago
AddUniqueKey.php
5 years ago
BatchInsert.php
5 years ago
BoundSql.php
5 years ago
ChangeColumn.php
5 years ago
ChangeColumnType.php
5 years ago
ChangeColumnTypes.php
5 years ago
CreateTable.php
4 years ago
DropColumn.php
5 years ago
DropColumns.php
4 years ago
DropIndex.php
5 years ago
DropPrimaryKey.php
5 years ago
DropTable.php
5 years ago
Factory.php
5 years ago
Insert.php
5 years ago
Sql.php
3 years ago
Sql.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | namespace Piwik\Updater\Migration\Db; |
| 9 | use Piwik\Common; |
| 10 | use Piwik\Config; |
| 11 | use Piwik\Db; |
| 12 | use Piwik\Updater\Migration\Db as DbMigration; |
| 13 | |
| 14 | /** |
| 15 | * Executes a given SQL query. |
| 16 | * |
| 17 | * @see Factory::sql() |
| 18 | * @ignore |
| 19 | */ |
| 20 | class Sql extends DbMigration |
| 21 | { |
| 22 | |
| 23 | /** |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $sql; |
| 27 | |
| 28 | /** |
| 29 | * @var false|int|array |
| 30 | */ |
| 31 | private $errorCodesToIgnore; |
| 32 | |
| 33 | /** |
| 34 | * Sql constructor. |
| 35 | * |
| 36 | * @param string $sql |
| 37 | * @param int|int[] $errorCodesToIgnore If no error should be ignored use an empty array. |
| 38 | */ |
| 39 | public function __construct($sql, $errorCodesToIgnore) |
| 40 | { |
| 41 | if (!is_array($errorCodesToIgnore)) { |
| 42 | $errorCodesToIgnore = array($errorCodesToIgnore); |
| 43 | } |
| 44 | |
| 45 | $this->sql = $sql; |
| 46 | $globalErrorCodesToIgnore = Config::getInstance()->database['ignore_error_codes'] ?? []; |
| 47 | $this->errorCodesToIgnore = array_merge($errorCodesToIgnore, (is_array($globalErrorCodesToIgnore) ? $globalErrorCodesToIgnore : [])); |
| 48 | } |
| 49 | |
| 50 | public function shouldIgnoreError($exception) |
| 51 | { |
| 52 | if (empty($this->errorCodesToIgnore)) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | foreach ($this->errorCodesToIgnore as $code) { |
| 57 | if (Db::get()->isErrNo($exception, $code)) { |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @internal |
| 67 | * @param int $errorCode |
| 68 | * @return $this |
| 69 | */ |
| 70 | public function addErrorCodeToIgnore($errorCode) |
| 71 | { |
| 72 | $this->errorCodesToIgnore[] = $errorCode; |
| 73 | |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @internal |
| 79 | */ |
| 80 | public function getErrorCodesToIgnore() |
| 81 | { |
| 82 | return $this->errorCodesToIgnore; |
| 83 | } |
| 84 | |
| 85 | public function __toString() |
| 86 | { |
| 87 | $sql = $this->sql; |
| 88 | |
| 89 | if (!empty($sql) && !Common::stringEndsWith($sql, ';')) { |
| 90 | $sql .= ';'; |
| 91 | } |
| 92 | |
| 93 | return $sql; |
| 94 | } |
| 95 | |
| 96 | public function exec() |
| 97 | { |
| 98 | if (!empty($this->sql)) { |
| 99 | Db::exec($this->sql); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 |