Skip to content

Commit 5422ad5

Browse files
committed
Update tdbm for Symfony 6 and doctrine v3
1 parent fde7479 commit 5422ad5

26 files changed

+1026
-764
lines changed

composer.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,21 @@
1919
],
2020
"require" : {
2121
"php": "^7.4 || ^8.0",
22-
"mouf/magic-query": "^1.4.3",
22+
"mouf/magic-query": "2.0.x-dev",
2323
"mouf/schema-analyzer": "^2.0",
2424
"doctrine/dbal": "^3.0",
2525
"psr/log": "^1 || ^2 || ^3",
2626
"doctrine/inflector": "^1.4.3 || ^2",
2727
"mouf/classname-mapper": "~1.0",
2828
"doctrine/cache": "^1.6",
2929
"greenlion/php-sql-parser": "^4.3.0",
30-
"symfony/console": "^2 || ^3 || ^4 || ^5",
30+
"symfony/console": "^2 || ^3 || ^4 || ^5 || ^6",
3131
"mouf/utils.log.psr.multi-logger": "^1.0",
32-
"symfony/filesystem": "^2.7 || ^3 || ^4 || ^5",
32+
"symfony/filesystem": "^2.7 || ^3 || ^4 || ^5 || ^6",
3333
"ramsey/uuid": "^3.7 || ^4.0",
3434
"doctrine/annotations": "^1.10",
3535
"laminas/laminas-code": "^4.7",
3636
"psr/container": "^1 || ^2",
37-
"brain-diminished/schema-version-control": "^1.0.5",
3837
"ext-PDO": "*",
3938
"ext-json": "*",
4039
"ext-hash": "*",
@@ -47,7 +46,7 @@
4746
"wa72/simplelogger": "^1.0",
4847
"friendsofphp/php-cs-fixer": "^3.11",
4948
"symfony/process": "^3 || ^4 || ^5",
50-
"thecodingmachine/tdbm-fluid-schema-builder": "^1.0.0",
49+
"thecodingmachine/tdbm-fluid-schema-builder": "^v2.0.0",
5150
"phpstan/phpstan": "^0.12.81",
5251
"thecodingmachine/phpstan-strict-rules": "^0.12.1",
5352
"bamarni/composer-bin-plugin": "^1.4.1",

src/Configuration.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ public function getCodeGeneratorListener(): CodeGeneratorListenerInterface
221221
*/
222222
private function getConnectionUniqueId(): string
223223
{
224-
return hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName());
224+
$params = $this->connection->getParams();
225+
$host = $params['host'] ?? null;
226+
$port = $params['port'] ?? null;
227+
return hash('md4', $host.'-'.$port.'-'.$this->connection->getDatabase().'-'.$this->connection->getDatabasePlatform()->getName());
225228
}
226229

227230
/**

src/DbRow.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function _dbLoadIfNotLoaded(): void
211211
$this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform());
212212
}
213213

214-
$result->closeCursor();
214+
$result->free();
215215

216216
$this->status = TDBMObjectStateEnum::STATE_LOADED;
217217
}

src/InnerResultArray.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private function toIndex($offset): void
9292
if ($offset < 0 || filter_var($offset, FILTER_VALIDATE_INT) === false) {
9393
throw new TDBMInvalidOffsetException('Trying to access result set using offset "'.$offset.'". An offset must be a positive integer.');
9494
}
95-
if ($this->statement === null) {
95+
if ($this->result === null) {
9696
$this->executeQuery();
9797
}
9898
while (!isset($this->results[$offset])) {

src/InnerResultIterator.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace TheCodingMachine\TDBM;
66

7-
use Doctrine\DBAL\Driver\ResultStatement;
87
use Doctrine\DBAL\Platforms\AbstractPlatform;
98
use Doctrine\DBAL\Platforms\MySqlPlatform;
9+
use Doctrine\DBAL\Result;
1010
use Doctrine\DBAL\Statement;
1111
use Mouf\Database\MagicQuery;
1212
use Psr\Log\LoggerInterface;
@@ -35,8 +35,8 @@
3535
*/
3636
class InnerResultIterator implements \Iterator, InnerResultIteratorInterface
3737
{
38-
/** @var ResultStatement|Statement */
39-
protected $statement;
38+
/** @var Result */
39+
protected $result;
4040

4141
/** @var bool */
4242
protected $fetchStarted = false;
@@ -117,7 +117,7 @@ protected function executeQuery(): void
117117

118118
$this->logger->debug('Running SQL request: '.$sql);
119119

120-
$this->statement = $this->tdbmService->getConnection()->executeQuery($sql, $this->parameters, DbalUtils::generateTypes($this->parameters));
120+
$this->result = $this->tdbmService->getConnection()->executeQuery($sql, $this->parameters, DbalUtils::generateTypes($this->parameters));
121121

122122
$this->fetchStarted = true;
123123
}
@@ -135,8 +135,8 @@ public function count()
135135

136136
if ($this->fetchStarted && $this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
137137
// Optimisation: we don't need a separate "count" SQL request in MySQL.
138-
assert($this->statement instanceof Statement);
139-
$this->count = (int)$this->statement->rowCount();
138+
assert($this->result instanceof Result);
139+
$this->count = (int)$this->result->rowCount();
140140
return $this->count;
141141
}
142142
return $this->getRowCountViaSqlQuery();
@@ -152,7 +152,7 @@ private function getRowCountViaSqlQuery(): int
152152

153153
$this->logger->debug('Running count SQL request: '.$countSql);
154154

155-
$this->count = (int) $this->tdbmService->getConnection()->fetchColumn($countSql, $this->parameters, 0, DbalUtils::generateTypes($this->parameters));
155+
$this->count = (int) $this->tdbmService->getConnection()->fetchOne($countSql, $this->parameters, DbalUtils::generateTypes($this->parameters));
156156
return $this->count;
157157
}
158158

@@ -182,7 +182,7 @@ public function key()
182182
*/
183183
public function next(): void
184184
{
185-
$row = $this->statement->fetch(\PDO::FETCH_ASSOC);
185+
$row = $this->result->fetchAssociative();
186186
if ($row) {
187187
/** @var array<string, array<string, array<string, mixed>>> $beansData array<tablegroup, array<table, array<column, value>>>*/
188188
$beansData = [];

src/QueryFactory/FindObjectsFromRawSqlQueryFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private function formatSelect(array $baseSelect): array
252252
]
253253
];
254254
$formattedSelect[] = $astColumn;
255-
if (in_array($columnName, $pkColumns)) {
255+
if (array_key_exists($columnName, $pkColumns)) {
256256
$formattedCountSelect[] = $astColumn;
257257
}
258258
$columnDescriptors[$alias] = [
@@ -322,7 +322,7 @@ private function generateSimpleSqlCount(array $parsedSql): array
322322
if ($this->isDistinctQuery($parsedSql)) {
323323
// Only MySQL can do DISTINCT counts.
324324
// Other databases should wrap the query
325-
if (!$this->tdbmService->getConnection()->getSchemaManager()->getDatabasePlatform() instanceof MySqlPlatform) {
325+
if (!$this->tdbmService->getConnection()->getDatabasePlatform() instanceof MySqlPlatform) {
326326
return $this->generateWrappedSqlCount($parsedSql);
327327
}
328328

src/ResultIterator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function executeCountQuery(): void
110110
{
111111
$sql = $this->magicQuery->buildPreparedStatement($this->queryFactory->getMagicSqlCount(), $this->parameters);
112112
$this->logger->debug('Running count query: '.$sql);
113-
$this->totalCount = (int) $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters, 0, DbalUtils::generateTypes($this->parameters));
113+
$this->totalCount = (int) $this->tdbmService->getConnection()->fetchOne($sql, $this->parameters, DbalUtils::generateTypes($this->parameters));
114114
}
115115

116116
/**

src/SchemaLockFileDumper.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace TheCodingMachine\TDBM;
44

5-
use BrainDiminished\SchemaVersionControl\SchemaVersionControlService;
65
use Doctrine\Common\Cache\Cache;
76
use Doctrine\DBAL\Connection;
87
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
98
use Doctrine\DBAL\Schema\Schema;
109
use Doctrine\DBAL\Schema\Table;
1110
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
11+
use TheCodingMachine\TDBM\SchemaVersionControl\SchemaVersionControlService;
1212
use TheCodingMachine\TDBM\Utils\ColumnsReorderer;
1313
use TheCodingMachine\TDBM\Utils\ImmutableCaster;
1414

@@ -68,7 +68,10 @@ public function __construct(Connection $connection, Cache $cache, string $lockFi
6868
public function getCachePrefix(): string
6969
{
7070
if ($this->cachePrefix === null) {
71-
$this->cachePrefix = hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName());
71+
$params = $this->connection->getParams();
72+
$host = $params['host'] ?? null;
73+
$port = $params['port'] ?? null;
74+
$this->cachePrefix = hash('md4', $host.'-'.$port.'-'.$this->connection->getDatabase().'-'.$this->connection->getDatabasePlatform()?->getName());
7275
}
7376

7477
return $this->cachePrefix;
+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
namespace TheCodingMachine\TDBM\SchemaVersionControl;
4+
5+
use Doctrine\DBAL\Schema\Column;
6+
use Doctrine\DBAL\Schema\Schema;
7+
use Doctrine\DBAL\Schema\Table;
8+
9+
/**
10+
* Database schema builder.
11+
*
12+
* Given a deep associative array supposed to describe a database schema, SchemaBuilder::build will construct a
13+
* corresponding Schema object.
14+
*/
15+
class SchemaBuilder
16+
{
17+
/** @var array */
18+
protected $schemaDesc;
19+
20+
/**
21+
* Build an array descriptor into a Schema object
22+
* @param array $schemaDesc
23+
* @return Schema
24+
*/
25+
public function build(array $schemaDesc): Schema
26+
{
27+
$this->schemaDesc = $schemaDesc;
28+
$schema = new Schema();
29+
foreach ($schemaDesc['tables'] as $name => $tableDesc) {
30+
$table = $schema->createTable($name);
31+
$this->buildTable($tableDesc, $table);
32+
}
33+
return $schema;
34+
}
35+
36+
protected function buildTable(array $tableDesc, Table $table)
37+
{
38+
$pk_columns = [];
39+
40+
if (isset($tableDesc['comment'])) {
41+
$table->addOption("comment", $tableDesc['comment']);
42+
}
43+
44+
foreach ($tableDesc['columns'] as $columnName => $columnDesc) {
45+
if (!is_array($columnDesc)) {
46+
$columnDesc = ['type' => $columnDesc];
47+
}
48+
if (isset($columnDesc['primary_key'])
49+
&& $columnDesc['primary_key']) {
50+
$pk_columns[] = $columnName;
51+
}
52+
$column = $table->addColumn($columnName, $columnDesc['type']);
53+
$this->buildColumn($columnDesc, $column);
54+
}
55+
if (isset($tableDesc['indexes'])) {
56+
foreach ($tableDesc['indexes'] as $indexName => $indexDesc) {
57+
$this->buildIndex($indexDesc, $table, $indexName);
58+
}
59+
}
60+
if (!empty($pk_columns)) {
61+
$table->setPrimaryKey($pk_columns);
62+
}
63+
if (isset($tableDesc['foreign_keys'])) {
64+
foreach ($tableDesc['foreign_keys'] as $constraintName => $constraintDesc) {
65+
$this->buildForeignKeyConstraint($constraintDesc, $table, $constraintName);
66+
}
67+
}
68+
}
69+
70+
protected function buildColumn(array $columnDesc, Column $column)
71+
{
72+
if (isset($columnDesc['fixed'])) {
73+
$column->setFixed($columnDesc['fixed']);
74+
}
75+
if (isset($columnDesc['length'])) {
76+
$column->setLength($columnDesc['length']);
77+
}
78+
if (isset($columnDesc['precision'])) {
79+
$column->setPrecision($columnDesc['precision']);
80+
}
81+
if (isset($columnDesc['scale'])) {
82+
$column->setScale($columnDesc['scale']);
83+
}
84+
$column->setNotnull(isset($columnDesc['not_null']) && $columnDesc['not_null']);
85+
if (isset($columnDesc['default'])) {
86+
$column->setDefault($columnDesc['default']);
87+
}
88+
if (isset($columnDesc['auto_increment'])) {
89+
$column->setAutoincrement($columnDesc['auto_increment']);
90+
}
91+
if (isset($columnDesc['comment'])) {
92+
$column->setComment($columnDesc['comment']);
93+
}
94+
if (isset($columnDesc['custom'])) {
95+
$column->setCustomSchemaOptions($columnDesc['custom']);
96+
}
97+
}
98+
99+
protected function buildForeignKeyConstraint(array $constraintDesc, Table $table, string $name)
100+
{
101+
if (isset($constraintDesc['column'])) {
102+
$localColumns = [$constraintDesc['column']];
103+
} else {
104+
$localColumns = $constraintDesc['columns'];
105+
}
106+
$references = $constraintDesc['references'];
107+
if (is_array($references)) {
108+
$foreignTable = $references['table'];
109+
if (isset($references['column'])) {
110+
$foreignColumns = [$references['column']];
111+
} else {
112+
$foreignColumns = $references['columns'];
113+
}
114+
} else {
115+
$foreignTable = $references;
116+
$foreignColumns = $this->getPrimaryKeyColumns($foreignTable);
117+
if (!is_array($foreignColumns)) {
118+
$foreignColumns = [$foreignColumns];
119+
}
120+
}
121+
$options = array_diff_key($constraintDesc, ['column' => 0,'columns' => 0,'references' => 0]);
122+
$table->addForeignKeyConstraint($foreignTable, $localColumns, $foreignColumns, $options, $name);
123+
}
124+
125+
protected function getPrimaryKeyColumns(string $tableName)
126+
{
127+
$pkColumns = [];
128+
$tableDesc = $this->schemaDesc['tables'][$tableName];
129+
foreach ($tableDesc['columns'] as $columnName => $columnDesc) {
130+
if (isset($columnDesc['primary_key'])
131+
&& $columnDesc['primary_key']) {
132+
$pkColumns[] = $columnName;
133+
}
134+
}
135+
return $pkColumns;
136+
}
137+
138+
protected function buildIndex($indexDesc, Table $table, $name)
139+
{
140+
if (!is_array($indexDesc)) {
141+
$indexDesc = ['column' => $indexDesc];
142+
} elseif (array_keys($indexDesc) === range(0, count($indexDesc) - 1)) {
143+
$indexDesc = ['columns' => $indexDesc];
144+
}
145+
146+
if (isset($indexDesc['column'])) {
147+
$columns = [$indexDesc['column']];
148+
} else {
149+
$columns = $indexDesc['columns'];
150+
}
151+
152+
if (is_int($name)) {
153+
$name = implode('_', $columns);
154+
}
155+
156+
$options = array_diff_key($indexDesc, ['column' => 0,'columns' => 0]);
157+
if (isset($indexDesc['unique']) && $indexDesc['unique'] === true) {
158+
$table->addUniqueIndex($columns, $name, [], $options);
159+
} else {
160+
$table->addIndex($columns, $name, [], $options);
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)