2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Database column information.
21 * @copyright 2008 Petr Skoda (http://skodak.org)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Detailed database field information.
30 * It is based on the adodb library's ADOFieldObject object.
31 * 'column' does mean 'the field' here.
34 * @copyright 2008 Petr Skoda (http://skodak.org)
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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.
41 * character type - number of characters
42 * blob - number of bytes
43 * integer - number of digits
44 * float - digits left from floating point
46 * @property-read int $max_length size of the database field, eg how much data can you put in there.
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.
57 * Standardised one character column type, uppercased and enumerated as follows:
58 * R - counter (integer primary key)
60 * N - numbers (floats)
61 * C - characters and strings
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
69 class database_column_info
{
72 * @var array The internal storage of column data.
77 * Magic set function. This is a read only object and you aren't allowed to write to any variables.
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
83 public function __set($name, $value) {
84 throw new coding_exception('database_column_info is a ready only object to allow for faster caching.');
90 * @param string $variablename variable name to return the value of.
91 * @return mixed The variable contents.
93 * @throws coding_exception You cannot request a variable that is not allowed.
95 public function __get($variablename) {
96 if (isset($this->data
[$variablename]) ||
array_key_exists($variablename, $this->data
)) {
97 return $this->data
[$variablename];
99 throw new coding_exception('Asked for a variable that is not available . ('.$variablename.').');
103 * Magic isset function.
105 * @param string $variablename The name of the property to test if isset().
106 * @return bool Whether the value is set or not.
108 public function __isset($variablename) {
109 return isset($this->data
[$variablename]);
115 * @param mixed $data object or array with properties
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;
126 $this->data
[$element] = null;
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;
138 $this->data
['auto_increment'] = false;
139 $this->data
['binary'] = false;