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 |
||
153 | class Connection extends Component implements ConnectionInterface |
||
154 | { |
||
155 | /** |
||
156 | * @event [[yii\base\Event|Event]] an event that is triggered after a DB connection is established |
||
157 | */ |
||
158 | const EVENT_AFTER_OPEN = 'afterOpen'; |
||
159 | /** |
||
160 | * @event [[yii\base\Event|Event]] an event that is triggered right before a top-level transaction is started |
||
161 | */ |
||
162 | const EVENT_BEGIN_TRANSACTION = 'beginTransaction'; |
||
163 | /** |
||
164 | * @event [[yii\base\Event|Event]] an event that is triggered right after a top-level transaction is committed |
||
165 | */ |
||
166 | const EVENT_COMMIT_TRANSACTION = 'commitTransaction'; |
||
167 | /** |
||
168 | * @event [[yii\base\Event|Event]] an event that is triggered right after a top-level transaction is rolled back |
||
169 | */ |
||
170 | const EVENT_ROLLBACK_TRANSACTION = 'rollbackTransaction'; |
||
171 | |||
172 | /** |
||
173 | * @var string|array the Data Source Name, or DSN, contains the information required to connect to the database. |
||
174 | * Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on |
||
175 | * the format of the DSN string. |
||
176 | * |
||
177 | * For [SQLite](http://php.net/manual/en/ref.pdo-sqlite.connection.php) you may use a [path alias](guide:concept-aliases) |
||
178 | * for specifying the database path, e.g. `sqlite:@app/data/db.sql`. |
||
179 | * |
||
180 | * Since version 3.0.0 an array can be passed to contruct a DSN string. |
||
181 | * The `driver` array key is used as the driver prefix of the DSN, |
||
182 | * all further key-value pairs are rendered as `key=value` and concatenated by `;`. For example: |
||
183 | * |
||
184 | * ```php |
||
185 | * 'dsn' => [ |
||
186 | * 'driver' => 'mysql', |
||
187 | * 'host' => '127.0.0.1', |
||
188 | * 'dbname' => 'demo', |
||
189 | * 'charset' => 'utf8', |
||
190 | * ], |
||
191 | * ``` |
||
192 | * |
||
193 | * Will result in the DSN string `mysql:host=127.0.0.1;dbname=demo`. |
||
194 | */ |
||
195 | public $dsn; |
||
196 | /** |
||
197 | * @var string the username for establishing DB connection. Defaults to `null` meaning no username to use. |
||
198 | */ |
||
199 | public $username; |
||
200 | /** |
||
201 | * @var string the password for establishing DB connection. Defaults to `null` meaning no password to use. |
||
202 | */ |
||
203 | public $password; |
||
204 | /** |
||
205 | * @var array PDO attributes (name => value) that should be set when calling [[open()]] |
||
206 | * to establish a DB connection. Please refer to the |
||
207 | * [PHP manual](http://php.net/manual/en/pdo.setattribute.php) for |
||
208 | * details about available attributes. |
||
209 | */ |
||
210 | public $attributes; |
||
211 | /** |
||
212 | * @var PDO the PHP PDO instance associated with this DB connection. |
||
213 | * This property is mainly managed by [[open()]] and [[close()]] methods. |
||
214 | * When a DB connection is active, this property will represent a PDO instance; |
||
215 | * otherwise, it will be null. |
||
216 | * @see pdoClass |
||
217 | */ |
||
218 | public $pdo; |
||
219 | /** |
||
220 | * @var bool whether to enable schema caching. |
||
221 | * Note that in order to enable truly schema caching, a valid cache component as specified |
||
222 | * by [[schemaCache]] must be enabled and [[enableSchemaCache]] must be set true. |
||
223 | * @see schemaCacheDuration |
||
224 | * @see schemaCacheExclude |
||
225 | * @see schemaCache |
||
226 | */ |
||
227 | public $enableSchemaCache = false; |
||
228 | /** |
||
229 | * @var int number of seconds that table metadata can remain valid in cache. |
||
230 | * Use 0 to indicate that the cached data will never expire. |
||
231 | * @see enableSchemaCache |
||
232 | */ |
||
233 | public $schemaCacheDuration = 3600; |
||
234 | /** |
||
235 | * @var array list of tables whose metadata should NOT be cached. Defaults to empty array. |
||
236 | * The table names may contain schema prefix, if any. Do not quote the table names. |
||
237 | * @see enableSchemaCache |
||
238 | */ |
||
239 | public $schemaCacheExclude = []; |
||
240 | /** |
||
241 | * @var CacheInterface|string the cache object or the ID of the cache application component that |
||
242 | * is used to cache the table metadata. |
||
243 | * @see enableSchemaCache |
||
244 | */ |
||
245 | public $schemaCache = 'cache'; |
||
246 | /** |
||
247 | * @var bool whether to enable query caching. |
||
248 | * Note that in order to enable query caching, a valid cache component as specified |
||
249 | * by [[queryCache]] must be enabled and [[enableQueryCache]] must be set true. |
||
250 | * Also, only the results of the queries enclosed within [[cache()]] will be cached. |
||
251 | * @see queryCache |
||
252 | * @see cache() |
||
253 | * @see noCache() |
||
254 | */ |
||
255 | public $enableQueryCache = true; |
||
256 | /** |
||
257 | * @var int the default number of seconds that query results can remain valid in cache. |
||
258 | * Defaults to 3600, meaning 3600 seconds, or one hour. Use 0 to indicate that the cached data will never expire. |
||
259 | * The value of this property will be used when [[cache()]] is called without a cache duration. |
||
260 | * @see enableQueryCache |
||
261 | * @see cache() |
||
262 | */ |
||
263 | public $queryCacheDuration = 3600; |
||
264 | /** |
||
265 | * @var CacheInterface|string the cache object or the ID of the cache application component |
||
266 | * that is used for query caching. |
||
267 | * @see enableQueryCache |
||
268 | */ |
||
269 | public $queryCache = 'cache'; |
||
270 | |||
271 | /** |
||
272 | * @var bool whether to turn on prepare emulation. Defaults to false, meaning PDO |
||
273 | * will use the native prepare support if available. For some databases (such as MySQL), |
||
274 | * this may need to be set true so that PDO can emulate the prepare support to bypass |
||
275 | * the buggy native prepare support. |
||
276 | * The default value is null, which means the PDO ATTR_EMULATE_PREPARES value will not be changed. |
||
277 | */ |
||
278 | public $emulatePrepare; |
||
279 | /** |
||
280 | * @var string the common prefix or suffix for table names. If a table name is given |
||
281 | * as `{{%TableName}}`, then the percentage character `%` will be replaced with this |
||
282 | * property value. For example, `{{%post}}` becomes `{{tbl_post}}`. |
||
283 | */ |
||
284 | public $tablePrefix = ''; |
||
285 | /** |
||
286 | * @var array mapping between PDO driver names and [[Schema]] classes. |
||
287 | * The keys of the array are PDO driver names while the values are either the corresponding |
||
288 | * schema class names or configurations. Please refer to [[Yii::createObject()]] for |
||
289 | * details on how to specify a configuration. |
||
290 | * |
||
291 | * This property is mainly used by [[getSchema()]] when fetching the database schema information. |
||
292 | * You normally do not need to set this property unless you want to use your own |
||
293 | * [[Schema]] class to support DBMS that is not supported by Yii. |
||
294 | */ |
||
295 | public $schemaMap = [ |
||
296 | 'pgsql' => pgsql\Schema::class, // PostgreSQL |
||
297 | 'mysqli' => mysql\Schema::class, // MySQL |
||
298 | 'mysql' => mysql\Schema::class, // MySQL |
||
299 | 'sqlite' => sqlite\Schema::class, // sqlite 3 |
||
300 | 'sqlite2' => sqlite\Schema::class, // sqlite 2 |
||
301 | ]; |
||
302 | /** |
||
303 | * @var string Custom PDO wrapper class. If not set, it will use [[PDO]] or [[\yii\db\mssql\PDO]] when MSSQL is used. |
||
304 | * @see pdo |
||
305 | */ |
||
306 | public $pdoClass; |
||
307 | /** |
||
308 | * @var array mapping between PDO driver names and [[Command]] classes. |
||
309 | * The keys of the array are PDO driver names while the values are either the corresponding |
||
310 | * command class names or configurations. Please refer to [[Yii::createObject()]] for |
||
311 | * details on how to specify a configuration. |
||
312 | * |
||
313 | * This property is mainly used by [[createCommand()]] to create new database [[Command]] objects. |
||
314 | * You normally do not need to set this property unless you want to use your own |
||
315 | * [[Command]] class or support DBMS that is not supported by Yii. |
||
316 | * @since 2.0.14 |
||
317 | */ |
||
318 | public $commandMap = [ |
||
319 | 'pgsql' => 'yii\db\Command', // PostgreSQL |
||
320 | 'mysqli' => 'yii\db\Command', // MySQL |
||
321 | 'mysql' => 'yii\db\Command', // MySQL |
||
322 | 'sqlite' => 'yii\db\sqlite\Command', // sqlite 3 |
||
323 | 'sqlite2' => 'yii\db\sqlite\Command', // sqlite 2 |
||
324 | 'sqlsrv' => 'yii\db\Command', // newer MSSQL driver on MS Windows hosts |
||
325 | 'oci' => 'yii\db\Command', // Oracle driver |
||
326 | 'mssql' => 'yii\db\Command', // older MSSQL driver on MS Windows hosts |
||
327 | 'dblib' => 'yii\db\Command', // dblib drivers on GNU/Linux (and maybe other OSes) hosts |
||
328 | ]; |
||
329 | /** |
||
330 | * @var bool whether to enable [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
||
331 | * Note that if the underlying DBMS does not support savepoint, setting this property to be true will have no effect. |
||
332 | */ |
||
333 | public $enableSavepoint = true; |
||
334 | /** |
||
335 | * @var CacheInterface|string|false the cache object or the ID of the cache application component that is used to store |
||
336 | * the health status of the DB servers specified in [[masters]] and [[slaves]]. |
||
337 | * This is used only when read/write splitting is enabled or [[masters]] is not empty. |
||
338 | * Set boolean `false` to disabled server status caching. |
||
339 | */ |
||
340 | public $serverStatusCache = 'cache'; |
||
341 | /** |
||
342 | * @var int the retry interval in seconds for dead servers listed in [[masters]] and [[slaves]]. |
||
343 | * This is used together with [[serverStatusCache]]. |
||
344 | */ |
||
345 | public $serverRetryInterval = 600; |
||
346 | /** |
||
347 | * @var bool whether to enable read/write splitting by using [[slaves]] to read data. |
||
348 | * Note that if [[slaves]] is empty, read/write splitting will NOT be enabled no matter what value this property takes. |
||
349 | */ |
||
350 | public $enableSlaves = true; |
||
351 | /** |
||
352 | * @var array list of slave connection configurations. Each configuration is used to create a slave DB connection. |
||
353 | * When [[enableSlaves]] is true, one of these configurations will be chosen and used to create a DB connection |
||
354 | * for performing read queries only. |
||
355 | * @see enableSlaves |
||
356 | * @see slaveConfig |
||
357 | */ |
||
358 | public $slaves = []; |
||
359 | /** |
||
360 | * @var array the configuration that should be merged with every slave configuration listed in [[slaves]]. |
||
361 | * For example, |
||
362 | * |
||
363 | * ```php |
||
364 | * [ |
||
365 | * 'username' => 'slave', |
||
366 | * 'password' => 'slave', |
||
367 | * 'attributes' => [ |
||
368 | * // use a smaller connection timeout |
||
369 | * PDO::ATTR_TIMEOUT => 10, |
||
370 | * ], |
||
371 | * ] |
||
372 | * ``` |
||
373 | */ |
||
374 | public $slaveConfig = []; |
||
375 | /** |
||
376 | * @var array list of master connection configurations. Each configuration is used to create a master DB connection. |
||
377 | * When [[open()]] is called, one of these configurations will be chosen and used to create a DB connection |
||
378 | * which will be used by this object. |
||
379 | * Note that when this property is not empty, the connection setting (e.g. "dsn", "username") of this object will |
||
380 | * be ignored. |
||
381 | * @see masterConfig |
||
382 | * @see shuffleMasters |
||
383 | */ |
||
384 | public $masters = []; |
||
385 | /** |
||
386 | * @var array the configuration that should be merged with every master configuration listed in [[masters]]. |
||
387 | * For example, |
||
388 | * |
||
389 | * ```php |
||
390 | * [ |
||
391 | * 'username' => 'master', |
||
392 | * 'password' => 'master', |
||
393 | * 'attributes' => [ |
||
394 | * // use a smaller connection timeout |
||
395 | * PDO::ATTR_TIMEOUT => 10, |
||
396 | * ], |
||
397 | * ] |
||
398 | * ``` |
||
399 | */ |
||
400 | public $masterConfig = []; |
||
401 | /** |
||
402 | * @var bool whether to shuffle [[masters]] before getting one. |
||
403 | * @since 2.0.11 |
||
404 | * @see masters |
||
405 | */ |
||
406 | public $shuffleMasters = true; |
||
407 | /** |
||
408 | * @var bool whether to enable logging of database queries. Defaults to true. |
||
409 | * You may want to disable this option in a production environment to gain performance |
||
410 | * if you do not need the information being logged. |
||
411 | * @since 2.0.12 |
||
412 | * @see enableProfiling |
||
413 | */ |
||
414 | public $enableLogging = true; |
||
415 | /** |
||
416 | * @var bool whether to enable profiling of opening database connection and database queries. Defaults to true. |
||
417 | * You may want to disable this option in a production environment to gain performance |
||
418 | * if you do not need the information being logged. |
||
419 | * @since 2.0.12 |
||
420 | * @see enableLogging |
||
421 | */ |
||
422 | public $enableProfiling = true; |
||
423 | |||
424 | /** |
||
425 | * @var Transaction the currently active transaction |
||
426 | */ |
||
427 | private $_transaction; |
||
428 | /** |
||
429 | * @var Schema the database schema |
||
430 | */ |
||
431 | private $_schema; |
||
432 | /** |
||
433 | * @var string driver name |
||
434 | */ |
||
435 | private $_driverName; |
||
436 | /** |
||
437 | * @var Connection|false the currently active master connection |
||
438 | */ |
||
439 | private $_master = false; |
||
440 | /** |
||
441 | * @var Connection|false the currently active slave connection |
||
442 | */ |
||
443 | private $_slave = false; |
||
444 | /** |
||
445 | * @var array query cache parameters for the [[cache()]] calls |
||
446 | */ |
||
447 | private $_queryCacheInfo = []; |
||
448 | |||
449 | |||
450 | /** |
||
451 | * {@inheritdoc} |
||
452 | */ |
||
453 | public function init() |
||
460 | |||
461 | /** |
||
462 | * Returns a value indicating whether the DB connection is established. |
||
463 | * @return bool whether the DB connection is established |
||
464 | */ |
||
465 | public function getIsActive() |
||
469 | |||
470 | /** |
||
471 | * Uses query cache for the queries performed with the callable. |
||
472 | * |
||
473 | * When query caching is enabled ([[enableQueryCache]] is true and [[queryCache]] refers to a valid cache), |
||
474 | * queries performed within the callable will be cached and their results will be fetched from cache if available. |
||
475 | * For example, |
||
476 | * |
||
477 | * ```php |
||
478 | * // The customer will be fetched from cache if available. |
||
479 | * // If not, the query will be made against DB and cached for use next time. |
||
480 | * $customer = $db->cache(function (Connection $db) { |
||
481 | * return $db->createCommand('SELECT * FROM customer WHERE id=1')->queryOne(); |
||
482 | * }); |
||
483 | * ``` |
||
484 | * |
||
485 | * Note that query cache is only meaningful for queries that return results. For queries performed with |
||
486 | * [[Command::execute()]], query cache will not be used. |
||
487 | * |
||
488 | * @param callable $callable a PHP callable that contains DB queries which will make use of query cache. |
||
489 | * The signature of the callable is `function (Connection $db)`. |
||
490 | * @param int $duration the number of seconds that query results can remain valid in the cache. If this is |
||
491 | * not set, the value of [[queryCacheDuration]] will be used instead. |
||
492 | * Use 0 to indicate that the cached data will never expire. |
||
493 | * @param \yii\caching\Dependency $dependency the cache dependency associated with the cached query results. |
||
494 | * @return mixed the return result of the callable |
||
495 | * @throws \Throwable if there is any exception during query |
||
496 | * @see enableQueryCache |
||
497 | * @see queryCache |
||
498 | * @see noCache() |
||
499 | */ |
||
500 | public function cache(callable $callable, $duration = null, $dependency = null) |
||
512 | |||
513 | /** |
||
514 | * Disables query cache temporarily. |
||
515 | * |
||
516 | * Queries performed within the callable will not use query cache at all. For example, |
||
517 | * |
||
518 | * ```php |
||
519 | * $db->cache(function (Connection $db) { |
||
520 | * |
||
521 | * // ... queries that use query cache ... |
||
522 | * |
||
523 | * return $db->noCache(function (Connection $db) { |
||
524 | * // this query will not use query cache |
||
525 | * return $db->createCommand('SELECT * FROM customer WHERE id=1')->queryOne(); |
||
526 | * }); |
||
527 | * }); |
||
528 | * ``` |
||
529 | * |
||
530 | * @param callable $callable a PHP callable that contains DB queries which should not use query cache. |
||
531 | * The signature of the callable is `function (Connection $db)`. |
||
532 | * @return mixed the return result of the callable |
||
533 | * @throws \Throwable if there is any exception during query |
||
534 | * @see enableQueryCache |
||
535 | * @see queryCache |
||
536 | * @see cache() |
||
537 | */ |
||
538 | public function noCache(callable $callable) |
||
550 | |||
551 | /** |
||
552 | * Returns the current query cache information. |
||
553 | * This method is used internally by [[Command]]. |
||
554 | * @param int $duration the preferred caching duration. If null, it will be ignored. |
||
555 | * @param \yii\caching\Dependency $dependency the preferred caching dependency. If null, it will be ignored. |
||
556 | * @return array the current query cache information, or null if query cache is not enabled. |
||
557 | * @internal |
||
558 | */ |
||
559 | 3 | public function getQueryCacheInfo($duration, $dependency) |
|
588 | |||
589 | /** |
||
590 | * Establishes a DB connection. |
||
591 | * It does nothing if a DB connection has already been established. |
||
592 | * @throws Exception if connection fails |
||
593 | */ |
||
594 | 3 | public function open() |
|
636 | |||
637 | /** |
||
638 | * Closes the currently active DB connection. |
||
639 | * It does nothing if the connection is already closed. |
||
640 | */ |
||
641 | 3 | public function close() |
|
664 | |||
665 | /** |
||
666 | * Creates the PDO instance. |
||
667 | * This method is called by [[open]] to establish a DB connection. |
||
668 | * The default implementation will create a PHP PDO instance. |
||
669 | * You may override this method if the default PDO needs to be adapted for certain DBMS. |
||
670 | * @return PDO the pdo instance |
||
671 | */ |
||
672 | 3 | protected function createPdoInstance() |
|
698 | |||
699 | /** |
||
700 | * Initializes the DB connection. |
||
701 | * This method is invoked right after the DB connection is established. |
||
702 | * The default implementation turns on `PDO::ATTR_EMULATE_PREPARES` |
||
703 | * if [[emulatePrepare]] is true. |
||
704 | * It then triggers an [[EVENT_AFTER_OPEN]] event. |
||
705 | */ |
||
706 | 3 | protected function initConnection() |
|
714 | |||
715 | /** |
||
716 | * Creates a command for execution. |
||
717 | * @param string $sql the SQL statement to be executed |
||
718 | * @param array $params the parameters to be bound to the SQL statement |
||
719 | * @return Command the DB command |
||
720 | */ |
||
721 | 3 | public function createCommand($sql = null, $params = []) |
|
734 | |||
735 | /** |
||
736 | * Returns the currently active transaction. |
||
737 | * @return Transaction|null the currently active transaction. Null if no active transaction. |
||
738 | */ |
||
739 | 3 | public function getTransaction() |
|
743 | |||
744 | /** |
||
745 | * Starts a transaction. |
||
746 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
747 | * See [[Transaction::begin()]] for details. |
||
748 | * @return Transaction the transaction initiated |
||
749 | */ |
||
750 | public function beginTransaction($isolationLevel = null) |
||
761 | |||
762 | /** |
||
763 | * Executes callback provided in a transaction. |
||
764 | * |
||
765 | * @param callable $callback a valid PHP callback that performs the job. Accepts connection instance as parameter. |
||
766 | * @param string|null $isolationLevel The isolation level to use for this transaction. |
||
767 | * See [[Transaction::begin()]] for details. |
||
768 | * @throws \Throwable if there is any exception during query. In this case the transaction will be rolled back. |
||
769 | * @return mixed result of callback function |
||
770 | */ |
||
771 | public function transaction(callable $callback, $isolationLevel = null) |
||
788 | |||
789 | /** |
||
790 | * Rolls back given [[Transaction]] object if it's still active and level match. |
||
791 | * In some cases rollback can fail, so this method is fail safe. Exception thrown |
||
792 | * from rollback will be caught and just logged with [[\Yii::error()]]. |
||
793 | * @param Transaction $transaction Transaction object given from [[beginTransaction()]]. |
||
794 | * @param int $level Transaction level just after [[beginTransaction()]] call. |
||
795 | */ |
||
796 | private function rollbackTransactionOnLevel($transaction, $level) |
||
808 | |||
809 | /** |
||
810 | * Returns the schema information for the database opened by this connection. |
||
811 | * @return Schema the schema information for the database opened by this connection. |
||
812 | * @throws NotSupportedException if there is no support for the current driver type |
||
813 | */ |
||
814 | 3 | public function getSchema() |
|
830 | |||
831 | /** |
||
832 | * Returns the query builder for the current DB connection. |
||
833 | * @return QueryBuilder the query builder for the current DB connection. |
||
834 | */ |
||
835 | 3 | public function getQueryBuilder() |
|
839 | |||
840 | /** |
||
841 | * Can be used to set [[QueryBuilder]] configuration via Connection configuration array. |
||
842 | * |
||
843 | * @param iterable $config the [[QueryBuilder]] properties to be configured. |
||
844 | * @since 2.0.14 |
||
845 | */ |
||
846 | public function setQueryBuilder(iterable $config) |
||
853 | |||
854 | /** |
||
855 | * Obtains the schema information for the named table. |
||
856 | * @param string $name table name. |
||
857 | * @param bool $refresh whether to reload the table schema even if it is found in the cache. |
||
858 | * @return TableSchema table schema information. Null if the named table does not exist. |
||
859 | */ |
||
860 | public function getTableSchema($name, $refresh = false) |
||
864 | |||
865 | /** |
||
866 | * Returns the ID of the last inserted row or sequence value. |
||
867 | * @param string $sequenceName name of the sequence object (required by some DBMS) |
||
868 | * @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
||
869 | * @see http://php.net/manual/en/pdo.lastinsertid.php |
||
870 | */ |
||
871 | public function getLastInsertID($sequenceName = '') |
||
875 | |||
876 | /** |
||
877 | * Quotes a string value for use in a query. |
||
878 | * Note that if the parameter is not a string, it will be returned without change. |
||
879 | * @param string $value string to be quoted |
||
880 | * @return string the properly quoted string |
||
881 | * @see http://php.net/manual/en/pdo.quote.php |
||
882 | */ |
||
883 | public function quoteValue($value) |
||
887 | |||
888 | /** |
||
889 | * Quotes a table name for use in a query. |
||
890 | * If the table name contains schema prefix, the prefix will also be properly quoted. |
||
891 | * If the table name is already quoted or contains special characters including '(', '[[' and '{{', |
||
892 | * then this method will do nothing. |
||
893 | * @param string $name table name |
||
894 | * @return string the properly quoted table name |
||
895 | */ |
||
896 | 3 | public function quoteTableName($name) |
|
897 | { |
||
898 | 3 | return $this->getSchema()->quoteTableName($name); |
|
899 | } |
||
900 | |||
901 | /** |
||
902 | * Quotes a column name for use in a query. |
||
903 | * If the column name contains prefix, the prefix will also be properly quoted. |
||
904 | * If the column name is already quoted or contains special characters including '(', '[[' and '{{', |
||
905 | * then this method will do nothing. |
||
906 | * @param string $name column name |
||
907 | * @return string the properly quoted column name |
||
908 | */ |
||
909 | public function quoteColumnName($name) |
||
913 | |||
914 | /** |
||
915 | * Processes a SQL statement by quoting table and column names that are enclosed within double brackets. |
||
916 | * Tokens enclosed within double curly brackets are treated as table names, while |
||
917 | * tokens enclosed within double square brackets are column names. They will be quoted accordingly. |
||
918 | * Also, the percentage character "%" at the beginning or ending of a table name will be replaced |
||
919 | * with [[tablePrefix]]. |
||
920 | * @param string $sql the SQL to be quoted |
||
921 | * @return string the quoted SQL |
||
922 | */ |
||
923 | 3 | public function quoteSql($sql) |
|
937 | |||
938 | /** |
||
939 | * Returns the name of the DB driver. Based on the the current [[dsn]], in case it was not set explicitly |
||
940 | * by an end user. |
||
941 | * @return string name of the DB driver |
||
942 | */ |
||
943 | 3 | public function getDriverName() |
|
955 | |||
956 | /** |
||
957 | * Changes the current driver name. |
||
958 | * @param string $driverName name of the DB driver |
||
959 | */ |
||
960 | public function setDriverName($driverName) |
||
964 | |||
965 | /** |
||
966 | * Returns a server version as a string comparable by [[\version_compare()]]. |
||
967 | * @return string server version as a string. |
||
968 | * @since 2.0.14 |
||
969 | */ |
||
970 | public function getServerVersion() |
||
974 | |||
975 | /** |
||
976 | * Returns the PDO instance for the currently active slave connection. |
||
977 | * When [[enableSlaves]] is true, one of the slaves will be used for read queries, and its PDO instance |
||
978 | * will be returned by this method. |
||
979 | * @param bool $fallbackToMaster whether to return a master PDO in case none of the slave connections is available. |
||
980 | * @return PDO the PDO instance for the currently active slave connection. `null` is returned if no slave connection |
||
981 | * is available and `$fallbackToMaster` is false. |
||
982 | */ |
||
983 | 3 | public function getSlavePdo($fallbackToMaster = true) |
|
992 | |||
993 | /** |
||
994 | * Returns the PDO instance for the currently active master connection. |
||
995 | * This method will open the master DB connection and then return [[pdo]]. |
||
996 | * @return PDO the PDO instance for the currently active master connection. |
||
997 | */ |
||
998 | 3 | public function getMasterPdo() |
|
1003 | |||
1004 | /** |
||
1005 | * Returns the currently active slave connection. |
||
1006 | * If this method is called for the first time, it will try to open a slave connection when [[enableSlaves]] is true. |
||
1007 | * @param bool $fallbackToMaster whether to return a master connection in case there is no slave connection available. |
||
1008 | * @return Connection the currently active slave connection. `null` is returned if there is no slave available and |
||
1009 | * `$fallbackToMaster` is false. |
||
1010 | */ |
||
1011 | 3 | public function getSlave($fallbackToMaster = true) |
|
1023 | |||
1024 | /** |
||
1025 | * Returns the currently active master connection. |
||
1026 | * If this method is called for the first time, it will try to open a master connection. |
||
1027 | * @return Connection the currently active master connection. `null` is returned if there is no master available. |
||
1028 | * @since 2.0.11 |
||
1029 | */ |
||
1030 | public function getMaster() |
||
1040 | |||
1041 | /** |
||
1042 | * Executes the provided callback by using the master connection. |
||
1043 | * |
||
1044 | * This method is provided so that you can temporarily force using the master connection to perform |
||
1045 | * DB operations even if they are read queries. For example, |
||
1046 | * |
||
1047 | * ```php |
||
1048 | * $result = $db->useMaster(function ($db) { |
||
1049 | * return $db->createCommand('SELECT * FROM user LIMIT 1')->queryOne(); |
||
1050 | * }); |
||
1051 | * ``` |
||
1052 | * |
||
1053 | * @param callable $callback a PHP callable to be executed by this method. Its signature is |
||
1054 | * `function (Connection $db)`. Its return value will be returned by this method. |
||
1055 | * @return mixed the return value of the callback |
||
1056 | * @throws \Throwable if there is any exception thrown from the callback |
||
1057 | */ |
||
1058 | public function useMaster(callable $callback) |
||
1076 | |||
1077 | /** |
||
1078 | * Opens the connection to a server in the pool. |
||
1079 | * This method implements the load balancing among the given list of the servers. |
||
1080 | * Connections will be tried in random order. |
||
1081 | * @param array $pool the list of connection configurations in the server pool |
||
1082 | * @param array $sharedConfig the configuration common to those given in `$pool`. |
||
1083 | * @return Connection the opened DB connection, or `null` if no server is available |
||
1084 | * @throws InvalidConfigException if a configuration does not specify "dsn" |
||
1085 | */ |
||
1086 | 3 | protected function openFromPool(array $pool, array $sharedConfig) |
|
1091 | |||
1092 | /** |
||
1093 | * Opens the connection to a server in the pool. |
||
1094 | * This method implements the load balancing among the given list of the servers. |
||
1095 | * Connections will be tried in sequential order. |
||
1096 | * @param array $pool the list of connection configurations in the server pool |
||
1097 | * @param array $sharedConfig the configuration common to those given in `$pool`. |
||
1098 | * @return Connection the opened DB connection, or `null` if no server is available |
||
1099 | * @throws InvalidConfigException if a configuration does not specify "dsn" |
||
1100 | * @since 2.0.11 |
||
1101 | */ |
||
1102 | 3 | protected function openFromPoolSequentially(array $pool, array $sharedConfig) |
|
1143 | |||
1144 | /** |
||
1145 | * Build the Data Source Name or DSN |
||
1146 | * @param array $config the DSN configurations |
||
1147 | * @return string the formated DSN |
||
1148 | * @throws InvalidConfigException if 'driver' key was not defined |
||
1149 | */ |
||
1150 | private function buildDSN(array $config) |
||
1165 | |||
1166 | /** |
||
1167 | * Close the connection before serializing. |
||
1168 | * @return array |
||
1169 | */ |
||
1170 | public function __sleep() |
||
1182 | |||
1183 | /** |
||
1184 | * Reset the connection after cloning. |
||
1185 | */ |
||
1186 | public function __clone() |
||
1199 | } |
||
1200 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: