Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Database column information.
  19   *
  20   * @package    core_dml
  21   * @copyright  2008 Petr Skoda (http://skodak.org)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Detailed database field information.
  29   *
  30   * It is based on the adodb library's ADOFieldObject object.
  31   * 'column' does mean 'the field' here.
  32   *
  33   * @package    core_dml
  34   * @copyright  2008 Petr Skoda (http://skodak.org)
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   *
  37   * @property-read string $name Name of column - lowercase.
  38   * @property-read string $type Driver dependent native data type. Not standardised, it's used to find meta_type.
  39   *
  40   * Max length:
  41   *  character type - number of characters
  42   *  blob - number of bytes
  43   *  integer - number of digits
  44   *  float - digits left from floating point
  45   *  boolean - 1
  46   * @property-read int    $max_length size of the database field, eg how much data can you put in there.
  47   *
  48   * @property-read int    $scale Scale of field, decimal points (float), null otherwise.
  49   * @property-read bool   $not_null true if the field is set to NOT NULL.
  50   * @property-read bool   $primary_key true if the field is the primary key. (usually 'id').
  51   * @property-read bool   $auto_increment True if field is autoincrementing or sequence.
  52   * @property-read bool   $binary True if the field is binary.
  53   * @property-read bool   $has_default True if the default value is defined.
  54   * @property-read string $default_value The default value (if defined).
  55   * @property-read bool   $unique True if the field values are unique, false if not.
  56   *
  57   * Standardised one character column type, uppercased and enumerated as follows:
  58   * R - counter (integer primary key)
  59   * I - integers
  60   * N - numbers (floats)
  61   * C - characters and strings
  62   * X - texts
  63   * B - binary blobs
  64   * L - boolean (1 bit)
  65   * T - timestamp - unsupported
  66   * D - date - unsupported
  67   * @property-read string $meta_type Standardised one character column type, uppercased and enumerated: R,I,N,C,X,B,L,T,D
  68   */
  69  class database_column_info {
  70  
  71      /**
  72       * @var array The internal storage of column data.
  73       */
  74      protected $data;
  75  
  76      /**
  77       * Magic set function.  This is a read only object and you aren't allowed to write to any variables.
  78       *
  79       * @param string $name ignored.
  80       * @param mixed $value ignored.
  81       * @throws coding_exception You are not allowed to set data on database_column_info
  82       */
  83      public function __set($name, $value) {
  84          throw new coding_exception('database_column_info is a ready only object to allow for faster caching.');
  85      }
  86  
  87      /**
  88       * Magic get function.
  89       *
  90       * @param string $variablename variable name to return the value of.
  91       * @return mixed The variable contents.
  92       *
  93       * @throws coding_exception You cannot request a variable that is not allowed.
  94       */
  95      public function __get($variablename) {
  96          if (isset($this->data[$variablename]) || array_key_exists($variablename, $this->data)) {
  97              return $this->data[$variablename];
  98          }
  99          throw new coding_exception('Asked for a variable that is not available . ('.$variablename.').');
 100      }
 101  
 102      /**
 103       * Magic isset function.
 104       *
 105       * @param string $variablename The name of the property to test if isset().
 106       * @return bool Whether the value is set or not.
 107       */
 108      public function __isset($variablename) {
 109          return isset($this->data[$variablename]);
 110      }
 111  
 112      /**
 113       * Constructor
 114       *
 115       * @param mixed $data object or array with properties
 116       */
 117      public function __construct($data) {
 118          // Initialize all the allowed variables to null so the array key exists.
 119          $validelements = array('name', 'type', 'max_length', 'scale', 'not_null', 'primary_key',
 120                                 'auto_increment', 'binary', 'has_default',  'default_value',
 121                                 'unique', 'meta_type');
 122          foreach ($validelements as $element) {
 123              if (isset($data->$element)) {
 124                  $this->data[$element] = $data->$element;
 125              } else {
 126                  $this->data[$element] = null;
 127              }
 128          }
 129  
 130          switch ($this->data['meta_type']) {
 131              case 'R': // normalise counters (usually 'id')
 132                  $this->data['binary']         = false;
 133                  $this->data['has_default']    = false;
 134                  $this->data['default_value']  = null;
 135                  $this->data['unique']         = true;
 136                  break;
 137              case 'C':
 138                  $this->data['auto_increment'] = false;
 139                  $this->data['binary']         = false;
 140                  break;
 141          }
 142      }
 143  }