Complex classes like Connection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
136 | class Connection extends Component |
||
137 | { |
||
138 | /** |
||
139 | * @event [[yii\base\Event|Event]] an event that is triggered after a DB connection is established |
||
140 | */ |
||
141 | const EVENT_AFTER_OPEN = 'afterOpen'; |
||
142 | /** |
||
143 | * @event [[yii\base\Event|Event]] an event that is triggered right before a top-level transaction is started |
||
144 | */ |
||
145 | const EVENT_BEGIN_TRANSACTION = 'beginTransaction'; |
||
146 | /** |
||
147 | * @event [[yii\base\Event|Event]] an event that is triggered right after a top-level transaction is committed |
||
148 | */ |
||
149 | const EVENT_COMMIT_TRANSACTION = 'commitTransaction'; |
||
150 | /** |
||
151 | * @event [[yii\base\Event|Event]] an event that is triggered right after a top-level transaction is rolled back |
||
152 | */ |
||
153 | const EVENT_ROLLBACK_TRANSACTION = 'rollbackTransaction'; |
||
154 | |||
155 | /** |
||
156 | * @var string the Data Source Name, or DSN, contains the information required to connect to the database. |
||
157 | * Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on |
||
158 | * the format of the DSN string. |
||
159 | * |
||
160 | * For [SQLite](http://php.net/manual/en/ref.pdo-sqlite.connection.php) you may use a [path alias](guide:concept-aliases) |
||
161 | * for specifying the database path, e.g. `sqlite:@app/data/db.sql`. |
||
162 | * |
||
163 | * @see charset |
||
164 | */ |
||
165 | public $dsn; |
||
166 | /** |
||
167 | * @var string the username for establishing DB connection. Defaults to `null` meaning no username to use. |
||
168 | */ |
||
169 | public $username; |
||
170 | /** |
||
171 | * @var string the password for establishing DB connection. Defaults to `null` meaning no password to use. |
||
172 | */ |
||
173 | public $password; |
||
174 | /** |
||
175 | * @var array PDO attributes (name => value) that should be set when calling [[open()]] |
||
176 | * to establish a DB connection. Please refer to the |
||
177 | * [PHP manual](http://php.net/manual/en/pdo.setattribute.php) for |
||
178 | * details about available attributes. |
||
179 | */ |
||
180 | public $attributes; |
||
181 | /** |
||
182 | * @var PDO the PHP PDO instance associated with this DB connection. |
||
183 | * This property is mainly managed by [[open()]] and [[close()]] methods. |
||
184 | * When a DB connection is active, this property will represent a PDO instance; |
||
185 | * otherwise, it will be null. |
||
186 | * @see pdoClass |
||
187 | */ |
||
188 | public $pdo; |
||
189 | /** |
||
190 | * @var bool whether to enable schema caching. |
||
191 | * Note that in order to enable truly schema caching, a valid cache component as specified |
||
192 | * by [[schemaCache]] must be enabled and [[enableSchemaCache]] must be set true. |
||
193 | * @see schemaCacheDuration |
||
194 | * @see schemaCacheExclude |
||
195 | * @see schemaCache |
||
196 | */ |
||
197 | public $enableSchemaCache = false; |
||
198 | /** |
||
199 | * @var int number of seconds that table metadata can remain valid in cache. |
||
200 | * Use 0 to indicate that the cached data will never expire. |
||
201 | * @see enableSchemaCache |
||
202 | */ |
||
203 | public $schemaCacheDuration = 3600; |
||
204 | /** |
||
205 | * @var array list of tables whose metadata should NOT be cached. Defaults to empty array. |
||
206 | * The table names may contain schema prefix, if any. Do not quote the table names. |
||
207 | * @see enableSchemaCache |
||
208 | */ |
||
209 | public $schemaCacheExclude = []; |
||
210 | /** |
||
211 | * @var CacheInterface|string the cache object or the ID of the cache application component that |
||
212 | * is used to cache the table metadata. |
||
213 | * @see enableSchemaCache |
||
214 | */ |
||
215 | public $schemaCache = 'cache'; |
||
216 | /** |
||
217 | * @var bool whether to enable query caching. |
||
218 | * Note that in order to enable query caching, a valid cache component as specified |
||
219 | * by [[queryCache]] must be enabled and [[enableQueryCache]] must be set true. |
||
220 | * Also, only the results of the queries enclosed within [[cache()]] will be cached. |
||
221 | * @see queryCache |
||
222 | * @see cache() |
||
223 | * @see noCache() |
||
224 | */ |
||
225 | public $enableQueryCache = true; |
||
226 | /** |
||
227 | * @var int the default number of seconds that query results can remain valid in cache. |
||
228 | * Defaults to 3600, meaning 3600 seconds, or one hour. Use 0 to indicate that the cached data will never expire. |
||
229 | * The value of this property will be used when [[cache()]] is called without a cache duration. |
||
230 | * @see enableQueryCache |
||
231 | * @see cache() |
||
232 | */ |
||
233 | public $queryCacheDuration = 3600; |
||
234 | /** |
||
235 | * @var CacheInterface|string the cache object or the ID of the cache application component |
||
236 | * that is used for query caching. |
||
237 | * @see enableQueryCache |
||
238 | */ |
||
239 | public $queryCache = 'cache'; |
||
240 | /** |
||
241 | * @var string the charset used for database connection. The property is only used |
||
242 | * for MySQL and PostgreSQL databases. Defaults to null, meaning using default charset |
||
243 | * as configured by the database. |
||
244 | * |
||
245 | * For Oracle Database, the charset must be specified in the [[dsn]], for example for UTF-8 by appending `;charset=UTF-8` |
||
246 | * to the DSN string. |
||
247 | * |
||
248 | * The same applies for if you're using GBK or BIG5 charset with MySQL, then it's highly recommended to |
||
249 | * specify charset via [[dsn]] like `'mysql:dbname=mydatabase;host=127.0.0.1;charset=GBK;'`. |
||
250 | */ |
||
251 | public $charset; |
||
252 | /** |
||
253 | * @var bool whether to turn on prepare emulation. Defaults to false, meaning PDO |
||
254 | * will use the native prepare support if available. For some databases (such as MySQL), |
||
255 | * this may need to be set true so that PDO can emulate the prepare support to bypass |
||
256 | * the buggy native prepare support. |
||
257 | * The default value is null, which means the PDO ATTR_EMULATE_PREPARES value will not be changed. |
||
258 | */ |
||
259 | public $emulatePrepare; |
||
260 | /** |
||
261 | * @var string the common prefix or suffix for table names. If a table name is given |
||
262 | * as `{{%TableName}}`, then the percentage character `%` will be replaced with this |
||
263 | * property value. For example, `{{%post}}` becomes `{{tbl_post}}`. |
||
264 | */ |
||
265 | public $tablePrefix = ''; |
||
266 | /** |
||
267 | * @var array mapping between PDO driver names and [[Schema]] classes. |
||
268 | * The keys of the array are PDO driver names while the values are either the corresponding |
||
269 | * schema class names or configurations. Please refer to [[Yii::createObject()]] for |
||
270 | * details on how to specify a configuration. |
||
271 | * |
||
272 | * This property is mainly used by [[getSchema()]] when fetching the database schema information. |
||
273 | * You normally do not need to set this property unless you want to use your own |
||
274 | * [[Schema]] class to support DBMS that is not supported by Yii. |
||
275 | */ |
||
276 | public $schemaMap = [ |
||
277 | 'pgsql' => pgsql\Schema::class, // PostgreSQL |
||
278 | 'mysqli' => mysql\Schema::class, // MySQL |
||
279 | 'mysql' => mysql\Schema::class, // MySQL |
||
280 | 'sqlite' => sqlite\Schema::class, // sqlite 3 |
||
281 | 'sqlite2' => sqlite\Schema::class, // sqlite 2 |
||
282 | ]; |
||
283 | /** |
||
284 | * @var string Custom PDO wrapper class. If not set, it will use [[PDO]] or [[\yii\db\mssql\PDO]] when MSSQL is used. |
||
285 | * @see pdo |
||
286 | */ |
||
287 | public $pdoClass; |
||
288 | /** |
||
289 | * @var array mapping between PDO driver names and [[Command]] classes. |
||
290 | * The keys of the array are PDO driver names while the values are either the corresponding |
||
291 | * command class names or configurations. Please refer to [[Yii::createObject()]] for |
||
292 | * details on how to specify a configuration. |
||
293 | * |
||
294 | * This property is mainly used by [[createCommand()]] to create new database [[Command]] objects. |
||
295 | * You normally do not need to set this property unless you want to use your own |
||
296 | * [[Command]] class or support DBMS that is not supported by Yii. |
||
297 | * @since 2.0.14 |
||
298 | */ |
||
299 | public $commandMap = [ |
||
300 | 'pgsql' => 'yii\db\Command', // PostgreSQL |
||
301 | 'mysqli' => 'yii\db\Command', // MySQL |
||
302 | 'mysql' => 'yii\db\Command', // MySQL |
||
303 | 'sqlite' => 'yii\db\sqlite\Command', // sqlite 3 |
||
304 | 'sqlite2' => 'yii\db\sqlite\Command', // sqlite 2 |
||
305 | 'sqlsrv' => 'yii\db\Command', // newer MSSQL driver on MS Windows hosts |
||
306 | 'oci' => 'yii\db\Command', // Oracle driver |
||
307 | 'mssql' => 'yii\db\Command', // older MSSQL driver on MS Windows hosts |
||
308 | 'dblib' => 'yii\db\Command', // dblib drivers on GNU/Linux (and maybe other OSes) hosts |
||
309 | ]; |
||
310 | /** |
||
311 | * @var bool whether to enable [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
||
312 | * Note that if the underlying DBMS does not support savepoint, setting this property to be true will have no effect. |
||
313 | */ |
||
314 | public $enableSavepoint = true; |
||
315 | /** |
||
316 | * @var CacheInterface|string the cache object or the ID of the cache application component that is used to store |
||
317 | * the health status of the DB servers specified in [[masters]] and [[slaves]]. |
||
318 | * This is used only when read/write splitting is enabled or [[masters]] is not empty. |
||
319 | */ |
||
320 | public $serverStatusCache = 'cache'; |
||
321 | /** |
||
322 | * @var int the retry interval in seconds for dead servers listed in [[masters]] and [[slaves]]. |
||
323 | * This is used together with [[serverStatusCache]]. |
||
324 | */ |
||
325 | public $serverRetryInterval = 600; |
||
326 | /** |
||
327 | * @var bool whether to enable read/write splitting by using [[slaves]] to read data. |
||
328 | * Note that if [[slaves]] is empty, read/write splitting will NOT be enabled no matter what value this property takes. |
||
329 | */ |
||
330 | public $enableSlaves = true; |
||
331 | /** |
||
332 | * @var array list of slave connection configurations. Each configuration is used to create a slave DB connection. |
||
333 | * When [[enableSlaves]] is true, one of these configurations will be chosen and used to create a DB connection |
||
334 | * for performing read queries only. |
||
335 | * @see enableSlaves |
||
336 | * @see slaveConfig |
||
337 | */ |
||
338 | public $slaves = []; |
||
339 | /** |
||
340 | * @var array the configuration that should be merged with every slave configuration listed in [[slaves]]. |
||
341 | * For example, |
||
342 | * |
||
343 | * ```php |
||
344 | * [ |
||
345 | * 'username' => 'slave', |
||
346 | * 'password' => 'slave', |
||
347 | * 'attributes' => [ |
||
348 | * // use a smaller connection timeout |
||
349 | * PDO::ATTR_TIMEOUT => 10, |
||
350 | * ], |
||
351 | * ] |
||
352 | * ``` |
||
353 | */ |
||
354 | public $slaveConfig = []; |
||
355 | /** |
||
356 | * @var array list of master connection configurations. Each configuration is used to create a master DB connection. |
||
357 | * When [[open()]] is called, one of these configurations will be chosen and used to create a DB connection |
||
358 | * which will be used by this object. |
||
359 | * Note that when this property is not empty, the connection setting (e.g. "dsn", "username") of this object will |
||
360 | * be ignored. |
||
361 | * @see masterConfig |
||
362 | * @see shuffleMasters |
||
363 | */ |
||
364 | public $masters = []; |
||
365 | /** |
||
366 | * @var array the configuration that should be merged with every master configuration listed in [[masters]]. |
||
367 | * For example, |
||
368 | * |
||
369 | * ```php |
||
370 | * [ |
||
371 | * 'username' => 'master', |
||
372 | * 'password' => 'master', |
||
373 | * 'attributes' => [ |
||
374 | * // use a smaller connection timeout |
||
375 | * PDO::ATTR_TIMEOUT => 10, |
||
376 | * ], |
||
377 | * ] |
||
378 | * ``` |
||
379 | */ |
||
380 | public $masterConfig = []; |
||
381 | /** |
||
382 | * @var bool whether to shuffle [[masters]] before getting one. |
||
383 | * @since 2.0.11 |
||
384 | * @see masters |
||
385 | */ |
||
386 | public $shuffleMasters = true; |
||
387 | /** |
||
388 | * @var bool whether to enable logging of database queries. Defaults to true. |
||
389 | * You may want to disable this option in a production environment to gain performance |
||
390 | * if you do not need the information being logged. |
||
391 | * @since 2.0.12 |
||
392 | * @see enableProfiling |
||
393 | */ |
||
394 | public $enableLogging = true; |
||
395 | /** |
||
396 | * @var bool whether to enable profiling of opening database connection and database queries. Defaults to true. |
||
397 | * You may want to disable this option in a production environment to gain performance |
||
398 | * if you do not need the information being logged. |
||
399 | * @since 2.0.12 |
||
400 | * @see enableLogging |
||
401 | */ |
||
402 | public $enableProfiling = true; |
||
403 | |||
404 | /** |
||
405 | * @var Transaction the currently active transaction |
||
406 | */ |
||
407 | private $_transaction; |
||
408 | /** |
||
409 | * @var Schema the database schema |
||
410 | */ |
||
411 | private $_schema; |
||
412 | /** |
||
413 | * @var string driver name |
||
414 | */ |
||
415 | private $_driverName; |
||
416 | /** |
||
417 | * @var Connection|false the currently active master connection |
||
418 | */ |
||
419 | private $_master = false; |
||
420 | /** |
||
421 | * @var Connection|false the currently active slave connection |
||
422 | */ |
||
423 | private $_slave = false; |
||
424 | /** |
||
425 | * @var array query cache parameters for the [[cache()]] calls |
||
426 | */ |
||
427 | private $_queryCacheInfo = []; |
||
428 | |||
429 | /** |
||
430 | * {@inheritdoc} |
||
431 | */ |
||
432 | 1936 | public function init() |
|
433 | { |
||
434 | 1936 | if (is_array($this->dsn)) { |
|
435 | 4 | $this->dsn = $this->buildDSN($this->dsn); |
|
436 | } |
||
437 | 1936 | } |
|
438 | |||
439 | /** |
||
440 | * Returns a value indicating whether the DB connection is established. |
||
441 | * @return bool whether the DB connection is established |
||
442 | */ |
||
443 | 314 | public function getIsActive() |
|
447 | |||
448 | /** |
||
449 | * Uses query cache for the queries performed with the callable. |
||
450 | * |
||
451 | * When query caching is enabled ([[enableQueryCache]] is true and [[queryCache]] refers to a valid cache), |
||
452 | * queries performed within the callable will be cached and their results will be fetched from cache if available. |
||
453 | * For example, |
||
454 | * |
||
455 | * ```php |
||
456 | * // The customer will be fetched from cache if available. |
||
457 | * // If not, the query will be made against DB and cached for use next time. |
||
458 | * $customer = $db->cache(function (Connection $db) { |
||
459 | * return $db->createCommand('SELECT * FROM customer WHERE id=1')->queryOne(); |
||
460 | * }); |
||
461 | * ``` |
||
462 | * |
||
463 | * Note that query cache is only meaningful for queries that return results. For queries performed with |
||
464 | * [[Command::execute()]], query cache will not be used. |
||
465 | * |
||
466 | * @param callable $callable a PHP callable that contains DB queries which will make use of query cache. |
||
467 | * The signature of the callable is `function (Connection $db)`. |
||
468 | * @param int $duration the number of seconds that query results can remain valid in the cache. If this is |
||
469 | * not set, the value of [[queryCacheDuration]] will be used instead. |
||
470 | * Use 0 to indicate that the cached data will never expire. |
||
471 | * @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query results. |
||
472 | * @return mixed the return result of the callable |
||
473 | * @throws \Throwable if there is any exception during query |
||
474 | * @see enableQueryCache |
||
475 | * @see queryCache |
||
476 | * @see noCache() |
||
477 | */ |
||
478 | 6 | public function cache(callable $callable, $duration = null, $dependency = null) |
|
490 | |||
491 | /** |
||
492 | * Disables query cache temporarily. |
||
493 | * |
||
494 | * Queries performed within the callable will not use query cache at all. For example, |
||
495 | * |
||
496 | * ```php |
||
497 | * $db->cache(function (Connection $db) { |
||
498 | * |
||
499 | * // ... queries that use query cache ... |
||
500 | * |
||
501 | * return $db->noCache(function (Connection $db) { |
||
502 | * // this query will not use query cache |
||
503 | * return $db->createCommand('SELECT * FROM customer WHERE id=1')->queryOne(); |
||
504 | * }); |
||
505 | * }); |
||
506 | * ``` |
||
507 | * |
||
508 | * @param callable $callable a PHP callable that contains DB queries which should not use query cache. |
||
509 | * The signature of the callable is `function (Connection $db)`. |
||
510 | * @return mixed the return result of the callable |
||
511 | * @throws \Throwable if there is any exception during query |
||
512 | * @see enableQueryCache |
||
513 | * @see queryCache |
||
514 | * @see cache() |
||
515 | */ |
||
516 | 38 | public function noCache(callable $callable) |
|
528 | |||
529 | /** |
||
530 | * Returns the current query cache information. |
||
531 | * This method is used internally by [[Command]]. |
||
532 | * @param int $duration the preferred caching duration. If null, it will be ignored. |
||
533 | * @param \yii\caching\Dependency $dependency the preferred caching dependency. If null, it will be ignored. |
||
534 | * @return array the current query cache information, or null if query cache is not enabled. |
||
535 | * @internal |
||
536 | */ |
||
537 | 1269 | public function getQueryCacheInfo($duration, $dependency) |
|
566 | |||
567 | /** |
||
568 | * Establishes a DB connection. |
||
569 | * It does nothing if a DB connection has already been established. |
||
570 | * @throws Exception if connection fails |
||
571 | */ |
||
572 | 1590 | public function open() |
|
614 | |||
615 | /** |
||
616 | * Closes the currently active DB connection. |
||
617 | * It does nothing if the connection is already closed. |
||
618 | */ |
||
619 | 1827 | public function close() |
|
642 | |||
643 | /** |
||
644 | * Creates the PDO instance. |
||
645 | * This method is called by [[open]] to establish a DB connection. |
||
646 | * The default implementation will create a PHP PDO instance. |
||
647 | * You may override this method if the default PDO needs to be adapted for certain DBMS. |
||
648 | * @return PDO the pdo instance |
||
649 | */ |
||
650 | 1535 | protected function createPdoInstance() |
|
676 | |||
677 | /** |
||
678 | * Initializes the DB connection. |
||
679 | * This method is invoked right after the DB connection is established. |
||
680 | * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES` |
||
681 | * if [[emulatePrepare]] is true, and sets the database [[charset]] if it is not empty. |
||
682 | * It then triggers an [[EVENT_AFTER_OPEN]] event. |
||
683 | */ |
||
684 | 1535 | protected function initConnection() |
|
695 | |||
696 | /** |
||
697 | * Creates a command for execution. |
||
698 | * @param string $sql the SQL statement to be executed |
||
699 | * @param array $params the parameters to be bound to the SQL statement |
||
700 | * @return Command the DB command |
||
701 | */ |
||
702 | 1354 | public function createCommand($sql = null, $params = []) |
|
715 | |||
716 | /** |
||
717 | * Returns the currently active transaction. |
||
718 | * @return Transaction the currently active transaction. Null if no active transaction. |
||
719 | */ |
||
720 | 1325 | public function getTransaction() |
|
724 | |||
725 | /** |
||
726 | * Starts a transaction. |
||
727 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
728 | * See [[Transaction::begin()]] for details. |
||
729 | * @return Transaction the transaction initiated |
||
730 | */ |
||
731 | 35 | public function beginTransaction($isolationLevel = null) |
|
742 | |||
743 | /** |
||
744 | * Executes callback provided in a transaction. |
||
745 | * |
||
746 | * @param callable $callback a valid PHP callback that performs the job. Accepts connection instance as parameter. |
||
747 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
748 | * See [[Transaction::begin()]] for details. |
||
749 | * @throws \Throwable if there is any exception during query. In this case the transaction will be rolled back. |
||
750 | * @return mixed result of callback function |
||
751 | */ |
||
752 | 19 | public function transaction(callable $callback, $isolationLevel = null) |
|
769 | |||
770 | /** |
||
771 | * Rolls back given [[Transaction]] object if it's still active and level match. |
||
772 | * In some cases rollback can fail, so this method is fail safe. Exception thrown |
||
773 | * from rollback will be caught and just logged with [[\Yii::error()]]. |
||
774 | * @param Transaction $transaction Transaction object given from [[beginTransaction()]]. |
||
775 | * @param int $level Transaction level just after [[beginTransaction()]] call. |
||
776 | */ |
||
777 | 4 | private function rollbackTransactionOnLevel($transaction, $level) |
|
789 | |||
790 | /** |
||
791 | * Returns the schema information for the database opened by this connection. |
||
792 | * @return Schema the schema information for the database opened by this connection. |
||
793 | * @throws NotSupportedException if there is no support for the current driver type |
||
794 | */ |
||
795 | 1731 | public function getSchema() |
|
811 | |||
812 | /** |
||
813 | * Returns the query builder for the current DB connection. |
||
814 | * @return QueryBuilder the query builder for the current DB connection. |
||
815 | */ |
||
816 | 981 | public function getQueryBuilder() |
|
820 | |||
821 | /** |
||
822 | * Can be used to set [[QueryBuilder]] configuration via Connection configuration array. |
||
823 | * |
||
824 | * @param array $value the [[QueryBuilder]] properties to be configured. |
||
825 | * @since 2.0.14 |
||
826 | */ |
||
827 | public function setQueryBuilder($value) |
||
831 | |||
832 | /** |
||
833 | * Obtains the schema information for the named table. |
||
834 | * @param string $name table name. |
||
835 | * @param bool $refresh whether to reload the table schema even if it is found in the cache. |
||
836 | * @return TableSchema table schema information. Null if the named table does not exist. |
||
837 | */ |
||
838 | 201 | public function getTableSchema($name, $refresh = false) |
|
842 | |||
843 | /** |
||
844 | * Returns the ID of the last inserted row or sequence value. |
||
845 | * @param string $sequenceName name of the sequence object (required by some DBMS) |
||
846 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
847 | * @see http://php.net/manual/en/pdo.lastinsertid.php |
||
848 | */ |
||
849 | public function getLastInsertID($sequenceName = '') |
||
853 | |||
854 | /** |
||
855 | * Quotes a string value for use in a query. |
||
856 | * Note that if the parameter is not a string, it will be returned without change. |
||
857 | * @param string $value string to be quoted |
||
858 | * @return string the properly quoted string |
||
859 | * @see http://php.net/manual/en/pdo.quote.php |
||
860 | */ |
||
861 | 957 | public function quoteValue($value) |
|
865 | |||
866 | /** |
||
867 | * Quotes a table name for use in a query. |
||
868 | * If the table name contains schema prefix, the prefix will also be properly quoted. |
||
869 | * If the table name is already quoted or contains special characters including '(', '[[' and '{{', |
||
870 | * then this method will do nothing. |
||
871 | * @param string $name table name |
||
872 | * @return string the properly quoted table name |
||
873 | */ |
||
874 | 1193 | public function quoteTableName($name) |
|
878 | |||
879 | /** |
||
880 | * Quotes a column name for use in a query. |
||
881 | * If the column name contains prefix, the prefix will also be properly quoted. |
||
882 | * If the column name is already quoted or contains special characters including '(', '[[' and '{{', |
||
883 | * then this method will do nothing. |
||
884 | * @param string $name column name |
||
885 | * @return string the properly quoted column name |
||
886 | */ |
||
887 | 1216 | public function quoteColumnName($name) |
|
891 | |||
892 | /** |
||
893 | * Processes a SQL statement by quoting table and column names that are enclosed within double brackets. |
||
894 | * Tokens enclosed within double curly brackets are treated as table names, while |
||
895 | * tokens enclosed within double square brackets are column names. They will be quoted accordingly. |
||
896 | * Also, the percentage character "%" at the beginning or ending of a table name will be replaced |
||
897 | * with [[tablePrefix]]. |
||
898 | * @param string $sql the SQL to be quoted |
||
899 | * @return string the quoted SQL |
||
900 | */ |
||
901 | 1397 | public function quoteSql($sql) |
|
915 | |||
916 | /** |
||
917 | * Returns the name of the DB driver. Based on the the current [[dsn]], in case it was not set explicitly |
||
918 | * by an end user. |
||
919 | * @return string name of the DB driver |
||
920 | */ |
||
921 | 1739 | public function getDriverName() |
|
933 | |||
934 | /** |
||
935 | * Changes the current driver name. |
||
936 | * @param string $driverName name of the DB driver |
||
937 | */ |
||
938 | public function setDriverName($driverName) |
||
942 | |||
943 | /** |
||
944 | * Returns a server version as a string comparable by [[\version_compare()]]. |
||
945 | * @return string server version as a string. |
||
946 | * @since 2.0.14 |
||
947 | */ |
||
948 | 48 | public function getServerVersion() |
|
952 | |||
953 | /** |
||
954 | * Returns the PDO instance for the currently active slave connection. |
||
955 | * When [[enableSlaves]] is true, one of the slaves will be used for read queries, and its PDO instance |
||
956 | * will be returned by this method. |
||
957 | * @param bool $fallbackToMaster whether to return a master PDO in case none of the slave connections is available. |
||
958 | * @return PDO the PDO instance for the currently active slave connection. `null` is returned if no slave connection |
||
959 | * is available and `$fallbackToMaster` is false. |
||
960 | */ |
||
961 | 1328 | public function getSlavePdo($fallbackToMaster = true) |
|
970 | |||
971 | /** |
||
972 | * Returns the PDO instance for the currently active master connection. |
||
973 | * This method will open the master DB connection and then return [[pdo]]. |
||
974 | * @return PDO the PDO instance for the currently active master connection. |
||
975 | */ |
||
976 | 1366 | public function getMasterPdo() |
|
981 | |||
982 | /** |
||
983 | * Returns the currently active slave connection. |
||
984 | * If this method is called for the first time, it will try to open a slave connection when [[enableSlaves]] is true. |
||
985 | * @param bool $fallbackToMaster whether to return a master connection in case there is no slave connection available. |
||
986 | * @return Connection the currently active slave connection. `null` is returned if there is no slave available and |
||
987 | * `$fallbackToMaster` is false. |
||
988 | */ |
||
989 | 1330 | public function getSlave($fallbackToMaster = true) |
|
1001 | |||
1002 | /** |
||
1003 | * Returns the currently active master connection. |
||
1004 | * If this method is called for the first time, it will try to open a master connection. |
||
1005 | * @return Connection the currently active master connection. `null` is returned if there is no master available. |
||
1006 | * @since 2.0.11 |
||
1007 | */ |
||
1008 | 3 | public function getMaster() |
|
1018 | |||
1019 | /** |
||
1020 | * Executes the provided callback by using the master connection. |
||
1021 | * |
||
1022 | * This method is provided so that you can temporarily force using the master connection to perform |
||
1023 | * DB operations even if they are read queries. For example, |
||
1024 | * |
||
1025 | * ```php |
||
1026 | * $result = $db->useMaster(function ($db) { |
||
1027 | * return $db->createCommand('SELECT * FROM user LIMIT 1')->queryOne(); |
||
1028 | * }); |
||
1029 | * ``` |
||
1030 | * |
||
1031 | * @param callable $callback a PHP callable to be executed by this method. Its signature is |
||
1032 | * `function (Connection $db)`. Its return value will be returned by this method. |
||
1033 | * @return mixed the return value of the callback |
||
1034 | * @throws \Throwable if there is any exception thrown from the callback |
||
1035 | */ |
||
1036 | 87 | public function useMaster(callable $callback) |
|
1054 | |||
1055 | /** |
||
1056 | * Opens the connection to a server in the pool. |
||
1057 | * This method implements the load balancing among the given list of the servers. |
||
1058 | * Connections will be tried in random order. |
||
1059 | * @param array $pool the list of connection configurations in the server pool |
||
1060 | * @param array $sharedConfig the configuration common to those given in `$pool`. |
||
1061 | * @return Connection the opened DB connection, or `null` if no server is available |
||
1062 | * @throws InvalidConfigException if a configuration does not specify "dsn" |
||
1063 | */ |
||
1064 | 1243 | protected function openFromPool(array $pool, array $sharedConfig) |
|
1069 | |||
1070 | /** |
||
1071 | * Opens the connection to a server in the pool. |
||
1072 | * This method implements the load balancing among the given list of the servers. |
||
1073 | * Connections will be tried in sequential order. |
||
1074 | * @param array $pool the list of connection configurations in the server pool |
||
1075 | * @param array $sharedConfig the configuration common to those given in `$pool`. |
||
1076 | * @return Connection the opened DB connection, or `null` if no server is available |
||
1077 | * @throws InvalidConfigException if a configuration does not specify "dsn" |
||
1078 | * @since 2.0.11 |
||
1079 | */ |
||
1080 | 1243 | protected function openFromPoolSequentially(array $pool, array $sharedConfig) |
|
1121 | |||
1122 | /** |
||
1123 | * Build the Data Source Name or DSN |
||
1124 | * @param array $config the DSN configurations |
||
1125 | * @return string the formated DSN |
||
1126 | * @throws InvalidConfigException if 'driver' key was not defined |
||
1127 | */ |
||
1128 | 4 | private function buildDSN(array $config) |
|
1144 | |||
1145 | /** |
||
1146 | * Close the connection before serializing. |
||
1147 | * @return array |
||
1148 | */ |
||
1149 | 17 | public function __sleep() |
|
1161 | |||
1162 | /** |
||
1163 | * Reset the connection after cloning. |
||
1164 | */ |
||
1165 | 7 | public function __clone() |
|
1178 | } |
||
1179 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: