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 Mssql extends AbsractAdapter |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $port; |
26
|
|
|
|
27
|
|
|
protected $schema = array ( 'public' ); |
28
|
|
|
|
29
|
|
|
public function __construct ( AbstractAdapter $adapterConfig ) |
30
|
|
|
{ |
31
|
|
|
parent::__construct ( $adapterConfig ); |
32
|
|
|
if ( $adapterConfig->hasSchemas () ) { |
33
|
|
|
$this->schema = $adapterConfig->getSchemas (); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
|
|
protected function convertTypeToSimple ( $str ) |
42
|
|
|
{ |
43
|
|
|
if ( preg_match ( '/(tinyint|bit)/' , $str ) ) |
44
|
|
|
{ |
45
|
|
|
$res = 'boolean'; |
46
|
|
|
} elseif ( preg_match ( '/(date|time|text|binary|char|xml|uniqueidentifier)/' , $str ) ) |
47
|
|
|
{ |
48
|
|
|
$res = 'string'; |
49
|
|
|
} elseif ( preg_match ( '/(decimal|numeric|real|float|money)/' , $str ) ) |
50
|
|
|
{ |
51
|
|
|
$res = 'float'; |
52
|
|
|
} elseif ( preg_match ( '#^(?:tiny|small|medium|long|big|var)?(\w+)(?:\(\d+\))?(?:\s\w+)*$#' , $str , $matches ) ) |
53
|
|
|
{ |
54
|
|
|
$res = $matches[ 1 ]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $res; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function getHost(){ |
61
|
|
|
$host = $this->host; |
62
|
|
|
if (!empty($this->port)) { |
63
|
|
|
$seperator = ':'; |
64
|
|
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
65
|
|
|
$seperator = ','; |
66
|
|
|
} |
67
|
|
|
$host .= $seperator . $this->port; |
68
|
|
|
unset($this->port); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $host; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getPDOString () |
79
|
|
|
{ |
80
|
|
|
return sprintf ( |
81
|
|
|
"mssql:host=%s;dbname=%s" , |
82
|
|
|
$this->getHost() , |
83
|
|
|
$this->database |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getPDOSocketString () |
92
|
|
|
{ |
93
|
|
|
// TODO: implement here |
94
|
|
|
return ""; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritDoc |
99
|
|
|
* @return string[] |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function getListNameTable () |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if ( empty( $this->tableList ) ) |
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
$sqlTables = ! empty( $this->tablesName ) |
107
|
|
|
? "AND table_name IN ( $this->tablesName )" : ''; |
108
|
|
|
|
109
|
|
|
$strSchema = implode ( "', '" , $this->schema ); |
110
|
|
|
|
111
|
|
|
$this->tableList = $this->getPDO () |
|
|
|
|
112
|
|
|
->query ( |
113
|
|
|
"SELECT table_schema, |
114
|
|
|
table_name |
115
|
|
|
FROM {$this->database}.information_schema.tables |
116
|
|
|
WHERE |
117
|
|
|
table_type = 'BASE TABLE' |
118
|
|
|
AND table_schema IN ( '$strSchema' ) $sqlTables |
119
|
|
|
ORDER by |
120
|
|
|
table_schema, |
121
|
|
|
table_name |
122
|
|
|
ASC" |
123
|
|
|
) |
124
|
|
|
->fetchAll (); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->tableList; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* retorna multiplos arrays com dados da column em array |
132
|
|
|
* |
133
|
|
|
* @return array[] |
134
|
|
|
*/ |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* retorna multiplos arrays com dados da column em array |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function getListColumns () |
142
|
|
|
{ |
143
|
|
|
$sqlTables = ! empty( $this->tablesName ) |
144
|
|
|
? "AND c.table_name IN ( $this->tablesName )" : ''; |
145
|
|
|
$strSchema = implode ( "', '" , $this->schema ); |
146
|
|
|
|
147
|
|
|
return $this->getPDO () |
148
|
|
|
->query ( |
149
|
|
|
"SELECT distinct |
150
|
|
|
c.table_schema, |
151
|
|
|
c.table_name, |
152
|
|
|
c.column_name , |
153
|
|
|
c.data_type, |
154
|
|
|
is_nullable, |
155
|
|
|
character_maximum_length AS max_length |
156
|
|
|
FROM |
157
|
|
|
{$this->database}.information_schema.tables AS st |
158
|
|
|
INNER JOIN {$this->database}.information_schema.columns AS c |
159
|
|
|
ON st.table_name=c.table_name and st.table_type = 'BASE TABLE' |
160
|
|
|
$sqlTables and c.table_schema IN ('$strSchema') |
161
|
|
|
order by c.table_name asc" |
162
|
|
|
) |
163
|
|
|
->fetchAll ( \PDO::FETCH_ASSOC ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* retorna o numero total de tabelas |
168
|
|
|
* |
169
|
|
|
* @return int |
170
|
|
|
*/ |
171
|
|
View Code Duplication |
public function getTotalTables () |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
if ( empty( $this->totalTables ) ) |
174
|
|
|
{ |
175
|
|
|
$sqlTables = ! empty( $this->tablesName ) |
176
|
|
|
? "AND table_name IN ( $this->tablesName )" : ''; |
177
|
|
|
|
178
|
|
|
$strSchema = implode ( "', '" , $this->schema ); |
179
|
|
|
|
180
|
|
|
$this->totalTables = $this->getPDO () |
181
|
|
|
->query ( |
182
|
|
|
"SELECT COUNT(table_name) AS total |
183
|
|
|
FROM {$this->database}.information_schema.tables |
184
|
|
|
WHERE |
185
|
|
|
table_type = 'BASE TABLE' |
186
|
|
|
AND table_schema IN ( '" . $strSchema . "' ) $sqlTables" |
187
|
|
|
) |
188
|
|
|
->fetchColumn (); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return (int) $this->totalTables; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function getSequence ( $table , $column , $schema = 0 ) |
195
|
|
|
{ |
196
|
|
|
$return = $this->getPDO () |
197
|
|
|
->query ( |
198
|
|
|
"SELECT is_identity FROM sys.columns WHERE object_id = object_id('{$schema}.{$table}') AND name = '{$column}';" |
199
|
|
|
) |
200
|
|
|
->fetchColumn(); |
201
|
|
|
|
202
|
|
|
if ( ! $return ) |
203
|
|
|
{ |
204
|
|
|
return; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return "{$table}_{$column}_seq"; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getListConstrant () |
211
|
|
|
{ |
212
|
|
|
$sqlTables = ! empty( $this->tablesName ) |
213
|
|
|
? "AND tc.table_name IN ( $this->tablesName )" : ''; |
214
|
|
|
$strSchema = implode ( "', '" , $this->schema ); |
215
|
|
|
|
216
|
|
|
return $this->getPDO () |
217
|
|
|
->query ( |
218
|
|
|
"SELECT distinct |
219
|
|
|
tc.constraint_type, |
220
|
|
|
tc.constraint_name, |
221
|
|
|
tc.table_schema, |
222
|
|
|
tc.table_name, |
223
|
|
|
kcu.column_name, |
224
|
|
|
ccu.table_schema AS foreign_schema, |
225
|
|
|
ccu.table_name AS foreign_table, |
226
|
|
|
ccu.column_name as foreign_column |
227
|
|
|
FROM |
228
|
|
|
{$this->database}.information_schema.table_constraints AS tc |
229
|
|
|
JOIN {$this->database}.information_schema.key_column_usage AS kcu |
230
|
|
|
ON tc.constraint_name = kcu.constraint_name |
231
|
|
|
AND tc.table_schema IN ('$strSchema') |
232
|
|
|
AND tc.constraint_type IN ('FOREIGN KEY','PRIMARY KEY') |
233
|
|
|
$sqlTables |
234
|
|
|
JOIN {$this->database}.information_schema.constraint_column_usage AS ccu |
235
|
|
|
ON tc.constraint_name = ccu.constraint_name |
236
|
|
|
AND tc.constraint_schema = ccu.constraint_schema |
237
|
|
|
ORDER by tc.table_schema" |
238
|
|
|
) |
239
|
|
|
->fetchAll ( \PDO::FETCH_ASSOC ); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
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.