3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
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. //
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: //
23 // http://www.gnu.org/copyleft/gpl.html //
25 ///////////////////////////////////////////////////////////////////////////
27 /// This class will show the PHP needed to perform the desired action
28 /// with the specified fields/keys/indexes.
30 class view_table_php
extends XMLDBAction
{
33 * Init method, every subclass will have its own
38 /// Set own custom attributes
40 /// Get needed strings
41 $this->loadStrings(array(
42 'selectaction' => 'xmldb',
43 'selectfieldkeyindex' => 'xmldb',
46 'selectonecommand' => 'xmldb',
47 'selectonefieldkeyindex' => 'xmldb',
48 'mustselectonefield' => 'xmldb',
49 'mustselectonekey' => 'xmldb',
50 'mustselectoneindex' => 'xmldb',
56 * Invoke method, every class will have its own
57 * returns true/false on completion, setting both
58 * errormsg and output as necessary
65 /// Set own core attributes
66 $this->does_generate
= ACTION_GENERATE_HTML
;
68 /// These are always here
71 /// Do the job, setting result as needed
72 /// Get the dir containing the file
73 $dirpath = required_param('dir', PARAM_PATH
);
74 $dirpath = $CFG->dirroot
. stripslashes_safe($dirpath);
76 /// Get the correct dirs
77 if (!empty($XMLDB->dbdirs
)) {
78 $dbdir =& $XMLDB->dbdirs
[$dirpath];
82 if (!empty($XMLDB->editeddirs
)) {
83 $editeddir =& $XMLDB->editeddirs
[$dirpath];
84 $structure =& $editeddir->xml_file
->getStructure();
86 /// ADD YOUR CODE HERE
88 $tableparam = required_param('table', PARAM_PATH
);
90 $table =& $structure->getTable($tableparam);
91 $fields = $table->getFields();
92 $field = reset($fields);
93 $defaultfieldkeyindex = null;
95 $defaultfieldkeyindex = 'f#' . $field->getName();
97 $keys = $table->getKeys();
98 $indexes = $table->getIndexes();
101 $commandparam = optional_param('command', 'add_field', PARAM_PATH
);
102 $origfieldkeyindexparam = optional_param('fieldkeyindex', $defaultfieldkeyindex, PARAM_PATH
);
103 $fieldkeyindexparam = preg_replace('/[fki]#/i', '', $origfieldkeyindexparam); ///Strip the initials
104 $fieldkeyindexinitial = substr($origfieldkeyindexparam, 0, 1); //To know what we have selected
106 /// The back to edit xml button
107 $b = ' <p class="centerpara buttons">';
108 $b .= '<a href="index.php?action=edit_table&dir=' . urlencode(str_replace($CFG->dirroot
, '', $dirpath)) . '&table=' . $tableparam . '">[' . $this->str
['back'] . ']</a>';
112 /// The table currently being edited
113 $o .= '<h3 class="main">' . $this->str
['table'] . ': ' . s($tableparam) . '</h3>';
115 /// To indent the menu selections
116 $optionspacer = ' ';
118 /// Calculate the popup of commands
119 $commands = array('Fields',
120 $optionspacer . 'add_field',
121 $optionspacer . 'drop_field',
122 $optionspacer . 'rename_field',
123 $optionspacer . 'change_field_type',
124 $optionspacer . 'change_field_precision',
125 $optionspacer . 'change_field_unsigned',
126 $optionspacer . 'change_field_notnull',
127 $optionspacer . 'change_field_enum',
128 $optionspacer . 'change_field_default',
130 $optionspacer . 'add_key',
131 $optionspacer . 'drop_key',
132 $optionspacer . 'rename_key',
134 $optionspacer . 'add_index',
135 $optionspacer . 'drop_index',
136 $optionspacer . 'rename_index');
137 foreach ($commands as $command) {
138 $popcommands[str_replace($optionspacer, '', $command)] = str_replace('_', ' ', $command);
140 /// Calculate the popup of fields/keys/indexes
142 $popfields['fieldshead'] = 'Fields';
143 foreach ($fields as $field) {
144 $popfields['f#' . $field->getName()] = $optionspacer . $field->getName();
148 $popfields['keyshead'] = 'Keys';
149 foreach ($keys as $key) {
150 $popfields['k#' . $key->getName()] = $optionspacer . $key->getName();
154 $popfields['indexeshead'] = 'Indexes';
155 foreach ($indexes as $index) {
156 $popfields['i#' . $index->getName()] = $optionspacer . $index->getName();
160 /// Now build the form
161 $o.= '<form id="form" action="index.php" method="post">';
163 $o.= ' <input type="hidden" name ="dir" value="' . str_replace($CFG->dirroot
, '', $dirpath) . '" />';
164 $o.= ' <input type="hidden" name ="table" value="' . s($tableparam) . '" />';
165 $o.= ' <input type="hidden" name ="action" value="view_table_php" />';
166 $o.= ' <table id="formelements" class="boxaligncenter" cellpadding="5">';
167 $o.= ' <tr><td><label for="action" accesskey="c">' . $this->str
['selectaction'] .' </label>' . choose_from_menu($popcommands, 'command', $commandparam, '', '', 0, true) . ' <label for="fieldkeyindex" accesskey="f">' . $this->str
['selectfieldkeyindex'] . ' </label>' .choose_from_menu($popfields, 'fieldkeyindex', $origfieldkeyindexparam, '', '', 0, true) . '</td></tr>';
168 $o.= ' <tr><td colspan="2" align="center"><input type="submit" value="' .$this->str
['view'] . '" /></td></tr>';
170 $o.= '</div></form>';
172 $o.= ' <table id="phpcode" class="boxaligncenter" cellpadding="5">';
173 $o.= ' <tr><td><textarea cols="80" rows="32">';
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']);
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']);
181 /// Based on current params, call the needed function
182 switch ($commandparam) {
184 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
185 $o.= s($this->add_field_php($structure, $tableparam, $fieldkeyindexparam));
187 $o.= $this->str
['mustselectonefield'];
191 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
192 $o.= s($this->drop_field_php($structure, $tableparam, $fieldkeyindexparam));
194 $o.= $this->str
['mustselectonefield'];
198 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
199 $o.= s($this->rename_field_php($structure, $tableparam, $fieldkeyindexparam));
201 $o.= $this->str
['mustselectonefield'];
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));
208 $o.= $this->str
['mustselectonefield'];
211 case 'change_field_precision':
212 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
213 $o.= s($this->change_field_precision_php($structure, $tableparam, $fieldkeyindexparam));
215 $o.= $this->str
['mustselectonefield'];
218 case 'change_field_unsigned':
219 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
220 $o.= s($this->change_field_unsigned_php($structure, $tableparam, $fieldkeyindexparam));
222 $o.= $this->str
['mustselectonefield'];
225 case 'change_field_notnull':
226 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
227 $o.= s($this->change_field_notnull_php($structure, $tableparam, $fieldkeyindexparam));
229 $o.= $this->str
['mustselectonefield'];
232 case 'change_field_enum':
233 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
234 $o.= s($this->change_field_enum_php($structure, $tableparam, $fieldkeyindexparam));
236 $o.= $this->str
['mustselectonefield'];
239 case 'change_field_default':
240 if ($fieldkeyindexinitial == 'f') { //Only if we have got one field
241 $o.= s($this->change_field_default_php($structure, $tableparam, $fieldkeyindexparam));
243 $o.= $this->str
['mustselectonefield'];
247 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
248 $o.= s($this->add_key_php($structure, $tableparam, $fieldkeyindexparam));
250 $o.= $this->str
['mustselectonekey'];
254 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
255 $o.= s($this->drop_key_php($structure, $tableparam, $fieldkeyindexparam));
257 $o.= $this->str
['mustselectonekey'];
261 if ($fieldkeyindexinitial == 'k') { //Only if we have got one key
262 $o.= s($this->rename_key_php($structure, $tableparam, $fieldkeyindexparam));
264 $o.= $this->str
['mustselectonekey'];
268 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
269 $o.= s($this->add_index_php($structure, $tableparam, $fieldkeyindexparam));
271 $o.= $this->str
['mustselectoneindex'];
275 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
276 $o.= s($this->drop_index_php($structure, $tableparam, $fieldkeyindexparam));
278 $o.= $this->str
['mustselectoneindex'];
282 if ($fieldkeyindexinitial == 'i') { //Only if we have got one index
283 $o.= s($this->rename_index_php($structure, $tableparam, $fieldkeyindexparam));
285 $o.= $this->str
['mustselectoneindex'];
290 $o.= '</textarea></td></tr>';
295 /// Launch postaction if exists (leave this here!)
296 if ($this->getPostAction() && $result) {
297 return $this->launch($this->getPostAction());
300 /// Return ok if arrived here
305 * This function will generate all the PHP code needed to
306 * create one field using XMLDB objects and functions
308 * @param XMLDBStructure structure object containing all the info
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
313 function add_field_php($structure, $table, $field) {
316 /// Validate if we can do it
317 if (!$table = $structure->getTable($table)) {
320 if (!$field = $table->getField($field)) {
323 if ($table->getAllErrors()) {
327 /// Add the standard PHP header
328 $result .= XMLDB_PHP_HEADER
;
331 $result .= XMLDB_LINEFEED
;
332 $result .= ' /// Define field ' . $field->getName() . ' to be added to ' . $table->getName() . XMLDB_LINEFEED
;
333 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
334 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
335 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
337 /// Launch the proper DDL
338 $result .= XMLDB_LINEFEED
;
339 $result .= ' /// Launch add field ' . $field->getName() . XMLDB_LINEFEED
;
340 $result .= ' $result = $result && add_field($table, $field);' . XMLDB_LINEFEED
;
342 /// Add the proper upgrade_xxxx_savepoint call
343 $result .= $this->upgrade_savepoint_php ($structure);
345 /// Add standard PHP footer
346 $result .= XMLDB_PHP_FOOTER
;
352 * This function will generate all the PHP code needed to
353 * drop one field using XMLDB objects and functions
355 * @param XMLDBStructure structure object containing all the info
356 * @param string table table name
357 * @param string field field name to be dropped
358 * @return string PHP code to be used to drop the field
360 function drop_field_php($structure, $table, $field) {
363 /// Validate if we can do it
364 if (!$table = $structure->getTable($table)) {
367 if (!$field = $table->getField($field)) {
370 if ($table->getAllErrors()) {
374 /// Add the standard PHP header
375 $result .= XMLDB_PHP_HEADER
;
378 $result .= XMLDB_LINEFEED
;
379 $result .= ' /// Define field ' . $field->getName() . ' to be dropped from ' . $table->getName() . XMLDB_LINEFEED
;
380 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
381 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
383 /// Launch the proper DDL
384 $result .= XMLDB_LINEFEED
;
385 $result .= ' /// Launch drop field ' . $field->getName() . XMLDB_LINEFEED
;
386 $result .= ' $result = $result && drop_field($table, $field);' . XMLDB_LINEFEED
;
388 /// Add the proper upgrade_xxxx_savepoint call
389 $result .= $this->upgrade_savepoint_php ($structure);
391 /// Add standard PHP footer
392 $result .= XMLDB_PHP_FOOTER
;
398 * This function will generate all the PHP code needed to
399 * rename one field using XMLDB objects and functions
401 * @param XMLDBStructure structure object containing all the info
402 * @param string table table name
403 * @param string field field name to be renamed
404 * @return string PHP code to be used to rename the field
406 function rename_field_php($structure, $table, $field) {
409 /// Validate if we can do it
410 if (!$table = $structure->getTable($table)) {
413 if (!$field = $table->getField($field)) {
416 if ($table->getAllErrors()) {
420 /// Add the standard PHP header
421 $result .= XMLDB_PHP_HEADER
;
424 $result .= XMLDB_LINEFEED
;
425 $result .= ' /// Rename field ' . $field->getName() . ' on table ' . $table->getName() . ' to NEWNAMEGOESHERE'. XMLDB_LINEFEED
;
426 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
427 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
428 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
430 /// Launch the proper DDL
431 $result .= XMLDB_LINEFEED
;
432 $result .= ' /// Launch rename field ' . $field->getName() . XMLDB_LINEFEED
;
433 $result .= ' $result = $result && rename_field($table, $field, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED
;
435 /// Add the proper upgrade_xxxx_savepoint call
436 $result .= $this->upgrade_savepoint_php ($structure);
438 /// Add standard PHP footer
439 $result .= XMLDB_PHP_FOOTER
;
445 * This function will generate all the PHP code needed to
446 * change the type of one field using XMLDB objects and functions.
447 * Currently these conversions are supported:
455 * @param XMLDBStructure structure object containing all the info
456 * @param string table table name
457 * @param string field field name to change precision
459 function change_field_type_php($structure, $table, $field) {
462 /// Validate if we can do it
463 if (!$table = $structure->getTable($table)) {
466 if (!$field = $table->getField($field)) {
469 if ($table->getAllErrors()) {
473 /// Calculate the type tip text
474 $type = $field->getXMLDBTypeName($field->getType());
476 /// Add the standard PHP header
477 $result .= XMLDB_PHP_HEADER
;
480 $result .= XMLDB_LINEFEED
;
481 $result .= ' /// Changing type of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $type . XMLDB_LINEFEED
;
482 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
483 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
484 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
486 /// Launch the proper DDL
487 $result .= XMLDB_LINEFEED
;
488 $result .= ' /// Launch change of type for field ' . $field->getName() . XMLDB_LINEFEED
;
489 $result .= ' $result = $result && change_field_type($table, $field);' . XMLDB_LINEFEED
;
491 /// Add the proper upgrade_xxxx_savepoint call
492 $result .= $this->upgrade_savepoint_php ($structure);
494 /// Add standard PHP footer
495 $result .= XMLDB_PHP_FOOTER
;
501 * This function will generate all the PHP code needed to
502 * change the precision of one field using XMLDB objects and functions
504 * @param XMLDBStructure structure object containing all the info
505 * @param string table table name
506 * @param string field field name to change precision
508 function change_field_precision_php($structure, $table, $field) {
511 /// Validate if we can do it
512 if (!$table = $structure->getTable($table)) {
515 if (!$field = $table->getField($field)) {
518 if ($table->getAllErrors()) {
522 /// Calculate the precision tip text
523 $precision = '(' . $field->getLength();
524 if ($field->getDecimals()) {
525 $precision .= ', ' . $field->getDecimals();
529 /// Add the standard PHP header
530 $result .= XMLDB_PHP_HEADER
;
533 $result .= XMLDB_LINEFEED
;
534 $result .= ' /// Changing precision of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $precision . XMLDB_LINEFEED
;
535 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
536 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
537 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
539 /// Launch the proper DDL
540 $result .= XMLDB_LINEFEED
;
541 $result .= ' /// Launch change of precision for field ' . $field->getName() . XMLDB_LINEFEED
;
542 $result .= ' $result = $result && change_field_precision($table, $field);' . XMLDB_LINEFEED
;
544 /// Add the proper upgrade_xxxx_savepoint call
545 $result .= $this->upgrade_savepoint_php ($structure);
547 /// Add standard PHP footer
548 $result .= XMLDB_PHP_FOOTER
;
554 * This function will generate all the PHP code needed to
555 * change the unsigned/signed of one field using XMLDB objects and functions
557 * @param XMLDBStructure structure object containing all the info
558 * @param string table table name
559 * @param string field field name to change unsigned/signed
561 function change_field_unsigned_php($structure, $table, $field) {
564 /// Validate if we can do it
565 if (!$table = $structure->getTable($table)) {
568 if (!$field = $table->getField($field)) {
571 if ($table->getAllErrors()) {
575 /// Calculate the unsigned tip text
576 $unsigned = $field->getUnsigned() ?
'unsigned' : 'signed';
578 /// Add the standard PHP header
579 $result .= XMLDB_PHP_HEADER
;
582 $result .= XMLDB_LINEFEED
;
583 $result .= ' /// Changing sign of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $unsigned . XMLDB_LINEFEED
;
584 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
585 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
586 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
588 /// Launch the proper DDL
589 $result .= XMLDB_LINEFEED
;
590 $result .= ' /// Launch change of sign for field ' . $field->getName() . XMLDB_LINEFEED
;
591 $result .= ' $result = $result && change_field_unsigned($table, $field);' . XMLDB_LINEFEED
;
593 /// Add the proper upgrade_xxxx_savepoint call
594 $result .= $this->upgrade_savepoint_php ($structure);
596 /// Add standard PHP footer
597 $result .= XMLDB_PHP_FOOTER
;
603 * This function will generate all the PHP code needed to
604 * change the nullability of one field using XMLDB objects and functions
606 * @param XMLDBStructure structure object containing all the info
607 * @param string table table name
608 * @param string field field name to change null/not null
610 function change_field_notnull_php($structure, $table, $field) {
613 /// Validate if we can do it
614 if (!$table = $structure->getTable($table)) {
617 if (!$field = $table->getField($field)) {
620 if ($table->getAllErrors()) {
624 /// Calculate the notnull tip text
625 $notnull = $field->getNotnull() ?
'not null' : 'null';
627 /// Add the standard PHP header
628 $result .= XMLDB_PHP_HEADER
;
631 $result .= XMLDB_LINEFEED
;
632 $result .= ' /// Changing nullability of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $notnull . XMLDB_LINEFEED
;
633 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
634 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
635 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
637 /// Launch the proper DDL
638 $result .= XMLDB_LINEFEED
;
639 $result .= ' /// Launch change of nullability for field ' . $field->getName() . XMLDB_LINEFEED
;
640 $result .= ' $result = $result && change_field_notnull($table, $field);' . XMLDB_LINEFEED
;
642 /// Add the proper upgrade_xxxx_savepoint call
643 $result .= $this->upgrade_savepoint_php ($structure);
645 /// Add standard PHP footer
646 $result .= XMLDB_PHP_FOOTER
;
652 * This function will generate all the PHP code needed to
653 * change the enum values (check constraint) of one field
654 * using XMLDB objects and functions
656 * @param XMLDBStructure structure object containing all the info
657 * @param string table table name
658 * @param string field field name to change its enum
660 function change_field_enum_php($structure, $table, $field) {
663 /// Validate if we can do it
664 if (!$table = $structure->getTable($table)) {
667 if (!$field = $table->getField($field)) {
670 if ($table->getAllErrors()) {
674 /// Calculate the enum tip text
675 $enum = $field->getEnum() ?
implode(', ', $field->getEnumValues()) : 'none';
677 /// Add the standard PHP header
678 $result .= XMLDB_PHP_HEADER
;
681 $result .= XMLDB_LINEFEED
;
682 $result .= ' /// Changing list of values (enum) of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $enum . XMLDB_LINEFEED
;
683 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
684 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
685 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
687 /// Launch the proper DDL
688 $result .= XMLDB_LINEFEED
;
689 $result .= ' /// Launch change of list of values for field ' . $field->getName() . XMLDB_LINEFEED
;
690 $result .= ' $result = $result && change_field_enum($table, $field);' . XMLDB_LINEFEED
;
692 /// Add the proper upgrade_xxxx_savepoint call
693 $result .= $this->upgrade_savepoint_php ($structure);
695 /// Add standard PHP footer
696 $result .= XMLDB_PHP_FOOTER
;
702 * This function will generate all the PHP code needed to
703 * change the default of one field using XMLDB objects and functions
705 * @param XMLDBStructure structure object containing all the info
706 * @param string table table name
707 * @param string field field name to change null/not null
709 function change_field_default_php($structure, $table, $field) {
712 /// Validate if we can do it
713 if (!$table = $structure->getTable($table)) {
716 if (!$field = $table->getField($field)) {
719 if ($table->getAllErrors()) {
723 /// Calculate the default tip text
724 $default = $field->getDefault() === null ?
'drop it' : $field->getDefault();
726 /// Add the standard PHP header
727 $result .= XMLDB_PHP_HEADER
;
730 $result .= XMLDB_LINEFEED
;
731 $result .= ' /// Changing the default of field ' . $field->getName() . ' on table ' . $table->getName() . ' to ' . $default . XMLDB_LINEFEED
;
732 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
733 $result .= ' $field = new XMLDBField(' . "'" . $field->getName() . "'" . ');' . XMLDB_LINEFEED
;
734 $result .= ' $field->setAttributes(' . $field->getPHP(true) . ');' . XMLDB_LINEFEED
;
736 /// Launch the proper DDL
737 $result .= XMLDB_LINEFEED
;
738 $result .= ' /// Launch change of default for field ' . $field->getName() . XMLDB_LINEFEED
;
739 $result .= ' $result = $result && change_field_default($table, $field);' . XMLDB_LINEFEED
;
741 /// Add the proper upgrade_xxxx_savepoint call
742 $result .= $this->upgrade_savepoint_php ($structure);
744 /// Add standard PHP footer
745 $result .= XMLDB_PHP_FOOTER
;
751 * This function will generate all the PHP code needed to
752 * create one key using XMLDB objects and functions
754 * @param XMLDBStructure structure object containing all the info
755 * @param string table table name
756 * @param string key key name to be created
757 * @return string PHP code to be used to create the key
759 function add_key_php($structure, $table, $key) {
762 /// Validate if we can do it
763 if (!$table = $structure->getTable($table)) {
766 if (!$key = $table->getKey($key)) {
769 if ($table->getAllErrors()) {
773 /// Add the standard PHP header
774 $result .= XMLDB_PHP_HEADER
;
777 $result .= XMLDB_LINEFEED
;
778 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be added to ' . $table->getName() . XMLDB_LINEFEED
;
779 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
780 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED
;
781 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED
;
783 /// Launch the proper DDL
784 $result .= XMLDB_LINEFEED
;
785 $result .= ' /// Launch add key ' . $key->getName() . XMLDB_LINEFEED
;
786 $result .= ' $result = $result && add_key($table, $key);' . XMLDB_LINEFEED
;
788 /// Add the proper upgrade_xxxx_savepoint call
789 $result .= $this->upgrade_savepoint_php ($structure);
791 /// Add standard PHP footer
792 $result .= XMLDB_PHP_FOOTER
;
798 * This function will generate all the PHP code needed to
799 * drop one key using XMLDB objects and functions
801 * @param XMLDBStructure structure object containing all the info
802 * @param string table table name
803 * @param string key key name to be dropped
804 * @return string PHP code to be used to drop the key
806 function drop_key_php($structure, $table, $key) {
809 /// Validate if we can do it
810 if (!$table = $structure->getTable($table)) {
813 if (!$key = $table->getKey($key)) {
816 if ($table->getAllErrors()) {
820 /// Add the standard PHP header
821 $result .= XMLDB_PHP_HEADER
;
824 $result .= XMLDB_LINEFEED
;
825 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED
;
826 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
827 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED
;
828 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED
;
830 /// Launch the proper DDL
831 $result .= XMLDB_LINEFEED
;
832 $result .= ' /// Launch drop key ' . $key->getName() . XMLDB_LINEFEED
;
833 $result .= ' $result = $result && drop_key($table, $key);' . XMLDB_LINEFEED
;
835 /// Add the proper upgrade_xxxx_savepoint call
836 $result .= $this->upgrade_savepoint_php ($structure);
838 /// Add standard PHP footer
839 $result .= XMLDB_PHP_FOOTER
;
845 * This function will generate all the PHP code needed to
846 * rename one key using XMLDB objects and functions
848 * @param XMLDBStructure structure object containing all the info
849 * @param string table table name
850 * @param string key key name to be renamed
851 * @return string PHP code to be used to rename the key
853 function rename_key_php($structure, $table, $key) {
856 /// Validate if we can do it
857 if (!$table = $structure->getTable($table)) {
860 if (!$key = $table->getKey($key)) {
863 if ($table->getAllErrors()) {
867 /// Prepend warning. This function isn't usable!
868 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED
. XMLDB_LINEFEED
;
870 /// Add the standard PHP header
871 $result .= XMLDB_PHP_HEADER
;
874 $result .= XMLDB_LINEFEED
;
875 $result .= ' /// Define key ' . $key->getName() . ' ('. $key->getXMLDBKeyName($key->getType()) . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED
;
876 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
877 $result .= ' $key = new XMLDBKey(' . "'" . $key->getName() . "'" . ');' . XMLDB_LINEFEED
;
878 $result .= ' $key->setAttributes(' . $key->getPHP(true) . ');' . XMLDB_LINEFEED
;
880 /// Launch the proper DDL
881 $result .= XMLDB_LINEFEED
;
882 $result .= ' /// Launch rename key ' . $key->getName() . XMLDB_LINEFEED
;
883 $result .= ' $result = $result && rename_key($table, $key, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED
;
885 /// Add the proper upgrade_xxxx_savepoint call
886 $result .= $this->upgrade_savepoint_php ($structure);
888 /// Add standard PHP footer
889 $result .= XMLDB_PHP_FOOTER
;
895 * This function will generate all the PHP code needed to
896 * create one index using XMLDB objects and functions
898 * @param XMLDBStructure structure object containing all the info
899 * @param string table table name
900 * @param string index index name to be created
901 * @return string PHP code to be used to create the index
903 function add_index_php($structure, $table, $index) {
906 /// Validate if we can do it
907 if (!$table = $structure->getTable($table)) {
910 if (!$index = $table->getIndex($index)) {
913 if ($table->getAllErrors()) {
917 /// Add the standard PHP header
918 $result .= XMLDB_PHP_HEADER
;
921 $result .= XMLDB_LINEFEED
;
922 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ?
'unique' : 'not unique') . ') to be added to ' . $table->getName() . XMLDB_LINEFEED
;
923 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
924 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED
;
925 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED
;
927 /// Launch the proper DDL
928 $result .= XMLDB_LINEFEED
;
929 $result .= ' /// Launch add index ' . $index->getName() . XMLDB_LINEFEED
;
930 $result .= ' $result = $result && add_index($table, $index);' . XMLDB_LINEFEED
;
932 /// Add the proper upgrade_xxxx_savepoint call
933 $result .= $this->upgrade_savepoint_php ($structure);
935 /// Add standard PHP footer
936 $result .= XMLDB_PHP_FOOTER
;
942 * This function will generate all the PHP code needed to
943 * drop one index using XMLDB objects and functions
945 * @param XMLDBStructure structure object containing all the info
946 * @param string table table name
947 * @param string index index name to be dropped
948 * @return string PHP code to be used to drop the index
950 function drop_index_php($structure, $table, $index) {
953 /// Validate if we can do it
954 if (!$table = $structure->getTable($table)) {
957 if (!$index = $table->getIndex($index)) {
960 if ($table->getAllErrors()) {
964 /// Add the standard PHP header
965 $result .= XMLDB_PHP_HEADER
;
968 $result .= XMLDB_LINEFEED
;
969 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ?
'unique' : 'not unique') . ') to be dropped form ' . $table->getName() . XMLDB_LINEFEED
;
970 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
971 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED
;
972 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED
;
974 /// Launch the proper DDL
975 $result .= XMLDB_LINEFEED
;
976 $result .= ' /// Launch drop index ' . $index->getName() . XMLDB_LINEFEED
;
977 $result .= ' $result = $result && drop_index($table, $index);' . XMLDB_LINEFEED
;
979 /// Add the proper upgrade_xxxx_savepoint call
980 $result .= $this->upgrade_savepoint_php ($structure);
982 /// Add standard PHP footer
983 $result .= XMLDB_PHP_FOOTER
;
989 * This function will generate all the PHP code needed to
990 * rename one index using XMLDB objects and functions
992 * @param XMLDBStructure structure object containing all the info
993 * @param string table table name
994 * @param string index index name to be renamed
995 * @return string PHP code to be used to rename the index
997 function rename_index_php($structure, $table, $index) {
1000 /// Validate if we can do it
1001 if (!$table = $structure->getTable($table)) {
1004 if (!$index = $table->getIndex($index)) {
1007 if ($table->getAllErrors()) {
1011 /// Prepend warning. This function isn't usable!
1012 $result .= 'DON\'T USE THIS FUNCTION (IT\'S ONLY EXPERIMENTAL). SOME DBs DON\'T SUPPORT IT!' . XMLDB_LINEFEED
. XMLDB_LINEFEED
;
1014 /// Add the standard PHP header
1015 $result .= XMLDB_PHP_HEADER
;
1018 $result .= XMLDB_LINEFEED
;
1019 $result .= ' /// Define index ' . $index->getName() . ' ('. ($index->getUnique() ?
'unique' : 'not unique') . ') to be renamed to NEWNAMEGOESHERE' . XMLDB_LINEFEED
;
1020 $result .= ' $table = new XMLDBTable(' . "'" . $table->getName() . "'" . ');' . XMLDB_LINEFEED
;
1021 $result .= ' $index = new XMLDBIndex(' . "'" . $index->getName() . "'" . ');' . XMLDB_LINEFEED
;
1022 $result .= ' $index->setAttributes(' . $index->getPHP(true) . ');' . XMLDB_LINEFEED
;
1024 /// Launch the proper DDL
1025 $result .= XMLDB_LINEFEED
;
1026 $result .= ' /// Launch rename index ' . $index->getName() . XMLDB_LINEFEED
;
1027 $result .= ' $result = $result && rename_index($table, $index, ' . "'" . 'NEWNAMEGOESHERE' . "'" . ');' . XMLDB_LINEFEED
;
1029 /// Add the proper upgrade_xxxx_savepoint call
1030 $result .= $this->upgrade_savepoint_php ($structure);
1032 /// Add standard PHP footer
1033 $result .= XMLDB_PHP_FOOTER
;
1039 * This function will generate the PHP code needed to
1040 * implement the upgrade_xxxx_savepoint() php calls in
1041 * upgrade code generated from the editor
1043 * @param XMLDBStructure structure object containing all the info
1044 * @return string PHP code to be used to stabilish a savepoint
1046 function upgrade_savepoint_php ($structure) {
1048 $path = $structure->getPath();
1054 $result = XMLDB_LINEFEED
.
1055 ' /// Main savepoint reached' . XMLDB_LINEFEED
.
1056 ' upgrade_main_savepoint($result, XXXXXXXXXX);' . XMLDB_LINEFEED
;