2
2
3
3
namespace Laminas \Db \Adapter ;
4
4
5
+ use InvalidArgumentException ;
5
6
use Laminas \Db \ResultSet ;
6
7
8
+ use function func_get_args ;
9
+ use function in_array ;
10
+ use function is_array ;
11
+ use function is_bool ;
12
+ use function is_string ;
13
+ use function strpos ;
14
+ use function strtolower ;
15
+
7
16
/**
8
17
* @property Driver\DriverInterface $driver
9
18
* @property Platform\PlatformInterface $platform
@@ -13,58 +22,45 @@ class Adapter implements AdapterInterface, Profiler\ProfilerAwareInterface
13
22
/**
14
23
* Query Mode Constants
15
24
*/
16
- const QUERY_MODE_EXECUTE = 'execute ' ;
17
- const QUERY_MODE_PREPARE = 'prepare ' ;
25
+ public const QUERY_MODE_EXECUTE = 'execute ' ;
26
+ public const QUERY_MODE_PREPARE = 'prepare ' ;
18
27
19
28
/**
20
29
* Prepare Type Constants
21
30
*/
22
- const PREPARE_TYPE_POSITIONAL = 'positional ' ;
23
- const PREPARE_TYPE_NAMED = 'named ' ;
31
+ public const PREPARE_TYPE_POSITIONAL = 'positional ' ;
32
+ public const PREPARE_TYPE_NAMED = 'named ' ;
24
33
25
- const FUNCTION_FORMAT_PARAMETER_NAME = 'formatParameterName ' ;
26
- const FUNCTION_QUOTE_IDENTIFIER = 'quoteIdentifier ' ;
27
- const FUNCTION_QUOTE_VALUE = 'quoteValue ' ;
34
+ public const FUNCTION_FORMAT_PARAMETER_NAME = 'formatParameterName ' ;
35
+ public const FUNCTION_QUOTE_IDENTIFIER = 'quoteIdentifier ' ;
36
+ public const FUNCTION_QUOTE_VALUE = 'quoteValue ' ;
28
37
29
- const VALUE_QUOTE_SEPARATOR = 'quoteSeparator ' ;
38
+ public const VALUE_QUOTE_SEPARATOR = 'quoteSeparator ' ;
30
39
31
- /**
32
- * @var Driver\DriverInterface
33
- */
34
- protected $ driver = null ;
40
+ /** @var Driver\DriverInterface */
41
+ protected $ driver ;
35
42
36
- /**
37
- * @var Platform\PlatformInterface
38
- */
39
- protected $ platform = null ;
43
+ /** @var Platform\PlatformInterface */
44
+ protected $ platform ;
40
45
41
- /**
42
- * @var Profiler\ProfilerInterface
43
- */
44
- protected $ profiler = null ;
46
+ /** @var Profiler\ProfilerInterface */
47
+ protected $ profiler ;
45
48
46
- /**
47
- * @var ResultSet\ResultSetInterface
48
- */
49
- protected $ queryResultSetPrototype = null ;
49
+ /** @var ResultSet\ResultSetInterface */
50
+ protected $ queryResultSetPrototype ;
50
51
51
- /**
52
- * @var Driver\StatementInterface
53
- */
54
- protected $ lastPreparedStatement = null ;
52
+ /** @var Driver\StatementInterface */
53
+ protected $ lastPreparedStatement ;
55
54
56
55
/**
57
56
* @param Driver\DriverInterface|array $driver
58
- * @param Platform\PlatformInterface $platform
59
- * @param ResultSet\ResultSetInterface $queryResultPrototype
60
- * @param Profiler\ProfilerInterface $profiler
61
57
* @throws Exception\InvalidArgumentException
62
58
*/
63
59
public function __construct (
64
60
$ driver ,
65
- Platform \PlatformInterface $ platform = null ,
66
- ResultSet \ResultSetInterface $ queryResultPrototype = null ,
67
- Profiler \ProfilerInterface $ profiler = null
61
+ ? Platform \PlatformInterface $ platform = null ,
62
+ ? ResultSet \ResultSetInterface $ queryResultPrototype = null ,
63
+ ? Profiler \ProfilerInterface $ profiler = null
68
64
) {
69
65
// first argument can be an array of parameters
70
66
$ parameters = [];
@@ -88,16 +84,15 @@ public function __construct(
88
84
$ platform = $ this ->createPlatform ($ parameters );
89
85
}
90
86
91
- $ this ->platform = $ platform ;
92
- $ this ->queryResultSetPrototype = ( $ queryResultPrototype) ?: new ResultSet \ResultSet ();
87
+ $ this ->platform = $ platform ;
88
+ $ this ->queryResultSetPrototype = $ queryResultPrototype ?: new ResultSet \ResultSet ();
93
89
94
90
if ($ profiler ) {
95
91
$ this ->setProfiler ($ profiler );
96
92
}
97
93
}
98
94
99
95
/**
100
- * @param Profiler\ProfilerInterface $profiler
101
96
* @return self Provides a fluent interface
102
97
*/
103
98
public function setProfiler (Profiler \ProfilerInterface $ profiler )
@@ -147,6 +142,7 @@ public function getQueryResultSetPrototype()
147
142
return $ this ->queryResultSetPrototype ;
148
143
}
149
144
145
+ /** @return string */
150
146
public function getCurrentSchema ()
151
147
{
152
148
return $ this ->driver ->getConnection ()->getCurrentSchema ();
@@ -157,30 +153,30 @@ public function getCurrentSchema()
157
153
*
158
154
* @param string $sql
159
155
* @param string|array|ParameterContainer $parametersOrQueryMode
160
- * @param \Laminas\Db\ResultSet\ResultSetInterface $resultPrototype
161
156
* @throws Exception\InvalidArgumentException
162
157
* @return Driver\StatementInterface|ResultSet\ResultSet
163
158
*/
164
159
public function query (
165
160
$ sql ,
166
161
$ parametersOrQueryMode = self ::QUERY_MODE_PREPARE ,
167
- ResultSet \ResultSetInterface $ resultPrototype = null
162
+ ? ResultSet \ResultSetInterface $ resultPrototype = null
168
163
) {
169
- if (is_string ($ parametersOrQueryMode )
164
+ if (
165
+ is_string ($ parametersOrQueryMode )
170
166
&& in_array ($ parametersOrQueryMode , [self ::QUERY_MODE_PREPARE , self ::QUERY_MODE_EXECUTE ])
171
167
) {
172
- $ mode = $ parametersOrQueryMode ;
168
+ $ mode = $ parametersOrQueryMode ;
173
169
$ parameters = null ;
174
170
} elseif (is_array ($ parametersOrQueryMode ) || $ parametersOrQueryMode instanceof ParameterContainer) {
175
- $ mode = self ::QUERY_MODE_PREPARE ;
171
+ $ mode = self ::QUERY_MODE_PREPARE ;
176
172
$ parameters = $ parametersOrQueryMode ;
177
173
} else {
178
174
throw new Exception \InvalidArgumentException (
179
175
'Parameter 2 to this method must be a flag, an array, or ParameterContainer '
180
176
);
181
177
}
182
178
183
- if ($ mode == self ::QUERY_MODE_PREPARE ) {
179
+ if ($ mode === self ::QUERY_MODE_PREPARE ) {
184
180
$ this ->lastPreparedStatement = null ;
185
181
$ this ->lastPreparedStatement = $ this ->driver ->createStatement ($ sql );
186
182
$ this ->lastPreparedStatement ->prepare ();
@@ -199,7 +195,7 @@ public function query(
199
195
}
200
196
201
197
if ($ result instanceof Driver \ResultInterface && $ result ->isQueryResult ()) {
202
- $ resultSet = clone ( $ resultPrototype ? : $ this ->queryResultSetPrototype ) ;
198
+ $ resultSet = $ resultPrototype !== null ? clone $ resultPrototype : $ this ->queryResultSetPrototype ;
203
199
$ resultSet ->initialize ($ result );
204
200
return $ resultSet ;
205
201
}
@@ -217,11 +213,12 @@ public function query(
217
213
public function createStatement ($ initialSql = null , $ initialParameters = null )
218
214
{
219
215
$ statement = $ this ->driver ->createStatement ($ initialSql );
220
- if ($ initialParameters === null
216
+ if (
217
+ $ initialParameters === null
221
218
|| ! $ initialParameters instanceof ParameterContainer
222
219
&& is_array ($ initialParameters )
223
220
) {
224
- $ initialParameters = new ParameterContainer (( is_array ($ initialParameters ) ? $ initialParameters : []) );
221
+ $ initialParameters = new ParameterContainer (is_array ($ initialParameters ) ? $ initialParameters : []);
225
222
}
226
223
$ statement ->setParameterContainer ($ initialParameters );
227
224
return $ statement ;
@@ -230,7 +227,7 @@ public function createStatement($initialSql = null, $initialParameters = null)
230
227
public function getHelpers ()
231
228
{
232
229
$ functions = [];
233
- $ platform = $ this ->platform ;
230
+ $ platform = $ this ->platform ;
234
231
foreach (func_get_args () as $ arg ) {
235
232
switch ($ arg ) {
236
233
case self ::FUNCTION_QUOTE_IDENTIFIER :
@@ -248,7 +245,7 @@ public function getHelpers()
248
245
}
249
246
250
247
/**
251
- * @param $name
248
+ * @param string $name
252
249
* @throws Exception\InvalidArgumentException
253
250
* @return Driver\DriverInterface|Platform\PlatformInterface
254
251
*/
@@ -267,7 +264,7 @@ public function __get($name)
267
264
/**
268
265
* @param array $parameters
269
266
* @return Driver\DriverInterface
270
- * @throws \ InvalidArgumentException
267
+ * @throws InvalidArgumentException
271
268
* @throws Exception\InvalidArgumentException
272
269
*/
273
270
protected function createDriver ($ parameters )
@@ -313,7 +310,7 @@ protected function createDriver($parameters)
313
310
break ;
314
311
case 'pdo ' :
315
312
default :
316
- if ($ driverName == 'pdo ' || strpos ($ driverName , 'pdo ' ) === 0 ) {
313
+ if ($ driverName === 'pdo ' || strpos ($ driverName , 'pdo ' ) === 0 ) {
317
314
$ driver = new Driver \Pdo \Pdo ($ parameters );
318
315
}
319
316
}
@@ -342,7 +339,7 @@ protected function createPlatform(array $parameters)
342
339
}
343
340
344
341
// currently only supported by the IbmDb2 & Oracle concrete implementations
345
- $ options = ( isset ( $ parameters ['platform_options ' ])) ? $ parameters [ ' platform_options ' ] : [];
342
+ $ options = $ parameters ['platform_options ' ] ?? [];
346
343
347
344
switch ($ platformName ) {
348
345
case 'Mysql ' :
@@ -355,7 +352,7 @@ protected function createPlatform(array $parameters)
355
352
return new Platform \Mysql ($ driver );
356
353
case 'SqlServer ' :
357
354
// PDO is only supported driver for quoting values in this platform
358
- return new Platform \SqlServer (( $ this ->driver instanceof Driver \Pdo \Pdo) ? $ this ->driver : null );
355
+ return new Platform \SqlServer ($ this ->driver instanceof Driver \Pdo \Pdo ? $ this ->driver : null );
359
356
case 'Oracle ' :
360
357
if ($ this ->driver instanceof Driver \Oci8 \Oci8 || $ this ->driver instanceof Driver \Pdo \Pdo) {
361
358
$ driver = $ this ->driver ;
@@ -386,41 +383,42 @@ protected function createPlatform(array $parameters)
386
383
}
387
384
388
385
/**
389
- *
390
386
* @param array $parameters
391
387
* @return Profiler\ProfilerInterface
392
388
* @throws Exception\InvalidArgumentException
393
389
*/
394
390
protected function createProfiler ($ parameters )
395
391
{
396
392
if ($ parameters ['profiler ' ] instanceof Profiler \ProfilerInterface) {
397
- $ profiler = $ parameters ['profiler ' ];
398
- } elseif (is_bool ($ parameters ['profiler ' ])) {
399
- $ profiler = ($ parameters ['profiler ' ] == true ) ? new Profiler \Profiler : null ;
400
- } else {
401
- throw new Exception \InvalidArgumentException (
402
- '"profiler" parameter must be an instance of ProfilerInterface or a boolean '
403
- );
393
+ return $ parameters ['profiler ' ];
404
394
}
405
- return $ profiler ;
395
+
396
+ if (is_bool ($ parameters ['profiler ' ])) {
397
+ return $ parameters ['profiler ' ] === true ? new Profiler \Profiler () : null ;
398
+ }
399
+
400
+ throw new Exception \InvalidArgumentException (
401
+ '"profiler" parameter must be an instance of ProfilerInterface or a boolean '
402
+ );
406
403
}
407
404
408
405
/**
406
+ * @deprecated
407
+ *
409
408
* @param array $parameters
410
409
* @return Driver\DriverInterface
411
- * @throws \ InvalidArgumentException
410
+ * @throws InvalidArgumentException
412
411
* @throws Exception\InvalidArgumentException
413
- * @deprecated
414
412
*/
415
413
protected function createDriverFromParameters (array $ parameters )
416
414
{
417
415
return $ this ->createDriver ($ parameters );
418
416
}
419
417
420
418
/**
421
- * @param Driver\DriverInterface $driver
422
- * @return Platform\PlatformInterface
423
419
* @deprecated
420
+ *
421
+ * @return Platform\PlatformInterface
424
422
*/
425
423
protected function createPlatformFromDriver (Driver \DriverInterface $ driver )
426
424
{
0 commit comments