Skip to content

Commit 492647c

Browse files
committed
Add support for accessing dbal native connection
Dbal V3 contains: ```php /** * Connection interface. * Driver connections must implement this interface. * * @method resource|object getNativeConnection() */ interface Connection ```
1 parent dd37786 commit 492647c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/Tracing/Doctrine/DBAL/TracingDriverConnection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Driver\Result;
99
use Doctrine\DBAL\Driver\Statement;
1010
use Doctrine\DBAL\ParameterType;
11+
use LogicException;
1112
use Sentry\State\HubInterface;
1213
use Sentry\Tracing\SpanContext;
1314

@@ -193,6 +194,15 @@ public function getWrappedConnection(): DriverConnectionInterface
193194
return $this->decoratedConnection;
194195
}
195196

197+
public function getNativeConnection()
198+
{
199+
if (!method_exists($this->decoratedConnection, 'getNativeConnection')) {
200+
throw new LogicException(sprintf('The decoratedConnection %s does not support accessing the native connection.', \get_class($this->decoratedConnection)));
201+
}
202+
203+
return $this->decoratedConnection->getNativeConnection();
204+
}
205+
196206
/**
197207
* @phpstan-template T
198208
*

src/Tracing/Doctrine/DBAL/TracingDriverConnectionInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@
99
interface TracingDriverConnectionInterface extends Connection
1010
{
1111
public function getWrappedConnection(): Connection;
12+
13+
/**
14+
* @return resource|object
15+
*/
16+
public function getNativeConnection();
1217
}

src/Tracing/Doctrine/DBAL/TracingServerInfoAwareDriverConnection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1010
use Doctrine\DBAL\Driver\Statement;
1111
use Doctrine\DBAL\ParameterType;
12+
use LogicException;
13+
14+
use function method_exists;
15+
use function sprintf;
1216

1317
/**
1418
* This is a simple implementation of the {@see ServerInfoAwareConnection} interface
@@ -161,4 +165,13 @@ public function getWrappedConnection(): Connection
161165
{
162166
return $this->decoratedConnection->getWrappedConnection();
163167
}
168+
169+
public function getNativeConnection()
170+
{
171+
if (!method_exists($this->decoratedConnection, 'getNativeConnection')) {
172+
throw new LogicException(sprintf('The decoratedConnection %s does not support accessing the native connection.', \get_class($this->decoratedConnection)));
173+
}
174+
175+
return $this->decoratedConnection->getNativeConnection();
176+
}
164177
}

0 commit comments

Comments
 (0)