MDL-29314 enforce index size limits
[moodle.git] / lib / xmldb / xmldb_field.php
CommitLineData
4a0e2e63 1<?php
8165877a 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
a8cb94f6 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
8165877a 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 represent one XMLDB Field
28
a8cb94f6 29class xmldb_field extends xmldb_object {
eef868d1 30
8165877a 31 var $type;
32 var $length;
33 var $unsigned;
34 var $notnull;
35 var $default;
36 var $sequence;
8165877a 37 var $decimals;
38
39 /**
a8cb94f6 40 * Creates one new xmldb_field
8165877a 41 */
2a88f626 42 function __construct($name, $type=null, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) {
8165877a 43 $this->type = NULL;
44 $this->length = NULL;
d6125c4a 45 $this->unsigned = true;
8165877a 46 $this->notnull = false;
47 $this->default = NULL;
48 $this->sequence = false;
8165877a 49 $this->decimals = NULL;
69b80cc2 50 parent::__construct($name);
2a88f626 51 $this->set_attributes($type, $precision, $unsigned, $notnull, $sequence, $default, $previous);
69b80cc2 52 }
53
54/// TODO: Delete for 2.1 (deprecated in 2.0).
55/// Deprecated API starts here
56
57 function setAttributes($type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $enum=null, $enumvalues=null, $default=null, $previous=null) {
58
59 debugging('XMLDBField->setAttributes() has been deprecated in Moodle 2.0. Will be out in Moodle 2.1. Please use xmldb_field->set_attributes() instead.', DEBUG_DEVELOPER);
2a88f626 60 if ($enum) {
61 debugging('Also, ENUMs support has been dropped in Moodle 2.0. Your fields specs are incorrect because you are trying to introduce one new ENUM. Created DB estructures will ignore that.');
62 }
69b80cc2 63
2a88f626 64 return $this->set_attributes($type, $precision, $unsigned, $notnull, $sequence, $default, $previous);
8165877a 65 }
69b80cc2 66/// Deprecated API ends here
8165877a 67
76fd4736 68 /**
a8cb94f6 69 * Set all the attributes of one xmldb_field
76fd4736 70 *
71 * @param string type XMLDB_TYPE_INTEGER, XMLDB_TYPE_NUMBER, XMLDB_TYPE_CHAR, XMLDB_TYPE_TEXT, XMLDB_TYPE_BINARY
72 * @param string precision length for integers and chars, two-comma separated numbers for numbers and 'small', 'medium', 'big' for texts and binaries
73 * @param string unsigned XMLDB_UNSIGNED or null (or false)
74 * @param string notnull XMLDB_NOTNULL or null (or false)
75 * @param string sequence XMLDB_SEQUENCE or null (or false)
76fd4736 76 * @param string default meaningful default o null (or false)
77 */
2a88f626 78 function set_attributes($type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) {
76fd4736 79 $this->type = $type;
80 /// Try to split the precision into length and decimals and apply
81 /// each one as needed
82 $precisionarr = explode(',', $precision);
83 if (isset($precisionarr[0])) {
84 $this->length = trim($precisionarr[0]);
85 }
86 if (isset($precisionarr[1])) {
87 $this->decimals = trim($precisionarr[1]);
88 }
89 $this->precision = $type;
90 $this->unsigned = !empty($unsigned) ? true : false;
91 $this->notnull = !empty($notnull) ? true : false;
3a8c55c3 92 $this->sequence = !empty($sequence) ? true : false;
b4464372 93 $this->setDefault($default);
48c2be79 94
95 $this->previous = $previous;
76fd4736 96 }
97
8165877a 98 /**
99 * Get the type
100 */
101 function getType() {
102 return $this->type;
103 }
104
105 /**
106 * Get the length
107 */
108 function getLength() {
109 return $this->length;
110 }
111
112 /**
113 * Get the decimals
114 */
115 function getDecimals() {
116 return $this->decimals;
117 }
118
119 /**
120 * Get the notnull
121 */
122 function getNotNull() {
123 return $this->notnull;
124 }
125
126 /**
127 * Get the unsigned
128 */
129 function getUnsigned() {
130 return $this->unsigned;
131 }
132
133 /**
134 * Get the sequence
135 */
136 function getSequence() {
137 return $this->sequence;
138 }
139
8165877a 140 /**
141 * Get the default
142 */
143 function getDefault() {
144 return $this->default;
145 }
146
147 /**
148 * Set the field type
149 */
150 function setType($type) {
151 $this->type = $type;
152 }
153
154 /**
155 * Set the field length
156 */
157 function setLength($length) {
158 $this->length = $length;
159 }
160
161 /**
162 * Set the field decimals
163 */
164 function setDecimals($decimals) {
165 $this->decimals = $decimals;
166 }
167
168 /**
169 * Set the field unsigned
170 */
171 function setUnsigned($unsigned=true) {
172 $this->unsigned = $unsigned;
173 }
174
175 /**
176 * Set the field notnull
177 */
178 function setNotNull($notnull=true) {
179 $this->notnull = $notnull;
180 }
181
182 /**
183 * Set the field sequence
184 */
185 function setSequence($sequence=true) {
186 $this->sequence = $sequence;
187 }
188
8165877a 189 /**
190 * Set the field default
191 */
192 function setDefault($default) {
b4464372 193 /// Check, warn and auto-fix '' (empty) defaults for CHAR NOT NULL columns, changing them
194 /// to NULL so XMLDB will apply the proper default
195 if ($this->type == XMLDB_TYPE_CHAR && $this->notnull && $default === '') {
196 $this->errormsg = 'XMLDB has detected one CHAR NOT NULL column (' . $this->name . ") with '' (empty string) as DEFAULT value. This type of columns must have one meaningful DEFAULT declared or none (NULL). XMLDB have fixed it automatically changing it to none (NULL). The process will continue ok and proper defaults will be created accordingly with each DB requirements. Please fix it in source (XML and/or upgrade script) to avoid this message to be displayed.";
197 $this->debug($this->errormsg);
0a7e899d 198 $default = null;
199 }
200 /// Check, warn and autofix TEXT|BINARY columns having a default clause (only null is allowed)
201 if (($this->type == XMLDB_TYPE_TEXT || $this->type == XMLDB_TYPE_BINARY) && $default !== null) {
202 $this->errormsg = 'XMLDB has detected one TEXT/BINARY column (' . $this->name . ") with some DEFAULT defined. This type of columns cannot have any default value. Please fix it in source (XML and/or upgrade script) to avoid this message to be displayed.";
203 $this->debug($this->errormsg);
204 $default = null;
b4464372 205 }
8165877a 206 $this->default = $default;
207 }
208
209 /**
210 * Load data from XML to the table
211 */
a8cb94f6 212 function arr2xmldb_field($xmlarr) {
8165877a 213
214 $result = true;
215
216 /// Debug the table
217 /// traverse_xmlize($xmlarr); //Debug
218 /// print_object ($GLOBALS['traverse_array']); //Debug
219 /// $GLOBALS['traverse_array']=""; //Debug
220
221 /// Process table attributes (name, type, length, unsigned,
2a88f626 222 /// notnull, sequence, decimals, comment, previous, next)
8165877a 223 if (isset($xmlarr['@']['NAME'])) {
224 $this->name = trim($xmlarr['@']['NAME']);
225 } else {
226 $this->errormsg = 'Missing NAME attribute';
5bbe4831 227 $this->debug($this->errormsg);
8165877a 228 $result = false;
229 }
230
231 if (isset($xmlarr['@']['TYPE'])) {
232 /// Check for valid type
233 $type = $this->getXMLDBFieldType(trim($xmlarr['@']['TYPE']));
234 if ($type) {
235 $this->type = $type;
236 } else {
237 $this->errormsg = 'Invalid TYPE attribute';
5bbe4831 238 $this->debug($this->errormsg);
8165877a 239 $result = false;
240 }
241 } else {
242 $this->errormsg = 'Missing TYPE attribute';
5bbe4831 243 $this->debug($this->errormsg);
8165877a 244 $result = false;
245 }
246
247 if (isset($xmlarr['@']['LENGTH'])) {
248 $length = trim($xmlarr['@']['LENGTH']);
249 /// Check for integer values
250 if ($this->type == XMLDB_TYPE_INTEGER ||
251 $this->type == XMLDB_TYPE_NUMBER ||
252 $this->type == XMLDB_TYPE_CHAR) {
253 if (!(is_numeric($length)&&(intval($length)==floatval($length)))) {
254 $this->errormsg = 'Incorrect LENGTH attribute for int, number or char fields';
5bbe4831 255 $this->debug($this->errormsg);
8165877a 256 $result = false;
257 } else if (!$length) {
258 $this->errormsg = 'Zero LENGTH attribute';
5bbe4831 259 $this->debug($this->errormsg);
8165877a 260 $result = false;
261 }
262 }
263 /// Check for big, medium, small to be applied to text and binary
264 if ($this->type == XMLDB_TYPE_TEXT ||
265 $this->type == XMLDB_TYPE_BINARY) {
266 if (!$length) {
267 $length == 'big';
268 }
269 if ($length != 'big' &&
270 $length != 'medium' &&
271 $length != 'small') {
272 $this->errormsg = 'Incorrect LENGTH attribute for text and binary fields (only big, medium and small allowed)';
5bbe4831 273 $this->debug($this->errormsg);
8165877a 274 $result = false;
275 }
276 }
277 /// Finally, set the length
278 $this->length = $length;
279 }
280
281 if (isset($xmlarr['@']['UNSIGNED'])) {
282 $unsigned = strtolower(trim($xmlarr['@']['UNSIGNED']));
283 if ($unsigned == 'true') {
284 $this->unsigned = true;
285 } else if ($unsigned == 'false') {
286 $this->unsigned = false;
287 } else {
288 $this->errormsg = 'Incorrect UNSIGNED attribute (true/false allowed)';
5bbe4831 289 $this->debug($this->errormsg);
8165877a 290 $result = false;
291 }
292 }
293
294 if (isset($xmlarr['@']['NOTNULL'])) {
295 $notnull = strtolower(trim($xmlarr['@']['NOTNULL']));
296 if ($notnull == 'true') {
297 $this->notnull = true;
298 } else if ($notnull == 'false') {
299 $this->notnull = false;
300 } else {
301 $this->errormsg = 'Incorrect NOTNULL attribute (true/false allowed)';
5bbe4831 302 $this->debug($this->errormsg);
8165877a 303 $result = false;
304 }
305 }
306
307 if (isset($xmlarr['@']['SEQUENCE'])) {
308 $sequence = strtolower(trim($xmlarr['@']['SEQUENCE']));
309 if ($sequence == 'true') {
310 $this->sequence = true;
311 } else if ($sequence == 'false') {
312 $this->sequence = false;
313 } else {
314 $this->errormsg = 'Incorrect SEQUENCE attribute (true/false allowed)';
5bbe4831 315 $this->debug($this->errormsg);
8165877a 316 $result = false;
317 }
318 }
319
320 if (isset($xmlarr['@']['DEFAULT'])) {
b4464372 321 $this->setDefault(trim($xmlarr['@']['DEFAULT']));
8165877a 322 }
323
8165877a 324 $decimals = NULL;
325 if (isset($xmlarr['@']['DECIMALS'])) {
326 $decimals = trim($xmlarr['@']['DECIMALS']);
327 /// Check for integer values
eef868d1 328 if ($this->type == XMLDB_TYPE_NUMBER ||
8165877a 329 $this->type == XMLDB_TYPE_FLOAT) {
330 if (!(is_numeric($decimals)&&(intval($decimals)==floatval($decimals)))) {
331 $this->errormsg = 'Incorrect DECIMALS attribute for number field';
5bbe4831 332 $this->debug($this->errormsg);
8165877a 333 $result = false;
334 } else if ($this->length <= $decimals){
335 $this->errormsg = 'Incorrect DECIMALS attribute (bigget than length)';
5bbe4831 336 $this->debug($this->errormsg);
8165877a 337 $result = false;
338 }
339 } else {
340 $this->errormsg = 'Incorrect DECIMALS attribute for non-number field';
5bbe4831 341 $this->debug($this->errormsg);
8165877a 342 $result = false;
343 }
344 } else {
345 if ($this->type == XMLDB_TYPE_NUMBER) {
346 $decimals = 0;
347 }
348 }
349 // Finally, set the decimals
350 if ($this->type == XMLDB_TYPE_NUMBER ||
351 $this->type == XMLDB_TYPE_FLOAT) {
352 $this->decimals = $decimals;
353 }
354
355 if (isset($xmlarr['@']['COMMENT'])) {
356 $this->comment = trim($xmlarr['@']['COMMENT']);
357 }
358
359 if (isset($xmlarr['@']['PREVIOUS'])) {
360 $this->previous = trim($xmlarr['@']['PREVIOUS']);
361 }
362
363 if (isset($xmlarr['@']['NEXT'])) {
364 $this->next = trim($xmlarr['@']['NEXT']);
365 }
366
16454b0f 367 /// TODO: Drop this check in Moodle 2.1
368 /// Detect if there is old enum information in the XML file
369 if (isset($xmlarr['@']['ENUM']) && isset($xmlarr['@']['ENUMVALUES'])) {
370 $this->hasenums = true;
371 if ($xmlarr['@']['ENUM'] == 'true') {
372 $this->hasenumsenabled = true;
373 }
374 }
375
8165877a 376 /// Set some attributes
377 if ($result) {
378 $this->loaded = true;
379 }
380 $this->calculateHash();
381 return $result;
382 }
383
384 /**
385 * This function returns the correct XMLDB_TYPE_XXX value for the
386 * string passed as argument
387 */
388 function getXMLDBFieldType($type) {
389
390 $result = XMLDB_TYPE_INCORRECT;
eef868d1 391
8165877a 392 switch (strtolower($type)) {
393 case 'int':
394 $result = XMLDB_TYPE_INTEGER;
395 break;
396 case 'number':
397 $result = XMLDB_TYPE_NUMBER;
398 break;
399 case 'float':
400 $result = XMLDB_TYPE_FLOAT;
401 break;
402 case 'char':
403 $result = XMLDB_TYPE_CHAR;
404 break;
405 case 'text':
406 $result = XMLDB_TYPE_TEXT;
407 break;
408 case 'binary':
409 $result = XMLDB_TYPE_BINARY;
410 break;
411 case 'datetime':
412 $result = XMLDB_TYPE_DATETIME;
413 break;
414 }
415 /// Return the normalized XMLDB_TYPE
416 return $result;
417 }
418
419 /**
420 * This function returns the correct name value for the
421 * XMLDB_TYPE_XXX passed as argument
422 */
423 function getXMLDBTypeName($type) {
424
425 $result = "";
eef868d1 426
8165877a 427 switch (strtolower($type)) {
428 case XMLDB_TYPE_INTEGER:
429 $result = 'int';
430 break;
431 case XMLDB_TYPE_NUMBER:
432 $result = 'number';
433 break;
434 case XMLDB_TYPE_FLOAT:
435 $result = 'float';
436 break;
437 case XMLDB_TYPE_CHAR:
438 $result = 'char';
439 break;
440 case XMLDB_TYPE_TEXT:
441 $result = 'text';
442 break;
443 case XMLDB_TYPE_BINARY:
444 $result = 'binary';
445 break;
446 case XMLDB_TYPE_DATETIME:
447 $result = 'datetime';
448 break;
449 }
450 /// Return the normalized name
451 return $result;
452 }
453
454 /**
a8cb94f6 455 * This function calculate and set the hash of one xmldb_field
8165877a 456 */
457 function calculateHash($recursive = false) {
458 if (!$this->loaded) {
459 $this->hash = NULL;
460 } else {
461 $key = $this->name . $this->type . $this->length .
462 $this->unsigned . $this->notnull . $this->sequence .
463 $this->decimals . $this->comment;
8165877a 464 $this->hash = md5($key);
465 }
466 }
467
eef868d1 468 /**
8165877a 469 *This function will output the XML text for one field
470 */
471 function xmlOutput() {
472 $o = '';
473 $o.= ' <FIELD NAME="' . $this->name . '"';
474 $o.= ' TYPE="' . $this->getXMLDBTypeName($this->type) . '"';
475 if ($this->length) {
476 $o.= ' LENGTH="' . $this->length . '"';
477 }
478 if ($this->notnull) {
479 $notnull = 'true';
480 } else {
481 $notnull = 'false';
482 }
483 $o.= ' NOTNULL="' . $notnull . '"';
8165877a 484 if ($this->type == XMLDB_TYPE_INTEGER ||
485 $this->type == XMLDB_TYPE_NUMBER ||
486 $this->type == XMLDB_TYPE_FLOAT) {
487 if ($this->unsigned) {
488 $unsigned = 'true';
489 } else {
490 $unsigned = 'false';
491 }
492 $o.= ' UNSIGNED="' . $unsigned . '"';
493 }
494 if (!$this->sequence && $this->default !== NULL) {
495 $o.= ' DEFAULT="' . $this->default . '"';
496 }
497 if ($this->sequence) {
498 $sequence = 'true';
499 } else {
500 $sequence = 'false';
501 }
502 $o.= ' SEQUENCE="' . $sequence . '"';
8165877a 503 if ($this->decimals !== NULL) {
504 $o.= ' DECIMALS="' . $this->decimals . '"';
505 }
506 if ($this->comment) {
507 $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';
508 }
509 if ($this->previous) {
510 $o.= ' PREVIOUS="' . $this->previous . '"';
511 }
512 if ($this->next) {
513 $o.= ' NEXT="' . $this->next . '"';
514 }
515 $o.= '/>' . "\n";
516
517 return $o;
518 }
519
520 /**
a8cb94f6 521 * This function will set all the attributes of the xmldb_field object
8165877a 522 * based on information passed in one ADOField
523 */
524 function setFromADOField($adofield) {
525
526 /// Calculate the XMLDB_TYPE
527 switch (strtolower($adofield->type)) {
528 case 'int':
529 case 'tinyint':
530 case 'smallint':
531 case 'bigint':
532 case 'integer':
533 $this->type = XMLDB_TYPE_INTEGER;
534 break;
535 case 'number':
536 case 'decimal':
537 case 'dec':
538 case 'numeric':
539 $this->type = XMLDB_TYPE_NUMBER;
540 break;
541 case 'float':
542 case 'double':
543 $this->type = XMLDB_TYPE_FLOAT;
544 break;
545 case 'char':
546 case 'varchar':
547 case 'enum':
548 $this->type = XMLDB_TYPE_CHAR;
549 break;
550 case 'text':
551 case 'tinytext':
552 case 'mediumtext':
553 case 'longtext':
554 $this->type = XMLDB_TYPE_TEXT;
555 break;
556 case 'blob':
557 case 'tinyblob':
558 case 'mediumblob':
559 case 'longblob':
560 $this->type = XMLDB_TYPE_BINARY;
561 break;
562 case 'datetime':
563 case 'timestamp':
564 $this->type = XMLDB_TYPE_DATETIME;
565 break;
566 default:
567 $this->type = XMLDB_TYPE_TEXT;
568 }
569 /// Calculate the length of the field
eef868d1 570 if ($adofield->max_length > 0 &&
8165877a 571 ($this->type == XMLDB_TYPE_INTEGER ||
572 $this->type == XMLDB_TYPE_NUMBER ||
573 $this->type == XMLDB_TYPE_FLOAT ||
574 $this->type == XMLDB_TYPE_CHAR)) {
575 $this->length = $adofield->max_length;
576 }
577 if ($this->type == XMLDB_TYPE_TEXT) {
578 switch (strtolower($adofield->type)) {
579 case 'tinytext':
580 case 'text':
581 $this->length = 'small';
582 break;
583 case 'mediumtext':
584 $this->length = 'medium';
585 break;
586 case 'longtext':
587 $this->length = 'big';
588 break;
589 default:
590 $this->length = 'small';
591 }
592 }
593 if ($this->type == XMLDB_TYPE_BINARY) {
594 switch (strtolower($adofield->type)) {
595 case 'tinyblob':
596 case 'blob':
597 $this->length = 'small';
598 break;
599 case 'mediumblob':
600 $this->length = 'medium';
601 break;
602 case 'longblob':
603 $this->length = 'big';
604 break;
605 default:
606 $this->length = 'small';
607 }
608 }
609 /// Calculate the decimals of the field
610 if ($adofield->max_length > 0 &&
eef868d1 611 $adofield->scale &&
8165877a 612 ($this->type == XMLDB_TYPE_NUMBER ||
613 $this->type == XMLDB_TYPE_FLOAT)) {
614 $this->decimals = $adofield->scale;
615 }
616 /// Calculate the unsigned field
eef868d1 617 if ($adofield->unsigned &&
8165877a 618 ($this->type == XMLDB_TYPE_INTEGER ||
619 $this->type == XMLDB_TYPE_NUMBER ||
620 $this->type == XMLDB_TYPE_FLOAT)) {
621 $this->unsigned = true;
622 }
623 /// Calculate the notnull field
624 if ($adofield->not_null) {
625 $this->notnull = true;
626 }
627 /// Calculate the default field
628 if ($adofield->has_default) {
629 $this->default = $adofield->default_value;
eef868d1 630 }
8165877a 631 /// Calculate the sequence field
632 if ($adofield->auto_increment) {
633 $this->sequence = true;
634 /// Sequence fields are always unsigned
635 $this->unsigned = true;
636 }
8165877a 637 /// Some more fields
638 $this->loaded = true;
639 $this->changed = true;
640 }
641
7b31a94e 642 /**
a8cb94f6 643 * Returns the PHP code needed to define one xmldb_field
7b31a94e 644 */
645 function getPHP($includeprevious=true) {
646
647 $result = '';
648
649 /// The XMLDBTYPE
650 switch ($this->getType()) {
651 case XMLDB_TYPE_INTEGER:
652 $result .= 'XMLDB_TYPE_INTEGER' . ', ';
653 break;
654 case XMLDB_TYPE_NUMBER:
655 $result .= 'XMLDB_TYPE_NUMBER' . ', ';
656 break;
657 case XMLDB_TYPE_FLOAT:
658 $result .= 'XMLDB_TYPE_FLOAT' . ', ';
659 break;
660 case XMLDB_TYPE_CHAR:
661 $result .= 'XMLDB_TYPE_CHAR' . ', ';
662 break;
663 case XMLDB_TYPE_TEXT:
664 $result .= 'XMLDB_TYPE_TEXT' . ', ';
665 break;
666 case XMLDB_TYPE_BINARY:
667 $result .= 'XMLDB_TYPE_BINARY' . ', ';
668 break;
669 case XMLDB_TYPE_DATETIME:
670 $result .= 'XMLDB_TYPE_DATETIME' . ', ';
671 break;
672 case XMLDB_TYPE_TIMESTAMP:
673 $result .= 'XMLDB_TYPE_TIMESTAMP' . ', ';
674 break;
675 }
676 /// The length
677 $length = $this->getLength();
678 $decimals = $this->getDecimals();
679 if (!empty($length)) {
680 $result .= "'" . $length;
681 if (!empty($decimals)) {
682 $result .= ', ' . $decimals;
683 }
684 $result .= "', ";
685 } else {
686 $result .= 'null, ';
687 }
051b9588 688 /// Unsigned (only applicable to numbers)
7b31a94e 689 $unsigned = $this->getUnsigned();
051b9588 690 if (!empty($unsigned) &&
691 ($this->getType() == XMLDB_TYPE_INTEGER || $this->getType() == XMLDB_TYPE_NUMBER || $this->getType() == XMLDB_TYPE_FLOAT)) {
7b31a94e 692 $result .= 'XMLDB_UNSIGNED' . ', ';
693 } else {
694 $result .= 'null, ';
695 }
696 /// Not Null
697 $notnull = $this->getNotnull();
698 if (!empty($notnull)) {
699 $result .= 'XMLDB_NOTNULL' . ', ';
700 } else {
701 $result .= 'null, ';
702 }
703 /// Sequence
704 $sequence = $this->getSequence();
705 if (!empty($sequence)) {
706 $result .= 'XMLDB_SEQUENCE' . ', ';
707 } else {
708 $result .= 'null, ';
709 }
7b31a94e 710 /// Default
711 $default = $this->getDefault();
88bf0213 712 if ($default !== null && !$this->getSequence()) {
7b31a94e 713 $result .= "'" . $default . "'";
714 } else {
715 $result .= 'null';
716 }
717 /// Previous (decided by parameter)
718 if ($includeprevious) {
719 $previous = $this->getPrevious();
720 if (!empty($previous)) {
721 $result .= ", '" . $previous . "'";
722 } else {
723 $result .= ', null';
724 }
725 }
726 /// Return result
727 return $result;
728 }
729
8165877a 730 /**
731 * Shows info in a readable format
732 */
733 function readableInfo() {
734 $o = '';
735 /// type
736 $o .= $this->getXMLDBTypeName($this->type);
737 /// length
738 if ($this->type == XMLDB_TYPE_INTEGER ||
739 $this->type == XMLDB_TYPE_NUMBER ||
740 $this->type == XMLDB_TYPE_FLOAT ||
741 $this->type == XMLDB_TYPE_CHAR) {
742 if ($this->length) {
743 $o .= ' (' . $this->length;
744 if ($this->type == XMLDB_TYPE_NUMBER ||
745 $this->type == XMLDB_TYPE_FLOAT) {
746 if ($this->decimals !== NULL) {
747 $o .= ', ' . $this->decimals;
748 }
749 }
750 $o .= ')';
751 }
752 }
753 if ($this->type == XMLDB_TYPE_TEXT ||
754 $this->type == XMLDB_TYPE_BINARY) {
755 $o .= ' (' . $this->length . ')';
756 }
8165877a 757 /// unsigned
758 if ($this->type == XMLDB_TYPE_INTEGER ||
759 $this->type == XMLDB_TYPE_NUMBER ||
760 $this->type == XMLDB_TYPE_FLOAT) {
761 if ($this->unsigned) {
762 $o .= ' unsigned';
763 } else {
764 $o .= ' signed';
765 }
766 }
767 /// not null
768 if ($this->notnull) {
769 $o .= ' not null';
770 }
771 /// default
772 if ($this->default !== NULL) {
773 $o .= ' default ';
774 if ($this->type == XMLDB_TYPE_CHAR ||
775 $this->type == XMLDB_TYPE_TEXT) {
776 $o .= "'" . $this->default . "'";
777 } else {
778 $o .= $this->default;
779 }
780 }
781 /// sequence
782 if ($this->sequence) {
783 $o .= ' auto-numbered';
784 }
785
786 return $o;
787 }
788}
789
adc31ef5 790/// TODO: Delete for 2.1 (deprecated in 2.0).
9c9fad9d 791/// Deprecated API starts here
792class XMLDBField extends xmldb_field {
793
794 function __construct($name) {
795 parent::__construct($name);
796 }
797
798}
799/// Deprecated API ends here