Skip to content

Commit e52a533

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 517493a commit e52a533

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1010
use Doctrine\DBAL\Driver\Statement;
1111
use Doctrine\DBAL\ParameterType;
12+
use LogicException;
13+
use function method_exists;
14+
use function sprintf;
1215

1316
/**
1417
* This is a simple implementation of the {@see ServerInfoAwareConnection} interface
@@ -161,4 +164,13 @@ public function getWrappedConnection(): Connection
161164
{
162165
return $this->decoratedConnection->getWrappedConnection();
163166
}
167+
168+
public function getNativeConnection()
169+
{
170+
if (!method_exists($this->decoratedConnection, 'getNativeConnection')) {
171+
throw new LogicException(sprintf('The decoratedConnection %s does not support accessing the native connection.', \get_class($this->decoratedConnection)));
172+
}
173+
174+
return $this->decoratedConnection->getNativeConnection();
175+
}
164176
}

0 commit comments

Comments
 (0)