MDL-32003 fix phpdocs in xmldb abstraction
[moodle.git] / lib / xmldb / xmldb_field.php
blobf84879aa2e15742ae185901306853b0dd5bb9211
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This class represent one XMLDB Field
20 * @package core_xmldb
21 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
22 * 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 class xmldb_field extends xmldb_object {
31 /** @var int XMLDB_TYPE_ constants */
32 var $type;
34 /** @var int size of field */
35 var $length;
37 /** @var bool is null forbidden? XMLDB_NOTNULL */
38 var $notnull;
40 /** @var mixed default value */
41 var $default;
43 /** @var bool use automatic counter */
44 var $sequence;
46 /** @var int number of decimals */
47 var $decimals;
49 /**
50 * Note:
51 * - Oracle: VARCHAR2 has a limit of 4000 bytes
52 * - SQL Server: NVARCHAR has a limit of 40000 chars
53 * - MySQL: VARCHAR 65,535 chars
54 * - PostgreSQL: no limit
56 * @const maximum length of text field
58 const CHAR_MAX_LENGTH = 1333;
61 /**
62 * @const maximum number of digits of integers
64 const INTEGER_MAX_LENGTH = 20;
66 /**
67 * @const max length of decimals
69 const NUMBER_MAX_LENGTH = 20;
71 /**
72 * @const max length of floats
74 const FLOAT_MAX_LENGTH = 20;
76 /**
77 * Note:
78 * - Oracle has 30 chars limit for all names
80 * @const maximumn length of field names
82 const NAME_MAX_LENGTH = 30;
84 /**
85 * Creates one new xmldb_field
86 * @param string $name of field
87 * @param int $type XMLDB_TYPE_INTEGER, XMLDB_TYPE_NUMBER, XMLDB_TYPE_CHAR, XMLDB_TYPE_TEXT, XMLDB_TYPE_BINARY
88 * @param string $precision length for integers and chars, two-comma separated numbers for numbers
89 * @param bool $unsigned XMLDB_UNSIGNED or null (or false)
90 * @param bool $notnull XMLDB_NOTNULL or null (or false)
91 * @param bool $sequence XMLDB_SEQUENCE or null (or false)
92 * @param mixed $default meaningful default o null (or false)
93 * @param xmldb_object $previous
95 function __construct($name, $type=null, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) {
96 $this->type = NULL;
97 $this->length = NULL;
98 $this->notnull = false;
99 $this->default = NULL;
100 $this->sequence = false;
101 $this->decimals = NULL;
102 parent::__construct($name);
103 $this->set_attributes($type, $precision, $unsigned, $notnull, $sequence, $default, $previous);
107 * Set all the attributes of one xmldb_field
109 * @param int $type XMLDB_TYPE_INTEGER, XMLDB_TYPE_NUMBER, XMLDB_TYPE_CHAR, XMLDB_TYPE_TEXT, XMLDB_TYPE_BINARY
110 * @param string $precision length for integers and chars, two-comma separated numbers for numbers
111 * @param bool $unsigned XMLDB_UNSIGNED or null (or false)
112 * @param bool $notnull XMLDB_NOTNULL or null (or false)
113 * @param bool $sequence XMLDB_SEQUENCE or null (or false)
114 * @param mixed $default meaningful default o null (or false)
115 * @param xmldb_object $previous
117 function set_attributes($type, $precision=null, $unsigned=null, $notnull=null, $sequence=null, $default=null, $previous=null) {
118 $this->type = $type;
119 /// Try to split the precision into length and decimals and apply
120 /// each one as needed
121 $precisionarr = explode(',', $precision);
122 if (isset($precisionarr[0])) {
123 $this->length = trim($precisionarr[0]);
125 if (isset($precisionarr[1])) {
126 $this->decimals = trim($precisionarr[1]);
128 $this->precision = $type;
129 $this->notnull = !empty($notnull) ? true : false;
130 $this->sequence = !empty($sequence) ? true : false;
131 $this->setDefault($default);
133 if ($this->type == XMLDB_TYPE_BINARY || $this->type == XMLDB_TYPE_TEXT) {
134 $this->length = null;
135 $this->decimals = null;
138 $this->previous = $previous;
142 * Get the type
143 * @return int
145 function getType() {
146 return $this->type;
150 * Get the length
151 * @return int
153 function getLength() {
154 return $this->length;
158 * Get the decimals
159 * @return string
161 function getDecimals() {
162 return $this->decimals;
166 * Get the notnull
167 * @return bool
169 function getNotNull() {
170 return $this->notnull;
174 * Get the unsigned
175 * @deprecated since moodle 2.3
176 * @return bool
178 function getUnsigned() {
179 return false;
183 * Get the sequence
184 * @return bool
186 function getSequence() {
187 return $this->sequence;
191 * Get the default
192 * @return mixed
194 function getDefault() {
195 return $this->default;
199 * Set the field type
200 * @param int $type
202 function setType($type) {
203 $this->type = $type;
207 * Set the field length
208 * @param int $length
210 function setLength($length) {
211 $this->length = $length;
215 * Set the field decimals
216 * @param string
218 function setDecimals($decimals) {
219 $this->decimals = $decimals;
223 * Set the field unsigned
224 * @deprecated since moodle 2.3
225 * @param bool $unsigned
227 function setUnsigned($unsigned=true) {
231 * Set the field notnull
232 * @param bool $notnull
234 function setNotNull($notnull=true) {
235 $this->notnull = $notnull;
239 * Set the field sequence
240 * @param bool $sequence
242 function setSequence($sequence=true) {
243 $this->sequence = $sequence;
247 * Set the field default
248 * @param mixed $default
250 function setDefault($default) {
251 // Check, warn and auto-fix '' (empty) defaults for CHAR NOT NULL columns, changing them
252 // to NULL so XMLDB will apply the proper default
253 if ($this->type == XMLDB_TYPE_CHAR && $this->notnull && $default === '') {
254 $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.";
255 $this->debug($this->errormsg);
256 $default = null;
258 // Check, warn and autofix TEXT|BINARY columns having a default clause (only null is allowed)
259 if (($this->type == XMLDB_TYPE_TEXT || $this->type == XMLDB_TYPE_BINARY) && $default !== null) {
260 $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.";
261 $this->debug($this->errormsg);
262 $default = null;
264 $this->default = $default;
268 * Load data from XML to the table
269 * @param array $xmlarr
271 function arr2xmldb_field($xmlarr) {
273 $result = true;
275 // Debug the table
276 // traverse_xmlize($xmlarr); //Debug
277 // print_object ($GLOBALS['traverse_array']); //Debug
278 // $GLOBALS['traverse_array']=""; //Debug
280 // Process table attributes (name, type, length
281 // notnull, sequence, decimals, comment, previous, next)
282 if (isset($xmlarr['@']['NAME'])) {
283 $this->name = trim($xmlarr['@']['NAME']);
284 } else {
285 $this->errormsg = 'Missing NAME attribute';
286 $this->debug($this->errormsg);
287 $result = false;
290 if (isset($xmlarr['@']['TYPE'])) {
291 // Check for valid type
292 $type = $this->getXMLDBFieldType(trim($xmlarr['@']['TYPE']));
293 if ($type) {
294 $this->type = $type;
295 } else {
296 $this->errormsg = 'Invalid TYPE attribute';
297 $this->debug($this->errormsg);
298 $result = false;
300 } else {
301 $this->errormsg = 'Missing TYPE attribute';
302 $this->debug($this->errormsg);
303 $result = false;
306 if (isset($xmlarr['@']['LENGTH'])) {
307 $length = trim($xmlarr['@']['LENGTH']);
308 // Check for integer values
309 if ($this->type == XMLDB_TYPE_INTEGER ||
310 $this->type == XMLDB_TYPE_NUMBER ||
311 $this->type == XMLDB_TYPE_CHAR) {
312 if (!(is_numeric($length)&&(intval($length)==floatval($length)))) {
313 $this->errormsg = 'Incorrect LENGTH attribute for int, number or char fields';
314 $this->debug($this->errormsg);
315 $result = false;
316 } else if (!$length) {
317 $this->errormsg = 'Zero LENGTH attribute';
318 $this->debug($this->errormsg);
319 $result = false;
322 // Remove length from text and binary
323 if ($this->type == XMLDB_TYPE_TEXT ||
324 $this->type == XMLDB_TYPE_BINARY) {
325 $length = null;
327 // Finally, set the length
328 $this->length = $length;
331 if (isset($xmlarr['@']['NOTNULL'])) {
332 $notnull = strtolower(trim($xmlarr['@']['NOTNULL']));
333 if ($notnull == 'true') {
334 $this->notnull = true;
335 } else if ($notnull == 'false') {
336 $this->notnull = false;
337 } else {
338 $this->errormsg = 'Incorrect NOTNULL attribute (true/false allowed)';
339 $this->debug($this->errormsg);
340 $result = false;
344 if (isset($xmlarr['@']['SEQUENCE'])) {
345 $sequence = strtolower(trim($xmlarr['@']['SEQUENCE']));
346 if ($sequence == 'true') {
347 $this->sequence = true;
348 } else if ($sequence == 'false') {
349 $this->sequence = false;
350 } else {
351 $this->errormsg = 'Incorrect SEQUENCE attribute (true/false allowed)';
352 $this->debug($this->errormsg);
353 $result = false;
357 if (isset($xmlarr['@']['DEFAULT'])) {
358 $this->setDefault(trim($xmlarr['@']['DEFAULT']));
361 $decimals = NULL;
362 if (isset($xmlarr['@']['DECIMALS'])) {
363 $decimals = trim($xmlarr['@']['DECIMALS']);
364 // Check for integer values
365 if ($this->type == XMLDB_TYPE_NUMBER ||
366 $this->type == XMLDB_TYPE_FLOAT) {
367 if (!(is_numeric($decimals)&&(intval($decimals)==floatval($decimals)))) {
368 $this->errormsg = 'Incorrect DECIMALS attribute for number field';
369 $this->debug($this->errormsg);
370 $result = false;
371 } else if ($this->length <= $decimals){
372 $this->errormsg = 'Incorrect DECIMALS attribute (bigget than length)';
373 $this->debug($this->errormsg);
374 $result = false;
376 } else {
377 $this->errormsg = 'Incorrect DECIMALS attribute for non-number field';
378 $this->debug($this->errormsg);
379 $result = false;
381 } else {
382 if ($this->type == XMLDB_TYPE_NUMBER) {
383 $decimals = 0;
386 // Finally, set the decimals
387 if ($this->type == XMLDB_TYPE_NUMBER ||
388 $this->type == XMLDB_TYPE_FLOAT) {
389 $this->decimals = $decimals;
392 if (isset($xmlarr['@']['COMMENT'])) {
393 $this->comment = trim($xmlarr['@']['COMMENT']);
396 if (isset($xmlarr['@']['PREVIOUS'])) {
397 $this->previous = trim($xmlarr['@']['PREVIOUS']);
400 if (isset($xmlarr['@']['NEXT'])) {
401 $this->next = trim($xmlarr['@']['NEXT']);
404 // Set some attributes
405 if ($result) {
406 $this->loaded = true;
408 $this->calculateHash();
409 return $result;
413 * This function returns the correct XMLDB_TYPE_XXX value for the
414 * string passed as argument
415 * @param string $type
416 * @return int
418 function getXMLDBFieldType($type) {
420 $result = XMLDB_TYPE_INCORRECT;
422 switch (strtolower($type)) {
423 case 'int':
424 $result = XMLDB_TYPE_INTEGER;
425 break;
426 case 'number':
427 $result = XMLDB_TYPE_NUMBER;
428 break;
429 case 'float':
430 $result = XMLDB_TYPE_FLOAT;
431 break;
432 case 'char':
433 $result = XMLDB_TYPE_CHAR;
434 break;
435 case 'text':
436 $result = XMLDB_TYPE_TEXT;
437 break;
438 case 'binary':
439 $result = XMLDB_TYPE_BINARY;
440 break;
441 case 'datetime':
442 $result = XMLDB_TYPE_DATETIME;
443 break;
445 // Return the normalized XMLDB_TYPE
446 return $result;
450 * This function returns the correct name value for the
451 * XMLDB_TYPE_XXX passed as argument
452 * @param int $type
453 * @return string
455 function getXMLDBTypeName($type) {
457 $result = "";
459 switch (strtolower($type)) {
460 case XMLDB_TYPE_INTEGER:
461 $result = 'int';
462 break;
463 case XMLDB_TYPE_NUMBER:
464 $result = 'number';
465 break;
466 case XMLDB_TYPE_FLOAT:
467 $result = 'float';
468 break;
469 case XMLDB_TYPE_CHAR:
470 $result = 'char';
471 break;
472 case XMLDB_TYPE_TEXT:
473 $result = 'text';
474 break;
475 case XMLDB_TYPE_BINARY:
476 $result = 'binary';
477 break;
478 case XMLDB_TYPE_DATETIME:
479 $result = 'datetime';
480 break;
482 // Return the normalized name
483 return $result;
487 * This function calculate and set the hash of one xmldb_field
488 * @param bool $recursive
489 * @return void, modifies $this->hash
491 function calculateHash($recursive = false) {
492 if (!$this->loaded) {
493 $this->hash = NULL;
494 } else {
495 $key = $this->name . $this->type . $this->length .
496 $this->notnull . $this->sequence .
497 $this->decimals . $this->comment;
498 $this->hash = md5($key);
503 * This function will output the XML text for one field
504 * @return string
506 function xmlOutput() {
507 $o = '';
508 $o.= ' <FIELD NAME="' . $this->name . '"';
509 $o.= ' TYPE="' . $this->getXMLDBTypeName($this->type) . '"';
510 if ($this->length) {
511 $o.= ' LENGTH="' . $this->length . '"';
513 if ($this->notnull) {
514 $notnull = 'true';
515 } else {
516 $notnull = 'false';
518 $o.= ' NOTNULL="' . $notnull . '"';
519 if (!$this->sequence && $this->default !== NULL) {
520 $o.= ' DEFAULT="' . $this->default . '"';
522 if ($this->sequence) {
523 $sequence = 'true';
524 } else {
525 $sequence = 'false';
527 $o.= ' SEQUENCE="' . $sequence . '"';
528 if ($this->decimals !== NULL) {
529 $o.= ' DECIMALS="' . $this->decimals . '"';
531 if ($this->comment) {
532 $o.= ' COMMENT="' . htmlspecialchars($this->comment) . '"';
534 if ($this->previous) {
535 $o.= ' PREVIOUS="' . $this->previous . '"';
537 if ($this->next) {
538 $o.= ' NEXT="' . $this->next . '"';
540 $o.= '/>' . "\n";
542 return $o;
546 * This function will set all the attributes of the xmldb_field object
547 * based on information passed in one ADOField
548 * @param string $adofield
549 * @return void, sets $this->type
551 function setFromADOField($adofield) {
553 // Calculate the XMLDB_TYPE
554 switch (strtolower($adofield->type)) {
555 case 'int':
556 case 'tinyint':
557 case 'smallint':
558 case 'bigint':
559 case 'integer':
560 $this->type = XMLDB_TYPE_INTEGER;
561 break;
562 case 'number':
563 case 'decimal':
564 case 'dec':
565 case 'numeric':
566 $this->type = XMLDB_TYPE_NUMBER;
567 break;
568 case 'float':
569 case 'double':
570 $this->type = XMLDB_TYPE_FLOAT;
571 break;
572 case 'char':
573 case 'varchar':
574 case 'enum':
575 $this->type = XMLDB_TYPE_CHAR;
576 break;
577 case 'text':
578 case 'tinytext':
579 case 'mediumtext':
580 case 'longtext':
581 $this->type = XMLDB_TYPE_TEXT;
582 break;
583 case 'blob':
584 case 'tinyblob':
585 case 'mediumblob':
586 case 'longblob':
587 $this->type = XMLDB_TYPE_BINARY;
588 break;
589 case 'datetime':
590 case 'timestamp':
591 $this->type = XMLDB_TYPE_DATETIME;
592 break;
593 default:
594 $this->type = XMLDB_TYPE_TEXT;
596 // Calculate the length of the field
597 if ($adofield->max_length > 0 &&
598 ($this->type == XMLDB_TYPE_INTEGER ||
599 $this->type == XMLDB_TYPE_NUMBER ||
600 $this->type == XMLDB_TYPE_FLOAT ||
601 $this->type == XMLDB_TYPE_CHAR)) {
602 $this->length = $adofield->max_length;
604 if ($this->type == XMLDB_TYPE_TEXT) {
605 $this->length = null;
607 if ($this->type == XMLDB_TYPE_BINARY) {
608 $this->length = null;
610 // Calculate the decimals of the field
611 if ($adofield->max_length > 0 &&
612 $adofield->scale &&
613 ($this->type == XMLDB_TYPE_NUMBER ||
614 $this->type == XMLDB_TYPE_FLOAT)) {
615 $this->decimals = $adofield->scale;
617 // Calculate the notnull field
618 if ($adofield->not_null) {
619 $this->notnull = true;
621 // Calculate the default field
622 if ($adofield->has_default) {
623 $this->default = $adofield->default_value;
625 // Calculate the sequence field
626 if ($adofield->auto_increment) {
627 $this->sequence = true;
629 // Some more fields
630 $this->loaded = true;
631 $this->changed = true;
635 * Returns the PHP code needed to define one xmldb_field
636 * @param bool $includeprevious
637 * @return string
639 function getPHP($includeprevious=true) {
641 $result = '';
643 // The XMLDBTYPE
644 switch ($this->getType()) {
645 case XMLDB_TYPE_INTEGER:
646 $result .= 'XMLDB_TYPE_INTEGER' . ', ';
647 break;
648 case XMLDB_TYPE_NUMBER:
649 $result .= 'XMLDB_TYPE_NUMBER' . ', ';
650 break;
651 case XMLDB_TYPE_FLOAT:
652 $result .= 'XMLDB_TYPE_FLOAT' . ', ';
653 break;
654 case XMLDB_TYPE_CHAR:
655 $result .= 'XMLDB_TYPE_CHAR' . ', ';
656 break;
657 case XMLDB_TYPE_TEXT:
658 $result .= 'XMLDB_TYPE_TEXT' . ', ';
659 break;
660 case XMLDB_TYPE_BINARY:
661 $result .= 'XMLDB_TYPE_BINARY' . ', ';
662 break;
663 case XMLDB_TYPE_DATETIME:
664 $result .= 'XMLDB_TYPE_DATETIME' . ', ';
665 break;
666 case XMLDB_TYPE_TIMESTAMP:
667 $result .= 'XMLDB_TYPE_TIMESTAMP' . ', ';
668 break;
670 // The length
671 $length = $this->getLength();
672 $decimals = $this->getDecimals();
673 if (!empty($length)) {
674 $result .= "'" . $length;
675 if (!empty($decimals)) {
676 $result .= ', ' . $decimals;
678 $result .= "', ";
679 } else {
680 $result .= 'null, ';
682 // Unsigned is not used any more since Moodle 2.3
683 $result .= 'null, ';
684 // Not Null
685 $notnull = $this->getNotnull();
686 if (!empty($notnull)) {
687 $result .= 'XMLDB_NOTNULL' . ', ';
688 } else {
689 $result .= 'null, ';
691 // Sequence
692 $sequence = $this->getSequence();
693 if (!empty($sequence)) {
694 $result .= 'XMLDB_SEQUENCE' . ', ';
695 } else {
696 $result .= 'null, ';
698 // Default
699 $default = $this->getDefault();
700 if ($default !== null && !$this->getSequence()) {
701 $result .= "'" . $default . "'";
702 } else {
703 $result .= 'null';
705 // Previous (decided by parameter)
706 if ($includeprevious) {
707 $previous = $this->getPrevious();
708 if (!empty($previous)) {
709 $result .= ", '" . $previous . "'";
710 } else {
711 $result .= ', null';
714 // Return result
715 return $result;
719 * Shows info in a readable format
720 * @return string
722 function readableInfo() {
723 $o = '';
724 // type
725 $o .= $this->getXMLDBTypeName($this->type);
726 // length
727 if ($this->type == XMLDB_TYPE_INTEGER ||
728 $this->type == XMLDB_TYPE_NUMBER ||
729 $this->type == XMLDB_TYPE_FLOAT ||
730 $this->type == XMLDB_TYPE_CHAR) {
731 if ($this->length) {
732 $o .= ' (' . $this->length;
733 if ($this->type == XMLDB_TYPE_NUMBER ||
734 $this->type == XMLDB_TYPE_FLOAT) {
735 if ($this->decimals !== NULL) {
736 $o .= ', ' . $this->decimals;
739 $o .= ')';
742 // not null
743 if ($this->notnull) {
744 $o .= ' not null';
746 // default
747 if ($this->default !== NULL) {
748 $o .= ' default ';
749 if ($this->type == XMLDB_TYPE_CHAR ||
750 $this->type == XMLDB_TYPE_TEXT) {
751 $o .= "'" . $this->default . "'";
752 } else {
753 $o .= $this->default;
756 // sequence
757 if ($this->sequence) {
758 $o .= ' auto-numbered';
761 return $o;
765 * Validates the field restrictions.
767 * The error message should not be localised because it is intended for developers,
768 * end users and admins should never see these problems!
770 * @param xmldb_table $xmldb_table optional when object is table
771 * @return string null if ok, error message if problem found
773 function validateDefinition(xmldb_table $xmldb_table=null) {
774 if (!$xmldb_table) {
775 return 'Invalid xmldb_field->validateDefinition() call, $xmldb_table is required.';
778 $name = $this->getName();
779 if (strlen($name) > self::NAME_MAX_LENGTH) {
780 return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name is too long.'
781 .' Limit is '.self::NAME_MAX_LENGTH.' chars.';
783 if (!preg_match('/^[a-z][a-z0-9_]*$/', $name)) {
784 return 'Invalid field name in table {'.$xmldb_table->getName().'}: field "'.$this->getName().'" name includes invalid characters.';
787 switch ($this->getType()) {
788 case XMLDB_TYPE_INTEGER:
789 $length = $this->getLength();
790 if (!is_number($length) or $length <= 0 or $length > self::INTEGER_MAX_LENGTH) {
791 return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid length';
793 $default = $this->getDefault();
794 if (!empty($default) and !is_number($default)) {
795 return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid default';
797 break;
799 case XMLDB_TYPE_NUMBER:
800 $maxlength = self::NUMBER_MAX_LENGTH;
801 if ($xmldb_table->getName() === 'question_numerical_units' and $name === 'multiplier') {
802 //TODO: remove after MDL-32113 is resolved
803 $maxlength = 40;
805 $length = $this->getLength();
806 if (!is_number($length) or $length <= 0 or $length > $maxlength) {
807 return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid length';
809 $decimals = $this->getDecimals();
810 $decimals = empty($decimals) ? 0 : $decimals; // fix missing decimals
811 if (!is_number($decimals) or $decimals < 0 or $decimals > $length) {
812 return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid decimals';
814 $default = $this->getDefault();
815 if (!empty($default) and !is_numeric($default)) {
816 return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid default';
818 break;
820 case XMLDB_TYPE_FLOAT:
821 $length = $this->getLength();
822 $length = empty($length) ? 6 : $length; // weird, it might be better to require something here...
823 if (!is_number($length) or $length <= 0 or $length > self::FLOAT_MAX_LENGTH) {
824 return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid length';
826 $decimals = $this->getDecimals();
827 $decimals = empty($decimals) ? 0 : $decimals; // fix missing decimals
828 if (!is_number($decimals) or $decimals < 0 or $decimals > $length) {
829 return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid decimals';
831 $default = $this->getDefault();
832 if (!empty($default) and !is_numeric($default)) {
833 return 'Invalid field definition in table {'.$xmldb_table->getName().'}: XMLDB_TYPE_FLOAT field "'.$this->getName().'" has invalid default';
835 break;
837 case XMLDB_TYPE_CHAR:
838 if ($this->getLength() > self::CHAR_MAX_LENGTH) {
839 return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_CHAR field "'.$this->getName().'" is too long.'
840 .' Limit is '.self::CHAR_MAX_LENGTH.' chars.';
842 break;
844 case XMLDB_TYPE_TEXT:
845 break;
847 case XMLDB_TYPE_BINARY:
848 break;
850 case XMLDB_TYPE_DATETIME:
851 break;
853 case XMLDB_TYPE_TIMESTAMP:
854 break;
857 return null;