1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Utils\Database; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
9
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
10
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
11
|
|
|
use function count; |
12
|
|
|
|
13
|
|
|
class DatabaseUtils |
14
|
|
|
{ |
15
|
|
|
/* @var $em EntityManager */ |
|
|
|
|
16
|
|
|
private $em; |
17
|
|
|
private $kernel; |
18
|
|
|
|
19
|
2 |
|
public function __construct($kernel, EntityManagerInterface $em) |
20
|
|
|
{ |
21
|
2 |
|
$this->kernel = $kernel; |
22
|
2 |
|
$this->em = $em; |
23
|
2 |
|
} |
24
|
|
|
|
25
|
2 |
|
public function getFieldType($entity, $field) |
26
|
|
|
{ |
27
|
2 |
|
$metadata = $this->em->getClassMetadata(get_class($entity)); |
28
|
2 |
|
$fieldMetadata = $metadata->fieldMappings[$field]; |
|
|
|
|
29
|
|
|
|
30
|
2 |
|
$fieldType = $fieldMetadata['type']; |
31
|
|
|
|
32
|
2 |
|
return $fieldType; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
public function isRecordChanged($entity, $fieldname, $oldvalue, $newvalue) |
36
|
|
|
{ |
37
|
1 |
|
$fieldtype = $this->getFieldType(new $entity(), $fieldname); |
38
|
1 |
|
if ('boolean' === $fieldtype) { |
39
|
1 |
|
return $oldvalue !== $newvalue; |
40
|
|
|
} |
41
|
1 |
|
if ('datetime' === $fieldtype || 'date' === $fieldtype) { |
42
|
1 |
|
return $this->isDateChanged($oldvalue, $newvalue); |
43
|
|
|
} |
44
|
1 |
|
if (is_array($oldvalue)) { |
45
|
1 |
|
return $this->isArrayChanged($oldvalue, $newvalue); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return $oldvalue !== $newvalue; |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
public function isDateChanged($oldvalue, $newvalue) |
52
|
|
|
{ |
53
|
1 |
|
$datenewvalue = new DateTime(); |
54
|
1 |
|
$datenewvalue->setTimestamp($newvalue); |
55
|
1 |
|
$twoboth = !$oldvalue && !$newvalue; |
56
|
1 |
|
if ($twoboth) { |
57
|
1 |
|
return false; |
58
|
|
|
} |
59
|
1 |
|
$onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue); |
|
|
|
|
60
|
1 |
|
if ($onlyonenull) { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
1 |
|
$changed = ($oldvalue != $datenewvalue); |
64
|
|
|
|
65
|
1 |
|
return $changed; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function isArrayChanged($oldvalue, $newvalue) |
69
|
|
|
{ |
70
|
1 |
|
$twoboth = !$oldvalue && !$newvalue; |
71
|
1 |
|
if ($twoboth) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
1 |
|
$onlyonenull = (!$oldvalue && $newvalue) || ($oldvalue && !$newvalue); |
|
|
|
|
75
|
1 |
|
if ($onlyonenull) { |
76
|
1 |
|
return true; |
77
|
|
|
} |
78
|
1 |
|
$numdiff = array_diff($oldvalue, $newvalue); |
79
|
|
|
|
80
|
1 |
|
return count($numdiff) > 0; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function truncateTable($entityclass, $cascade = false) |
84
|
|
|
{ |
85
|
1 |
|
$cmd = $this->em->getClassMetadata($entityclass); |
86
|
1 |
|
$connection = $this->em->getConnection(); |
87
|
1 |
|
$dbPlatform = $connection->getDatabasePlatform(); |
88
|
1 |
|
$dbtype = $connection->getDriver()->getDatabasePlatform()->getName(); |
89
|
1 |
|
$retval = false; |
90
|
|
|
|
91
|
1 |
|
switch ($dbtype) { |
92
|
1 |
|
case 'mysql': |
93
|
|
|
$connection->query('SET FOREIGN_KEY_CHECKS=0'); |
94
|
|
|
$q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade); |
95
|
|
|
$retval = $connection->executeUpdate($q); |
96
|
|
|
$connection->query('SET FOREIGN_KEY_CHECKS=1'); |
97
|
|
|
break; |
98
|
1 |
|
case 'postgresql': |
99
|
1 |
|
$cascadesql = $cascade ? 'CASCADE' : ''; |
100
|
1 |
|
$tablename = $cmd->getTableName(); |
101
|
1 |
|
if ($cmd->getSchemaName()) { |
102
|
1 |
|
$tablename = $cmd->getSchemaName().'.'.$tablename; |
103
|
|
|
} |
104
|
1 |
|
$retval = $connection->executeQuery(sprintf('TRUNCATE TABLE %s '.$cascadesql, $tablename)); |
105
|
1 |
|
break; |
106
|
|
|
default: |
107
|
|
|
$q = $dbPlatform->getTruncateTableSql($cmd->getTableName(), $cascade); |
108
|
|
|
$retval = $connection->executeUpdate($q); |
109
|
|
|
break; |
110
|
|
|
} |
111
|
1 |
|
$this->em->clear(); |
112
|
|
|
|
113
|
1 |
|
return $retval; |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
public function isSchemaChanged() |
117
|
|
|
{ |
118
|
2 |
|
$application = new Application($this->kernel); |
119
|
2 |
|
$application->setAutoExit(false); |
120
|
|
|
|
121
|
2 |
|
$input = new ArrayInput(array( |
122
|
2 |
|
'command' => 'doctrine:schema:update', |
123
|
|
|
'--dump-sql' => true, |
124
|
|
|
'--no-debug' => true, |
125
|
2 |
|
'--env' => $this->kernel->getEnvironment(), |
126
|
|
|
)); |
127
|
|
|
|
128
|
|
|
// You can use NullOutput() if you don't need the output |
129
|
2 |
|
$output = new BufferedOutput(); |
130
|
2 |
|
$application->run($input, $output); |
131
|
|
|
|
132
|
|
|
// return the output, don't use if you used NullOutput() |
133
|
2 |
|
$content = $output->fetch(); |
134
|
2 |
|
$changed = strpos($content, 'Nothing to update'); |
135
|
|
|
|
136
|
2 |
|
return 0 !== $changed; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|