Skip to content

Commit fc29cf2

Browse files
authored
Cache created decorated connection (cakephp#2191)
1 parent 695068c commit fc29cf2

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

src/Phinx/Db/Adapter/MysqlAdapter.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Phinx\Db\Adapter;
99

10-
use Cake\Database\Connection;
1110
use Cake\Database\Driver\Mysql as MysqlDriver;
1211
use InvalidArgumentException;
1312
use PDO;
@@ -1531,7 +1530,7 @@ public function getColumnTypes(): array
15311530
/**
15321531
* @inheritDoc
15331532
*/
1534-
public function getDecoratedConnection(): Connection
1533+
protected function getDecoratedConnectionConfig(): array
15351534
{
15361535
$options = $this->getOptions();
15371536
$options = [
@@ -1541,9 +1540,6 @@ public function getDecoratedConnection(): Connection
15411540
'quoteIdentifiers' => true,
15421541
] + $options;
15431542

1544-
$driver = new MysqlDriver($options);
1545-
$driver->setConnection($this->connection);
1546-
1547-
return new Connection(['driver' => $driver] + $options);
1543+
return ['driver' => new MysqlDriver($options)] + $options;
15481544
}
15491545
}

src/Phinx/Db/Adapter/PdoAdapter.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ abstract class PdoAdapter extends AbstractAdapter implements DirectActionInterfa
4949
*/
5050
protected $connection;
5151

52+
/**
53+
* @var \Cake\Database\Connection|null
54+
*/
55+
protected $decoratedConnection;
56+
5257
/**
5358
* Writes a message to stdout if verbose output is on
5459
*
@@ -198,13 +203,34 @@ public function execute(string $sql, array $params = []): int
198203
return $result ? $stmt->rowCount() : $result;
199204
}
200205

206+
/**
207+
* Returns the config for a new decorated connection.
208+
*
209+
* @return array
210+
*/
211+
abstract protected function getDecoratedConnectionConfig(): array;
212+
201213
/**
202214
* Returns the Cake\Database connection object using the same underlying
203215
* PDO object as this connection.
204216
*
205217
* @return \Cake\Database\Connection
206218
*/
207-
abstract public function getDecoratedConnection(): Connection;
219+
public function getDecoratedConnection(): Connection
220+
{
221+
if (isset($this->decoratedConnection)) {
222+
return $this->decoratedConnection;
223+
}
224+
225+
$config = $this->getDecoratedConnectionConfig();
226+
if (!isset($config['driver'])) {
227+
throw new RuntimeException('Decorated connection config is missing the driver.');
228+
}
229+
230+
$config['driver']->setConnection($this->connection);
231+
232+
return $this->decoratedConnection = new Connection($config);
233+
}
208234

209235
/**
210236
* @inheritDoc

src/Phinx/Db/Adapter/PostgresAdapter.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Phinx\Db\Adapter;
99

10-
use Cake\Database\Connection;
1110
use Cake\Database\Driver\Postgres as PostgresDriver;
1211
use InvalidArgumentException;
1312
use PDO;
@@ -1566,7 +1565,7 @@ public function castToBool($value)
15661565
/**
15671566
* @inheritDoc
15681567
*/
1569-
public function getDecoratedConnection(): Connection
1568+
protected function getDecoratedConnectionConfig(): array
15701569
{
15711570
$options = $this->getOptions();
15721571
$options = [
@@ -1576,11 +1575,7 @@ public function getDecoratedConnection(): Connection
15761575
'quoteIdentifiers' => true,
15771576
] + $options;
15781577

1579-
$driver = new PostgresDriver($options);
1580-
1581-
$driver->setConnection($this->connection);
1582-
1583-
return new Connection(['driver' => $driver] + $options);
1578+
return ['driver' => new PostgresDriver($options)] + $options;
15841579
}
15851580

15861581
/**

src/Phinx/Db/Adapter/SQLiteAdapter.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Phinx\Db\Adapter;
99

1010
use BadMethodCallException;
11-
use Cake\Database\Connection;
1211
use Cake\Database\Driver\Sqlite as SqliteDriver;
1312
use InvalidArgumentException;
1413
use PDO;
@@ -1932,7 +1931,7 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey): string
19321931
/**
19331932
* @inheritDoc
19341933
*/
1935-
public function getDecoratedConnection(): Connection
1934+
protected function getDecoratedConnectionConfig(): array
19361935
{
19371936
$options = $this->getOptions();
19381937
$options['quoteIdentifiers'] = true;
@@ -1949,9 +1948,6 @@ public function getDecoratedConnection(): Connection
19491948
throw new RuntimeException('You need to connect first.');
19501949
}
19511950

1952-
$driver = new SqliteDriver($options);
1953-
$driver->setConnection($this->connection);
1954-
1955-
return new Connection(['driver' => $driver] + $options);
1951+
return ['driver' => new SqliteDriver($options)] + $options;
19561952
}
19571953
}

src/Phinx/Db/Adapter/SqlServerAdapter.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Phinx\Db\Adapter;
99

1010
use BadMethodCallException;
11-
use Cake\Database\Connection;
1211
use Cake\Database\Driver\Sqlserver as SqlServerDriver;
1312
use InvalidArgumentException;
1413
use PDO;
@@ -1359,7 +1358,7 @@ public function migrated(MigrationInterface $migration, string $direction, strin
13591358
/**
13601359
* @inheritDoc
13611360
*/
1362-
public function getDecoratedConnection(): Connection
1361+
protected function getDecoratedConnectionConfig(): array
13631362
{
13641363
$options = $this->getOptions();
13651364
$options = [
@@ -1369,9 +1368,6 @@ public function getDecoratedConnection(): Connection
13691368
'quoteIdentifiers' => true,
13701369
] + $options;
13711370

1372-
$driver = new SqlServerDriver($options);
1373-
$driver->setConnection($this->connection);
1374-
1375-
return new Connection(['driver' => $driver] + $options);
1371+
return ['driver' => new SqlServerDriver($options)] + $options;
13761372
}
13771373
}

0 commit comments

Comments
 (0)