|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\Core\Persistence\Database\DatabaseHandler; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationStep; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceBagInterface; |
|
8
|
|
|
|
|
9
|
|
|
class SQLExecutor extends AbstractExecutor |
|
10
|
|
|
{ |
|
11
|
|
|
use IgnorableStepExecutorTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var DatabaseHandler $connection |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $dbHandler; |
|
17
|
|
|
|
|
18
|
|
|
protected $supportedStepTypes = array('sql'); |
|
19
|
|
|
|
|
20
|
|
|
/** @var ReferenceBagInterface $referenceResolver */ |
|
21
|
|
|
protected $referenceResolver; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param DatabaseHandler $dbHandler |
|
25
|
|
|
* @param ReferenceBagInterface $referenceResolver |
|
26
|
73 |
|
*/ |
|
27
|
|
|
public function __construct(DatabaseHandler $dbHandler, ReferenceBagInterface $referenceResolver) |
|
28
|
73 |
|
{ |
|
29
|
73 |
|
$this->dbHandler = $dbHandler; |
|
30
|
73 |
|
$this->referenceResolver = $referenceResolver; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param MigrationStep $step |
|
35
|
|
|
* @return integer |
|
36
|
|
|
* @throws \Exception if migration step is not for this type of db |
|
37
|
3 |
|
*/ |
|
38
|
|
|
public function execute(MigrationStep $step) |
|
39
|
3 |
|
{ |
|
40
|
|
|
parent::execute($step); |
|
41
|
3 |
|
|
|
42
|
|
|
$this->skipStepIfNeeded($step); |
|
43
|
3 |
|
|
|
44
|
|
|
$conn = $this->dbHandler->getConnection(); |
|
45
|
3 |
|
// @see http://doctrine-orm.readthedocs.io/projects/doctrine-dbal/en/latest/reference/platforms.html |
|
46
|
|
|
$dbType = strtolower(preg_replace('/([0-9]+|Platform)/', '', $conn->getDatabasePlatform()->getName())); |
|
47
|
3 |
|
|
|
48
|
|
|
$dsl = $step->dsl; |
|
49
|
|
|
|
|
50
|
3 |
|
if (!isset($dsl[$dbType])) { |
|
51
|
|
|
throw new \Exception("Current database type '$dbType' is not supported by the SQL migration"); |
|
52
|
|
|
} |
|
53
|
3 |
|
$sql = $dsl[$dbType]; |
|
54
|
|
|
|
|
55
|
2 |
|
// returns the number of affected rows |
|
56
|
|
|
$result = $conn->exec($sql); |
|
57
|
2 |
|
|
|
58
|
|
|
$this->setReferences($result, $dsl); |
|
59
|
|
|
|
|
60
|
2 |
|
return $result; |
|
61
|
|
|
} |
|
62
|
2 |
|
|
|
63
|
1 |
|
protected function setReferences($result, $dsl) |
|
64
|
|
|
{ |
|
65
|
|
|
if (!array_key_exists('references', $dsl)) { |
|
66
|
1 |
|
return false; |
|
67
|
1 |
|
} |
|
68
|
1 |
|
|
|
69
|
1 |
|
foreach ($dsl['references'] as $reference) { |
|
70
|
1 |
|
switch ($reference['attribute']) { |
|
71
|
|
|
case 'affected_rows': |
|
72
|
|
|
$value = $result; |
|
73
|
|
|
break; |
|
74
|
|
|
default: |
|
75
|
1 |
|
throw new \InvalidArgumentException('Sql Executor does not support setting references for attribute ' . $reference['attribute']); |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
$overwrite = false; |
|
79
|
1 |
|
if (isset($reference['overwrite'])) { |
|
80
|
|
|
$overwrite = $reference['overwrite']; |
|
81
|
1 |
|
} |
|
82
|
|
|
$this->referenceResolver->addReference($reference['identifier'], $value, $overwrite); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|