Issues (16)

src/Loader/Helper/SqlModeHelper.php (1 issue)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Loader\Helper;
5
6
use SetBased\Stratum\MySql\Exception\MySqlQueryErrorException;
7
use SetBased\Stratum\MySql\MySqlMetadataLayer;
8
9
/**
10
 * Helper class for handling the SQL mode of the MySQL instance.
11
 */
12
class SqlModeHelper
13
{
14
  //--------------------------------------------------------------------------------------------------------------------
15
  /**
16
   * The canonical SQL mode.
17
   *
18
   * @var string
19
   */
20
  private string $canonicalSqlMode;
21
22
  /**
23
   * The canonical SQL mode with ORACLE.
24
   *
25
   * @var ?string
26
   */
27
  private ?string $canonicalSqlModeWithOracle = null;
28
29
  /**
30
   * The current SQL mode (also in canonical order).
31
   *
32
   * @var string
33
   */
34
  private string $currentSqlMode;
35
36
  /**
37
   * The metadata layer.
38
   *
39
   * @var MySqlMetadataLayer
40
   */
41
  private MySqlMetadataLayer $dl;
42
43
  //--------------------------------------------------------------------------------------------------------------------
44
  /**
45
   * Object constructor.
46
   *
47
   * @param MySqlMetadataLayer $dl      The metadata layer.
48
   * @param string             $sqlMode The SQL mode.
49
   */
50
  public function __construct(MySqlMetadataLayer $dl, string $sqlMode)
51
  {
52
    $this->dl = $dl;
53
54
    try
55
    {
56
      $this->dl->setSqlMode('ORACLE');
57
      $hasOracleMode = true;
58
    }
59
    catch (MySqlQueryErrorException)
60
    {
61
      $hasOracleMode = false;
62
    }
63
64
    if ($hasOracleMode)
65
    {
66
      $parts   = explode(',', $sqlMode);
67
      $parts[] = 'ORACLE';
68
      $this->dl->setSqlMode(implode(',', $parts));
69
      $this->canonicalSqlModeWithOracle = $this->dl->getCanonicalSqlMode();
70
    }
71
    else
72
    {
73
      $this->canonicalSqlModeWithOracle = null;
74
    }
75
76
    $this->dl->setSqlMode($sqlMode);
77
    $this->canonicalSqlMode = $this->dl->getCanonicalSqlMode();
78
    $this->currentSqlMode   = $this->canonicalSqlMode;
79
  }
80
81
  //--------------------------------------------------------------------------------------------------------------------
82
  /**
83
   * Adds ORACLE to the current SQL_MODE if the initial SQL_MODE did not include ORACLE.
84
   */
85
  public function addIfRequiredOracleMode(): void
86
  {
87
    if ($this->currentSqlMode!==$this->canonicalSqlModeWithOracle)
88
    {
89
      $this->dl->setSqlMode($this->canonicalSqlModeWithOracle);
0 ignored issues
show
It seems like $this->canonicalSqlModeWithOracle can also be of type null; however, parameter $sqlMode of SetBased\Stratum\MySql\M...dataLayer::setSqlMode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
      $this->dl->setSqlMode(/** @scrutinizer ignore-type */ $this->canonicalSqlModeWithOracle);
Loading history...
90
      $this->currentSqlMode = (string)$this->canonicalSqlModeWithOracle;
91
    }
92
  }
93
94
  //--------------------------------------------------------------------------------------------------------------------
95
  /**
96
   * Compares a SQL mode with the current SQL mode with or without ORACLE when appropriate.
97
   *
98
   * @param string $sqlMode The SQL mode.
99
   */
100
  public function compare(string $sqlMode): bool
101
  {
102
    $parts = explode(',', $sqlMode);
103
    if (in_array('ORACLE', $parts))
104
    {
105
      return ($sqlMode===$this->canonicalSqlModeWithOracle);
106
    }
107
108
    return ($sqlMode===$this->canonicalSqlMode);
109
  }
110
111
  //--------------------------------------------------------------------------------------------------------------------
112
  /**
113
   * Returns the SQL mode in canonical  order.
114
   */
115
  public function getCanonicalSqlMode(): string
116
  {
117
    return $this->canonicalSqlMode;
118
  }
119
120
  //--------------------------------------------------------------------------------------------------------------------
121
  /**
122
   * Returns whether the MySQL instance has ORACLE SQL mode.
123
   */
124
  public function hasOracleMode(): bool
125
  {
126
    return ($this->canonicalSqlModeWithOracle!==null);
127
  }
128
129
  //--------------------------------------------------------------------------------------------------------------------
130
  /**
131
   * Removes ORACLE from the current SQL_MODE if the initial SQL_MODE did not include ORACLE.
132
   */
133
  public function removeIfRequiredOracleMode(): void
134
  {
135
    if ($this->currentSqlMode!==$this->canonicalSqlMode)
136
    {
137
      $this->dl->setSqlMode($this->canonicalSqlMode);
138
      $this->currentSqlMode = $this->canonicalSqlMode;
139
    }
140
  }
141
142
  //--------------------------------------------------------------------------------------------------------------------
143
}
144
145
//----------------------------------------------------------------------------------------------------------------------
146