1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare - Setup * |
---|
4 | * http://www.egroupware.org * |
---|
5 | * -------------------------------------------- * |
---|
6 | * This file written by Michael Dean<mdean@users.sourceforge.net> * |
---|
7 | * and Miles Lott<milosch@groupwhere.org> * |
---|
8 | * -------------------------------------------- * |
---|
9 | * This program is free software; you can redistribute it and/or modify it * |
---|
10 | * under the terms of the GNU General Public License as published by the * |
---|
11 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
12 | * option) any later version. * |
---|
13 | \**************************************************************************/ |
---|
14 | |
---|
15 | |
---|
16 | class schema_proc_mysql |
---|
17 | { |
---|
18 | var $m_sStatementTerminator; |
---|
19 | /* Following added to convert sql to array */ |
---|
20 | var $sCol = array(); |
---|
21 | var $pk = array(); |
---|
22 | var $fk = array(); |
---|
23 | var $ix = array(); |
---|
24 | var $uc = array(); |
---|
25 | |
---|
26 | function schema_proc_mysql() |
---|
27 | { |
---|
28 | $this->m_sStatementTerminator = ';'; |
---|
29 | } |
---|
30 | |
---|
31 | /* Return a type suitable for DDL */ |
---|
32 | function TranslateType($sType, $iPrecision = 0, $iScale = 0) |
---|
33 | { |
---|
34 | $sTranslated = ''; |
---|
35 | switch($sType) |
---|
36 | { |
---|
37 | case 'auto': |
---|
38 | $sTranslated = 'int(11) auto_increment not null'; |
---|
39 | break; |
---|
40 | case 'blob': |
---|
41 | $sTranslated = 'blob'; |
---|
42 | break; |
---|
43 | case 'bool': |
---|
44 | $sTranslated = 'tinyint(1)'; |
---|
45 | break; |
---|
46 | case 'char': |
---|
47 | if($iPrecision > 0 && $iPrecision < 256) |
---|
48 | { |
---|
49 | $sTranslated = sprintf("char(%d)", $iPrecision); |
---|
50 | } |
---|
51 | if($iPrecision > 255) |
---|
52 | { |
---|
53 | $sTranslated = 'text'; |
---|
54 | } |
---|
55 | break; |
---|
56 | case 'date': |
---|
57 | $sTranslated = 'date'; |
---|
58 | break; |
---|
59 | case 'decimal': |
---|
60 | $sTranslated = sprintf("decimal(%d,%d)", $iPrecision, $iScale); |
---|
61 | break; |
---|
62 | case 'float': |
---|
63 | switch($iPrecision) |
---|
64 | { |
---|
65 | case 4: |
---|
66 | $sTranslated = 'float'; |
---|
67 | break; |
---|
68 | case 8: |
---|
69 | $sTranslated = 'double'; |
---|
70 | break; |
---|
71 | } |
---|
72 | break; |
---|
73 | case 'int': |
---|
74 | switch($iPrecision) |
---|
75 | { |
---|
76 | case 2: |
---|
77 | $sTranslated = 'smallint'; |
---|
78 | break; |
---|
79 | case 4: |
---|
80 | $sTranslated = 'int'; |
---|
81 | break; |
---|
82 | case 8: |
---|
83 | $sTranslated = 'bigint'; |
---|
84 | break; |
---|
85 | } |
---|
86 | break; |
---|
87 | case 'longtext': |
---|
88 | $sTranslated = 'longtext'; |
---|
89 | break; |
---|
90 | case 'text': |
---|
91 | $sTranslated = 'text'; |
---|
92 | break; |
---|
93 | case 'timestamp': |
---|
94 | $sTranslated = 'datetime'; |
---|
95 | break; |
---|
96 | case 'varchar': |
---|
97 | if($iPrecision > 0 && $iPrecision < 256) |
---|
98 | { |
---|
99 | $sTranslated = sprintf("varchar(%d)", $iPrecision); |
---|
100 | } |
---|
101 | if($iPrecision > 255) |
---|
102 | { |
---|
103 | $sTranslated = 'text'; |
---|
104 | } |
---|
105 | break; |
---|
106 | } |
---|
107 | return $sTranslated; |
---|
108 | } |
---|
109 | |
---|
110 | function TranslateDefault($sDefault) |
---|
111 | { |
---|
112 | switch($sDefault) |
---|
113 | { |
---|
114 | case 'current_date': |
---|
115 | case 'current_timestamp': |
---|
116 | $sDefault = 'now'; |
---|
117 | } |
---|
118 | return "'$sDefault'"; |
---|
119 | } |
---|
120 | |
---|
121 | /* Inverse of above, convert sql column types to array info */ |
---|
122 | function rTranslateType($sType, $iPrecision = 0, $iScale = 0) |
---|
123 | { |
---|
124 | $sTranslated = ''; |
---|
125 | if($sType == 'int' || $sType == 'tinyint' || $sType == 'smallint' || $sType == 'bigint') |
---|
126 | { |
---|
127 | if($iPrecision > 8) |
---|
128 | { |
---|
129 | $iPrecision = 8; |
---|
130 | } |
---|
131 | elseif($iPrecision > 4) |
---|
132 | { |
---|
133 | $iPrecision = 4; |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | $iPrecision = 2; |
---|
138 | } |
---|
139 | } |
---|
140 | switch($sType) |
---|
141 | { |
---|
142 | case 'tinyint': |
---|
143 | case 'smallint': |
---|
144 | $sTranslated = "'type' => 'int', 'precision' => 2"; |
---|
145 | break; |
---|
146 | case 'int': |
---|
147 | $sTranslated = "'type' => 'int', 'precision' => 4"; |
---|
148 | break; |
---|
149 | case 'bigint': |
---|
150 | $sTranslated = "'type' => 'int', 'precision' => 8"; |
---|
151 | break; |
---|
152 | case 'char': |
---|
153 | if($iPrecision > 0 && $iPrecision < 256) |
---|
154 | { |
---|
155 | $sTranslated = "'type' => 'char', 'precision' => $iPrecision"; |
---|
156 | } |
---|
157 | if($iPrecision > 255) |
---|
158 | { |
---|
159 | $sTranslated = "'type' => 'text'"; |
---|
160 | } |
---|
161 | break; |
---|
162 | case 'decimal': |
---|
163 | $sTranslated = "'type' => 'decimal', 'precision' => $iPrecision, 'scale' => $iScale"; |
---|
164 | break; |
---|
165 | case 'float': |
---|
166 | case 'double': |
---|
167 | $sTranslated = "'type' => 'float', 'precision' => $iPrecision"; |
---|
168 | break; |
---|
169 | case 'datetime': |
---|
170 | $sTranslated = "'type' => 'timestamp'"; |
---|
171 | break; |
---|
172 | case 'enum': |
---|
173 | /* Here comes a nasty assumption */ |
---|
174 | /* $sTranslated = "'type' => 'varchar', 'precision' => 255"; */ |
---|
175 | $sTranslated = "'type' => 'varchar', 'precision' => $iPrecision"; |
---|
176 | break; |
---|
177 | case 'varchar': |
---|
178 | if($iPrecision > 0 && $iPrecision < 256) |
---|
179 | { |
---|
180 | $sTranslated = "'type' => 'varchar', 'precision' => $iPrecision"; |
---|
181 | } |
---|
182 | if($iPrecision > 255) |
---|
183 | { |
---|
184 | $sTranslated = "'type' => 'text'"; |
---|
185 | } |
---|
186 | break; |
---|
187 | case 'longtext': |
---|
188 | case 'text': |
---|
189 | case 'blob': |
---|
190 | case 'date': |
---|
191 | $sTranslated = "'type' => '$sType'"; |
---|
192 | break; |
---|
193 | } |
---|
194 | return $sTranslated; |
---|
195 | } |
---|
196 | |
---|
197 | function GetPKSQL($sFields) |
---|
198 | { |
---|
199 | return "PRIMARY KEY($sFields)"; |
---|
200 | } |
---|
201 | |
---|
202 | function GetUCSQL($sFields) |
---|
203 | { |
---|
204 | return "UNIQUE($sFields)"; |
---|
205 | } |
---|
206 | |
---|
207 | function GetIXSQL($sFields,&$append,$options=False) |
---|
208 | { |
---|
209 | $append = False; |
---|
210 | $type = 'INDEX'; |
---|
211 | $length = ''; |
---|
212 | if(strtoupper($options) == 'FULLTEXT') |
---|
213 | { |
---|
214 | $type = 'FULLTEXT'; |
---|
215 | } |
---|
216 | if(is_numeric($options)) |
---|
217 | { |
---|
218 | $length = "($options)"; |
---|
219 | } |
---|
220 | return "$type($sFields $length)"; |
---|
221 | } |
---|
222 | |
---|
223 | function _GetColumns($oProc, $sTableName, &$sColumns, $sDropColumn = '') |
---|
224 | { |
---|
225 | $sColumns = ''; |
---|
226 | $this->pk = array(); |
---|
227 | $this->fk = array(); |
---|
228 | $this->ix = array(); |
---|
229 | $this->uc = array(); |
---|
230 | |
---|
231 | /* Field, Type, Null, Key, Default, Extra */ |
---|
232 | $oProc->m_odb->query("describe $sTableName"); |
---|
233 | while($oProc->m_odb->next_record()) |
---|
234 | { |
---|
235 | $type = $default = $null = $nullcomma = $prec = $scale = $ret = $colinfo = $scales = ''; |
---|
236 | if($sColumns != '') |
---|
237 | { |
---|
238 | $sColumns .= ','; |
---|
239 | } |
---|
240 | $sColumns .= $oProc->m_odb->f(0); |
---|
241 | |
---|
242 | /* The rest of this is used only for SQL->array */ |
---|
243 | $colinfo = explode('(',$oProc->m_odb->f(1)); |
---|
244 | $prec = ereg_replace('\).*','',$colinfo[1]); |
---|
245 | $scales = explode(',',$prec); |
---|
246 | |
---|
247 | if($colinfo[0] == 'enum') |
---|
248 | { |
---|
249 | /* set prec to length of longest enum-value */ |
---|
250 | for($prec=0; list($nul,$name) = @each($scales);) |
---|
251 | { |
---|
252 | if($prec < (strlen($name) - 2)) |
---|
253 | { |
---|
254 | /* -2 as name is like "'name'" */ |
---|
255 | $prec = (strlen($name) - 2); |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | elseif($scales[1]) |
---|
260 | { |
---|
261 | $prec = $scales[0]; |
---|
262 | $scale = $scales[1]; |
---|
263 | } |
---|
264 | $type = $this->rTranslateType($colinfo[0], $prec, $scale); |
---|
265 | |
---|
266 | if($oProc->m_odb->f(2) == 'YES') |
---|
267 | { |
---|
268 | $null = "'nullable' => True"; |
---|
269 | } |
---|
270 | else |
---|
271 | { |
---|
272 | $null = "'nullable' => False"; |
---|
273 | } |
---|
274 | if($oProc->m_odb->f(4) != '') |
---|
275 | { |
---|
276 | $default = "'default' => '".$oProc->m_odb->f(4)."'"; |
---|
277 | $nullcomma = ','; |
---|
278 | } |
---|
279 | else |
---|
280 | { |
---|
281 | $default = ''; |
---|
282 | $nullcomma = ''; |
---|
283 | } |
---|
284 | if($oProc->m_odb->f(5)) |
---|
285 | { |
---|
286 | $type = "'type' => 'auto'"; |
---|
287 | } |
---|
288 | $this->sCol[] = "\t\t\t\t'" . $oProc->m_odb->f(0)."' => array(" . $type . ',' . $null . $nullcomma . $default . '),' . "\n"; |
---|
289 | /* |
---|
290 | if($oProc->m_odb->f(3) == 'PRI') |
---|
291 | { |
---|
292 | $this->pk[] = $oProc->m_odb->f(0); |
---|
293 | } |
---|
294 | if($oProc->m_odb->f(3) == 'UNI') |
---|
295 | { |
---|
296 | $this->uc[] = $oProc->m_odb->f(0); |
---|
297 | } |
---|
298 | // index over multiple columns |
---|
299 | if($oProc->m_odb->f(3) == 'MUL') |
---|
300 | { |
---|
301 | $this->ix[] = $oProc->m_odb->f(0); |
---|
302 | } |
---|
303 | */ |
---|
304 | } |
---|
305 | $this->_GetIndices($oProc,$sTableName,$this->pk,$this->ix,$this->uc,$this->fk); |
---|
306 | |
---|
307 | /* ugly as heck, but is here to chop the trailing comma on the last element (for php3) */ |
---|
308 | $this->sCol[count($this->sCol) - 1] = substr($this->sCol[count($this->sCol) - 1],0,-2) . "\n"; |
---|
309 | |
---|
310 | return false; |
---|
311 | } |
---|
312 | |
---|
313 | function _GetIndices($oProc,$sTableName,&$aPk,&$aIx,&$aUc,&$aFk) |
---|
314 | { |
---|
315 | $aPk = $aIx = $aUc = $aFk = array(); |
---|
316 | $seq = $ix = $uc = 0; |
---|
317 | |
---|
318 | $oProc->m_odb->query("show index from $sTableName"); |
---|
319 | while($oProc->m_odb->next_record()) |
---|
320 | { |
---|
321 | if($seq >= $oProc->m_odb->f('Seq_in_index')) // new index started |
---|
322 | { |
---|
323 | $$type += 1; |
---|
324 | } |
---|
325 | if($oProc->m_odb->f('Key_name') == 'PRIMARY') // pk |
---|
326 | { |
---|
327 | $aPk[] = $oProc->m_odb->f('Column_name'); |
---|
328 | $type = 'pk'; |
---|
329 | } |
---|
330 | elseif($oProc->m_odb->f('Non_unique')) // ix |
---|
331 | { |
---|
332 | $aIx[$ix][] = $oProc->m_odb->f('Column_name'); |
---|
333 | $type = 'ix'; |
---|
334 | if($oProc->m_odb->f('Comment') == 'FULLTEXT') |
---|
335 | { |
---|
336 | $aIx[$ix]['options'] = array('mysql' => 'FULLTEXT'); |
---|
337 | } |
---|
338 | elseif((int)$oProc->m_odb->f('Sub_part')) |
---|
339 | { |
---|
340 | $aIx[$ix]['options'] = array('mysql' => (int)$oProc->m_odb->f('Sub_part')); |
---|
341 | } |
---|
342 | } |
---|
343 | else // uc |
---|
344 | { |
---|
345 | $aUc[$uc][] = $oProc->m_odb->f('Column_name'); |
---|
346 | $type = 'uc'; |
---|
347 | } |
---|
348 | $seq = $oProc->m_odb->f('Seq_in_index'); |
---|
349 | } |
---|
350 | //echo "Indices from $sTableName<pre>pk=".print_r($aPk,True)."\nix=".print_r($aIx,True)."\nuc=".print_r($aUc,True)."</pre>\n"; |
---|
351 | } |
---|
352 | |
---|
353 | function DropTable($oProc, &$aTables, $sTableName) |
---|
354 | { |
---|
355 | return !!($oProc->m_odb->query("DROP TABLE " . $sTableName)); |
---|
356 | } |
---|
357 | |
---|
358 | function DropColumn($oProc, &$aTables, $sTableName, $aNewTableDef, $sColumnName, $bCopyData = true) |
---|
359 | { |
---|
360 | return !!($oProc->m_odb->query("ALTER TABLE $sTableName DROP COLUMN $sColumnName")); |
---|
361 | } |
---|
362 | |
---|
363 | function RenameTable($oProc, &$aTables, $sOldTableName, $sNewTableName) |
---|
364 | { |
---|
365 | return !!($oProc->m_odb->query("ALTER TABLE $sOldTableName RENAME $sNewTableName")); |
---|
366 | } |
---|
367 | |
---|
368 | function RenameColumn($oProc, &$aTables, $sTableName, $sOldColumnName, $sNewColumnName, $bCopyData = true) |
---|
369 | { |
---|
370 | /* |
---|
371 | TODO: This really needs testing - it can affect primary keys, and other table-related objects |
---|
372 | like sequences and such |
---|
373 | */ |
---|
374 | if($GLOBALS['DEBUG']) { echo '<br>RenameColumn: calling _GetFieldSQL for ' . $sNewColumnName; } |
---|
375 | if($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sNewColumnName], $sNewColumnSQL)) |
---|
376 | { |
---|
377 | return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sOldColumnName $sNewColumnName " . $sNewColumnSQL)); |
---|
378 | } |
---|
379 | return false; |
---|
380 | } |
---|
381 | |
---|
382 | function AlterColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef, $bCopyData = true) |
---|
383 | { |
---|
384 | if($GLOBALS['DEBUG']) { echo '<br>AlterColumn: calling _GetFieldSQL for ' . $sNewColumnName; } |
---|
385 | if($oProc->_GetFieldSQL($aTables[$sTableName]["fd"][$sColumnName], $sNewColumnSQL)) |
---|
386 | { |
---|
387 | return !!($oProc->m_odb->query("ALTER TABLE $sTableName MODIFY $sColumnName " . $sNewColumnSQL)); |
---|
388 | /* return !!($oProc->m_odb->query("ALTER TABLE $sTableName CHANGE $sColumnName $sColumnName " . $sNewColumnSQL)); */ |
---|
389 | } |
---|
390 | |
---|
391 | return false; |
---|
392 | } |
---|
393 | |
---|
394 | function AddColumn($oProc, &$aTables, $sTableName, $sColumnName, &$aColumnDef) |
---|
395 | { |
---|
396 | $oProc->_GetFieldSQL($aColumnDef, $sFieldSQL); |
---|
397 | $query = "ALTER TABLE $sTableName ADD COLUMN $sColumnName $sFieldSQL"; |
---|
398 | |
---|
399 | return !!($oProc->m_odb->query($query)); |
---|
400 | } |
---|
401 | |
---|
402 | function GetSequenceSQL($sTableName, &$sSequenceSQL) |
---|
403 | { |
---|
404 | $sSequenceSQL = ''; |
---|
405 | return true; |
---|
406 | } |
---|
407 | |
---|
408 | function CreateTable($oProc, &$aTables, $sTableName, $aTableDef) |
---|
409 | { |
---|
410 | if($oProc->_GetTableSQL($sTableName, $aTableDef, $sTableSQL, $sSequenceSQL,$append_ix)) |
---|
411 | { |
---|
412 | /* create sequence first since it will be needed for default */ |
---|
413 | if($sSequenceSQL != '') |
---|
414 | { |
---|
415 | $oProc->m_odb->query($sSequenceSQL); |
---|
416 | } |
---|
417 | |
---|
418 | $query = "CREATE TABLE $sTableName ($sTableSQL)"; |
---|
419 | // override default table type, if a fulltext index is requested, as only MyISAM can do fulltext |
---|
420 | if (stristr($sTableSQL,'FULLTEXT') !== false) |
---|
421 | { |
---|
422 | $query .= 'TYPE=MyISAM'; |
---|
423 | } |
---|
424 | return !!($oProc->m_odb->query($query)); |
---|
425 | } |
---|
426 | |
---|
427 | return false; |
---|
428 | } |
---|
429 | } |
---|
430 | ?> |
---|