1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Classes\AdaptersDriver; |
4
|
|
|
|
5
|
|
|
use Classes\Db\Column; |
6
|
|
|
use Classes\Db\Constrant; |
7
|
|
|
use Classes\Db\DbTable; |
8
|
|
|
|
9
|
|
|
require_once 'Classes/AdaptersDriver/AbsractAdapter.php'; |
10
|
|
|
require_once 'Classes/Db/Column.php'; |
11
|
|
|
require_once 'Classes/Db/Constrant.php'; |
12
|
|
|
require_once 'Classes/Db/DbTable.php'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Pedro Alarcao <[email protected]> |
16
|
|
|
* @link https://github.com/pedro151/orm-generator |
17
|
|
|
*/ |
18
|
|
|
class Mysql extends AbsractAdapter |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $port = 3306; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* converts MySQL data types to Simple data types |
28
|
|
|
* |
29
|
|
|
* @param string $str |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
protected function convertTypeToSimple ( $str ) |
34
|
|
|
{ |
35
|
|
|
$res = ''; |
36
|
|
|
if ( preg_match ( '/(tinyint\(1\)|bit)/', $str ) ) { |
37
|
|
|
$res = 'boolean'; |
38
|
|
|
} |
39
|
|
|
elseif ( preg_match ( '/(timestamp|blob|char|enum)/', $str ) ) { |
40
|
|
|
$res = 'string'; |
41
|
|
|
} |
42
|
|
|
elseif ( preg_match ( '/(text)/', $str ) ) { |
43
|
|
|
$res = 'text'; |
44
|
|
|
} |
45
|
|
|
elseif ( preg_match ( '/(decimal|numeric|float|double)/', $str ) ) { |
46
|
|
|
$res = 'float'; |
47
|
|
|
} |
48
|
|
|
elseif ( preg_match ( '#^(?:tiny|small|medium|long|big|var)?(\w+)(?:\(\d+\))?(?:\s\w+)*$#', $str, $matches ) ) { |
49
|
|
|
$res = $matches[ 1 ]; |
50
|
|
|
} |
51
|
|
|
elseif ( preg_match ( '/(date)/', $str ) ) { |
52
|
|
|
$res = 'date'; |
53
|
|
|
} |
54
|
|
|
elseif ( preg_match ( '/(datetime)/', $str ) ) { |
55
|
|
|
$res = 'datetime'; |
56
|
|
|
} |
57
|
|
|
else { |
58
|
|
|
print "Can't convert column type to Simple - Unrecognized type: $str"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $res; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getPDOString () |
69
|
|
|
{ |
70
|
|
|
return sprintf ( |
71
|
|
|
"mysql:host=%s;port=%s;dbname=%s", |
72
|
|
|
$this->host, |
73
|
|
|
$this->port, |
74
|
|
|
$this->database |
75
|
|
|
|
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritDoc |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getPDOSocketString () |
84
|
|
|
{ |
85
|
|
|
return sprintf ( |
86
|
|
|
"mysql:unix_socket=%s;dbname=%s", |
87
|
|
|
$this->socket, |
88
|
|
|
$this->database |
89
|
|
|
|
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
* @return string[] |
96
|
|
|
*/ |
97
|
|
|
public function getListNameTable () |
98
|
|
|
{ |
99
|
|
|
if ( empty( $this->tableList ) ) { |
100
|
|
|
$this->tableList = $this->getPDO () |
|
|
|
|
101
|
|
|
->query ( |
102
|
|
|
"show tables" |
103
|
|
|
) |
104
|
|
|
->fetchAll (); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->tableList; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* retorna multiplos arrays com dados da column em array |
112
|
|
|
* |
113
|
|
|
* @return array[] |
114
|
|
|
*/ |
115
|
|
|
public function getListColumns () |
116
|
|
|
{ |
117
|
|
|
|
118
|
|
|
return $this->getPDO () |
119
|
|
|
->query ( |
120
|
|
|
"select |
121
|
|
|
0 AS table_schema, |
122
|
|
|
table_name, |
123
|
|
|
column_name , |
124
|
|
|
data_type, |
125
|
|
|
is_nullable, |
126
|
|
|
character_maximum_length AS max_length |
127
|
|
|
from information_schema.columns |
128
|
|
|
where table_schema IN ('{$this->database}') |
129
|
|
|
order by table_name,ordinal_position" |
130
|
|
|
) |
131
|
|
|
->fetchAll ( \PDO::FETCH_ASSOC ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function getListConstrant () |
138
|
|
|
{ |
139
|
|
|
return $this->getPDO () |
140
|
|
|
->query ( |
141
|
|
|
"SELECT distinct |
142
|
|
|
i.constraint_type, |
143
|
|
|
k.constraint_name, |
144
|
|
|
-- k.table_schema, |
145
|
|
|
0 AS table_schema, |
146
|
|
|
k.table_name, |
147
|
|
|
k.column_name, |
148
|
|
|
-- k.REFERENCED_TABLE_SCHEMA AS foreign_schema, |
149
|
|
|
0 AS foreign_schema, |
150
|
|
|
k.REFERENCED_TABLE_NAME AS foreign_table, |
151
|
|
|
k.REFERENCED_COLUMN_NAME AS foreign_column |
152
|
|
|
FROM information_schema.TABLE_CONSTRAINTS as i |
153
|
|
|
inner join information_schema.key_column_usage as k |
154
|
|
|
ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME and k.TABLE_SCHEMA <> 'mysql' |
155
|
|
|
WHERE |
156
|
|
|
i.TABLE_SCHEMA IN ('{$this->database}') AND i.CONSTRAINT_TYPE IN ('FOREIGN KEY', 'PRIMARY KEY' ) |
157
|
|
|
order by k.table_schema, k.table_name;" |
158
|
|
|
) |
159
|
|
|
->fetchAll ( \PDO::FETCH_ASSOC ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* retorna o numero total de tabelas |
164
|
|
|
* |
165
|
|
|
* @return int |
166
|
|
|
*/ |
167
|
|
|
public function getTotalTables () |
168
|
|
|
{ |
169
|
|
|
if ( empty( $this->totalTables ) ) { |
170
|
|
|
|
171
|
|
|
$this->totalTables = $this->getPDO () |
172
|
|
|
->query ( |
173
|
|
|
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{$this->database}'" |
174
|
|
|
) |
175
|
|
|
->fetchColumn (); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return (int) $this->totalTables; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritDoc |
183
|
|
|
* |
184
|
|
|
* @param $table |
185
|
|
|
* @param $column |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function getSequence ( $table, $column, $schema = 0 ) |
190
|
|
|
{ |
191
|
|
|
$return = $this->getPDO () |
192
|
|
|
->query ( |
193
|
|
|
"select * from information_schema.columns where extra like '%auto_increment%' and TABLE_SCHEMA='{$this->database}' AND TABLE_NAME='{$table}' AND COLUMN_NAME='{$column}';" |
194
|
|
|
) |
195
|
|
|
->fetch ( \PDO::FETCH_ASSOC ); |
196
|
|
|
|
197
|
|
|
if ( !$return ) { |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return "{$return['TABLE_NAME']}_{$return['COLUMN_NAME']}_seq"; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
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.