Completed
Push — master ( 3bb391...1a7c41 )
by P.R.
03:42
created

SqlModeHelper::getCanonicalSqlMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\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 $canonicalSqlMode;
21
22
  /**
23
   * The canonical SQL mode with ORACLE.
24
   *
25
   * @var ?string
26
   */
27
  private $canonicalSqlModeWithOracle = null;
28
29
  /**
30
   * The current SQL mode (also in canonical order).
31
   *
32
   * @var string
33
   */
34
  private $currentSqlMode;
35
36
  /**
37
   * The metadata layer.
38
   *
39
   * @var MySqlMetaDataLayer
40
   */
41
  private $dl;
42
43
  /**
44
   * True if and only if the MySQL instance has ORACLE SQL mode.
45
   *
46
   * @var bool|null
47
   */
48
  private $hasOracleMode;
49
50
  //--------------------------------------------------------------------------------------------------------------------
51
  /**
52
   * Object constructor.
53
   *
54
   * @param MySqlMetaDataLayer $dl      The metadata layer.
55
   * @param string             $sqlMode The SQL mode.
56
   *
57
   * @throws MySqlQueryErrorException
58
   */
59 1
  public function __construct(MySqlMetaDataLayer $dl, string $sqlMode)
60
  {
61 1
    $this->dl = $dl;
62
63
    try
64
    {
65 1
      $this->dl->setSqlMode('ORACLE');
66 1
      $this->hasOracleMode = true;
67
    }
68
    catch (MySqlQueryErrorException $e)
69
    {
70
      $this->hasOracleMode = false;
71
    }
72
73 1
    if ($this->hasOracleMode)
74
    {
75 1
      $parts   = explode(',', $sqlMode);
76 1
      $parts[] = 'ORACLE';
77 1
      $this->dl->setSqlMode(implode(',', $parts));
78 1
      $this->canonicalSqlModeWithOracle = $this->dl->getCanonicalSqlMode();
79
    }
80
81 1
    $this->dl->setSqlMode($sqlMode);
82 1
    $this->canonicalSqlMode = $this->dl->getCanonicalSqlMode();
83 1
    $this->currentSqlMode   = $this->canonicalSqlMode;
84 1
  }
85
86
  //--------------------------------------------------------------------------------------------------------------------
87
  /**
88
   * Adds ORACLE too the current SQL_MODE if the initial SQL_MODE did not include ORACLE.
89
   *
90
   * @throws MySqlQueryErrorException
91
   */
92
  public function addIfRequiredOracleMode(): void
93
  {
94
    if ($this->currentSqlMode!==$this->canonicalSqlModeWithOracle)
95
    {
96
      $this->dl->setSqlMode($this->canonicalSqlModeWithOracle);
0 ignored issues
show
Bug introduced by
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

96
      $this->dl->setSqlMode(/** @scrutinizer ignore-type */ $this->canonicalSqlModeWithOracle);
Loading history...
97
      $this->currentSqlMode = $this->canonicalSqlModeWithOracle;
98
    }
99
  }
100
101
  //--------------------------------------------------------------------------------------------------------------------
102
  /**
103
   * Compares a SQL mode with the current SQL mode with or without ORACLE when appropriate.
104
   *
105
   * @param string $sqlMode The SQL mode.
106
   *
107
   * @return bool
108
   */
109 1
  public function compare(string $sqlMode): bool
110
  {
111 1
    $parts = explode(',', $sqlMode);
112 1
    if (in_array('ORACLE', $parts))
113
    {
114
      return ($sqlMode===$this->canonicalSqlModeWithOracle);
115
    }
116
117 1
    return ($sqlMode===$this->canonicalSqlMode);
118
  }
119
120
  //--------------------------------------------------------------------------------------------------------------------
121
  /**
122
   * Returns the canonical SQL mode.
123
   *
124
   * @return string
125
   */
126
  public function getCanonicalSqlMode(): string
127
  {
128
    return $this->canonicalSqlMode;
129
  }
130
131
  //--------------------------------------------------------------------------------------------------------------------
132
  /**
133
   * Returns the canonical SQL mode.
134
   *
135
   * @return string
136
   */
137
  public function getCanonicalSqlModeWithOracle(): string
138
  {
139
    return $this->canonicalSqlModeWithOracle;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->canonicalSqlModeWithOracle could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
140
  }
141
142
  //--------------------------------------------------------------------------------------------------------------------
143
  /**
144
   * Returns true if and only if the MySQL instance has ORACLE SQL mode.
145
   *
146
   * @return bool
147
   */
148
  public function hasOracleMode(): bool
149
  {
150
    return $this->hasOracleMode;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hasOracleMode could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
151
  }
152
153
  //--------------------------------------------------------------------------------------------------------------------
154
  /**
155
   * Removes ORACLE from the current SQL_MODE if the initial SQL_MODE did not include ORACLE.
156
   *
157
   * @throws MySqlQueryErrorException
158
   */
159
  public function removeIfRequiredOracleMode(): void
160
  {
161
    if ($this->currentSqlMode!==$this->canonicalSqlMode)
162
    {
163
      $this->dl->setSqlMode($this->canonicalSqlMode);
164
      $this->currentSqlMode = $this->canonicalSqlMode;
165
    }
166
  }
167
168
  //--------------------------------------------------------------------------------------------------------------------
169
}
170
171
//----------------------------------------------------------------------------------------------------------------------
172