@@ -24,19 +24,10 @@ public function connect(array $config)
24
24
$ connection = $ this ->createConnection ($ dsn , $ config , $ options );
25
25
26
26
if (! empty ($ config ['database ' ])) {
27
- $ connection ->exec ("use ` {$ config ['database ' ]}`; " );
27
+ $ connection ->exec ("USE ` {$ config ['database ' ]}`; " );
28
28
}
29
29
30
- $ this ->configureIsolationLevel ($ connection , $ config );
31
-
32
- $ this ->configureEncoding ($ connection , $ config );
33
-
34
- // Next, we will check to see if a timezone has been specified in this config
35
- // and if it has we will issue a statement to modify the timezone with the
36
- // database. Setting this DB timezone is an optional configuration item.
37
- $ this ->configureTimezone ($ connection , $ config );
38
-
39
- $ this ->setModes ($ connection , $ config );
30
+ $ this ->configureConnection ($ connection , $ config );
40
31
41
32
return $ connection ;
42
33
}
@@ -48,57 +39,40 @@ public function connect(array $config)
48
39
* @param array $config
49
40
* @return void
50
41
*/
51
- protected function configureIsolationLevel ($ connection , array $ config )
42
+ protected function configureConnection ($ connection , array $ config )
52
43
{
53
- if (! isset ($ config ['isolation_level ' ])) {
54
- return ;
55
- }
44
+ $ statements = [];
56
45
57
- $ connection -> prepare (
58
- " SET SESSION TRANSACTION ISOLATION LEVEL { $ config ['isolation_level ' ]}"
59
- )-> execute ( );
60
- }
46
+ // First, we set the transaction isolation level.
47
+ if ( isset ( $ config ['isolation_level ' ])) {
48
+ $ statements [] = sprintf ( ' SESSION TRANSACTION ISOLATION LEVEL %s ' , $ config [ ' isolation_level ' ] );
49
+ }
61
50
62
- /**
63
- * Set the connection character set and collation.
64
- *
65
- * @param \PDO $connection
66
- * @param array $config
67
- * @return void|\PDO
68
- */
69
- protected function configureEncoding ($ connection , array $ config )
70
- {
71
- if (! isset ($ config ['charset ' ])) {
72
- return $ connection ;
51
+ // Now, we set the charset and possibly the collation.
52
+ if (isset ($ config ['charset ' ])) {
53
+ if (isset ($ config ['collation ' ])) {
54
+ $ statements [] = sprintf ("NAMES '%s' COLLATE '%s' " , $ config ['charset ' ], $ config ['collation ' ]);
55
+ } else {
56
+ $ statements [] = sprintf ("NAMES '%s' " , $ config ['charset ' ]);
57
+ }
73
58
}
74
59
75
- $ connection ->prepare (
76
- "set names ' {$ config ['charset ' ]}' " .$ this ->getCollation ($ config )
77
- )->execute ();
78
- }
60
+ // Next, we will check to see if a timezone has been specified in this config
61
+ // and if it has we will issue a statement to modify the timezone with the
62
+ // database. Setting this DB timezone is an optional configuration item.
63
+ if (isset ($ config ['timezone ' ])) {
64
+ $ statements [] = sprintf ("time_zone='%s' " , $ config ['timezone ' ]);
65
+ }
79
66
80
- /**
81
- * Get the collation for the configuration.
82
- *
83
- * @param array $config
84
- * @return string
85
- */
86
- protected function getCollation (array $ config )
87
- {
88
- return isset ($ config ['collation ' ]) ? " collate ' {$ config ['collation ' ]}' " : '' ;
89
- }
67
+ // Next, we set the correct sql_mode mode according to the config.
68
+ $ sqlMode = $ this ->getSqlMode ($ connection , $ config );
69
+ if (null !== $ sqlMode ) {
70
+ $ statements [] = sprintf ("SESSION sql_mode='%s' " , $ sqlMode );
71
+ }
90
72
91
- /**
92
- * Set the timezone on the connection.
93
- *
94
- * @param \PDO $connection
95
- * @param array $config
96
- * @return void
97
- */
98
- protected function configureTimezone ($ connection , array $ config )
99
- {
100
- if (isset ($ config ['timezone ' ])) {
101
- $ connection ->prepare ('set time_zone=" ' .$ config ['timezone ' ].'" ' )->execute ();
73
+ // Finally, execute a single SET command with all our statements.
74
+ if ([] !== $ statements ) {
75
+ $ connection ->exec (sprintf ('SET %s; ' , implode (', ' , $ statements )));
102
76
}
103
77
}
104
78
@@ -155,54 +129,32 @@ protected function getHostDsn(array $config)
155
129
}
156
130
157
131
/**
158
- * Set the modes for the connection .
132
+ * Get the sql_mode value .
159
133
*
160
134
* @param \PDO $connection
161
135
* @param array $config
162
- * @return void
136
+ * @return string|null
163
137
*/
164
- protected function setModes ( PDO $ connection , array $ config )
138
+ protected function getSqlMode ( $ connection , $ config )
165
139
{
166
140
if (isset ($ config ['modes ' ])) {
167
- $ this ->setCustomModes ($ connection , $ config );
168
- } elseif (isset ($ config ['strict ' ])) {
169
- if ($ config ['strict ' ]) {
170
- $ connection ->prepare ($ this ->strictMode ($ connection , $ config ))->execute ();
171
- } else {
172
- $ connection ->prepare ("set session sql_mode='NO_ENGINE_SUBSTITUTION' " )->execute ();
173
- }
141
+ return implode (', ' , $ config ['modes ' ]);
174
142
}
175
- }
176
143
177
- /**
178
- * Set the custom modes on the connection.
179
- *
180
- * @param \PDO $connection
181
- * @param array $config
182
- * @return void
183
- */
184
- protected function setCustomModes (PDO $ connection , array $ config )
185
- {
186
- $ modes = implode (', ' , $ config ['modes ' ]);
144
+ if (! isset ($ config ['strict ' ])) {
145
+ return null ;
146
+ }
187
147
188
- $ connection ->prepare ("set session sql_mode=' {$ modes }' " )->execute ();
189
- }
148
+ if (! $ config ['strict ' ]) {
149
+ return 'NO_ENGINE_SUBSTITUTION ' ;
150
+ }
190
151
191
- /**
192
- * Get the query to enable strict mode.
193
- *
194
- * @param \PDO $connection
195
- * @param array $config
196
- * @return string
197
- */
198
- protected function strictMode (PDO $ connection , $ config )
199
- {
200
152
$ version = $ config ['version ' ] ?? $ connection ->getAttribute (PDO ::ATTR_SERVER_VERSION );
201
153
202
154
if (version_compare ($ version , '8.0.11 ' ) >= 0 ) {
203
- return " set session sql_mode= 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'" ;
155
+ return 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION ' ;
204
156
}
205
157
206
- return " set session sql_mode= 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'" ;
158
+ return 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION ' ;
207
159
}
208
160
}
0 commit comments