3 * StatusNet, the distributed open-source microblogging tool
5 * Database schema utilities
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
35 * A class encapsulating the structure of a column in a table.
39 * @author Evan Prodromou <evan@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
45 /** name of the column. */
47 /** type of column, e.g. 'int', 'varchar' */
49 /** size of the column. */
51 /** boolean flag; can it be null? */
54 * type of key: null = no key; 'PRI' => primary;
55 * 'UNI' => unique key; 'MUL' => multiple values.
58 /** default value if any. */
60 /** 'extra' stuff. Returned by MySQL, largely
63 /** auto increment this field if no value is specific for it during an insert **/
64 public $auto_increment;
69 * @param string $name name of the column
70 * @param string $type type of the column
71 * @param int $size size of the column
72 * @param boolean $nullable can this be null?
73 * @param string $key type of key
74 * @param value $default default value
75 * @param value $extra unused
76 * @param boolean $auto_increment
78 function __construct($name=null, $type=null, $size=null,
79 $nullable=true, $key=null, $default=null,
80 $extra=null, $auto_increment=false)
82 $this->name = strtolower($name);
83 $this->type = strtolower($type);
84 $this->size = $size+0;
85 $this->nullable = $nullable;
87 $this->default = $default;
88 $this->extra = $extra;
89 $this->auto_increment = $auto_increment;
93 * Compares this columndef with another to see
94 * if they're functionally equivalent.
96 * @param ColumnDef $other column to compare
98 * @return boolean true if equivalent, otherwise false.
100 function equals($other)
102 return ($this->name == $other->name &&
103 $this->_typeMatch($other) &&
104 $this->_defaultMatch($other) &&
105 $this->_nullMatch($other) &&
106 $this->key == $other->key &&
107 $this->auto_increment == $other->auto_increment);
111 * Does the type of this column match the
112 * type of the other column?
114 * Checks the type and size of a column. Tries
115 * to ignore differences between synonymous
116 * data types, like 'integer' and 'int'.
118 * @param ColumnDef $other other column to check
120 * @return boolean true if they're about equivalent
122 private function _typeMatch($other)
124 switch ($this->type) {
127 return ($other->type == 'integer' ||
128 $other->type == 'int');
131 return ($this->type == $other->type &&
132 $this->size == $other->size);
137 * Does the default behaviour of this column match
140 * @param ColumnDef $other other column to check
142 * @return boolean true if defaults are effectively the same.
144 private function _defaultMatch($other)
146 return ((is_null($this->default) && is_null($other->default)) ||
147 ($this->default == $other->default));
151 * Does the null behaviour of this column match
154 * @param ColumnDef $other other column to check
156 * @return boolean true if these columns 'null' the same.
158 private function _nullMatch($other)
160 return ((!is_null($this->default) && !is_null($other->default) &&
161 $this->default == $other->default) ||
162 ($this->nullable == $other->nullable));