Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Sql.php « Db « Migration « Updater « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 451007500842b80a0abec57a1b885729d8c72c57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
namespace Piwik\Updater\Migration\Db;
use Piwik\Common;
use Piwik\Db;
use Piwik\Updater\Migration as MigrationInterface;
use Piwik\Updater\Migration\Db as DbMigration;

/**
 * Executes a given SQL query.
 *
 * @see Factory::sql()
 * @ignore
 */
class Sql extends DbMigration
{

    /**
     * @var string
     */
    protected $sql;

    /**
     * @var false|int|array
     */
    private $errorCodesToIgnore;

    /**
     * Sql constructor.
     * @param string $sql
     * @param int|int[] $errorCodesToIgnore  If no error should be ignored use an empty array.
     */
    public function __construct($sql, $errorCodesToIgnore)
    {
        if (!is_array($errorCodesToIgnore)) {
            $errorCodesToIgnore = array($errorCodesToIgnore);
        }

        $this->sql = $sql;
        $this->errorCodesToIgnore = $errorCodesToIgnore;
    }

    public function shouldIgnoreError($exception)
    {
        if (empty($this->errorCodesToIgnore)) {
            return false;
        }

        foreach ($this->errorCodesToIgnore as $code) {
            if (Db::get()->isErrNo($exception, $code)) {
                return true;
            }
        }

        return false;
    }

    /**
     * @internal
     * @param int $errorCode
     * @return $this
     */
    public function addErrorCodeToIgnore($errorCode)
    {
        $this->errorCodesToIgnore[] = $errorCode;

        return $this;
    }

    /**
     * @internal
     */
    public function getErrorCodesToIgnore()
    {
        return $this->errorCodesToIgnore;
    }

    public function __toString()
    {
        $sql = $this->sql;

        if (!Common::stringEndsWith($sql, ';')) {
            $sql .= ';';
        }

        return $sql;
    }

    public function exec()
    {
        Db::exec($this->sql);
    }
}