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