Base.php
5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
class App_Unificacao_Base
{
protected $chavesManterPrimeiroVinculo = [];
protected $chavesManterTodosVinculos = [];
protected $chavesDeletarDuplicados = [];
protected $triggersNecessarias = [];
protected $codigoUnificador;
protected $codigosDuplicados;
protected $codPessoaLogada;
protected $db;
protected $transacao;
public function __construct($codigoUnificador, $codigosDuplicados, $codPessoaLogada, clsBanco $db, $transacao = true)
{
$this->codigoUnificador = $codigoUnificador;
$this->codigosDuplicados = $codigosDuplicados;
$this->codPessoaLogada = $codPessoaLogada;
$this->db = $db;
$this->transacao = $transacao;
}
public function unifica()
{
$this->validaParametros();
$this->desabilitaTodasTriggers();
$this->habilitaTriggersNecessarias();
$this->processaChavesDeletarDuplicados();
$this->processaChavesManterPrimeiroVinculo();
$this->processaChavesManterTodosVinculos();
$this->habilitaTodasTriggers();
}
protected function processaChavesDeletarDuplicados()
{
$stringCodigosDuplicados = implode(',', $this->codigosDuplicados);
foreach ($this->chavesDeletarDuplicados as $key => $value) {
try {
$this->db->Consulta(
"
UPDATE {$value['tabela']}
SET {$value['coluna']} = {$this->codigoUnificador}
WHERE {$value['coluna']} IN ({$stringCodigosDuplicados})
"
);
} catch (Exception $e) {
$this->db->Consulta(
"
DELETE FROM {$value['tabela']}
WHERE {$value['coluna']} IN ({$stringCodigosDuplicados})
"
);
}
}
}
protected function processaChavesManterTodosVinculos()
{
$stringCodigosDuplicados = implode(',', $this->codigosDuplicados);
foreach ($this->chavesManterTodosVinculos as $key => $value) {
$this->db->Consulta(
"
UPDATE {$value['tabela']}
SET {$value['coluna']} = {$this->codigoUnificador}
WHERE {$value['coluna']} IN ({$stringCodigosDuplicados})
"
);
}
}
protected function processaChavesManterPrimeiroVinculo()
{
$chavesConsultar = $this->codigosDuplicados;
$chavesConsultar[] = $this->codigoUnificador;
$chavesConsultarString = implode(',', $chavesConsultar);
foreach ($this->chavesManterPrimeiroVinculo as $key => $value) {
$this->db->Consulta(
"
DELETE FROM {$value['tabela']}
WHERE {$value['coluna']} <>
(
SELECT {$value['coluna']}
from {$value['tabela']}
WHERE {$value['coluna']} in ({$chavesConsultarString})
ORDER BY {$value['coluna']} = {$this->codigoUnificador} DESC
LIMIT 1
)
AND {$value['coluna']} in ({$chavesConsultarString})
"
);
$this->db->Consulta(
"
UPDATE {$value['tabela']}
SET {$value['coluna']} = {$this->codigoUnificador}
WHERE {$value['coluna']} IN ({$chavesConsultarString})
"
);
}
}
protected function tabelasEnvolvidas()
{
$todasChaves = array_merge($this->chavesManterPrimeiroVinculo, $this->chavesManterTodosVinculos);
$todasTabelas = [];
foreach ($todasChaves as $key => $value) {
$todasTabelas[$value['tabela']] = $value['tabela'];
}
return array_values($todasTabelas);
}
protected function habilitaTriggersNecessarias()
{
foreach ($this->triggersNecessarias as $triggerNecessaria) {
$this->db->Consulta("ALTER TABLE {$triggerNecessaria['tabela']} ENABLE TRIGGER {$triggerNecessaria['trigger']}");
}
}
protected function desabilitaTodasTriggers()
{
$tabelasEnvolvidas = $this->tabelasEnvolvidas();
foreach ($tabelasEnvolvidas as $key => $tabela) {
$this->db->Consulta("ALTER TABLE {$tabela} DISABLE TRIGGER ALL");
}
}
protected function habilitaTodasTriggers()
{
$tabelasEnvolvidas = $this->tabelasEnvolvidas();
foreach ($tabelasEnvolvidas as $key => $tabela) {
$this->db->Consulta("ALTER TABLE {$tabela} ENABLE TRIGGER ALL");
}
}
protected function validaParametros()
{
if ($this->codigoUnificador != (int) $this->codigoUnificador) {
throw new CoreExt_Exception('Parâmetro 1 deve ser o código unificador');
}
if (!is_array($this->codigosDuplicados) || !count($this->codigosDuplicados)) {
throw new CoreExt_Exception('Parâmetro 2 deve ser um array de códigos duplicados');
}
if ($this->codPessoaLogada != (int) $this->codPessoaLogada) {
throw new CoreExt_Exception('Parâmetro 3 deve ser um inteiro');
}
}
}