88use PDO ;
99use PDOException ;
1010use PDOStatement ;
11- use Psr \Log \LoggerInterface ;
1211use Psr \Log \LogLevel ;
1312use Throwable ;
1413use Yiisoft \Cache \CacheInterface ;
1514use Yiisoft \Cache \Dependency \Dependency ;
16- use Yiisoft \Db \Cache \QueryCache ;
1715use Yiisoft \Db \Connection \ConnectionInterface ;
1816use Yiisoft \Db \Data \DataReader ;
1917use Yiisoft \Db \Exception \Exception ;
2018use Yiisoft \Db \Expression \Expression ;
2119use Yiisoft \Db \Pdo \PdoValue ;
2220use Yiisoft \Db \Query \Query ;
23- use Yiisoft \Profiler \ProfilerInterface ;
2421
2522use function array_map ;
2623use function call_user_func_array ;
@@ -110,27 +107,16 @@ class Command
110107 */
111108 private $ retryHandler ;
112109
113- private ProfilerInterface $ profiler ;
114- private LoggerInterface $ logger ;
115110 private ConnectionInterface $ db ;
116111 private ?PDOStatement $ pdoStatement = null ;
117112 private int $ fetchMode = PDO ::FETCH_ASSOC ;
118113 private ?int $ queryCacheDuration = null ;
119114 private ?Dependency $ queryCacheDependency = null ;
120- private QueryCache $ queryCache ;
121-
122- public function __construct (
123- ProfilerInterface $ profiler ,
124- LoggerInterface $ logger ,
125- ConnectionInterface $ db ,
126- QueryCache $ queryCache ,
127- ?string $ sql
128- ) {
115+
116+ public function __construct (ConnectionInterface $ db , ?string $ sql )
117+ {
129118 $ this ->db = $ db ;
130- $ this ->logger = $ logger ;
131- $ this ->profiler = $ profiler ;
132119 $ this ->sql = $ sql ;
133- $ this ->queryCache = $ queryCache ;
134120 }
135121
136122 /**
@@ -145,7 +131,9 @@ public function __construct(
145131 */
146132 public function cache (?int $ duration = null , Dependency $ dependency = null ): self
147133 {
148- $ this ->queryCacheDuration = $ duration ?? $ this ->queryCache ->getDuration ();
134+ $ queryCache = $ this ->db ->getQueryCache ();
135+
136+ $ this ->queryCacheDuration = $ duration ?? $ queryCache ->getDuration ();
149137 $ this ->queryCacheDependency = $ dependency ;
150138
151139 return $ this ;
@@ -1244,6 +1232,7 @@ public function dropView(string $viewName): self
12441232 */
12451233 public function execute (): int
12461234 {
1235+ $ profiler = $ this ->db ->getProfiler ();
12471236 $ sql = $ this ->getSql ();
12481237
12491238 [$ profile , $ rawSql ] = $ this ->logQuery (__METHOD__ );
@@ -1256,22 +1245,22 @@ public function execute(): int
12561245
12571246 try {
12581247 if ($ this ->db ->isProfilingEnabled ()) {
1259- $ this -> profiler ->begin ((string ) $ rawSql , [__METHOD__ ]);
1248+ $ profiler ->begin ((string ) $ rawSql , [__METHOD__ ]);
12601249 }
12611250
12621251 $ this ->internalExecute ($ rawSql );
12631252 $ n = $ this ->pdoStatement ->rowCount ();
12641253
12651254 if ($ this ->db ->isProfilingEnabled ()) {
1266- $ this -> profiler ->end ((string ) $ rawSql , [__METHOD__ ]);
1255+ $ profiler ->end ((string ) $ rawSql , [__METHOD__ ]);
12671256 }
12681257
12691258 $ this ->refreshTableSchema ();
12701259
12711260 return $ n ;
12721261 } catch (Exception $ e ) {
12731262 if ($ this ->db ->isProfilingEnabled ()) {
1274- $ this -> profiler ->end ((string ) $ rawSql , [__METHOD__ ]);
1263+ $ profiler ->end ((string ) $ rawSql , [__METHOD__ ]);
12751264 }
12761265
12771266 throw $ e ;
@@ -1289,9 +1278,11 @@ public function execute(): int
12891278 */
12901279 protected function logQuery (string $ category ): array
12911280 {
1281+ $ logger = $ this ->db ->getLogger ();
1282+
12921283 if ($ this ->db ->isLoggingEnabled ()) {
12931284 $ rawSql = $ this ->getRawSql ();
1294- $ this -> logger ->log (LogLevel::INFO , $ rawSql , [$ category ]);
1285+ $ logger ->log (LogLevel::INFO , $ rawSql , [$ category ]);
12951286 }
12961287
12971288 if (!$ this ->db ->isProfilingEnabled ()) {
@@ -1316,10 +1307,14 @@ protected function logQuery(string $category): array
13161307 */
13171308 protected function queryInternal (string $ method , $ fetchMode = null )
13181309 {
1310+ $ logger = $ this ->db ->getLogger ();
1311+ $ profiler = $ this ->db ->getProfiler ();
1312+ $ queryCache = $ this ->db ->getqueryCache ();
1313+
13191314 [, $ rawSql ] = $ this ->logQuery (__CLASS__ . '::query ' );
13201315
13211316 if ($ method !== '' ) {
1322- $ info = $ this -> queryCache ->info (
1317+ $ info = $ queryCache ->info (
13231318 $ this ->queryCacheDuration ,
13241319 $ this ->queryCacheDependency
13251320 );
@@ -1336,7 +1331,7 @@ protected function queryInternal(string $method, $fetchMode = null)
13361331
13371332 if (is_array ($ result ) && isset ($ result [0 ])) {
13381333 if ($ this ->db ->isLoggingEnabled ()) {
1339- $ this -> logger ->log (
1334+ $ logger ->log (
13401335 LogLevel::DEBUG ,
13411336 'Query result served from cache ' ,
13421337 [__CLASS__ . '::query ' ]
@@ -1352,7 +1347,7 @@ protected function queryInternal(string $method, $fetchMode = null)
13521347
13531348 try {
13541349 if ($ this ->db ->isProfilingEnabled ()) {
1355- $ this -> profiler ->begin ((string ) $ rawSql , [__CLASS__ . '::query ' ]);
1350+ $ profiler ->begin ((string ) $ rawSql , [__CLASS__ . '::query ' ]);
13561351 }
13571352
13581353 $ this ->internalExecute ($ rawSql );
@@ -1370,11 +1365,11 @@ protected function queryInternal(string $method, $fetchMode = null)
13701365 }
13711366
13721367 if ($ this ->db ->isProfilingEnabled ()) {
1373- $ this -> profiler ->end ((string ) $ rawSql , [__CLASS__ . '::query ' ]);
1368+ $ profiler ->end ((string ) $ rawSql , [__CLASS__ . '::query ' ]);
13741369 }
13751370 } catch (Exception $ e ) {
13761371 if ($ this ->db ->isProfilingEnabled ()) {
1377- $ this -> profiler ->end ((string ) $ rawSql , [__CLASS__ . '::query ' ]);
1372+ $ profiler ->end ((string ) $ rawSql , [__CLASS__ . '::query ' ]);
13781373 }
13791374
13801375 throw $ e ;
@@ -1389,7 +1384,7 @@ protected function queryInternal(string $method, $fetchMode = null)
13891384 );
13901385
13911386 if ($ this ->db ->isLoggingEnabled ()) {
1392- $ this -> logger ->log (
1387+ $ logger ->log (
13931388 LogLevel::DEBUG ,
13941389 'Saved query result in cache ' ,
13951390 [__CLASS__ . '::query ' ]
0 commit comments