MDL-19349 upgrade_plugin_savepoint() added 2 suggestions
[moodle.git] / admin / xmldb / actions / view_table_php / view_table_php.class.php
CommitLineData
c5a6ae17 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
dcbef199 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
c5a6ae17 11// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
12// //
13// This program is free software; you can redistribute it and/or modify //
14// it under the terms of the GNU General Public License as published by //
15// the Free Software Foundation; either version 2 of the License, or //
16// (at your option) any later version. //
17// //
18// This program is distributed in the hope that it will be useful, //
19// but WITHOUT ANY WARRANTY; without even the implied warranty of //
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21// GNU General Public License for more details: //
22// //
23// http://www.gnu.org/copyleft/gpl.html //
24// //
25///////////////////////////////////////////////////////////////////////////
26
27/// This class will show the PHP needed to perform the desired action
28/// with the specified fields/keys/indexes.
29
30class view_table_php extends XMLDBAction {
31
eef868d1 32 /**
c5a6ae17 33 * Init method, every subclass will have its own
34 */
35 function init() {
36 parent::init();
37
38 /// Set own custom attributes
39
40 /// Get needed strings
41 $this->loadStrings(array(
42 'selectaction' => 'xmldb',
43 'selectfieldkeyindex' => 'xmldb',
44 'view' => 'xmldb',
eafc2b70 45 'table' => 'xmldb',
313b5a35 46 'selectonecommand' => 'xmldb',
eafc2b70 47 'selectonefieldkeyindex' => 'xmldb',
313b5a35 48 'mustselectonefield' => 'xmldb',
49 'mustselectonekey' => 'xmldb',
50 'mustselectoneindex' => 'xmldb',
c5a6ae17 51 'back' => 'xmldb'
52 ));
53 }
54
55 /**
56 * Invoke method, every class will have its own
57 * returns true/false on completion, setting both
58 * errormsg and output as necessary
59 */
60 function invoke() {
61 parent::invoke();
62
63 $result = true;
64
65 /// Set own core attributes
66 $this->does_generate = ACTION_GENERATE_HTML;
67
68 /// These are always here
69 global $CFG, $XMLDB;
70
71 /// Do the job, setting result as needed
72 /// Get the dir containing the file
73 $dirpath = required_param('dir', PARAM_PATH);
294ce987 74 $dirpath = $CFG->dirroot . $dirpath;
c5a6ae17 75
76 /// Get the correct dirs
77 if (!empty($XMLDB->dbdirs)) {
78 $dbdir =& $XMLDB->dbdirs[$dirpath];
79 } else {
80 return false;
81 }
82 if (!empty($XMLDB->editeddirs)) {
83 $editeddir =& $XMLDB->editeddirs[$dirpath];
84 $structure =& $editeddir->xml_file->getStructure();
85 }
86 /// ADD YOUR CODE HERE
87
88 $tableparam = required_param('table', PARAM_PATH);
eef868d1 89
c5a6ae17 90 $table =& $structure->getTable($tableparam);
91 $fields = $table->getFields();
92 $field = reset($fields);
93 $defaultfieldkeyindex = null;
94 if ($field) {
eafc2b70 95 $defaultfieldkeyindex = 'f#' . $field->getName();
c5a6ae17 96 }
eafc2b70 97 $keys = $table->getKeys();
98 $indexes = $table->getIndexes();
c5a6ae17 99
100 /// Get parameters
101 $commandparam = optional_param('command', 'add_field', PARAM_PATH);
3be91b01 102 $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH);
103 $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///Strip the initials
313b5a35 104 $fieldkeyindexinitial = substr($origfieldkeyindexparam, 0, 1); //To know what we have selected
c5a6ae17 105
106 /// The back to edit xml button
007456de 107 $b = ' <p class="centerpara buttons">';
eafc2b70 108 $b .= '<a href="index.php?action=edit_table&amp;dir=' . urlencode(str_replace($CFG->dirroot, '', $dirpath)) . '&amp;table=' . $tableparam . '">[' . $this->str['back'] . ']</a>';
c5a6ae17 109 $b .= '</p>';
110 $o = $b;
111
eafc2b70 112 /// The table currently being edited
113 $o .= '<h3 class="main">' . $this->str['table'] . ': ' . s($tableparam) . '</h3>';
114
313b5a35 115 /// To indent the menu selections
116 $optionspacer = '&nbsp;&nbsp;&nbsp;';
117
c5a6ae17 118 /// Calculate the popup of commands
313b5a35 119 $commands = array('Fields',
120 $optionspacer . 'add_field',
121 $optionspacer . 'drop_field',
49173d25 122 $optionspacer . 'rename_field',
4fae686b 123 $optionspacer . 'change_field_type',
313b5a35 124 $optionspacer . 'change_field_precision',
125 $optionspacer . 'change_field_unsigned',
126 $optionspacer . 'change_field_notnull',
313b5a35 127 $optionspacer . 'change_field_default',
2a88f626 128 $optionspacer . 'drop_enum_from_field', /// TODO: Moodle 2.1 - Drop drop_enum_from_field
313b5a35 129 'Keys',
ceac2552 130 $optionspacer . 'add_key',
131 $optionspacer . 'drop_key',
132 $optionspacer . 'rename_key',
133 'Indexes',
134 $optionspacer . 'add_index',
135 $optionspacer . 'drop_index',
136 $optionspacer . 'rename_index');
c5a6ae17 137 foreach ($commands as $command) {
313b5a35 138 $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
c5a6ae17 139 }
eafc2b70 140 /// Calculate the popup of fields/keys/indexes
eafc2b70 141 if ($fields) {
3be91b01 142 $popfields['fieldshead'] = 'Fields';
eafc2b70 143 foreach ($fields as $field) {
3be91b01 144 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
eafc2b70 145 }
146 }
147 if ($keys) {
3be91b01 148 $popfields['keyshead'] = 'Keys';
eafc2b70 149 foreach ($keys as $key) {
3be91b01 150 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
eafc2b70 151 }
152 }
153 if ($indexes) {
3be91b01 154 $popfields['indexeshead'] = 'Indexes';
eafc2b70 155 foreach ($indexes as $index) {
3be91b01 156 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
eafc2b70 157 }
c5a6ae17 158 }
eafc2b70 159
c5a6ae17 160 /// Now build the form
161 $o.= '<form id="form" action="index.php" method="post">';
007456de 162 $o.= '<div>';
c5a6ae17 163 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot, '', $dirpath) . '" />';
eafc2b70 164 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
c5a6ae17 165 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
007456de 166 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
3be91b01 167 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . '&nbsp;<label for="fieldkeyindex" accesskey="f">' . $this->str['selectfieldkeyindex'] . ' </label>' .choose_from_menu($popfields, 'fieldkeyindex', $origfieldkeyindexparam, '', '', 0, true) . '</td></tr>';
c5a6ae17 168 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str['view'] . '" /></td></tr>';
169 $o.= ' </table>';
007456de 170 $o.= '</div></form>';
eafc2b70 171
007456de 172 $o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
c5a6ae17 173 $o.= ' <tr><td><textarea cols="80" rows="32">';
eafc2b70 174 /// Check we have selected some field/key/index from the popup
175 if ($fieldkeyindexparam == 'fieldshead' || $fieldkeyindexparam == 'keyshead' || $fieldkeyindexparam == 'indexeshead') {
176 $o.= s($this->str['selectonefieldkeyindex']);
313b5a35 177 /// Check we have selected some command from the popup
178 } else if ($commandparam == 'Fields' || $commandparam == 'Keys' || $commandparam == 'Indexes') {
179 $o.= s($this->str['selectonecommand']);
180 } else {
eafc2b70 181 /// Based on current params, call the needed function
182 switch ($commandparam) {
3be91b01 183 case 'add_field':
313b5a35 184 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
185 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
186 } else {
187 $o.= $this->str['mustselectonefield'];
188 }
eafc2b70 189 break;
3be91b01 190 case 'drop_field':
313b5a35 191 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
192 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
193 } else {
194 $o.= $this->str['mustselectonefield'];
195 }
eafc2b70 196 break;
3be91b01 197 case 'rename_field':
313b5a35 198 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
199 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
200 } else {
201 $o.= $this->str['mustselectonefield'];
202 }
eafc2b70 203 break;
4fae686b 204 case 'change_field_type':
205 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
206 $o.= s($this->change_field_type_php($structure, $tableparam, $fieldkeyindexparam));
207 } else {
208 $o.= $this->str['mustselectonefield'];
209 }
210 break;
1934b522 211 case 'change_field_precision':
313b5a35 212 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
213 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
214 } else {
215 $o.= $this->str['mustselectonefield'];
216 }
1934b522 217 break;
218 case 'change_field_unsigned':
313b5a35 219 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
220 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
221 } else {
222 $o.= $this->str['mustselectonefield'];
223 }
1934b522 224 break;
225 case 'change_field_notnull':
313b5a35 226 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
227 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
228 } else {
229 $o.= $this->str['mustselectonefield'];
230 }
1934b522 231 break;
2a88f626 232 case 'drop_enum_from_field': /// TODO: Moodle 2.1 - Drop drop_enum_from_field
49173d25 233 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
2a88f626 234 $o.= s($this->drop_enum_from_field_php($structure, $tableparam, $fieldkeyindexparam));
49173d25 235 } else {
236 $o.= $this->str['mustselectonefield'];
237 }
238 break;
1934b522 239 case 'change_field_default':
313b5a35 240 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
241 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
242 } else {
243 $o.= $this->str['mustselectonefield'];
244 }
1934b522 245 break;
ceac2552 246 case 'add_key':
247 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
248 $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
249 } else {
250 $o.= $this->str['mustselectonekey'];
251 }
252 break;
253 case 'drop_key':
254 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
255 $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
256 } else {
257 $o.= $this->str['mustselectonekey'];
258 }
259 break;
260 case 'rename_key':
261 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
262 $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
263 } else {
264 $o.= $this->str['mustselectonekey'];
265 }
266 break;
267 case 'add_index':
268 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
269 $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
270 } else {
271 $o.= $this->str['mustselectoneindex'];
272 }
273 break;
274 case 'drop_index':
275 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
276 $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
277 } else {
278 $o.= $this->str['mustselectoneindex'];
279 }
280 break;
281 case 'rename_index':
282 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
283 $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
284 } else {
285 $o.= $this->str['mustselectoneindex'];
286 }
287 break;
eafc2b70 288 }
c5a6ae17 289 }
290 $o.= '</textarea></td></tr>';
291 $o.= ' </table>';
292
293 $this->output = $o;
294
295 /// Launch postaction if exists (leave this here!)
eef868d1 296 if ($this->getPostAction() && $result) {
c5a6ae17 297 return $this->launch($this->getPostAction());
298 }
299
300 /// Return ok if arrived here
301 return $result;
302 }
303
304 /**
eef868d1 305 * This function will generate all the PHP code needed to
3be91b01 306 * create one field using XMLDB objects and functions
eef868d1 307 *
46293bd7 308 * @param xmldb_structure structure object containing all the info
3be91b01 309 * @param string table table name
310 * @param string field field name to be created
311 * @return string PHP code to be used to create the field
c5a6ae17 312 */
3be91b01 313 function add_field_php($structure, $table, $field) {
c5a6ae17 314
315 $result = '';
316 /// Validate if we can do it
317 if (!$table = $structure->getTable($table)) {
318 return false;
319 }
3be91b01 320 if (!$field = $table->getField($field)) {
321 return false;
322 }
c5a6ae17 323 if ($table->getAllErrors()) {
324 return false;
325 }
326
327 /// Add the standard PHP header
328 $result .= XMLDB_PHP_HEADER;
329
330 /// Add contents
331 $result .= XMLDB_LINEFEED;
3be91b01 332 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 333 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 334 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 335
336 /// Launch the proper DDL
337 $result .= XMLDB_LINEFEED;
82bba3e4 338 $result .= ' /// Conditionally launch add field ' . $field->getName() . XMLDB_LINEFEED;
cdb1e987 339 $result .= ' if (!$dbman->field_exists($table, $field)) {'. XMLDB_LINEFEED;
ef088e70 340 $result .= ' $dbman->add_field($table, $field);' . XMLDB_LINEFEED;
82bba3e4 341 $result .= ' }'. XMLDB_LINEFEED;
c5a6ae17 342
dcbef199 343 /// Add the proper upgrade_xxxx_savepoint call
344 $result .= $this->upgrade_savepoint_php ($structure);
345
c5a6ae17 346 /// Add standard PHP footer
347 $result .= XMLDB_PHP_FOOTER;
348
349 return $result;
350 }
351
352 /**
eef868d1 353 * This function will generate all the PHP code needed to
3be91b01 354 * drop one field using XMLDB objects and functions
eef868d1 355 *
46293bd7 356 * @param xmldb_structure structure object containing all the info
3be91b01 357 * @param string table table name
358 * @param string field field name to be dropped
359 * @return string PHP code to be used to drop the field
c5a6ae17 360 */
3be91b01 361 function drop_field_php($structure, $table, $field) {
c5a6ae17 362
363 $result = '';
364 /// Validate if we can do it
365 if (!$table = $structure->getTable($table)) {
366 return false;
367 }
3be91b01 368 if (!$field = $table->getField($field)) {
369 return false;
370 }
c5a6ae17 371 if ($table->getAllErrors()) {
372 return false;
373 }
374
375 /// Add the standard PHP header
376 $result .= XMLDB_PHP_HEADER;
377
378 /// Add contents
379 $result .= XMLDB_LINEFEED;
3be91b01 380 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 381 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
382 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 383
384 /// Launch the proper DDL
385 $result .= XMLDB_LINEFEED;
82bba3e4 386 $result .= ' /// Conditionally launch drop field ' . $field->getName() . XMLDB_LINEFEED;
cdb1e987 387 $result .= ' if ($dbman->field_exists($table, $field)) {' . XMLDB_LINEFEED;
ef088e70 388 $result .= ' $dbman->drop_field($table, $field);' . XMLDB_LINEFEED;
82bba3e4 389 $result .= ' }' . XMLDB_LINEFEED;
c5a6ae17 390
dcbef199 391 /// Add the proper upgrade_xxxx_savepoint call
392 $result .= $this->upgrade_savepoint_php ($structure);
393
c5a6ae17 394 /// Add standard PHP footer
395 $result .= XMLDB_PHP_FOOTER;
396
397 return $result;
398 }
399
400 /**
eef868d1 401 * This function will generate all the PHP code needed to
3be91b01 402 * rename one field using XMLDB objects and functions
eef868d1 403 *
46293bd7 404 * @param xmldb_structure structure object containing all the info
3be91b01 405 * @param string table table name
406 * @param string field field name to be renamed
ceac2552 407 * @return string PHP code to be used to rename the field
c5a6ae17 408 */
3be91b01 409 function rename_field_php($structure, $table, $field) {
c5a6ae17 410
411 $result = '';
412 /// Validate if we can do it
413 if (!$table = $structure->getTable($table)) {
414 return false;
415 }
3be91b01 416 if (!$field = $table->getField($field)) {
417 return false;
418 }
c5a6ae17 419 if ($table->getAllErrors()) {
420 return false;
421 }
422
423 /// Add the standard PHP header
424 $result .= XMLDB_PHP_HEADER;
425
426 /// Add contents
427 $result .= XMLDB_LINEFEED;
3be91b01 428 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED;
a8cb94f6 429 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 430 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
c5a6ae17 431
432 /// Launch the proper DDL
433 $result .= XMLDB_LINEFEED;
3be91b01 434 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 435 $result .= ' $dbman->rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
c5a6ae17 436
dcbef199 437 /// Add the proper upgrade_xxxx_savepoint call
438 $result .= $this->upgrade_savepoint_php ($structure);
439
c5a6ae17 440 /// Add standard PHP footer
441 $result .= XMLDB_PHP_FOOTER;
442
443 return $result;
444 }
3be91b01 445
4fae686b 446 /**
447 * This function will generate all the PHP code needed to
448 * change the type of one field using XMLDB objects and functions.
449 * Currently these conversions are supported:
450 * integer to char
451 * char to integer
452 * number to char
453 * char to number
454 * float to char
455 * char to float
456 *
46293bd7 457 * @param xmldb_structure structure object containing all the info
4fae686b 458 * @param string table table name
459 * @param string field field name to change precision
460 */
461 function change_field_type_php($structure, $table, $field) {
462
463 $result = '';
464 /// Validate if we can do it
465 if (!$table = $structure->getTable($table)) {
466 return false;
467 }
468 if (!$field = $table->getField($field)) {
469 return false;
470 }
471 if ($table->getAllErrors()) {
472 return false;
473 }
474
475 /// Calculate the type tip text
476 $type = $field->getXMLDBTypeName($field->getType());
477
478 /// Add the standard PHP header
479 $result .= XMLDB_PHP_HEADER;
480
481 /// Add contents
482 $result .= XMLDB_LINEFEED;
483 $result .= ' /// Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . XMLDB_LINEFEED;
a8cb94f6 484 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 485 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
4fae686b 486
487 /// Launch the proper DDL
488 $result .= XMLDB_LINEFEED;
489 $result .= ' /// Launch change of type for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 490 $result .= ' $dbman->change_field_type($table, $field);' . XMLDB_LINEFEED;
4fae686b 491
dcbef199 492 /// Add the proper upgrade_xxxx_savepoint call
493 $result .= $this->upgrade_savepoint_php ($structure);
494
4fae686b 495 /// Add standard PHP footer
496 $result .= XMLDB_PHP_FOOTER;
497
498 return $result;
499 }
500
1934b522 501 /**
502 * This function will generate all the PHP code needed to
503 * change the precision of one field using XMLDB objects and functions
504 *
46293bd7 505 * @param xmldb_structure structure object containing all the info
1934b522 506 * @param string table table name
507 * @param string field field name to change precision
508 */
509 function change_field_precision_php($structure, $table, $field) {
510
511 $result = '';
512 /// Validate if we can do it
513 if (!$table = $structure->getTable($table)) {
514 return false;
515 }
516 if (!$field = $table->getField($field)) {
517 return false;
518 }
519 if ($table->getAllErrors()) {
520 return false;
521 }
522
523 /// Calculate the precision tip text
524 $precision = '(' . $field->getLength();
525 if ($field->getDecimals()) {
526 $precision .= ', ' . $field->getDecimals();
527 }
528 $precision .= ')';
529
530 /// Add the standard PHP header
531 $result .= XMLDB_PHP_HEADER;
532
533 /// Add contents
534 $result .= XMLDB_LINEFEED;
535 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED;
a8cb94f6 536 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 537 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " .$field->getPHP(true) . ');' . XMLDB_LINEFEED;
1934b522 538
539 /// Launch the proper DDL
540 $result .= XMLDB_LINEFEED;
541 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 542 $result .= ' $dbman->change_field_precision($table, $field);' . XMLDB_LINEFEED;
1934b522 543
dcbef199 544 /// Add the proper upgrade_xxxx_savepoint call
545 $result .= $this->upgrade_savepoint_php ($structure);
546
1934b522 547 /// Add standard PHP footer
548 $result .= XMLDB_PHP_FOOTER;
549
550 return $result;
551 }
552
553 /**
554 * This function will generate all the PHP code needed to
555 * change the unsigned/signed of one field using XMLDB objects and functions
556 *
46293bd7 557 * @param xmldb_structure structure object containing all the info
1934b522 558 * @param string table table name
559 * @param string field field name to change unsigned/signed
560 */
561 function change_field_unsigned_php($structure, $table, $field) {
562
563 $result = '';
564 /// Validate if we can do it
565 if (!$table = $structure->getTable($table)) {
566 return false;
567 }
568 if (!$field = $table->getField($field)) {
569 return false;
570 }
571 if ($table->getAllErrors()) {
572 return false;
573 }
574
575 /// Calculate the unsigned tip text
576 $unsigned = $field->getUnsigned() ? 'unsigned' : 'signed';
577
578 /// Add the standard PHP header
579 $result .= XMLDB_PHP_HEADER;
580
581 /// Add contents
582 $result .= XMLDB_LINEFEED;
583 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED;
a8cb94f6 584 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 585 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
1934b522 586
587 /// Launch the proper DDL
588 $result .= XMLDB_LINEFEED;
589 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 590 $result .= ' $dbman->change_field_unsigned($table, $field);' . XMLDB_LINEFEED;
1934b522 591
dcbef199 592 /// Add the proper upgrade_xxxx_savepoint call
593 $result .= $this->upgrade_savepoint_php ($structure);
594
1934b522 595 /// Add standard PHP footer
596 $result .= XMLDB_PHP_FOOTER;
597
598 return $result;
599 }
600
601 /**
602 * This function will generate all the PHP code needed to
603 * change the nullability of one field using XMLDB objects and functions
604 *
46293bd7 605 * @param xmldb_structure structure object containing all the info
1934b522 606 * @param string table table name
607 * @param string field field name to change null/not null
608 */
609 function change_field_notnull_php($structure, $table, $field) {
610
611 $result = '';
612 /// Validate if we can do it
613 if (!$table = $structure->getTable($table)) {
614 return false;
615 }
616 if (!$field = $table->getField($field)) {
617 return false;
618 }
619 if ($table->getAllErrors()) {
620 return false;
621 }
622
623 /// Calculate the notnull tip text
624 $notnull = $field->getNotnull() ? 'not null' : 'null';
625
626 /// Add the standard PHP header
627 $result .= XMLDB_PHP_HEADER;
628
629 /// Add contents
630 $result .= XMLDB_LINEFEED;
631 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED;
a8cb94f6 632 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 633 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
1934b522 634
635 /// Launch the proper DDL
636 $result .= XMLDB_LINEFEED;
637 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 638 $result .= ' $dbman->change_field_notnull($table, $field);' . XMLDB_LINEFEED;
1934b522 639
dcbef199 640 /// Add the proper upgrade_xxxx_savepoint call
641 $result .= $this->upgrade_savepoint_php ($structure);
642
1934b522 643 /// Add standard PHP footer
644 $result .= XMLDB_PHP_FOOTER;
645
646 return $result;
647 }
648
49173d25 649 /**
650 * This function will generate all the PHP code needed to
2a88f626 651 * drop the enum values (check constraint) of one field
49173d25 652 * using XMLDB objects and functions
653 *
2a88f626 654 * Note this function is here as part of the process of
655 * dropping enums completely from Moodle 2.0: MDL-18577
656 * and will be out in Moodle 2.1
657 * TODO: Moodle 2.1 - Drop drop_enum_from_field_php
658 *
46293bd7 659 * @param xmldb_structure structure object containing all the info
49173d25 660 * @param string table table name
661 * @param string field field name to change its enum
662 */
2a88f626 663 function drop_enum_from_field_php($structure, $table, $field) {
49173d25 664
665 $result = '';
666 /// Validate if we can do it
667 if (!$table = $structure->getTable($table)) {
668 return false;
669 }
670 if (!$field = $table->getField($field)) {
671 return false;
672 }
673 if ($table->getAllErrors()) {
674 return false;
675 }
676
49173d25 677 /// Add the standard PHP header
678 $result .= XMLDB_PHP_HEADER;
679
680 /// Add contents
681 $result .= XMLDB_LINEFEED;
2a88f626 682 $result .= ' /// Drop list of values (enum) from field ' . $field->getName() . ' on table ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 683 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 684 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
49173d25 685
686 /// Launch the proper DDL
687 $result .= XMLDB_LINEFEED;
2a88f626 688 $result .= ' /// Launch drop of list of values from field ' . $field->getName() . XMLDB_LINEFEED;
689 $result .= ' $dbman->drop_enum_from_field($table, $field);' . XMLDB_LINEFEED;
49173d25 690
dcbef199 691 /// Add the proper upgrade_xxxx_savepoint call
692 $result .= $this->upgrade_savepoint_php ($structure);
693
49173d25 694 /// Add standard PHP footer
695 $result .= XMLDB_PHP_FOOTER;
696
697 return $result;
698 }
699
1934b522 700 /**
701 * This function will generate all the PHP code needed to
702 * change the default of one field using XMLDB objects and functions
703 *
46293bd7 704 * @param xmldb_structure structure object containing all the info
1934b522 705 * @param string table table name
706 * @param string field field name to change null/not null
707 */
708 function change_field_default_php($structure, $table, $field) {
709
710 $result = '';
711 /// Validate if we can do it
712 if (!$table = $structure->getTable($table)) {
713 return false;
714 }
715 if (!$field = $table->getField($field)) {
716 return false;
717 }
718 if ($table->getAllErrors()) {
719 return false;
720 }
721
722 /// Calculate the default tip text
723 $default = $field->getDefault() === null ? 'drop it' : $field->getDefault();
724
725 /// Add the standard PHP header
726 $result .= XMLDB_PHP_HEADER;
727
728 /// Add contents
729 $result .= XMLDB_LINEFEED;
730 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED;
a8cb94f6 731 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 732 $result .= ' $field = new xmldb_field(' . "'" . $field->getName() . "', " . $field->getPHP(true) . ');' . XMLDB_LINEFEED;
1934b522 733
734 /// Launch the proper DDL
735 $result .= XMLDB_LINEFEED;
736 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED;
ef088e70 737 $result .= ' $dbman->change_field_default($table, $field);' . XMLDB_LINEFEED;
1934b522 738
dcbef199 739 /// Add the proper upgrade_xxxx_savepoint call
740 $result .= $this->upgrade_savepoint_php ($structure);
741
1934b522 742 /// Add standard PHP footer
743 $result .= XMLDB_PHP_FOOTER;
744
745 return $result;
746 }
747
ceac2552 748 /**
749 * This function will generate all the PHP code needed to
750 * create one key using XMLDB objects and functions
751 *
46293bd7 752 * @param xmldb_structure structure object containing all the info
ceac2552 753 * @param string table table name
754 * @param string key key name to be created
755 * @return string PHP code to be used to create the key
756 */
757 function add_key_php($structure, $table, $key) {
758
759 $result = '';
760 /// Validate if we can do it
761 if (!$table = $structure->getTable($table)) {
762 return false;
763 }
764 if (!$key = $table->getKey($key)) {
765 return false;
766 }
767 if ($table->getAllErrors()) {
768 return false;
769 }
770
771 /// Add the standard PHP header
772 $result .= XMLDB_PHP_HEADER;
773
774 /// Add contents
775 $result .= XMLDB_LINEFEED;
776 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 777 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 778 $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 779
780 /// Launch the proper DDL
781 $result .= XMLDB_LINEFEED;
782 $result .= ' /// Launch add key ' . $key->getName() . XMLDB_LINEFEED;
ef088e70 783 $result .= ' $dbman->add_key($table, $key);' . XMLDB_LINEFEED;
ceac2552 784
dcbef199 785 /// Add the proper upgrade_xxxx_savepoint call
786 $result .= $this->upgrade_savepoint_php ($structure);
787
ceac2552 788 /// Add standard PHP footer
789 $result .= XMLDB_PHP_FOOTER;
790
791 return $result;
792 }
793
794 /**
795 * This function will generate all the PHP code needed to
796 * drop one key using XMLDB objects and functions
797 *
46293bd7 798 * @param xmldb_structure structure object containing all the info
ceac2552 799 * @param string table table name
800 * @param string key key name to be dropped
801 * @return string PHP code to be used to drop the key
802 */
803 function drop_key_php($structure, $table, $key) {
804
805 $result = '';
806 /// Validate if we can do it
807 if (!$table = $structure->getTable($table)) {
808 return false;
809 }
810 if (!$key = $table->getKey($key)) {
811 return false;
812 }
813 if ($table->getAllErrors()) {
814 return false;
815 }
816
817 /// Add the standard PHP header
818 $result .= XMLDB_PHP_HEADER;
819
820 /// Add contents
821 $result .= XMLDB_LINEFEED;
822 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 823 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 824 $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 825
826 /// Launch the proper DDL
827 $result .= XMLDB_LINEFEED;
828 $result .= ' /// Launch drop key ' . $key->getName() . XMLDB_LINEFEED;
ef088e70 829 $result .= ' $dbman->drop_key($table, $key);' . XMLDB_LINEFEED;
ceac2552 830
dcbef199 831 /// Add the proper upgrade_xxxx_savepoint call
832 $result .= $this->upgrade_savepoint_php ($structure);
833
ceac2552 834 /// Add standard PHP footer
835 $result .= XMLDB_PHP_FOOTER;
836
837 return $result;
838 }
839
840 /**
841 * This function will generate all the PHP code needed to
842 * rename one key using XMLDB objects and functions
843 *
46293bd7 844 * @param xmldb_structure structure object containing all the info
ceac2552 845 * @param string table table name
846 * @param string key key name to be renamed
847 * @return string PHP code to be used to rename the key
848 */
849 function rename_key_php($structure, $table, $key) {
850
851 $result = '';
852 /// Validate if we can do it
853 if (!$table = $structure->getTable($table)) {
854 return false;
855 }
856 if (!$key = $table->getKey($key)) {
857 return false;
858 }
859 if ($table->getAllErrors()) {
860 return false;
861 }
862
49173d25 863 /// Prepend warning. This function isn't usable!
864 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
865
ceac2552 866 /// Add the standard PHP header
867 $result .= XMLDB_PHP_HEADER;
868
869 /// Add contents
870 $result .= XMLDB_LINEFEED;
871 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
a8cb94f6 872 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 873 $result .= ' $key = new xmldb_key(' . "'" . $key->getName() . "', " . $key->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 874
875 /// Launch the proper DDL
876 $result .= XMLDB_LINEFEED;
877 $result .= ' /// Launch rename key ' . $key->getName() . XMLDB_LINEFEED;
ef088e70 878 $result .= ' $dbman->rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
ceac2552 879
dcbef199 880 /// Add the proper upgrade_xxxx_savepoint call
881 $result .= $this->upgrade_savepoint_php ($structure);
882
ceac2552 883 /// Add standard PHP footer
884 $result .= XMLDB_PHP_FOOTER;
885
886 return $result;
887 }
888
889 /**
890 * This function will generate all the PHP code needed to
891 * create one index using XMLDB objects and functions
892 *
46293bd7 893 * @param xmldb_structure structure object containing all the info
ceac2552 894 * @param string table table name
895 * @param string index index name to be created
896 * @return string PHP code to be used to create the index
897 */
898 function add_index_php($structure, $table, $index) {
899
900 $result = '';
901 /// Validate if we can do it
902 if (!$table = $structure->getTable($table)) {
903 return false;
904 }
905 if (!$index = $table->getIndex($index)) {
906 return false;
907 }
908 if ($table->getAllErrors()) {
909 return false;
910 }
911
912 /// Add the standard PHP header
913 $result .= XMLDB_PHP_HEADER;
914
915 /// Add contents
916 $result .= XMLDB_LINEFEED;
917 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be added to ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 918 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 919 $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 920
921 /// Launch the proper DDL
922 $result .= XMLDB_LINEFEED;
82bba3e4 923 $result .= ' /// Conditionally launch add index ' . $index->getName() . XMLDB_LINEFEED;
9dbebd14 924 $result .= ' if (!$dbman->index_exists($table, $index)) {' . XMLDB_LINEFEED;
ef088e70 925 $result .= ' $dbman->add_index($table, $index);' . XMLDB_LINEFEED;
82bba3e4 926 $result .= ' }' . XMLDB_LINEFEED;
ceac2552 927
dcbef199 928 /// Add the proper upgrade_xxxx_savepoint call
929 $result .= $this->upgrade_savepoint_php ($structure);
930
ceac2552 931 /// Add standard PHP footer
932 $result .= XMLDB_PHP_FOOTER;
933
934 return $result;
935 }
936
937 /**
938 * This function will generate all the PHP code needed to
939 * drop one index using XMLDB objects and functions
940 *
46293bd7 941 * @param xmldb_structure structure object containing all the info
ceac2552 942 * @param string table table name
943 * @param string index index name to be dropped
944 * @return string PHP code to be used to drop the index
945 */
946 function drop_index_php($structure, $table, $index) {
947
948 $result = '';
949 /// Validate if we can do it
950 if (!$table = $structure->getTable($table)) {
951 return false;
952 }
953 if (!$index = $table->getIndex($index)) {
954 return false;
955 }
956 if ($table->getAllErrors()) {
957 return false;
958 }
959
960 /// Add the standard PHP header
961 $result .= XMLDB_PHP_HEADER;
962
963 /// Add contents
964 $result .= XMLDB_LINEFEED;
965 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED;
a8cb94f6 966 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 967 $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 968
969 /// Launch the proper DDL
970 $result .= XMLDB_LINEFEED;
82bba3e4 971 $result .= ' /// Conditionally launch drop index ' . $index->getName() . XMLDB_LINEFEED;
9dbebd14 972 $result .= ' if ($dbman->index_exists($table, $index)) {' . XMLDB_LINEFEED;
ef088e70 973 $result .= ' $dbman->drop_index($table, $index);' . XMLDB_LINEFEED;
82bba3e4 974 $result .= ' }' . XMLDB_LINEFEED;
ceac2552 975
dcbef199 976 /// Add the proper upgrade_xxxx_savepoint call
977 $result .= $this->upgrade_savepoint_php ($structure);
978
ceac2552 979 /// Add standard PHP footer
980 $result .= XMLDB_PHP_FOOTER;
981
982 return $result;
983 }
984
985 /**
986 * This function will generate all the PHP code needed to
987 * rename one index using XMLDB objects and functions
988 *
46293bd7 989 * @param xmldb_structure structure object containing all the info
ceac2552 990 * @param string table table name
991 * @param string index index name to be renamed
992 * @return string PHP code to be used to rename the index
993 */
994 function rename_index_php($structure, $table, $index) {
995
996 $result = '';
997 /// Validate if we can do it
998 if (!$table = $structure->getTable($table)) {
999 return false;
1000 }
1001 if (!$index = $table->getIndex($index)) {
1002 return false;
1003 }
1004 if ($table->getAllErrors()) {
1005 return false;
1006 }
1007
49173d25 1008 /// Prepend warning. This function isn't usable!
1009 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED . XMLDB_LINEFEED;
1010
ceac2552 1011 /// Add the standard PHP header
1012 $result .= XMLDB_PHP_HEADER;
1013
1014 /// Add contents
1015 $result .= XMLDB_LINEFEED;
1016 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ? 'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED;
a8cb94f6 1017 $result .= ' $table = new xmldb_table(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED;
69b80cc2 1018 $result .= ' $index = new xmldb_index(' . "'" . $index->getName() . "', " . $index->getPHP(true) . ');' . XMLDB_LINEFEED;
ceac2552 1019
1020 /// Launch the proper DDL
1021 $result .= XMLDB_LINEFEED;
1022 $result .= ' /// Launch rename index ' . $index->getName() . XMLDB_LINEFEED;
ef088e70 1023 $result .= ' $dbman->rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED;
ceac2552 1024
dcbef199 1025 /// Add the proper upgrade_xxxx_savepoint call
1026 $result .= $this->upgrade_savepoint_php ($structure);
1027
ceac2552 1028 /// Add standard PHP footer
1029 $result .= XMLDB_PHP_FOOTER;
1030
1031 return $result;
1032 }
1033
dcbef199 1034 /**
1035 * This function will generate the PHP code needed to
1036 * implement the upgrade_xxxx_savepoint() php calls in
1037 * upgrade code generated from the editor
1038 *
46293bd7 1039 * @param xmldb_structure structure object containing all the info
dcbef199 1040 * @return string PHP code to be used to stabilish a savepoint
1041 */
1042 function upgrade_savepoint_php ($structure) {
1043
1044 $path = $structure->getPath();
1045
1046 $result = '';
1047
84774c55 1048 /// Split path into components
1049 $patharr = explode('/', $path);
1050
1051 switch ($patharr[0]) {
1052 case 'lib':
dcbef199 1053 $result = XMLDB_LINEFEED .
1054 ' /// Main savepoint reached' . XMLDB_LINEFEED .
1055 ' upgrade_main_savepoint($result, XXXXXXXXXX);' . XMLDB_LINEFEED;
1056 break;
84774c55 1057 case 'mod':
1058 $result = XMLDB_LINEFEED .
1059 ' /// ' . $patharr[1] . ' savepoint reached' . XMLDB_LINEFEED .
1060 ' upgrade_mod_savepoint($result, XXXXXXXXXX, ' . "'$patharr[1]'" . ');' . XMLDB_LINEFEED;
1061 break;
dcbef199 1062 }
1063 return $result;
1064 }
1065
c5a6ae17 1066}
1067?>