|
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
|
|
|
/** |
|
45
|
|
|
* Object constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param MySqlMetaDataLayer $dl The metadata layer. |
|
48
|
|
|
* @param string $sqlMode The SQL mode. |
|
49
|
|
|
* |
|
50
|
|
|
* @throws MySqlQueryErrorException |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function __construct(MySqlMetaDataLayer $dl, string $sqlMode) |
|
53
|
|
|
{ |
|
54
|
1 |
|
$this->dl = $dl; |
|
55
|
|
|
|
|
56
|
|
|
try |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->dl->setSqlMode('ORACLE'); |
|
59
|
1 |
|
$hasOracleMode = true; |
|
60
|
|
|
} |
|
61
|
|
|
catch (MySqlQueryErrorException $e) |
|
62
|
|
|
{ |
|
63
|
|
|
$hasOracleMode = false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
if ($hasOracleMode) |
|
67
|
|
|
{ |
|
68
|
1 |
|
$parts = explode(',', $sqlMode); |
|
69
|
1 |
|
$parts[] = 'ORACLE'; |
|
70
|
1 |
|
$this->dl->setSqlMode(implode(',', $parts)); |
|
71
|
1 |
|
$this->canonicalSqlModeWithOracle = $this->dl->getCanonicalSqlMode(); |
|
72
|
|
|
} |
|
73
|
|
|
else |
|
74
|
|
|
{ |
|
75
|
|
|
$this->canonicalSqlModeWithOracle = null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
$this->dl->setSqlMode($sqlMode); |
|
79
|
1 |
|
$this->canonicalSqlMode = $this->dl->getCanonicalSqlMode(); |
|
80
|
1 |
|
$this->currentSqlMode = $this->canonicalSqlMode; |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
84
|
|
|
/** |
|
85
|
|
|
* Adds ORACLE too the current SQL_MODE if the initial SQL_MODE did not include ORACLE. |
|
86
|
|
|
* |
|
87
|
|
|
* @throws MySqlQueryErrorException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function addIfRequiredOracleMode(): void |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->currentSqlMode!==$this->canonicalSqlModeWithOracle) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->dl->setSqlMode($this->canonicalSqlModeWithOracle); |
|
|
|
|
|
|
94
|
|
|
$this->currentSqlMode = $this->canonicalSqlModeWithOracle; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
99
|
|
|
/** |
|
100
|
|
|
* Compares a SQL mode with the current SQL mode with or without ORACLE when appropriate. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $sqlMode The SQL mode. |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function compare(string $sqlMode): bool |
|
107
|
|
|
{ |
|
108
|
1 |
|
$parts = explode(',', $sqlMode); |
|
109
|
1 |
|
if (in_array('ORACLE', $parts)) |
|
110
|
|
|
{ |
|
111
|
|
|
return ($sqlMode===$this->canonicalSqlModeWithOracle); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
return ($sqlMode===$this->canonicalSqlMode); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
118
|
|
|
/** |
|
119
|
|
|
* Returns true if and only if the MySQL instance has ORACLE SQL mode. |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function hasOracleMode(): bool |
|
124
|
|
|
{ |
|
125
|
|
|
return ($this->canonicalSqlModeWithOracle!==null); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
129
|
|
|
/** |
|
130
|
|
|
* Removes ORACLE from the current SQL_MODE if the initial SQL_MODE did not include ORACLE. |
|
131
|
|
|
* |
|
132
|
|
|
* @throws MySqlQueryErrorException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function removeIfRequiredOracleMode(): void |
|
135
|
|
|
{ |
|
136
|
|
|
if ($this->currentSqlMode!==$this->canonicalSqlMode) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->dl->setSqlMode($this->canonicalSqlMode); |
|
139
|
|
|
$this->currentSqlMode = $this->canonicalSqlMode; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
147
|
|
|
|