1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Classes\AdaptersDriver; |
4
|
|
|
|
5
|
|
|
use Classes\AdapterConfig\AbstractAdapter; |
6
|
|
|
use Classes\Db\Column; |
7
|
|
|
use Classes\Db\Constrant; |
8
|
|
|
use Classes\Db\DbTable; |
9
|
|
|
use Classes\Db\Iterators\DbTables; |
10
|
|
|
|
11
|
|
|
require_once 'Classes/Db/Iterators/DbTables.php'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Adapter com funcoes de analise das consultas |
15
|
|
|
* |
16
|
|
|
* @author Pedro Alarcao <[email protected]> |
17
|
|
|
* @link https://github.com/pedro151/orm-generator |
18
|
|
|
*/ |
19
|
|
|
abstract class AbsractAdapter |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var void variavel com tipo de dados para serem convertida |
24
|
|
|
*/ |
25
|
|
|
protected $dataTypesToSimple; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @type \PDO |
29
|
|
|
*/ |
30
|
|
|
protected $_pdo; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $port; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $host; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @type string |
44
|
|
|
*/ |
45
|
|
|
protected $username; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @type string |
49
|
|
|
*/ |
50
|
|
|
protected $password; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $database; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @type string |
59
|
|
|
*/ |
60
|
|
|
protected $tablesName; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @type string |
64
|
|
|
*/ |
65
|
|
|
protected $socket; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @type \Classes\Db\Iterators\DbTables[] |
69
|
|
|
*/ |
70
|
|
|
private $objDbTables = array (); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var AbstractAdapter |
74
|
|
|
*/ |
75
|
|
|
protected $config; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @type int |
79
|
|
|
*/ |
80
|
|
|
protected $totalTables; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* analisa e popula as Foreing keys, Primary keys e dependencias do banco nos objetos |
84
|
|
|
*/ |
85
|
|
|
protected function parseConstrants () |
86
|
|
|
{ |
87
|
|
|
foreach ( $this->getListConstrant () as $constrant ) |
88
|
|
|
{ |
89
|
|
|
|
90
|
|
|
$schema = $constrant[ 'table_schema' ]; |
91
|
|
|
$table_name = $constrant [ 'table_name' ]; |
92
|
|
|
$this->populateForeignAndPrimaryKeys ( $constrant , $table_name , $schema ); |
93
|
|
|
unset( $table_name , $schema ); |
94
|
|
|
|
95
|
|
|
if ( $constrant[ 'constraint_type' ] == "FOREIGN KEY" ) |
96
|
|
|
{ |
97
|
|
|
$schema = $constrant[ 'foreign_schema' ]; |
98
|
|
|
$table_name = $constrant [ 'foreign_table' ]; |
99
|
|
|
$this->populateDependece ( $constrant , $table_name , $schema ); |
100
|
|
|
unset( $table_name , $schema ); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param array $constrant |
107
|
|
|
* @param string $table_name |
108
|
|
|
* @param int $schema |
109
|
|
|
*/ |
110
|
|
|
private function populateForeignAndPrimaryKeys ( $constrant , $table_name , $schema = 0 ) |
111
|
|
|
{ |
112
|
|
|
if ( $this->hasTable ( $table_name , $schema ) ) |
113
|
|
|
{ |
114
|
|
|
$table = $this->getTable ( $table_name , $schema ); |
115
|
|
|
if ( $table->hasColumn ( $constrant[ "column_name" ] ) ) |
116
|
|
|
{ |
117
|
|
|
$objConstrant = Constrant::getInstance () |
118
|
|
|
->populate ( |
119
|
|
|
array ( |
120
|
|
|
'constrant' => $constrant[ 'constraint_name' ] , |
121
|
|
|
'schema' => $constrant[ 'foreign_schema' ] , |
122
|
|
|
'table' => $constrant[ 'foreign_table' ] , |
123
|
|
|
'column' => $constrant[ 'foreign_column' ] , |
124
|
|
|
'database' => $this->database |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
switch ( $constrant[ 'constraint_type' ] ) |
129
|
|
|
{ |
130
|
|
|
case "FOREIGN KEY": |
131
|
|
|
$table->getColumn ( $constrant[ "column_name" ] ) |
132
|
|
|
->addRefFk ( $objConstrant ); |
133
|
|
|
break; |
134
|
|
|
case"PRIMARY KEY": |
|
|
|
|
135
|
|
|
$table->getColumn ( $constrant[ "column_name" ] ) |
136
|
|
|
->setPrimaryKey ( $objConstrant ) |
137
|
|
|
->setSequence ( |
138
|
|
|
$this->getSequence ( |
139
|
|
|
$table_name , |
140
|
|
|
$constrant[ "column_name" ] , |
141
|
|
|
$schema |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
break; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param array $constrant |
152
|
|
|
* @param string $table_name |
153
|
|
|
* @param int $schema |
154
|
|
|
*/ |
155
|
|
|
private function populateDependece ( $constrant , $table_name , $schema = 0 ) |
156
|
|
|
{ |
157
|
|
|
if ( $this->hasTable ( $table_name , $schema ) ) |
158
|
|
|
{ |
159
|
|
|
$table = $this->getTable ( $table_name , $schema ); |
160
|
|
|
if ( $table->hasColumn ( $constrant[ "foreign_column" ] ) ) |
161
|
|
|
{ |
162
|
|
|
$table->getColumn ( $constrant[ "foreign_column" ] ) |
163
|
|
|
->createDependece ( |
164
|
|
|
$constrant[ 'constraint_name' ] , |
165
|
|
|
$constrant[ 'table_name' ] , |
166
|
|
|
$constrant[ 'column_name' ] , |
167
|
|
|
$this->database , |
168
|
|
|
$constrant[ 'table_schema' ] |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* cria um Array com nome das tabelas |
176
|
|
|
*/ |
177
|
|
|
public function parseTables () |
178
|
|
|
{ |
179
|
|
|
if ( $this->hasTables () ) |
180
|
|
|
{ |
181
|
|
|
return $this->getAllTables (); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
foreach ( $this->getListColumns () as $table ) |
185
|
|
|
{ |
186
|
|
|
$schema = $table[ 'table_schema' ]; |
187
|
|
|
$key = $table [ 'table_name' ]; |
188
|
|
|
if ( ! $this->hasTable ( $key , $schema ) ) |
189
|
|
|
{ |
190
|
|
|
$this->createTable ( $key , $schema ); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$column = Column::getInstance () |
194
|
|
|
->populate ( |
195
|
|
|
array ( |
196
|
|
|
'name' => $table [ 'column_name' ] , |
197
|
|
|
'type' => $this->convertTypeToSimple ( $table[ 'data_type' ] ) , |
198
|
|
|
'nullable' => (is_string($table[ 'is_nullable' ]) && strtolower($table[ 'is_nullable' ]) != 'no' ) , |
199
|
|
|
'max_length' => $table[ 'max_length' ] |
200
|
|
|
) |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
$this->getTable ( $key , $schema ) |
204
|
|
|
->addColumn ( $column ) |
205
|
|
|
->setNamespace ( |
206
|
|
|
$this->config->createClassNamespace ( $this->getTable ( $key , $schema ) ) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* retorna o numero total de tabelas |
213
|
|
|
* |
214
|
|
|
* @return int |
215
|
|
|
*/ |
216
|
|
|
abstract public function getTotalTables (); |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Retorna o Nome da Sequence da tabela |
220
|
|
|
* |
221
|
|
|
* @param $table |
222
|
|
|
* @param $column |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
abstract public function getSequence ( $table , $column , $schema = 0 ); |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
|
|
abstract public function getListConstrant (); |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $str |
235
|
|
|
* |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
protected function convertTypeToPhp ( $str ) |
239
|
|
|
{ |
240
|
|
|
if ( isset( $this->dataTypesToPhp[ $str ] ) ) |
241
|
|
|
{ |
242
|
|
|
return $this->dataTypesToPhp[ $str ]; |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return 'string'; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
protected function convertTypeToSimple ( $str ) |
249
|
|
|
{ |
250
|
|
|
if ( isset( $this->dataTypesToSimple[ $str ] ) ) |
251
|
|
|
{ |
252
|
|
|
return $this->dataTypesToSimple[ $str ]; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return 'string'; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
abstract public function getPDOString (); |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
abstract public function getPDOSocketString (); |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param $nameTable |
270
|
|
|
* @param int $schema |
271
|
|
|
* |
272
|
|
|
* @return \Classes\Db\DbTable |
273
|
|
|
*/ |
274
|
|
|
public function createTable ( $nameTable , $schema = 0 ) |
275
|
|
|
{ |
276
|
|
|
if ( ! isset( $this->objDbTables[ $schema ] ) ) |
277
|
|
|
{ |
278
|
|
|
$this->objDbTables[ $schema ] = new DbTables(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$this->objDbTables[ $schema ][ trim ( $nameTable ) ] = DbTable::getInstance () |
282
|
|
|
->populate ( |
283
|
|
|
array ( |
284
|
|
|
'table' => $nameTable , |
285
|
|
|
'schema' => $schema , |
286
|
|
|
'database' => $this->database |
287
|
|
|
) |
288
|
|
|
); |
289
|
|
|
|
290
|
|
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Retorna um Array Assoc com a chave com nome da tabela e o valor com objeto tables |
295
|
|
|
* |
296
|
|
|
* @return \Classes\Db\Iterators\DbTables |
297
|
|
|
*/ |
298
|
|
|
public function getTables ( $schema = 0 ) |
299
|
|
|
{ |
300
|
|
|
if ( ! isset( $this->objDbTables[ $schema ] ) ) |
301
|
|
|
{ |
302
|
|
|
return array (); |
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $this->objDbTables[ $schema ]; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function getAllTables () |
309
|
|
|
{ |
310
|
|
|
return $this->objDbTables; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function hasTables () |
314
|
|
|
{ |
315
|
|
|
return ! empty( $this->objDbTables ); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* retorna a tabela especifica |
320
|
|
|
* |
321
|
|
|
* @param string $nameTable Nome da tabela |
322
|
|
|
* |
323
|
|
|
* @return \Classes\Db\DbTable |
324
|
|
|
*/ |
325
|
|
|
public function getTable ( $nameTable , $schema = 0 ) |
326
|
|
|
{ |
327
|
|
|
return $this->objDbTables[ $schema ][ trim ( $nameTable ) ]; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @param string $nameTable |
332
|
|
|
* @param int|string $schema |
333
|
|
|
* |
334
|
|
|
* @return bool |
335
|
|
|
*/ |
336
|
|
|
public function hasTable ( $nameTable , $schema = 0 ) |
337
|
|
|
{ |
338
|
|
|
return isset( $this->objDbTables[ $schema ][ trim ( $nameTable ) ] ); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* retorna multiplos arrays com dados da column em array |
343
|
|
|
* |
344
|
|
|
* @return array[] |
345
|
|
|
*/ |
346
|
|
|
abstract public function getListColumns (); |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Retorna um Array com nome das tabelas |
350
|
|
|
* |
351
|
|
|
* @return string[] |
352
|
|
|
*/ |
353
|
|
|
abstract public function getListNameTable (); |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param \Classes\AdapterConfig\AbstractAdapter $adapterConfig |
357
|
|
|
*/ |
358
|
|
|
public function __construct ( AbstractAdapter $adapterConfig ) |
359
|
|
|
{ |
360
|
|
|
$this->config = $adapterConfig; |
361
|
|
|
$this->host = $adapterConfig->getHost (); |
362
|
|
|
$this->database = $adapterConfig->getDatabase (); |
363
|
|
|
$this->port = $adapterConfig->hasPort () ? $adapterConfig->getPort () |
364
|
|
|
: $this->port; |
365
|
|
|
$this->username = $adapterConfig->getUser (); |
366
|
|
|
$this->password = $adapterConfig->getPassword (); |
367
|
|
|
$this->socket = $adapterConfig->getSocket (); |
368
|
|
|
$this->tablesName = $adapterConfig->hasTablesName () |
369
|
|
|
? $adapterConfig->getListTablesName () : ''; |
370
|
|
|
|
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Executa as consultas do banco de dados |
375
|
|
|
*/ |
376
|
|
|
public function runDatabase () |
377
|
|
|
{ |
378
|
|
|
$this->parseTables (); |
379
|
|
|
$this->parseConstrants (); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* |
384
|
|
|
* @return \PDO |
385
|
|
|
*/ |
386
|
|
|
public function getPDO () |
387
|
|
|
{ |
388
|
|
|
if ( is_null ( $this->_pdo ) ) |
389
|
|
|
{ |
390
|
|
|
if ( ! empty( $this->socket ) ) |
391
|
|
|
{ |
392
|
|
|
$pdoString = $this->getPDOSocketString (); |
393
|
|
|
} else |
394
|
|
|
{ |
395
|
|
|
$pdoString = $this->getPDOString (); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
try |
399
|
|
|
{ |
400
|
|
|
$this->_pdo = new \PDO ( |
401
|
|
|
$pdoString , $this->username , $this->password |
402
|
|
|
); |
403
|
|
|
} catch ( \Exception $e ) |
404
|
|
|
{ |
405
|
|
|
die ( "\033[0;31mPDO error: " . $e->getMessage () . "\033[0m\n" ); |
|
|
|
|
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return $this->_pdo; |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.