1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Classes\AdaptersDriver; |
4
|
|
|
|
5
|
|
|
use Classes\AdapterConfig\AbstractAdapter; |
6
|
|
|
use Classes\Db\DbTable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Adapter com funcoes de analise das consultas |
10
|
|
|
* |
11
|
|
|
* @author Pedro Alarcao <[email protected]> |
12
|
|
|
* @link https://github.com/pedro151/ORM-Generator |
13
|
|
|
*/ |
14
|
|
|
abstract class AbsractAdapter |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var void variavel com tipo de dados para serem convertida |
19
|
|
|
*/ |
20
|
|
|
protected $dataTypes; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @type \PDO |
24
|
|
|
*/ |
25
|
|
|
protected $_pdo; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
protected $port; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $host; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @type string |
39
|
|
|
*/ |
40
|
|
|
protected $username; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @type string |
44
|
|
|
*/ |
45
|
|
|
protected $password; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $database; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @type string |
54
|
|
|
*/ |
55
|
|
|
protected $socket; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @type \Classes\Db\DbTable[][] |
59
|
|
|
*/ |
60
|
|
|
protected $objDbTables = array (); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var AbstractAdapter |
64
|
|
|
*/ |
65
|
|
|
protected $config; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @type int |
69
|
|
|
*/ |
70
|
|
|
protected $totalTables; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Popula as ForeingKeys do banco nos objetos |
74
|
|
|
*/ |
75
|
|
|
protected abstract function parseForeignKeys (); |
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* cria um Array com nome das tabelas |
79
|
|
|
*/ |
80
|
|
|
protected abstract function parseTables (); |
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* retorna o numero total de tabelas |
84
|
|
|
* |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
|
|
public abstract function getTotalTables (); |
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retorna o Nome da Sequence da tabela |
91
|
|
|
* |
92
|
|
|
* @param $table |
93
|
|
|
* @param $column |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public abstract function getSequence ( $table , $column ); |
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public abstract function getListConstrant (); |
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $str |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
protected function convertTypeToPhp ( $str ) |
110
|
|
|
{ |
111
|
|
|
if ( isset( $this->dataTypes[ $str ] ) ) |
112
|
|
|
{ |
113
|
|
|
return $this->dataTypes[ $str ]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return 'string'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public abstract function getPDOString (); |
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public abstract function getPDOSocketString (); |
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $nameTable |
131
|
|
|
* @param int $schema |
132
|
|
|
* |
133
|
|
|
* @return \Classes\Db\DbTable |
134
|
|
|
*/ |
135
|
|
|
public function createTable ( $nameTable , $schema = 0 ) |
136
|
|
|
{ |
137
|
|
|
$this->objDbTables[ $schema ][ trim ( $nameTable ) ] = new DbTable(); |
138
|
|
|
$this->objDbTables[ $schema ][ trim ( $nameTable ) ]->populate ( |
139
|
|
|
array ( |
140
|
|
|
'table' => $nameTable , |
141
|
|
|
'schema' => $schema , |
142
|
|
|
'database' => $this->database |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Retorna um Array Assoc com a chave com nome da tabela e o valor com objeto tables |
151
|
|
|
* |
152
|
|
|
* @return \Classes\Db\DbTable[] |
153
|
|
|
*/ |
154
|
|
|
public function getTables ( $schema = 0 ) |
155
|
|
|
{ |
156
|
|
|
if ( ! isset( $this->objDbTables[ $schema ] ) ) |
157
|
|
|
{ |
158
|
|
|
return array (); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->objDbTables[ $schema ]; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* retorna a tabela especifica |
166
|
|
|
* |
167
|
|
|
* @param $nameTable Nome da tabela |
168
|
|
|
* |
169
|
|
|
* @return \Classes\Db\DbTable|null |
170
|
|
|
*/ |
171
|
|
|
public function getTable ( $nameTable , $schema = 0 ) |
172
|
|
|
{ |
173
|
|
|
if ( isset( $this->objDbTables[ $schema ][ trim ( $nameTable ) ] ) ) |
174
|
|
|
{ |
175
|
|
|
return $this->objDbTables[ $schema ][ trim ( $nameTable ) ]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return null; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $nameTable |
183
|
|
|
* @param int|string $schema |
184
|
|
|
* |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
|
|
public function hasTable ( $nameTable , $schema = 0 ) |
188
|
|
|
{ |
189
|
|
|
if ( isset( $this->objDbTables[ $schema ][ trim ( $nameTable ) ] ) ) |
190
|
|
|
{ |
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* retorna multiplos arrays com dados da column em array |
199
|
|
|
* |
200
|
|
|
* @return array[] |
201
|
|
|
*/ |
202
|
|
|
public abstract function getListColumns (); |
|
|
|
|
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Retorna um Array com nome das tabelas |
206
|
|
|
* |
207
|
|
|
* @return string[] |
208
|
|
|
*/ |
209
|
|
|
public abstract function getListNameTable (); |
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param \Classes\AdapterConfig\AbstractAdapter $adapterConfig |
213
|
|
|
*/ |
214
|
|
|
public function __construct ( AbstractAdapter $adapterConfig ) |
215
|
|
|
{ |
216
|
|
|
$this->config = $adapterConfig; |
217
|
|
|
$this->host = $adapterConfig->getHost (); |
218
|
|
|
$this->database = $adapterConfig->getDatabase (); |
219
|
|
|
$this->port = $adapterConfig->hasPort () ? $adapterConfig->getPort () |
220
|
|
|
: $this->port; |
221
|
|
|
$this->username = $adapterConfig->getUser (); |
222
|
|
|
$this->password = $adapterConfig->getPassword (); |
223
|
|
|
$this->socket = $adapterConfig->getSocket (); |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Executa as consultas do banco de dados |
229
|
|
|
*/ |
230
|
|
|
public function runDatabase () |
231
|
|
|
{ |
232
|
|
|
$this->parseTables (); |
233
|
|
|
$this->parseForeignKeys (); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* |
238
|
|
|
* @return \PDO |
239
|
|
|
*/ |
240
|
|
|
public function getPDO () |
241
|
|
|
{ |
242
|
|
|
if ( is_null ( $this->_pdo ) ) |
243
|
|
|
{ |
244
|
|
|
if ( ! empty( $this->socket ) ) |
245
|
|
|
{ |
246
|
|
|
$pdoString = $this->getPDOSocketString (); |
247
|
|
|
} else |
248
|
|
|
{ |
249
|
|
|
$pdoString = $this->getPDOString (); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
try |
253
|
|
|
{ |
254
|
|
|
$this->_pdo = new \PDO ( |
255
|
|
|
$pdoString , $this->username , $this->password |
256
|
|
|
); |
257
|
|
|
} catch ( \Exception $e ) |
258
|
|
|
{ |
259
|
|
|
die ( "pdo error: " . $e->getMessage () . "\n" ); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $this->_pdo; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|