|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Classes\AdaptersDriver; |
|
4
|
|
|
|
|
5
|
|
|
use Classes\AdapterConfig\AbstractAdapter; |
|
6
|
|
|
use Classes\Db\Constrant; |
|
7
|
|
|
use Classes\Db\DbTable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Adapter com funcoes de analise das consultas |
|
11
|
|
|
* |
|
12
|
|
|
* @author Pedro Alarcao <[email protected]> |
|
13
|
|
|
* @link https://github.com/pedro151/ORM-Generator |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbsractAdapter |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var void variavel com tipo de dados para serem convertida |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $dataTypes; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @type \PDO |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_pdo; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $port; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $host; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @type string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $username; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @type string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $password; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $database; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @type string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $socket; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @type \Classes\Db\DbTable[][] |
|
60
|
|
|
*/ |
|
61
|
|
|
private $objDbTables = array (); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var AbstractAdapter |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $config; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @type int |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $totalTables; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* analisa e popula as Foreing keys, Primary keys e dependencias do banco nos objetos |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function parseConstrants () |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ( $this->getListConstrant () as $constrant ) |
|
79
|
|
|
{ |
|
80
|
|
|
|
|
81
|
|
|
$schema = $constrant[ 'table_schema' ]; |
|
82
|
|
|
$table_name = $constrant [ 'table_name' ]; |
|
83
|
|
|
$this->populateForeignAndPrimaryKeys ( $constrant , $table_name , $schema ); |
|
84
|
|
|
unset( $table_name , $schema ); |
|
85
|
|
|
|
|
86
|
|
|
if ( $constrant[ 'constraint_type' ] == "FOREIGN KEY" ) |
|
87
|
|
|
{ |
|
88
|
|
|
$schema = $constrant[ 'foreign_schema' ]; |
|
89
|
|
|
$table_name = $constrant [ 'foreign_table' ]; |
|
90
|
|
|
$this->populateDependece ( $constrant , $table_name , $schema ); |
|
91
|
|
|
unset( $table_name , $schema ); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param array $constrant |
|
98
|
|
|
* @param string $table_name |
|
99
|
|
|
* @param int $schema |
|
100
|
|
|
*/ |
|
101
|
|
|
private function populateForeignAndPrimaryKeys ( $constrant , $table_name , $schema = 0 ) |
|
102
|
|
|
{ |
|
103
|
|
|
if ( $this->hasTable ( $table_name , $schema ) ) |
|
104
|
|
|
{ |
|
105
|
|
|
$table = $this->getTable ( $table_name , $schema ); |
|
106
|
|
|
if ( $table->hasColumn ( $constrant[ "column_name" ] ) ) |
|
107
|
|
|
{ |
|
108
|
|
|
$objConstrant = Constrant::getInstance () |
|
109
|
|
|
->populate ( |
|
110
|
|
|
array ( |
|
111
|
|
|
'constrant' => $constrant[ 'constraint_name' ] , |
|
112
|
|
|
'schema' => $constrant[ 'foreign_schema' ] , |
|
113
|
|
|
'table' => $constrant[ 'foreign_table' ] , |
|
114
|
|
|
'column' => $constrant[ 'foreign_column' ] |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
switch ( $constrant[ 'constraint_type' ] ) |
|
119
|
|
|
{ |
|
120
|
|
|
case "FOREIGN KEY": |
|
121
|
|
|
$table->getColumn ( $constrant[ "column_name" ] ) |
|
122
|
|
|
->addRefFk ( $objConstrant ); |
|
123
|
|
|
break; |
|
124
|
|
|
case"PRIMARY KEY": |
|
|
|
|
|
|
125
|
|
|
$table->getColumn ( $constrant[ "column_name" ] ) |
|
126
|
|
|
->setPrimaryKey ( $objConstrant ) |
|
127
|
|
|
->setSequence ( |
|
128
|
|
|
$this->getSequence ( |
|
129
|
|
|
$schema . '.' . $table_name , |
|
130
|
|
|
$constrant[ "column_name" ] |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
break; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param array $constrant |
|
141
|
|
|
* @param string $table_name |
|
142
|
|
|
* @param int $schema |
|
143
|
|
|
*/ |
|
144
|
|
|
private function populateDependece ( $constrant , $table_name , $schema = 0 ) |
|
145
|
|
|
{ |
|
146
|
|
|
if ( $this->hasTable ( $table_name , $schema ) ) |
|
147
|
|
|
{ |
|
148
|
|
|
$table = $this->getTable ( $table_name , $schema ); |
|
149
|
|
|
if ( $table->hasColumn ( $constrant[ "foreign_column" ] ) ) |
|
150
|
|
|
{ |
|
151
|
|
|
$table->getColumn ( $constrant[ "foreign_column" ] ) |
|
152
|
|
|
->createDependece ( |
|
153
|
|
|
$constrant[ 'constraint_name' ] , |
|
154
|
|
|
$constrant[ 'table_name' ] , |
|
155
|
|
|
$constrant[ 'column_name' ] , |
|
156
|
|
|
$constrant[ 'table_schema' ] |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* cria um Array com nome das tabelas |
|
164
|
|
|
*/ |
|
165
|
|
|
protected abstract function parseTables (); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* retorna o numero total de tabelas |
|
169
|
|
|
* |
|
170
|
|
|
* @return int |
|
171
|
|
|
*/ |
|
172
|
|
|
public abstract function getTotalTables (); |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Retorna o Nome da Sequence da tabela |
|
176
|
|
|
* |
|
177
|
|
|
* @param $table |
|
178
|
|
|
* @param $column |
|
179
|
|
|
* |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
public abstract function getSequence ( $table , $column ); |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
|
public abstract function getListConstrant (); |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param string $str |
|
191
|
|
|
* |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function convertTypeToPhp ( $str ) |
|
195
|
|
|
{ |
|
196
|
|
|
if ( isset( $this->dataTypes[ $str ] ) ) |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->dataTypes[ $str ]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return 'string'; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
|
|
public abstract function getPDOString (); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return string |
|
211
|
|
|
*/ |
|
212
|
|
|
public abstract function getPDOSocketString (); |
|
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param $nameTable |
|
216
|
|
|
* @param int $schema |
|
217
|
|
|
* |
|
218
|
|
|
* @return \Classes\Db\DbTable |
|
219
|
|
|
*/ |
|
220
|
|
|
public function createTable ( $nameTable , $schema = 0 ) |
|
221
|
|
|
{ |
|
222
|
|
|
$this->objDbTables[ $schema ][ trim ( $nameTable ) ] = DbTable::getInstance () |
|
223
|
|
|
->populate ( |
|
224
|
|
|
array ( |
|
225
|
|
|
'table' => $nameTable , |
|
226
|
|
|
'schema' => $schema , |
|
227
|
|
|
'database' => $this->database |
|
228
|
|
|
) |
|
229
|
|
|
); |
|
230
|
|
|
|
|
231
|
|
|
return $this; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Retorna um Array Assoc com a chave com nome da tabela e o valor com objeto tables |
|
236
|
|
|
* |
|
237
|
|
|
* @return \Classes\Db\DbTable[] |
|
238
|
|
|
*/ |
|
239
|
|
|
public function getTables ( $schema = 0 ) |
|
240
|
|
|
{ |
|
241
|
|
|
if ( ! isset( $this->objDbTables[ $schema ] ) ) |
|
242
|
|
|
{ |
|
243
|
|
|
return array (); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return $this->objDbTables[ $schema ]; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
public function getAllTables () |
|
250
|
|
|
{ |
|
251
|
|
|
return $this->objDbTables; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function hasTables () |
|
255
|
|
|
{ |
|
256
|
|
|
return ! empty( $this->objDbTables ); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* retorna a tabela especifica |
|
261
|
|
|
* |
|
262
|
|
|
* @param $nameTable Nome da tabela |
|
263
|
|
|
* |
|
264
|
|
|
* @return \Classes\Db\DbTable |
|
265
|
|
|
*/ |
|
266
|
|
|
public function getTable ( $nameTable , $schema = 0 ) |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->objDbTables[ $schema ][ trim ( $nameTable ) ]; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* @param string $nameTable |
|
273
|
|
|
* @param int|string $schema |
|
274
|
|
|
* |
|
275
|
|
|
* @return bool |
|
276
|
|
|
*/ |
|
277
|
|
|
public function hasTable ( $nameTable , $schema = 0 ) |
|
278
|
|
|
{ |
|
279
|
|
|
return isset( $this->objDbTables[ $schema ][ trim ( $nameTable ) ] ); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* retorna multiplos arrays com dados da column em array |
|
284
|
|
|
* |
|
285
|
|
|
* @return array[] |
|
286
|
|
|
*/ |
|
287
|
|
|
public abstract function getListColumns (); |
|
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Retorna um Array com nome das tabelas |
|
291
|
|
|
* |
|
292
|
|
|
* @return string[] |
|
293
|
|
|
*/ |
|
294
|
|
|
public abstract function getListNameTable (); |
|
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @param \Classes\AdapterConfig\AbstractAdapter $adapterConfig |
|
298
|
|
|
*/ |
|
299
|
|
|
public function __construct ( AbstractAdapter $adapterConfig ) |
|
300
|
|
|
{ |
|
301
|
|
|
$this->config = $adapterConfig; |
|
302
|
|
|
$this->host = $adapterConfig->getHost (); |
|
303
|
|
|
$this->database = $adapterConfig->getDatabase (); |
|
304
|
|
|
$this->port = $adapterConfig->hasPort () ? $adapterConfig->getPort () |
|
305
|
|
|
: $this->port; |
|
306
|
|
|
$this->username = $adapterConfig->getUser (); |
|
307
|
|
|
$this->password = $adapterConfig->getPassword (); |
|
308
|
|
|
$this->socket = $adapterConfig->getSocket (); |
|
309
|
|
|
|
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Executa as consultas do banco de dados |
|
314
|
|
|
*/ |
|
315
|
|
|
public function runDatabase () |
|
316
|
|
|
{ |
|
317
|
|
|
$this->parseTables (); |
|
318
|
|
|
$this->parseConstrants (); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* |
|
323
|
|
|
* @return \PDO |
|
324
|
|
|
*/ |
|
325
|
|
|
public function getPDO () |
|
326
|
|
|
{ |
|
327
|
|
|
if ( is_null ( $this->_pdo ) ) |
|
328
|
|
|
{ |
|
329
|
|
|
if ( ! empty( $this->socket ) ) |
|
330
|
|
|
{ |
|
331
|
|
|
$pdoString = $this->getPDOSocketString (); |
|
332
|
|
|
} else |
|
333
|
|
|
{ |
|
334
|
|
|
$pdoString = $this->getPDOString (); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
try |
|
338
|
|
|
{ |
|
339
|
|
|
$this->_pdo = new \PDO ( |
|
340
|
|
|
$pdoString , $this->username , $this->password |
|
341
|
|
|
); |
|
342
|
|
|
} catch ( \Exception $e ) |
|
343
|
|
|
{ |
|
344
|
|
|
die ( "pdo error: " . $e->getMessage () . "\n" ); |
|
|
|
|
|
|
345
|
|
|
} |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
return $this->_pdo; |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
|
As per the PSR-2 coding standard, there must be a space after the
casekeyword, instead of the test immediately following it.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.