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