Skip to content

Commit cc3a9e2

Browse files
authored
Trace transaction events on PDOCollector (php-debugbar#785)
1 parent 0d4d0cd commit cc3a9e2

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/DebugBar/DataCollector/PDO/PDOCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ protected function collectPDO(TraceablePDO $pdo, ?TimeDataCollector $timeCollect
149149
foreach ($pdo->getExecutedStatements() as $stmt) {
150150
$stmts[] = array(
151151
'sql' => $this->renderSqlWithParams ? $stmt->getSqlWithParams($this->sqlQuotationChar) : $stmt->getSql(),
152+
'type' => $stmt->getQueryType(),
152153
'row_count' => $stmt->getRowCount(),
153154
'stmt_id' => $stmt->getPreparedId(),
154155
'prepared_stmt' => $stmt->getSql(),

src/DebugBar/DataCollector/PDO/TraceablePDO.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function __construct(PDO $pdo)
3131
*/
3232
public function beginTransaction() : bool
3333
{
34+
$this->addPdoEvent('Begin Transaction');
35+
3436
return $this->pdo->beginTransaction();
3537
}
3638

@@ -42,6 +44,8 @@ public function beginTransaction() : bool
4244
*/
4345
public function commit() : bool
4446
{
47+
$this->addPdoEvent('Commit Transaction');
48+
4549
return $this->pdo->commit();
4650
}
4751

@@ -180,6 +184,8 @@ public function quote($string, $parameter_type = PDO::PARAM_STR)
180184
*/
181185
public function rollBack() : bool
182186
{
187+
$this->addPdoEvent('Rollback Transaction');
188+
183189
return $this->pdo->rollBack();
184190
}
185191

@@ -241,6 +247,21 @@ public function addExecutedStatement(TracedStatement $stmt) : void
241247
$this->executedStatements[] = $stmt;
242248
}
243249

250+
/**
251+
* Adds a PDO event
252+
*
253+
* @param string $event
254+
*/
255+
public function addPdoEvent(string $event) : void
256+
{
257+
$stmt = new TracedStatement($event);
258+
$stmt->setQueryType('transaction');
259+
$stmt->start();
260+
$stmt->end();
261+
262+
$this->executedStatements[] = $stmt;
263+
}
264+
244265
/**
245266
* Returns the accumulated execution time of statements
246267
*

src/DebugBar/DataCollector/PDO/TracedStatement.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class TracedStatement
99
{
1010
protected $sql;
1111

12+
protected $type;
13+
1214
protected $rowCount;
1315

1416
protected $parameters;
@@ -41,6 +43,14 @@ public function __construct(string $sql, array $params = [], ?string $preparedId
4143
$this->preparedId = $preparedId;
4244
}
4345

46+
/**
47+
* @param string $type
48+
*/
49+
public function setQueryType(string $type) : void
50+
{
51+
$this->type = $type;
52+
}
53+
4454
/**
4555
* @param null $startTime
4656
* @param null $startMemory
@@ -277,4 +287,14 @@ public function getErrorMessage() : string
277287
{
278288
return $this->exception !== null ? $this->exception->getMessage() : '';
279289
}
290+
291+
/**
292+
* Returns the query type
293+
*
294+
* @return string
295+
*/
296+
public function getQueryType() : string
297+
{
298+
return $this->type !== null ? $this->type : '';
299+
}
280300
}

0 commit comments

Comments
 (0)