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
|
|
|
|
10
|
|
|
require_once 'Classes/AdaptersDriver/AbsractAdapter.php'; |
11
|
|
|
require_once 'Classes/Db/Column.php'; |
12
|
|
|
require_once 'Classes/Db/Constrant.php'; |
13
|
|
|
require_once 'Classes/Db/DbTable.php'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Pedro Alarcao <[email protected]> |
17
|
|
|
* @link https://github.com/pedro151/orm-generator |
18
|
|
|
*/ |
19
|
|
|
class Pgsql extends AbsractAdapter |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $port = 5432; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @type array|\string[] |
29
|
|
|
*/ |
30
|
|
|
protected $schema = array ( 'public' ); |
31
|
|
|
|
32
|
|
|
protected $dataTypesToSimple = array ( |
33
|
|
|
/* Numeric Types */ |
34
|
|
|
'smallint' => 'int', |
35
|
|
|
'integer' => 'int', |
36
|
|
|
'serial' => 'int', |
37
|
|
|
'bigint' => 'float', |
38
|
|
|
'decimal' => 'float', |
39
|
|
|
'numeric' => 'float', |
40
|
|
|
'real' => 'float', |
41
|
|
|
'double precision' => 'float', |
42
|
|
|
'bigserial' => 'float', |
43
|
|
|
/* Monetary Types */ |
44
|
|
|
'money' => 'float', |
45
|
|
|
/* Binary Data Types */ |
46
|
|
|
'bytea' => 'int', |
47
|
|
|
/* Character Types */ |
48
|
|
|
'character varyin' => 'string', |
49
|
|
|
'varchar' => 'string', |
50
|
|
|
'character' => 'string', |
51
|
|
|
'char' => 'string', |
52
|
|
|
'text' => 'text', |
53
|
|
|
/* Date/Time Types */ |
54
|
|
|
'datetime' => 'datetime', |
55
|
|
|
'timestamp without time zone' => 'timestamp', |
56
|
|
|
'date' => 'date', |
57
|
|
|
/* Boolean Type */ |
58
|
|
|
'boolean' => 'boolean' |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
public function __construct ( AbstractAdapter $adapterConfig ) |
62
|
|
|
{ |
63
|
|
|
parent::__construct ( $adapterConfig ); |
64
|
|
|
if ( $adapterConfig->hasSchemas () ) { |
65
|
|
|
$this->schema = $adapterConfig->getSchemas (); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retorna um Array com nome das tabelas |
72
|
|
|
* |
73
|
|
|
* @param void $schema |
|
|
|
|
74
|
|
|
* |
75
|
|
|
* @return string[] |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function getListNameTable () |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if ( empty( $this->tableList ) ) { |
80
|
|
|
|
81
|
|
|
$sqlTables = !empty($this->tablesName)?"AND table_name IN ( $this->tablesName )":''; |
82
|
|
|
|
83
|
|
|
$strSchema = implode ( "', '", $this->schema ); |
84
|
|
|
|
85
|
|
|
$this->tableList = $this->getPDO () |
|
|
|
|
86
|
|
|
->query ( |
87
|
|
|
"SELECT table_schema, |
88
|
|
|
table_name |
89
|
|
|
FROM information_schema.tables |
90
|
|
|
WHERE |
91
|
|
|
table_type = 'BASE TABLE' |
92
|
|
|
AND table_schema IN ( '$strSchema' ) $sqlTables |
93
|
|
|
ORDER by |
94
|
|
|
table_schema, |
95
|
|
|
table_name |
96
|
|
|
ASC" |
97
|
|
|
) |
98
|
|
|
->fetchAll (); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->tableList; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* retorna multiplos arrays com dados da column em array |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function getListColumns () |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$sqlTables = !empty($this->tablesName)?"AND c.table_name IN ( $this->tablesName )":''; |
112
|
|
|
$strSchema = implode ( "', '", $this->schema ); |
113
|
|
|
|
114
|
|
|
return $this->getPDO () |
115
|
|
|
->query ( |
116
|
|
|
"SELECT distinct |
117
|
|
|
c.table_schema, |
118
|
|
|
c.table_name, |
119
|
|
|
c.column_name , |
120
|
|
|
c.data_type, |
121
|
|
|
c.column_default, |
122
|
|
|
is_nullable, |
123
|
|
|
character_maximum_length AS max_length |
124
|
|
|
FROM |
125
|
|
|
information_schema.tables AS st |
126
|
|
|
INNER JOIN information_schema.columns AS c |
127
|
|
|
ON st.table_name=c.table_name and st.table_type = 'BASE TABLE' |
128
|
|
|
$sqlTables and c.table_schema IN ('$strSchema') |
129
|
|
|
order by c.table_name asc" |
130
|
|
|
) |
131
|
|
|
->fetchAll ( \PDO::FETCH_ASSOC ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function getListConstrant () |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$sqlTables = !empty($this->tablesName)?"AND tc.table_name IN ( $this->tablesName )":''; |
137
|
|
|
$strSchema = implode ( "', '", $this->schema ); |
138
|
|
|
|
139
|
|
|
return $this->getPDO () |
140
|
|
|
->query ( |
141
|
|
|
"SELECT distinct |
142
|
|
|
tc.constraint_type, |
143
|
|
|
tc.constraint_name, |
144
|
|
|
tc.table_schema, |
145
|
|
|
tc.table_name, |
146
|
|
|
kcu.column_name, |
147
|
|
|
ccu.table_schema AS foreign_schema, |
148
|
|
|
ccu.table_name AS foreign_table, |
149
|
|
|
ccu.column_name as foreign_column |
150
|
|
|
FROM |
151
|
|
|
information_schema.table_constraints AS tc |
152
|
|
|
JOIN information_schema.key_column_usage AS kcu |
153
|
|
|
ON tc.constraint_name = kcu.constraint_name |
154
|
|
|
AND tc.table_schema IN ('$strSchema') |
155
|
|
|
AND tc.constraint_type IN ('FOREIGN KEY','PRIMARY KEY') |
156
|
|
|
$sqlTables |
157
|
|
|
JOIN information_schema.constraint_column_usage AS ccu |
158
|
|
|
ON tc.constraint_name = ccu.constraint_name |
159
|
|
|
AND tc.constraint_schema = ccu.constraint_schema |
160
|
|
|
ORDER by tc.table_schema" |
161
|
|
|
) |
162
|
|
|
->fetchAll ( \PDO::FETCH_ASSOC ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Retorna o Nome da Sequence da tabela |
167
|
|
|
* |
168
|
|
|
* @param $table |
169
|
|
|
* @param $column |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function getSequence ( $table, $column, $schema = 0 ) |
174
|
|
|
{ |
175
|
|
|
$tableTemp = $table; |
176
|
|
|
if ( 0 !== $schema ) { |
177
|
|
|
$tableTemp = $schema . '.' . $table; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$pdo = $this->getPDO (); |
181
|
|
|
$return1 = $pdo->query ( "SELECT pg_get_serial_sequence('$tableTemp', '$column');" ) |
182
|
|
|
->fetchColumn (); |
183
|
|
|
|
184
|
|
|
if ( !is_null ( $return1 ) ) { |
185
|
|
|
return $return1; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$stmt = $pdo->prepare ( |
189
|
|
|
"SELECT distinct adsrc FROM pg_attrdef AS att |
190
|
|
|
INNER JOIN pg_class AS c |
191
|
|
|
ON adrelid = c.oid AND c.relname = ? --table |
192
|
|
|
INNER JOIN pg_attribute AS a |
193
|
|
|
ON att.adnum=a.attnum AND a.attname=? --column |
194
|
|
|
INNER JOIN pg_catalog.pg_namespace n |
195
|
|
|
ON n.oid = c.relnamespace and n.nspname=? --schema |
196
|
|
|
" |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$stmt->bindParam ( 1, $table ); |
200
|
|
|
$stmt->bindParam ( 2, $column ); |
201
|
|
|
$stmt->bindParam ( 3, $schema ); |
202
|
|
|
$stmt->execute (); |
203
|
|
|
$return2 = $stmt->fetchColumn (); |
204
|
|
|
|
205
|
|
|
if ( $return2 ) { |
206
|
|
|
return preg_filter ( |
207
|
|
|
array ( |
208
|
|
|
'/nextval\(\'/', |
209
|
|
|
'/\'::regclass\)/' |
210
|
|
|
), |
211
|
|
|
'', |
212
|
|
|
$return2 |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritDoc |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
public function getPDOString () |
223
|
|
|
{ |
224
|
|
|
return sprintf ( |
225
|
|
|
"pgsql:host=%s;port=%s;dbname=%s", |
226
|
|
|
$this->host, |
227
|
|
|
$this->port, |
228
|
|
|
$this->database |
229
|
|
|
|
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @inheritDoc |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function getPDOSocketString () |
238
|
|
|
{ |
239
|
|
|
return sprintf ( |
240
|
|
|
"pgsql:unix_socket=%s;dbname=%s", |
241
|
|
|
$this->socket, |
242
|
|
|
$this->database |
243
|
|
|
|
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* retorna o numero total de tabelas |
249
|
|
|
* |
250
|
|
|
* @return int |
251
|
|
|
*/ |
252
|
|
View Code Duplication |
public function getTotalTables () |
|
|
|
|
253
|
|
|
{ |
254
|
|
|
if ( empty( $this->totalTables ) ) { |
255
|
|
|
$sqlTables = !empty($this->tablesName)?"AND table_name IN ( $this->tablesName )":''; |
256
|
|
|
|
257
|
|
|
$strSchema = implode ( "', '", $this->schema ); |
258
|
|
|
|
259
|
|
|
$this->totalTables = $this->getPDO () |
260
|
|
|
->query ( |
261
|
|
|
"SELECT COUNT(table_name) AS total |
262
|
|
|
FROM information_schema.tables |
263
|
|
|
WHERE |
264
|
|
|
table_type = 'BASE TABLE' |
265
|
|
|
AND table_schema IN ( '" . $strSchema . "' ) $sqlTables" |
266
|
|
|
) |
267
|
|
|
->fetchColumn (); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return (int) $this->totalTables; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
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.